1 24 25 package org.objectweb.tribe.faultdetection; 26 27 import java.net.SocketException ; 28 import java.util.ArrayList ; 29 import java.util.Hashtable ; 30 31 import org.objectweb.tribe.common.Address; 32 import org.objectweb.tribe.common.IpAddress; 33 34 41 public class PingManagerThread implements Runnable 42 { 43 44 ArrayList addresses; 45 ArrayList listeners; 46 Hashtable sockets; 47 long waitInterval; 48 boolean stop; 49 IpAddress localAddress; 50 51 57 public PingManagerThread(IpAddress localAddress, long waitInterval) 58 { 59 super(); 60 addresses = new ArrayList (); 61 listeners = new ArrayList (); 62 sockets = new Hashtable (); 63 this.waitInterval = waitInterval; 64 this.localAddress = localAddress; 65 stop = false; 66 } 67 68 73 public void setStop(boolean stop) 74 { 75 this.stop = stop; 76 } 77 78 83 public long getWaitInterval() 84 { 85 return waitInterval; 86 } 87 88 93 public void setWaitInterval(long waitInterval) 94 { 95 this.waitInterval = waitInterval; 96 } 97 98 103 public void addPingHook(Address address) 104 { 105 synchronized (addresses) 106 { 107 addresses.add(address); 108 } 109 } 110 111 116 public void removePingHook(Address address) 117 { 118 synchronized (addresses) 119 { 120 addresses.remove(address); 121 } 122 } 123 124 130 public void addListener(FaultDetectionListener listener) 131 { 132 synchronized (listeners) 133 { 134 listeners.add(listener); 135 } 136 } 137 138 144 public void removeListener(FaultDetectionListener listener) 145 { 146 synchronized (listeners) 147 { 148 listeners.remove(listener); 149 } 150 } 151 152 158 private void doPing(IpAddress address) 159 { 160 Thread t = (Thread ) sockets.get(address); 161 if (t == null) 162 { 163 try 164 { 165 t = new UDPPingThread(localAddress, address, (int) waitInterval); 166 } 167 catch (SocketException e) 168 { 169 notifyListenerOfEvent(FaultDetectionListener.EVENT_ERROR_LOCAL_ADDRESS, 170 localAddress); 171 return; 172 } 173 } 174 t.start(); 175 } 176 177 182 public void getResult(IpAddress address) 183 { 184 UDPPingThread t = (UDPPingThread) sockets.get(address); 185 if (!t.isTargetAlive()) 186 { 187 notifyListenerOfEvent(FaultDetectionListener.THREAD_SUSPECTED, address); 188 } 189 else 190 { 191 notifyListenerOfEvent(FaultDetectionListener.THREAD_ALIVE, address); 192 } 193 } 194 195 202 public void notifyListenerOfEvent(int event, IpAddress address) 203 { 204 switch (event) 205 { 206 case FaultDetectionListener.THREAD_ALIVE : 207 synchronized (listeners) 208 { 209 for (int i = 0; i < listeners.size(); i++) 210 ((FaultDetectionListener) listeners.get(i)).alive(address); 211 } 212 break; 213 case FaultDetectionListener.THREAD_SUSPECTED : 214 synchronized (listeners) 215 { 216 for (int i = 0; i < listeners.size(); i++) 217 ((FaultDetectionListener) listeners.get(i)).noResponse(address); 218 } 219 break; 220 default : 221 synchronized (listeners) 222 { 223 for (int i = 0; i < listeners.size(); i++) 224 ((FaultDetectionListener) listeners.get(i)).pingServiceEvent(event); 225 } 226 break; 227 } 228 } 229 230 233 public void run() 234 { 235 while (!stop) 236 { 237 238 synchronized (addresses) 240 { 241 for (int i = 0; i < addresses.size(); i++) 242 doPing(((IpAddress) addresses.get(i))); 243 } 244 245 try 246 { 247 synchronized (this) 249 { 250 wait(waitInterval); 251 } 252 } 253 catch (InterruptedException e) 254 { 255 notifyListenerOfEvent(FaultDetectionListener.EVENT_INTERRUPTED, 257 localAddress); 258 } 259 260 synchronized (addresses) 262 { 263 for (int i = 0; i < addresses.size(); i++) 264 getResult((IpAddress) addresses.get(i)); 265 } 266 } 267 268 notifyListenerOfEvent(FaultDetectionListener.EVENT_QUIT, localAddress); 270 } 271 } | Popular Tags |