KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: TCPPING.java,v 1.23 2005/04/20 20:25:47 belaban Exp $
2

3 package org.jgroups.protocols;
4
5
6 import org.jgroups.Address;
7 import org.jgroups.Event;
8 import org.jgroups.Message;
9 import org.jgroups.stack.IpAddress;
10
11 import java.util.*;
12
13
14 /**
15  * The TCPPING protocol layer retrieves the initial membership in answer to the GMS's
16  * FIND_INITIAL_MBRS event. The initial membership is retrieved by directly contacting other group
17  * members, sending point-to-point mebership requests. The responses should allow us to determine
18  * the coordinator whom we have to contact in case we want to join the group. When we are a server
19  * (after having received the BECOME_SERVER event), we'll respond to TCPPING requests with a TCPPING
20  * response.
21  * <p>
22  * The FIND_INITIAL_MBRS event will eventually be answered with a FIND_INITIAL_MBRS_OK event up
23  * the stack.
24  * <p>
25  * The TCPPING protocol requires a static conifiguration, which assumes that you to know in advance
26  * where to find other members of your group. For dynamic discovery, use the PING protocol, which
27  * uses multicast discovery, or the TCPGOSSIP protocol, which contacts a Gossip Router to acquire
28  * the initial membership.
29  *
30  * @author Bela Ban
31  */

32 public class TCPPING extends Discovery {
33     int port_range=1; // number of ports to be probed for initial membership
34

35     /** List<IpAddress> */
36     ArrayList initial_hosts=null; // hosts to be contacted for the initial membership
37
final static String JavaDoc name="TCPPING";
38
39
40
41     public String JavaDoc getName() {
42         return name;
43     }
44
45
46
47     public boolean setProperties(Properties props) {
48         String JavaDoc str;
49
50         str=props.getProperty("port_range"); // if member cannot be contacted on base port,
51
if(str != null) { // how many times can we increment the port
52
port_range=Integer.parseInt(str);
53             if (port_range < 1) {
54                port_range = 1;
55             }
56             props.remove("port_range");
57         }
58
59         str=props.getProperty("initial_hosts");
60         if(str != null) {
61             props.remove("initial_hosts");
62             initial_hosts=createInitialHosts(str);
63         }
64
65         return super.setProperties(props);
66     }
67
68
69     public void localAddressSet(Address addr) {
70         // Add own address to initial_hosts if not present: we must always be able to ping ourself !
71
if(initial_hosts != null && addr != null) {
72             if(initial_hosts.contains(addr)) {
73                 initial_hosts.remove(addr);
74                 if(log.isDebugEnabled()) log.debug("[SET_LOCAL_ADDRESS]: removing my own address (" + addr +
75                                                    ") from initial_hosts; initial_hosts=" + initial_hosts);
76             }
77         }
78     }
79
80
81     public void sendGetMembersRequest() {
82         Message msg;
83
84         for(Iterator it=initial_hosts.iterator(); it.hasNext();) {
85             Address addr=(Address)it.next();
86             // if(tmpMbrs.contains(addr)) {
87
// ; // continue; // changed as suggested by Mark Kopec
88
// }
89
msg=new Message(addr, null, null);
90             msg.putHeader(name, new PingHeader(PingHeader.GET_MBRS_REQ, null));
91
92             if(log.isTraceEnabled()) log.trace("[FIND_INITIAL_MBRS] sending PING request to " + msg.getDest());
93             passDown(new Event(Event.MSG, msg));
94         }
95     }
96
97
98
99     /* -------------------------- Private methods ---------------------------- */
100
101     /**
102      * Input is "daddy[8880],sindhu[8880],camille[5555]. Return List of IpAddresses
103      */

104     private ArrayList createInitialHosts(String JavaDoc l) {
105         StringTokenizer tok=new StringTokenizer(l, ",");
106         String JavaDoc t;
107         IpAddress addr;
108         ArrayList retval=new ArrayList();
109
110         while(tok.hasMoreTokens()) {
111             try {
112                 t=tok.nextToken();
113                 String JavaDoc host=t.substring(0, t.indexOf('['));
114                 int port=Integer.parseInt(t.substring(t.indexOf('[') + 1, t.indexOf(']')));
115                 for(int i=port; i < port + port_range; i++) {
116                     addr=new IpAddress(host, i);
117                     retval.add(addr);
118                 }
119             }
120             catch(NumberFormatException JavaDoc e) {
121                 if(log.isErrorEnabled()) log.error("exeption is " + e);
122             }
123         }
124
125         return retval;
126     }
127
128 }
129
130
Popular Tags