KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > WANPING


1 // $Id: WANPING.java,v 1.9 2005/01/04 08:18:31 belaban Exp $
2

3 package org.jgroups.protocols;
4
5
6 import org.jgroups.Event;
7 import org.jgroups.Message;
8 import org.jgroups.util.List;
9
10 import java.util.Enumeration JavaDoc;
11 import java.util.Properties JavaDoc;
12 import java.util.StringTokenizer JavaDoc;
13
14
15 /**
16  * Similar to TCPPING, except that the initial host list is specified as a list of logical pipe names.
17  */

18 public class WANPING extends Discovery {
19     int port_range=5; // number of ports to be probed for initial membership
20
List initial_hosts=null; // hosts to be contacted for the initial membership
21

22
23     public String JavaDoc getName() {
24         return "WANPING";
25     }
26
27
28     public boolean setProperties(Properties JavaDoc props) {
29         String JavaDoc str;
30         str=props.getProperty("port_range"); // if member cannot be contacted on base port,
31
if(str != null) { // how many times can we increment the port
32
port_range=Integer.parseInt(str);
33             props.remove("port_range");
34         }
35
36         str=props.getProperty("initial_hosts");
37         if(str != null) {
38             props.remove("initial_hosts");
39             initial_hosts=createInitialHosts(str);
40             if(log.isInfoEnabled()) log.info("initial_hosts: " + initial_hosts);
41         }
42
43         if(initial_hosts == null || initial_hosts.size() == 0) {
44             System.err.println("WANPING.setProperties(): hosts to contact for initial membership " +
45                                "not specified. Cannot determine coordinator !");
46             return false;
47         }
48         return super.setProperties(props);
49     }
50
51     public void sendGetMembersRequest() {
52         Message msg, copy;
53         PingHeader hdr;
54         String JavaDoc h;
55
56         hdr=new PingHeader(PingHeader.GET_MBRS_REQ, null);
57         msg=new Message(null, null, null);
58         msg.putHeader(getName(), hdr);
59
60         for(Enumeration JavaDoc en=initial_hosts.elements(); en.hasMoreElements();) {
61             h=(String JavaDoc)en.nextElement();
62             copy=msg.copy();
63             copy.setDest(new WanPipeAddress(h));
64             passDown(new Event(Event.MSG, copy));
65         }
66     }
67
68
69
70     /* -------------------------- Private methods ---------------------------- */
71
72     /**
73      * Input is "pipe1,pipe2". Return List of Strings
74      */

75     private List createInitialHosts(String JavaDoc l) {
76         List tmp=new List();
77         StringTokenizer JavaDoc tok=new StringTokenizer JavaDoc(l, ",");
78         String JavaDoc t;
79
80         while(tok.hasMoreTokens()) {
81             try {
82                 t=tok.nextToken();
83                 tmp.add(t.trim());
84             }
85             catch(NumberFormatException JavaDoc e) {
86                 System.err.println("WANPING.createInitialHosts(): " + e);
87             }
88         }
89         return tmp;
90     }
91
92
93 }
94
95
Popular Tags