1 3 package org.jgroups.protocols; 4 5 import org.jgroups.*; 6 import org.jgroups.stack.GossipClient; 7 import org.jgroups.stack.IpAddress; 8 import org.jgroups.util.List; 9 import org.jgroups.util.Util; 10 11 import java.net.InetAddress ; 12 import java.util.Enumeration ; 13 import java.util.Properties ; 14 import java.util.StringTokenizer ; 15 import java.util.Vector ; 16 17 18 31 public class PING extends Discovery { 32 String gossip_host=null; 33 int gossip_port=0; 34 long gossip_refresh=20000; GossipClient client; 36 int port_range=1; List initial_hosts=null; public static final String name="PING"; 39 40 41 public String getName() { 42 return name; 43 } 44 45 46 47 59 public boolean setProperties(Properties props) { 60 String str; 61 62 str=props.getProperty("gossip_host"); 63 if(str != null) { 64 gossip_host=str; 65 props.remove("gossip_host"); 66 } 67 68 str=props.getProperty("gossip_port"); 69 if(str != null) { 70 gossip_port=Integer.parseInt(str); 71 props.remove("gossip_port"); 72 } 73 74 str=props.getProperty("gossip_refresh"); 75 if(str != null) { 76 gossip_refresh=Long.parseLong(str); 77 props.remove("gossip_refresh"); 78 } 79 80 if(gossip_host != null && gossip_port != 0) { 81 try { 82 client=new GossipClient(new IpAddress(InetAddress.getByName(gossip_host), gossip_port), gossip_refresh); 83 } 84 catch(Exception e) { 85 if(log.isErrorEnabled()) log.error("creation of GossipClient failed, exception=" + e); 86 return false; } 88 } 89 90 str=props.getProperty("port_range"); if(str != null) { port_range=Integer.parseInt(str); 93 if(port_range < 1) { 94 port_range=1; 95 } 96 props.remove("port_range"); 97 } 98 99 str=props.getProperty("initial_hosts"); 100 if(str != null) { 101 props.remove("initial_hosts"); 102 initial_hosts=createInitialHosts(str); 103 } 104 105 return super.setProperties(props); 106 } 107 108 109 public void stop() { 110 super.stop(); 111 if(client != null) { 112 client.stop(); 113 } 114 } 115 116 117 public void localAddressSet(Address addr) { 118 if(initial_hosts != null && local_addr != null) { 120 List hlist; 121 boolean inInitialHosts=false; 122 for(Enumeration en=initial_hosts.elements(); en.hasMoreElements() && !inInitialHosts;) { 123 hlist=(List)en.nextElement(); 124 if(hlist.contains(local_addr)) { 125 inInitialHosts=true; 126 } 127 } 128 if(!inInitialHosts) { 129 hlist=new List(); 130 hlist.add(local_addr); 131 initial_hosts.add(hlist); 132 if(log.isDebugEnabled()) 133 log.debug("adding my address (" + local_addr + ") to initial_hosts; initial_hosts=" + initial_hosts); 134 } 135 } 136 } 137 138 139 public void handleConnect() { 140 if(client != null) 141 client.register(group_addr, local_addr); 142 } 143 144 public void handleDisconnect() { 145 if(client != null) 146 client.stop(); 147 } 148 149 150 151 public void sendGetMembersRequest() { 152 Message msg; 153 PingHeader hdr; 154 Vector gossip_rsps=null; 155 156 if(client != null) { 157 gossip_rsps=client.getMembers(group_addr); 158 if(gossip_rsps != null && gossip_rsps.size() > 0) { 159 Event view_event=new Event(Event.TMP_VIEW, makeView(gossip_rsps)); 162 passDown(view_event); } 164 else { 165 passUp(new Event(Event.FIND_INITIAL_MBRS_OK, null)); 166 return; 167 } 168 169 if(gossip_rsps != null && gossip_rsps.size() > 0) { 170 for(int i=0; i < gossip_rsps.size(); i++) { 171 Address dest=(Address)gossip_rsps.elementAt(i); 172 msg=new Message(dest, null, null); msg.putHeader(getName(), new PingHeader(PingHeader.GET_MBRS_REQ, null)); 174 passDown(new Event(Event.MSG, msg)); 175 } 176 } 177 178 Util.sleep(500); 179 } 180 else { 181 if(initial_hosts != null && initial_hosts.size() > 0) { 182 IpAddress h; 183 List hlist; 184 int numMemberInitialHosts; 185 int numMembers; 186 Address coord; 187 msg=new Message(null, null, null); 188 msg.putHeader(getName(), new PingHeader(PingHeader.GET_MBRS_REQ, null)); 189 190 synchronized(members) { 191 numMembers=members.size(); 192 numMemberInitialHosts=0; 193 coord=numMembers > 0 ? (Address)members.firstElement() : local_addr; 194 } 195 for(Enumeration en=initial_hosts.elements(); en.hasMoreElements();) { 196 hlist=(List)en.nextElement(); 197 boolean isMember=false; 198 199 for(Enumeration hen=hlist.elements(); hen.hasMoreElements() && !isMember;) { 211 h=(IpAddress)hen.nextElement(); 212 msg.setDest(h); 213 if(log.isTraceEnabled()) 214 log.trace("[FIND_INITIAL_MBRS] sending PING request to " + msg.getDest()); 215 passDown(new Event(Event.MSG, msg.copy())); 216 } 217 } 218 } 219 else { 220 hdr=new PingHeader(PingHeader.GET_MBRS_REQ, null); 222 msg=new Message(null, null, null); msg.putHeader(getName(), hdr); sendMcastDiscoveryRequest(msg); 225 } 226 } 227 } 228 229 void sendMcastDiscoveryRequest(Message discovery_request) { 230 passDown(new Event(Event.MSG, discovery_request)); 231 } 232 233 234 235 236 237 240 private List createInitialHosts(String l) { 241 List tmp=new List(); 242 StringTokenizer tok=new StringTokenizer (l, ","); 243 String t; 244 245 while(tok.hasMoreTokens()) { 246 try { 247 t=tok.nextToken(); 248 String host=t.substring(0, t.indexOf('[')); 249 int port=Integer.parseInt(t.substring(t.indexOf('[') + 1, t.indexOf(']'))); 250 List hosts=new List(); 251 for(int i=port; i < port + port_range; i++) { 252 hosts.add(new IpAddress(host, i)); 253 } 254 tmp.add(hosts); 255 } 256 catch(NumberFormatException e) { 257 if(log.isErrorEnabled()) log.error("exeption is " + e); 258 } 259 } 260 return tmp; 261 } 262 263 264 } 265 | Popular Tags |