KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > gms > discovery > UdpDiscoveryService


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.tribe.gms.discovery;
26
27 import java.io.IOException JavaDoc;
28 import java.net.DatagramPacket JavaDoc;
29 import java.net.MulticastSocket JavaDoc;
30 import java.net.SocketException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32
33 import org.objectweb.tribe.common.GroupIdentifier;
34 import org.objectweb.tribe.common.IpAddress;
35 import org.objectweb.tribe.common.log.Trace;
36 import org.objectweb.tribe.gms.protocol.GroupDiscoveryMessage;
37 import org.objectweb.tribe.messages.DatagramMessage;
38
39 /**
40  * This class defines a UdpDiscoveryService.
41  * <p>
42  * <code>GroupDiscoveryMessage</code> are sent on the given multicast address.
43  * All registered listeners are called when a GroupDiscoveryMessage is received.
44  *
45  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
46  * @version 1.0
47  */

48 public class UdpDiscoveryService extends Thread JavaDoc implements DiscoveryService
49 {
50   private static final int RECEIVE_BUFFER_SIZE = 1024;
51
52   private MulticastSocket JavaDoc multicastSocket;
53   private IpAddress multicastAddress;
54   private IpAddress replyAddress;
55   private ArrayList JavaDoc listeners;
56   private boolean isKilled = false;
57
58   private Trace logger = Trace
59                                                    .getLogger("org.objectweb.tribe.discovery");
60
61   /**
62    * Creates a new <code>UdpDiscoveryService</code> object
63    *
64    * @param multicastAddress multicast address to use for discovery messages.
65    * @param inChannel the address on which responses must be sent back.
66    */

67   public UdpDiscoveryService(IpAddress multicastAddress, IpAddress replyAddress)
68   {
69     super("UdpDiscoveryService");
70     if (multicastAddress == null)
71       throw new IllegalArgumentException JavaDoc(
72           "Null addresses in UdpDiscoveryService constructor");
73
74     listeners = new ArrayList JavaDoc();
75     this.replyAddress = replyAddress;
76     try
77     {
78       // Create the multicast socket
79
logger.info("Creating UdpDiscoveryService on " + multicastAddress);
80       this.multicastAddress = multicastAddress;
81       multicastSocket = new MulticastSocket JavaDoc(multicastAddress.getPort());
82       multicastSocket.joinGroup(multicastAddress.getAddress());
83       start();
84     }
85     catch (SocketException JavaDoc e)
86     {
87       logger.error("Failed to create multicast socket", e);
88     }
89     catch (IOException JavaDoc e)
90     {
91       logger.error("Failed to create multicast socket", e);
92     }
93   }
94
95   /**
96    * @see org.objectweb.tribe.gms.discovery.DiscoveryService#sendGroupDiscovery(org.objectweb.tribe.common.GroupIdentifier)
97    */

98   public void sendGroupDiscovery(GroupIdentifier gid)
99   {
100     try
101     {
102       if (logger.isDebugEnabled())
103         logger.debug("Sending GroupDiscoveryMessage for group " + gid
104             + " reply to " + replyAddress);
105       DatagramPacket JavaDoc joinPacket = new GroupDiscoveryMessage(replyAddress,
106           multicastAddress, gid).getDatagramPacket();
107       multicastSocket.send(joinPacket);
108     }
109     catch (IOException JavaDoc e)
110     {
111       if (logger.isDebugEnabled())
112         logger.debug("Failed to send group discovery message for group " + gid,
113             e);
114       else if (logger.isInfoEnabled())
115         logger.info("Failed to send group discovery message for group " + gid
116             + " (" + e + ")");
117     }
118   }
119
120   /**
121    * @see org.objectweb.tribe.gms.discovery.DiscoveryService#registerDiscoveryListener(org.objectweb.tribe.gms.discovery.DiscoveryListener)
122    */

123   public void registerDiscoveryListener(DiscoveryListener listener)
124   {
125     synchronized (listeners)
126     {
127       listeners.add(listener);
128     }
129   }
130
131   /**
132    * @see org.objectweb.tribe.gms.discovery.DiscoveryService#unregisterDiscoveryListener(org.objectweb.tribe.gms.discovery.DiscoveryListener)
133    */

134   public boolean unregisterDiscoveryListener(DiscoveryListener listener)
135   {
136     synchronized (listeners)
137     {
138       return listeners.remove(listener);
139     }
140   }
141
142   /**
143    * The DiscoveryService threads looks for JOIN messages and notify appropriate
144    * listeners.
145    */

146   public void run()
147   {
148     DatagramPacket JavaDoc datagram = new DatagramPacket JavaDoc(new byte[RECEIVE_BUFFER_SIZE],
149         RECEIVE_BUFFER_SIZE);
150
151     while (!isKilled)
152     {
153       try
154       {
155         multicastSocket.receive(datagram);
156         Object JavaDoc received = DatagramMessage.getObjectFromDatagram(datagram);
157         if (received != null)
158         {
159           if (received instanceof GroupDiscoveryMessage)
160           {
161             GroupDiscoveryMessage msg = (GroupDiscoveryMessage) received;
162             if (logger.isDebugEnabled())
163               logger.debug("Received GroupDiscoveryMessage for group "
164                   + msg.getGroupIdentifier() + " from "
165                   + msg.getSourceAddress());
166             // Notify listeners
167
synchronized (listeners)
168             {
169               int size = listeners.size();
170               for (int i = 0; i < size; i++)
171               {
172                 DiscoveryListener listener = (DiscoveryListener) listeners
173                     .get(i);
174                 listener.discoveryRequest(msg.getGroupIdentifier(), msg
175                     .getSourceAddress());
176               }
177             }
178           }
179           else
180             logger.info("UdpDiscoveryService received an unexpected message: "
181                 + received);
182         }
183       }
184       catch (IOException JavaDoc e)
185       {
186         logger.error("Error while receiving message", e);
187       }
188     }
189   }
190
191   /**
192    * Returns the replyAddress value.
193    *
194    * @return Returns the replyAddress.
195    */

196   public IpAddress getReplyAddress()
197   {
198     return replyAddress;
199   }
200
201   /**
202    * Sets the replyAddress value.
203    *
204    * @param replyAddress The replyAddress to set.
205    */

206   public void setReplyAddress(IpAddress replyAddress)
207   {
208     this.replyAddress = replyAddress;
209   }
210
211   /**
212    * Terminate the UdpDiscoveryService.
213    */

214   public void kill()
215   {
216     isKilled = true;
217     multicastSocket.close();
218     this.interrupt();
219     try
220     {
221       this.join(1000);
222     }
223     catch (InterruptedException JavaDoc e)
224     {
225     }
226   }
227 }
Popular Tags