001: // $Id: Ping.java,v 1.7 2005/05/30 16:15:11 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import org.jgroups.*;
006: import org.jgroups.protocols.PingRsp;
007: import org.jgroups.util.Util;
008:
009: import java.util.Enumeration;
010: import java.util.Hashtable;
011: import java.util.Vector;
012:
013: /**
014: Determines the initial set of members given a group name and properties and prints them to stdout. Does not
015: connect to the group, but - using a minimal stack of UPD/PING or TCP/TCPPING - only sends a FIND_INITIAL_MBRS
016: down the stack (PING or TCPPING has to be present for this to work) and waits for FIND_INITIAL_MBRS_OK. When
017: received, the results are printed and then the app terminates.<p>
018: To connect to any group, it is essential that the groupname given (-group) is the same as the one of the group
019: and the properties are the same as the bottom 2 layers of the group properties, e.g. if the group properties are:
020: <pre>
021: TCP(start_port=7800):TCPPING(initial_hosts=daddy.nms.fnc.fujitsu.com[7800];port_range=2;timeout=5000;num_initial_members=3;up_thread=true;down_thread=true):pbcast.STABLE(desired_avg_gossip=200000;down_thread=false;up_thread=false):pbcast.NAKACK(down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=false;down_thread=true;up_thread=true)
022: </pre>
023: ,then the stack properties should be:
024: <pre>
025: TCP(start_port=7800):TCPPING(initial_hosts=daddy.nms.fnc.fujitsu.com[7800];port_range=2;timeout=5000;num_initial_members=3)
026: </pre>
027: @author Bela Ban, June 12 2001
028: */
029: public class Ping implements UpHandler {
030: Channel channel = null;
031: boolean print_all_events = false;
032:
033: public Ping(String props, boolean trace, boolean printall)
034: throws Exception {
035: print_all_events = printall;
036: if (trace)
037: channel = new JChannel(props);
038: channel.setUpHandler(this );
039: }
040:
041: public void go(String groupname) {
042:
043: try {
044: channel.connect(groupname);
045: channel.down(Event.FIND_INITIAL_MBRS_EVT);
046: } catch (Exception e) {
047: System.err.println("Ping.go(): " + e);
048: System.exit(1);
049: }
050: }
051:
052: /** UpHandler interface */
053: public void up(Event evt) {
054: Vector v;
055: PingRsp rsp;
056:
057: if (evt.getType() == Event.FIND_INITIAL_MBRS_OK) {
058: v = (Vector) evt.getArg();
059:
060: System.out.println("Found " + v.size() + " members");
061: for (int i = 0; i < v.size(); i++) {
062: rsp = (PingRsp) v.elementAt(i);
063: System.out.println("Rsp #" + (i + 1) + ": " + rsp);
064: }
065:
066: if (v.size() > 0)
067: verifyCoordinator(v);
068:
069: System.exit(1);
070: } else {
071: if (print_all_events)
072: System.out.println(">> " + evt);
073: }
074: }
075:
076: static void verifyCoordinator(Vector rsps) {
077: Hashtable votes = new Hashtable(); // coord address, list of members who voted for this guy
078: PingRsp rsp;
079: Vector v;
080: Address coord, mbr;
081:
082: for (int i = 0; i < rsps.size(); i++) {
083: rsp = (PingRsp) rsps.elementAt(i);
084: coord = rsp.getCoordAddress();
085: mbr = rsp.getAddress();
086: v = (Vector) votes.get(coord);
087: if (v == null) {
088: v = new Vector();
089: votes.put(coord, v);
090: }
091: if (!v.contains(mbr))
092: v.addElement(mbr);
093: }
094:
095: System.out.println("");
096: if (votes.size() > 1)
097: System.err.println("*** Found more than 1 coordinator !");
098:
099: printVotes(votes);
100: }
101:
102: static void printVotes(Hashtable votes) {
103: Object key;
104: Vector val;
105: for (Enumeration e = votes.keys(); e.hasMoreElements();) {
106: key = e.nextElement();
107: val = (Vector) votes.get(key);
108: System.out.println("\n\nCoord: " + key);
109: System.out.println("Votes: " + val + '\n');
110: }
111: }
112:
113: public static void main(String[] args) {
114: Ping ping = null;
115: boolean trace = false;
116: String groupname = Util.shortName(Util.getHostname());
117: boolean printall = false;
118: String props = "UDP(mcast_addr=224.0.0.200;mcast_port=7500;ip_ttl=0;"
119: + "ucast_send_buf_size=30000;ucast_recv_buf_size=60000):"
120: + "PING(timeout=5000;num_initial_members=30)";
121:
122: for (int i = 0; i < args.length; i++) {
123: if ("-help".equals(args[i])) {
124: usage();
125: return;
126: }
127: if ("-trace".equals(args[i])) {
128: trace = true;
129: continue;
130: }
131: if ("-printall".equals(args[i])) {
132: printall = true;
133: continue;
134: }
135: if ("-group".equals(args[i])) {
136: groupname = args[++i];
137: continue;
138: }
139: if ("-props".equals(args[i])) {
140: props = args[++i];
141: continue;
142: }
143: usage();
144: return;
145: }
146:
147: try {
148: ping = new Ping(props, trace, printall);
149: ping.go(groupname);
150: } catch (Exception e) {
151: System.err.println("Ping.main(): " + e);
152: System.exit(0);
153: }
154: }
155:
156: static void usage() {
157: System.out
158: .println("Ping [-help] [-trace] [-group <groupname>] [-props <properties>] [-printall]");
159: }
160:
161: }
|