KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > transport > packet > PacketPublisher


1 package transport.packet;
2
3 import java.util.Collection JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import transport.AddPacketListenerCommand;
12 import transport.RemovePacketListenerCommand;
13
14 import jegg.EggBase;
15 import jegg.Message;
16 import jegg.PortException;
17 import jegg.Port;
18
19 /**
20  * This egg distributes all incoming packets
21  *
22  *
23  * @author Bruce Lowery
24  */

25 public class PacketPublisher extends EggBase
26 {
27     /** Class logger */
28     private static final Log LOG = LogFactory.getLog(PacketPublisher.class);
29     /** Initial capacity of a new listener list */
30     private static final int LISTENER_LIST_INITIAL_CAPACITY = 5;
31     /** Initial capacity of the listener map */
32     private static final int LISTENER_MAP_INITIAL_CAPACITY = 10;
33     /** List of packet sources */
34     private Port packetReaderPort;
35     /** The packet listeners */
36     private Map JavaDoc listeners = new HashMap JavaDoc(LISTENER_MAP_INITIAL_CAPACITY);
37     
38     /** Create a packet publisher on the default dispatcher */
39     public PacketPublisher()
40     {
41         super();
42     }
43
44     public void init()
45     {
46         getContext().bindToPort(packetReaderPort);
47     }
48     
49     // -------------------------------------------------------------------
50
// Message Handlers
51
// -------------------------------------------------------------------
52

53     public void handle(Port p)
54     {
55         if (LOG.isDebugEnabled())
56             LOG.debug("handle("+p+")");
57         
58         packetReaderPort = p;
59 // try
60
// {
61
// packetReaderPort.send(new Object());
62
// }
63
// catch (PortException e)
64
// {
65
// LOG.error("Unable to start packet reader");
66
// }
67
}
68     
69     /* (non-Javadoc)
70      * @see egg.Egg#handle(java.lang.Object)
71      */

72     public void handle(Object JavaDoc message)
73     {
74         LOG.warn("Unexpected message: "+ message);
75     }
76     
77     /**
78      * Add a packet listener.
79      * @param al command object specifying the packet listener to add.
80      */

81     public void handle(AddPacketListenerCommand al)
82     {
83         LOG.debug("Received new packet listener");
84         
85         Object JavaDoc type = al.getType();
86         if (null == type) type = PacketTypeEnum.WILDCARD;
87         Object JavaDoc port = al.getPort();
88         Collection JavaDoc list = (Collection JavaDoc) listeners.get(type);
89         if (null == list)
90         {
91             list = new HashSet JavaDoc(LISTENER_LIST_INITIAL_CAPACITY);
92             listeners.put(type,list);
93         }
94         list.add(port);
95     }
96     
97     /**
98      * Remove a packet listener.
99      * @param rl command specifying packet listener to remove.
100      */

101     public void handle(RemovePacketListenerCommand rl)
102     {
103         Object JavaDoc type = rl.getType();
104         Object JavaDoc port = rl.getPort();
105         Collection JavaDoc list = (Collection JavaDoc) listeners.get(type);
106         
107         if (null != list)
108         {
109             list.remove(port);
110         }
111     }
112     
113     /**
114      * Forward a packet from a packet source to the packet listeners.
115      * @param p
116      */

117     public void handle(Packet p)
118     {
119         LOG.debug("Publishing packet to listeners");
120         
121         Object JavaDoc type = p.getType();
122         publishToTypeListeners(p, type);
123         publishToTypeListeners(p, PacketTypeEnum.WILDCARD);
124     }
125     
126     public void handle(AddPacketSource s)
127     {
128         getContext().bindToPort(s.getSource());
129     }
130     
131     // -------------------------------------------------------------------
132
// Implementation support
133
// -------------------------------------------------------------------
134

135     private void publishToTypeListeners(Packet p, Object JavaDoc type)
136     {
137         Collection JavaDoc list = (Collection JavaDoc) listeners.get(type);
138         if (null != list)
139         {
140             Message msg = getContext().createMessage(p);
141             
142             for (Iterator JavaDoc it = list.iterator(); it.hasNext(); )
143             {
144                 LOG.debug("Delivering packet to listener");
145                 Port port = (Port) it.next();
146                 try
147                 {
148                     port.send(msg);
149                 }
150                 catch (PortException e)
151                 {
152                     LOG.error("Failed to send packet to type listeners", e);
153                 }
154             }
155         }
156     }
157     
158 }
159
Popular Tags