1 3 package org.jgroups.tests; 4 5 6 import org.jgroups.*; 7 import org.jgroups.protocols.PingRsp; 8 import org.jgroups.util.Util; 9 10 import java.util.Enumeration ; 11 import java.util.Hashtable ; 12 import java.util.Vector ; 13 14 15 16 17 33 public class Ping implements UpHandler { 34 Channel channel=null; 35 boolean print_all_events=false; 36 37 38 public Ping(String props, boolean trace, boolean printall) throws Exception { 39 print_all_events=printall; 40 if(trace) 41 channel=new JChannel(props); 42 channel.setUpHandler(this); 43 } 44 45 46 public void go(String groupname) { 47 48 try { 49 channel.connect(groupname); 50 channel.down(Event.FIND_INITIAL_MBRS_EVT); 51 } 52 catch(Exception e) { 53 System.err.println("Ping.go(): " + e); 54 System.exit(1); 55 } 56 } 57 58 59 60 public void up(Event evt) { 61 Vector v; 62 PingRsp rsp; 63 64 if(evt.getType() == Event.FIND_INITIAL_MBRS_OK) { 65 v=(Vector )evt.getArg(); 66 67 System.out.println("Found " + v.size() + " members"); 68 for(int i=0; i < v.size(); i++) { 69 rsp=(PingRsp)v.elementAt(i); 70 System.out.println("Rsp #" + (i + 1) + ": " + rsp); 71 } 72 73 if(v.size() > 0) 74 verifyCoordinator(v); 75 76 System.exit(1); 77 } 78 else { 79 if(print_all_events) 80 System.out.println(">> " + evt); 81 } 82 } 83 84 85 static void verifyCoordinator(Vector rsps) { 86 Hashtable votes=new Hashtable (); PingRsp rsp; 88 Vector v; 89 Address coord, mbr; 90 91 for(int i=0; i < rsps.size(); i++) { 92 rsp=(PingRsp)rsps.elementAt(i); 93 coord=rsp.getCoordAddress(); 94 mbr=rsp.getAddress(); 95 v=(Vector )votes.get(coord); 96 if(v == null) { 97 v=new Vector (); 98 votes.put(coord, v); 99 } 100 if(!v.contains(mbr)) 101 v.addElement(mbr); 102 } 103 104 System.out.println(""); 105 if(votes.size() > 1) 106 System.err.println("*** Found more than 1 coordinator !"); 107 108 printVotes(votes); 109 } 110 111 112 static void printVotes(Hashtable votes) { 113 Object key; 114 Vector val; 115 for(Enumeration e=votes.keys(); e.hasMoreElements();) { 116 key=e.nextElement(); 117 val=(Vector )votes.get(key); 118 System.out.println("\n\nCoord: " + key); 119 System.out.println("Votes: " + val + '\n'); 120 } 121 } 122 123 124 public static void main(String [] args) { 125 Ping ping=null; 126 boolean trace=false; 127 String groupname=Util.shortName(Util.getHostname()); 128 boolean printall=false; 129 String props="UDP(mcast_addr=224.0.0.200;mcast_port=7500;ip_ttl=0;" + 130 "ucast_send_buf_size=30000;ucast_recv_buf_size=60000):" + 131 "PING(timeout=5000;num_initial_members=30)"; 132 133 for(int i=0; i < args.length; i++) { 134 if("-help".equals(args[i])) { 135 usage(); 136 return; 137 } 138 if("-trace".equals(args[i])) { 139 trace=true; 140 continue; 141 } 142 if("-printall".equals(args[i])) { 143 printall=true; 144 continue; 145 } 146 if("-group".equals(args[i])) { 147 groupname=args[++i]; 148 continue; 149 } 150 if("-props".equals(args[i])) { 151 props=args[++i]; 152 continue; 153 } 154 usage(); 155 return; 156 } 157 158 159 try { 160 ping=new Ping(props, trace, printall); 161 ping.go(groupname); 162 } 163 catch(Exception e) { 164 System.err.println("Ping.main(): " + e); 165 System.exit(0); 166 } 167 } 168 169 170 static void usage() { 171 System.out.println("Ping [-help] [-trace] [-group <groupname>] [-props <properties>] [-printall]"); 172 } 173 174 175 } 176 177 178 179 180 181 | Popular Tags |