KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > faultdetection > PingFaultDetectionService


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): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.tribe.faultdetection;
26
27 import java.net.InetAddress JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32 import org.objectweb.tribe.common.Address;
33 import org.objectweb.tribe.common.Group;
34 import org.objectweb.tribe.common.IpAddress;
35 import org.objectweb.tribe.common.Member;
36 import org.objectweb.tribe.common.Service;
37
38 /**
39  * This class defines a PingFaultDetectionService. This will detect missing
40  * members by pinging them using regular emission of packets.
41  *
42  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
43  * @version 1.0
44  */

45 public class PingFaultDetectionService
46     implements
47       FaultDetectionService,
48       Service
49 {
50   /** Contains a list of group that the fault detection service is listening to */
51   private ArrayList JavaDoc groups;
52   /** The ping manager thread, that will manager ping on members */
53   private PingManagerThread pingThread;
54   /** Hashtable of pong threads that will listen and respond to ping thread */
55   private Hashtable JavaDoc pongThreads;
56   /** The local address */
57   private IpAddress localAddress;
58
59   /** The port that will be used to emit ping */
60   private int emittingPort;
61   /** The port that will be used to listen to ping */
62   private int listeningPort;
63
64   /**
65    * Creates a new <code>PingFaultDetectionService</code> object
66    *
67    * @throws UnknownHostException
68    */

69   public PingFaultDetectionService() throws UnknownHostException JavaDoc
70   {
71     this(FaultDetectionService.DEFAULT_EMITTING_PORT,
72         FaultDetectionService.DEFAULT_LISTENING_PORT);
73   }
74
75   /**
76    * Creates a new <code>PingFaultDetectionService</code> object
77    *
78    * @param emittingPort
79    * @param listeningPort
80    * @throws UnknownHostException
81    */

82   public PingFaultDetectionService(int emittingPort, int listeningPort) throws UnknownHostException JavaDoc
83   {
84     this.emittingPort = emittingPort;
85     this.listeningPort = listeningPort;
86     groups = new ArrayList JavaDoc(10);
87     pongThreads = new Hashtable JavaDoc();
88     initThreads();
89   }
90
91   private void initThreads() throws UnknownHostException JavaDoc
92   {
93     pingThread = new PingManagerThread(localAddress,
94         FaultDetectionService.DEFAULT_REFREST_RATE);
95     localAddress = new IpAddress(InetAddress.getLocalHost(), emittingPort);
96   }
97
98   /**
99    * @see org.objectweb.tribe.faultdetection.FaultDetectionService#setRefreshRate(long)
100    */

101   public void setRefreshRate(long delay)
102   {
103     pingThread.setWaitInterval(delay);
104   }
105
106   /**
107    * @see org.objectweb.tribe.faultdetection.FaultDetectionService#getRefreshRate()
108    */

109   public long getRefreshRate()
110   {
111     return pingThread.getWaitInterval();
112   }
113
114   /**
115    * @see org.objectweb.tribe.faultdetection.FaultDetectionService#addGroup(org.objectweb.tribe.common.Group)
116    */

117   public void addGroup(Group group)
118   {
119     // add group to service
120
synchronized (groups)
121     {
122       groups.add(group);
123     }
124     // addentries in ping thread
125
ArrayList JavaDoc members = group.getMembers();
126     for (int i = 0; i < members.size(); i++)
127       addMember((Member) members.get(i));
128   }
129
130   /**
131    * @see org.objectweb.tribe.faultdetection.FaultDetectionService#addMember(org.objectweb.tribe.common.Member)
132    */

133   public void addMember(Member member)
134   {
135     // Get the address of the member
136
IpAddress address = (IpAddress) member.getAddress();
137     // Create a new address with the same host the and the listening port for
138
// fault detection
139
IpAddress pongAddress = new IpAddress(address.getAddress(), listeningPort);
140     // Tell the ping thread to ping this address
141
pingThread.addPingHook(address);
142     // Create and start a pong thread that will response to ping from this
143
// address
144
UDPPongThread pong = new UDPPongThread(localAddress, pongAddress);
145     new Thread JavaDoc(pong).start();
146     // Put the pong thread in the hashtable to remove it in the future
147
pongThreads.put(address, pong);
148   }
149
150   /**
151    * @see org.objectweb.tribe.faultdetection.FaultDetectionService#removeMember(org.objectweb.tribe.common.Member)
152    */

153   public void removeMember(Member member)
154   {
155     Address JavaDoc address = member.getAddress();
156     // Stop pinging this address
157
pingThread.removePingHook(address);
158     // Remove the pong thread from the hashtable
159
UDPPongThread thread = (UDPPongThread) pongThreads.remove(address);
160     // Stop answering to ping from this address
161
if (thread != null)
162       thread.setQuit(true);
163   }
164
165   /**
166    * @see org.objectweb.tribe.faultdetection.FaultDetectionService#removeGroup(org.objectweb.tribe.common.Group)
167    */

168   public Group removeGroup(Group group)
169   {
170     if (group == null)
171       return null;
172     boolean check = false;
173     synchronized (groups)
174     {
175       check = groups.contains(group) && groups.remove(group);
176     }
177     if (check)
178     {
179       // Remove addresses from ping thread
180
ArrayList JavaDoc members = group.getMembers();
181       for (int i = 0; i < members.size(); i++)
182         removeMember((Member) members.get(i));
183       // Return group
184
return group;
185     }
186     else
187       return null;
188   }
189
190   /**
191    * @see org.objectweb.tribe.faultdetection.FaultDetectionService#getGroups()
192    */

193   public ArrayList JavaDoc getGroups()
194   {
195     return groups;
196   }
197
198   /**
199    * @see org.objectweb.tribe.common.Service#start()
200    */

201   public void start()
202   {
203     // Start ping thread
204
new Thread JavaDoc(pingThread).start();
205   }
206
207   /**
208    * @see org.objectweb.tribe.common.Service#stop()
209    */

210   public void stop()
211   {
212     // Ask ping thread to stop
213
pingThread.setStop(true);
214   }
215
216   /**
217    * @see org.objectweb.tribe.faultdetection.FaultDetectionService#addListener(org.objectweb.tribe.faultdetection.FaultDetectionListener)
218    */

219   public void addListener(FaultDetectionListener listener)
220   {
221     // Add listener to ping thread
222
pingThread.addListener(listener);
223   }
224
225   /**
226    * @see org.objectweb.tribe.faultdetection.FaultDetectionService#removeListener(org.objectweb.tribe.faultdetection.FaultDetectionListener)
227    */

228   public void removeListener(FaultDetectionListener listener)
229   {
230     // Remove listener from ping thread
231
pingThread.removeListener(listener);
232   }
233 }
Popular Tags