KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > transport > PacketTransport


1
2 package transport;
3
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6
7 import transport.packet.Packet;
8
9 import jegg.EggBase;
10 import jegg.Message;
11 import jegg.PortException;
12 import jegg.Port;
13
14 /**
15  * PacketTransporter handles reading and writing packets to/from the network.
16  * Packet consumers can register as packet listeners, and packet senders can
17  * have their packets forwarded onto the network by sending the packet to this
18  * egg.
19  * <p>
20  * PacketTransporter can accept connections from remote clients. Each
21  * remote connection is assigned a channel number and save in a channel list.
22  */

23 public class PacketTransport extends EggBase
24 {
25     /** Class logger */
26     private static final Log LOG = LogFactory.getLog(PacketTransport.class);
27     
28     private Port publisherPort;
29     private Port writerPort;
30     private Port portmanagerPort;
31     
32     public Port getPort()
33     {
34         return getContext().getPort();
35     }
36     
37
38     public void handle(Object JavaDoc message)
39     {
40         LOG.warn("Unexpected message: "+message);
41     }
42
43     public void handle(Port p)
44     {
45         String JavaDoc name = (String JavaDoc) p.getId();
46         
47         if (LOG.isDebugEnabled())
48             LOG.debug(getContext().getId()+": "+p);
49         
50         if (name.endsWith("publisher"))
51             publisherPort = p;
52         else
53         if (name.endsWith("writer"))
54             writerPort = p;
55         else
56         if (name.endsWith("portmanager"))
57             portmanagerPort = p;
58     }
59     
60     /**
61      * Accept registration from a packet consumer.
62      */

63     public void handle(AddPacketListenerCommand a)
64     {
65         Message message = getContext().getCurrentMessage();
66         try
67         {
68             publisherPort.send(message);
69         }
70         catch (PortException e)
71         {
72             LOG.error("Failed to add packetlistener", e);
73         }
74     }
75     
76     /**
77      * Unregister a packet consumer.
78      */

79     public void handle(RemovePacketListenerCommand r)
80     {
81         Message message = getContext().getCurrentMessage();
82         try
83         {
84             publisherPort.send(message);
85         }
86         catch (PortException e)
87         {
88             LOG.error("Failed to remove packetlistener", e);
89         }
90     }
91    
92     /**
93      * Configure a new port to accept incoming connections on.
94      */

95     public void handle(ConfigureLocalAddressCommand c)
96     {
97         // Pass the configuration on to the port manager.
98
LocalAddress a = c.getAddress();
99         
100         try
101         {
102             portmanagerPort.send(getContext().createMessage(a));
103         }
104         catch (PortException e)
105         {
106             LOG.error("Failed to configure port manager", e);
107         }
108     }
109     
110     /**
111      * Get the list of ports that have been opened.
112      */

113     public void handle(GetLocalAddressCommand c)
114     {
115         Message message = getContext().getCurrentMessage();
116         try
117         {
118             portmanagerPort.send(message);
119         }
120         catch (PortException e)
121         {
122             LOG.error("Failed to fetch list of open ports", e);
123         }
124     }
125     
126     /**
127      * Write a packet to the network.
128      */

129     public void handle(Packet p)
130     {
131         // Forward packet to the packet writer.
132
Message message = getContext().getCurrentMessage();
133         try
134         {
135             writerPort.send(message);
136         }
137         catch (PortException e)
138         {
139             LOG.error("Failed to write packet to network", e);
140         }
141     }
142 }
143
Popular Tags