1 24 25 package org.objectweb.tribe.faultdetection; 26 27 import java.net.InetAddress ; 28 import java.net.UnknownHostException ; 29 import java.util.ArrayList ; 30 import java.util.Hashtable ; 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 45 public class PingFaultDetectionService 46 implements 47 FaultDetectionService, 48 Service 49 { 50 51 private ArrayList groups; 52 53 private PingManagerThread pingThread; 54 55 private Hashtable pongThreads; 56 57 private IpAddress localAddress; 58 59 60 private int emittingPort; 61 62 private int listeningPort; 63 64 69 public PingFaultDetectionService() throws UnknownHostException 70 { 71 this(FaultDetectionService.DEFAULT_EMITTING_PORT, 72 FaultDetectionService.DEFAULT_LISTENING_PORT); 73 } 74 75 82 public PingFaultDetectionService(int emittingPort, int listeningPort) throws UnknownHostException 83 { 84 this.emittingPort = emittingPort; 85 this.listeningPort = listeningPort; 86 groups = new ArrayList (10); 87 pongThreads = new Hashtable (); 88 initThreads(); 89 } 90 91 private void initThreads() throws UnknownHostException 92 { 93 pingThread = new PingManagerThread(localAddress, 94 FaultDetectionService.DEFAULT_REFREST_RATE); 95 localAddress = new IpAddress(InetAddress.getLocalHost(), emittingPort); 96 } 97 98 101 public void setRefreshRate(long delay) 102 { 103 pingThread.setWaitInterval(delay); 104 } 105 106 109 public long getRefreshRate() 110 { 111 return pingThread.getWaitInterval(); 112 } 113 114 117 public void addGroup(Group group) 118 { 119 synchronized (groups) 121 { 122 groups.add(group); 123 } 124 ArrayList members = group.getMembers(); 126 for (int i = 0; i < members.size(); i++) 127 addMember((Member) members.get(i)); 128 } 129 130 133 public void addMember(Member member) 134 { 135 IpAddress address = (IpAddress) member.getAddress(); 137 IpAddress pongAddress = new IpAddress(address.getAddress(), listeningPort); 140 pingThread.addPingHook(address); 142 UDPPongThread pong = new UDPPongThread(localAddress, pongAddress); 145 new Thread (pong).start(); 146 pongThreads.put(address, pong); 148 } 149 150 153 public void removeMember(Member member) 154 { 155 Address address = member.getAddress(); 156 pingThread.removePingHook(address); 158 UDPPongThread thread = (UDPPongThread) pongThreads.remove(address); 160 if (thread != null) 162 thread.setQuit(true); 163 } 164 165 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 ArrayList members = group.getMembers(); 181 for (int i = 0; i < members.size(); i++) 182 removeMember((Member) members.get(i)); 183 return group; 185 } 186 else 187 return null; 188 } 189 190 193 public ArrayList getGroups() 194 { 195 return groups; 196 } 197 198 201 public void start() 202 { 203 new Thread (pingThread).start(); 205 } 206 207 210 public void stop() 211 { 212 pingThread.setStop(true); 214 } 215 216 219 public void addListener(FaultDetectionListener listener) 220 { 221 pingThread.addListener(listener); 223 } 224 225 228 public void removeListener(FaultDetectionListener listener) 229 { 230 pingThread.removeListener(listener); 232 } 233 } | Popular Tags |