1 29 import java.net.InetAddress ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 import transport.AddPacketListenerCommand; 35 import transport.ConfigureLocalAddressCommand; 36 import transport.LocalAddress; 37 import transport.packet.Packet; 38 import jegg.EggBase; 39 import jegg.Port; 40 import jegg.PortException; 41 import jegg.timer.Timeout; 42 47 public class PrimeNumberClient extends EggBase 48 { 49 private static final Log LOG = LogFactory.getLog(PrimeNumberClient.class); 50 51 private int SERVICE_TCP_PORT; 52 private int MY_TCP_PORT; 53 private Port networkPort; 54 private int channelID; 55 56 public void init() 57 { 58 MY_TCP_PORT = Integer.parseInt(getContext().getProperty("my-tcp-port")); 59 SERVICE_TCP_PORT = Integer.parseInt(getContext().getProperty("server-tcp-port")); 60 if (LOG.isInfoEnabled()) 61 { 62 LOG.info("MY_TCP_PORT: " + MY_TCP_PORT); 63 LOG.info("SERVER_TCP_PORT: "+SERVICE_TCP_PORT); 64 } 65 66 LocalAddress la = new LocalAddress(null,MY_TCP_PORT); 67 try 69 { 70 networkPort.send(getContext().createMessage(new ConfigureLocalAddressCommand(la))); 71 networkPort.send(getContext().createMessage(new AddPacketListenerCommand(null,getContext().getPort()))); 73 } 74 catch (PortException pe) 75 { 76 LOG.error("Failed to configure network address: ", pe); 77 } 78 getContext().createSingleShotTimer(5000); 79 } 80 81 public void handle(Port p) 82 { 83 String name = (String ) p.getId(); 84 85 if (LOG.isDebugEnabled()) 86 LOG.debug("handle("+name+")"); 87 88 if (name.endsWith("packet-transport")) 90 { 91 networkPort = p; 92 } 93 } 94 95 98 public void handle(Object message) 99 { 100 LOG.warn("Unexpected message: " + message); 101 } 102 103 public void handle(Packet p) 104 { 105 108 Long el = (Long ) p.getPayload(); 109 LOG.info("Next prime number: " + el); 110 channelID = p.getChannelID(); 112 getContext().createSingleShotTimer(1000); 113 } 114 115 public void handle(Timeout t) 116 { 117 LOG.debug("TIMEOUT"); 118 119 LOG.debug("Sending request packet"); 121 122 Packet p = new Packet(null,null); 123 124 if (0 == channelID) 129 { 130 try 131 { 132 p.setIP(InetAddress.getLocalHost()); 133 p.setPort(SERVICE_TCP_PORT); 134 } 135 catch (Throwable th) 136 { 137 LOG.error("Unable to send request", th); 138 return; 139 } 140 } 141 else 142 { 143 p.setChannelID(channelID); 149 } 150 try 151 { 152 networkPort.send(getContext().createMessage(p)); 153 } 154 catch (PortException e) 155 { 156 LOG.error("Failed to send request packet: ", e); 157 } 158 } 159 } 160 | Popular Tags |