KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > Ping


1 // $Id: Ping.java,v 1.5 2005/04/20 20:25:50 belaban Exp $
2

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 JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Vector JavaDoc;
13
14
15
16
17 /**
18  Determines the initial set of members given a group name and properties and prints them to stdout. Does not
19  connect to the group, but - using a minimal stack of UPD/PING or TCP/TCPPING - only sends a FIND_INITIAL_MBRS
20  down the stack (PING or TCPPING has to be present for this to work) and waits for FIND_INITIAL_MBRS_OK. When
21  received, the results are printed and then the app terminates.<p>
22  To connect to any group, it is essential that the groupname given (-group) is the same as the one of the group
23  and the properties are the same as the bottom 2 layers of the group properties, e.g. if the group properties are:
24  <pre>
25  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)
26  </pre>
27  ,then the stack properties should be:
28  <pre>
29  TCP(start_port=7800):TCPPING(initial_hosts=daddy.nms.fnc.fujitsu.com[7800];port_range=2;timeout=5000;num_initial_members=3)
30  </pre>
31  @author Bela Ban, June 12 2001
32  */

33 public class Ping implements UpHandler {
34     Channel channel=null;
35     boolean print_all_events=false;
36
37
38     public Ping(String JavaDoc props, boolean trace, boolean printall) throws Exception JavaDoc {
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 JavaDoc groupname) {
47
48         try {
49             channel.connect(groupname);
50             channel.down(Event.FIND_INITIAL_MBRS_EVT);
51         }
52         catch(Exception JavaDoc e) {
53             System.err.println("Ping.go(): " + e);
54             System.exit(1);
55         }
56     }
57
58
59     /** UpHandler interface */
60     public void up(Event evt) {
61         Vector JavaDoc v;
62         PingRsp rsp;
63
64         if(evt.getType() == Event.FIND_INITIAL_MBRS_OK) {
65             v=(Vector JavaDoc)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 JavaDoc rsps) {
86         Hashtable JavaDoc votes=new Hashtable JavaDoc(); // coord address, list of members who voted for this guy
87
PingRsp rsp;
88         Vector JavaDoc 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 JavaDoc)votes.get(coord);
96             if(v == null) {
97                 v=new Vector JavaDoc();
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 JavaDoc votes) {
113         Object JavaDoc key;
114         Vector JavaDoc val;
115         for(Enumeration JavaDoc e=votes.keys(); e.hasMoreElements();) {
116             key=e.nextElement();
117             val=(Vector JavaDoc)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 JavaDoc[] args) {
125         Ping ping=null;
126         boolean trace=false;
127         String JavaDoc groupname=Util.shortName(Util.getHostname());
128         boolean printall=false;
129         String JavaDoc 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 JavaDoc 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