KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > mcast > BroadcastDispatcherImpl


1 package org.sapia.ubik.mcast;
2
3 import org.sapia.ubik.mcast.server.MulticastServer;
4
5 import java.io.*;
6
7 import java.net.DatagramPacket JavaDoc;
8 import java.net.MulticastSocket JavaDoc;
9 import org.sapia.ubik.rmi.server.Log;
10
11
12 /**
13  * Dispatches objects using a multicast channel.
14  *
15  * @author Yanick Duchesne
16  * <dl>
17  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
18  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
19  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
20  * </dl>
21  */

22 public class BroadcastDispatcherImpl /*extends Thread*/
23   implements BroadcastDispatcher {
24   static final int DEFAULT_BUFSZ = 5000;
25   static final int TTL = 7;
26   private boolean _started;
27   private String JavaDoc _node;
28   private String JavaDoc _domain;
29   private BroadcastServer _server;
30   private EventConsumer _consumer;
31   private int _bufsz = DEFAULT_BUFSZ;
32
33   public BroadcastDispatcherImpl(EventConsumer cons, String JavaDoc mcastHost,
34     int mcastPort) throws IOException {
35     _server = new BroadcastServer(cons, 15, mcastHost, mcastPort, TTL);
36     _server.setBufsize(_bufsz);
37     _consumer = cons;
38     _node = cons.getNode();
39     _domain = cons.getDomainName().toString();
40   }
41
42   public BroadcastDispatcherImpl(String JavaDoc node, String JavaDoc domain, String JavaDoc mcastHost,
43     int mcastPort, int ttl) throws IOException {
44     this(new EventConsumer(node, domain), mcastHost, mcastPort);
45   }
46
47   public BroadcastDispatcherImpl(String JavaDoc domain, String JavaDoc mcastHost, int mcastPort)
48     throws IOException {
49     this(new EventConsumer(domain), mcastHost, mcastPort);
50   }
51
52   /**
53    * Sets this instance buffer size. This size is used to create
54    * the byte arrays that store the data of incoming UDP datagrams.
55    * <p>
56    * The size should be large enough to hold the data of incoming
57    * datagrams.
58    *
59    * @param size a buffer size - corresponding to the size of expected
60    * UDP datagrams.
61    */

62   public void setBufsize(int size) {
63     _bufsz = size;
64     _server.setBufsize(size);
65   }
66
67   /**
68    * Returns the node identifier of this instance.
69    *
70    * @return this instance's node identifier.
71    */

72   public String JavaDoc getNode() {
73     return _node;
74   }
75
76   /**
77    * Closes this instance, which should thereafter not be used.
78    */

79   public synchronized void close() {
80     if (_server != null) {
81       _server.close();
82       _server = null;
83     }
84   }
85
86   /**
87    * @see BroadcastDispatcher#dispatch(boolean, String, Object)
88    */

89   public void dispatch(boolean alldomains, String JavaDoc evtType, Object JavaDoc data)
90     throws IOException {
91     RemoteEvent evt;
92
93     if (alldomains) {
94       evt = new RemoteEvent(null, evtType, data).setNode(_node);
95     } else {
96       evt = new RemoteEvent(_domain, evtType, data).setNode(_node);
97     }
98
99     _server.send(Util.toBytes(evt, _bufsz));
100   }
101
102   /**
103    * @see BroadcastDispatcher#dispatch(String, String, Object)
104    */

105   public void dispatch(String JavaDoc domain, String JavaDoc evtType, Object JavaDoc data)
106     throws IOException {
107     RemoteEvent evt;
108
109     if(Log.isDebug()){
110       Log.debug(getClass(), "Sending event bytes for: " + evtType);
111     }
112     evt = new RemoteEvent(domain, evtType, data).setNode(_node);
113     _server.send(Util.toBytes(evt, _bufsz));
114   }
115
116   /**
117    * Registers the given listener with the given event type.
118    *
119    * @param evtType the logical type of <code>RemoteEvents</code> to listen for.
120    * @param listener the <code>AsyncEventListener</code> to register.
121    */

122   public synchronized void registerAsyncListener(String JavaDoc evtType,
123     AsyncEventListener listener) {
124     _consumer.registerAsyncListener(evtType, listener);
125   }
126
127   /**
128    * Unregisters the given listener from this instance.
129    *
130    * @param listener the <code>AsyncEventListener</code> to unregister.
131    */

132   public synchronized void unregisterListener(AsyncEventListener listener) {
133     _consumer.unregisterListener(listener);
134   }
135
136   /**
137    * Starts this instance.
138    */

139   public void start() {
140     _server.setDaemon(true);
141     _server.start();
142   }
143
144   /**
145    * @see org.sapia.ubik.mcast.BroadcastDispatcher#getMulticastAddress()
146    */

147   public String JavaDoc getMulticastAddress() {
148     return _server.getMulticastAddress();
149   }
150
151   /**
152    * @see org.sapia.ubik.mcast.BroadcastDispatcher#getMulticastPort()
153    */

154   public int getMulticastPort() {
155     return _server.getMulticastPort();
156   }
157
158   /*////////////////////////////////////////////////////////////////
159                              INNER CLASSES
160   ////////////////////////////////////////////////////////////////*/

161
162   /**
163    * @author Yanick Duchesne
164    *
165    * <dl>
166    * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
167    * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
168    * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
169    * </dl>
170    */

171   static class BroadcastServer extends MulticastServer {
172     EventConsumer _consumer;
173
174     public BroadcastServer(EventConsumer consumer, int soTimeout,
175       String JavaDoc mcastAddress, int mcastPort, int ttl) throws IOException {
176       super("ubik.mcast.BroadcastServer", soTimeout, mcastAddress, mcastPort,
177         ttl);
178       _consumer = consumer;
179     }
180
181     protected void handle(DatagramPacket JavaDoc pack, MulticastSocket JavaDoc sock) {
182       try {
183         _consumer.onAsyncEvent((RemoteEvent) Util.fromDatagram(pack));
184       } catch (Exception JavaDoc e) {
185         Log.error(getClass(), "Could not deserialize remote event", e);
186       }
187     }
188
189     protected void handlePacketSizeToShort(DatagramPacket JavaDoc pack) {
190       System.err.println("Packet size to short: " + pack.getLength() +
191         " - increase buffer size to correct.");
192     }
193
194     protected void handleSoTimeout() {
195     }
196   }
197 }
198
Popular Tags