1 package org.jgroups.protocols;
2 
3 import org.jgroups.*;
4 import org.jgroups.annotations.DeprecatedProperty;
5 import org.jgroups.annotations.Property;
6 import org.jgroups.util.Promise;
7 import org.jgroups.util.UUID;
8 
9 import java.util.Arrays;
10 import java.util.List;
11 
12 
13 /**
14  * The PING protocol layer retrieves the initial membership (used by the GMS when started
15  * by sending event FIND_INITIAL_MBRS down the stack). We do this by mcasting PING
16  * requests to an IP MCAST address.
17  * The responses should allow us to determine the coordinator whom we have to
18  * contact, e.g. in case we want to join the group.  When we are a server (after having
19  * received the BECOME_SERVER event), we'll respond to PING requests with a PING
20  * response.
21  * @author Bela Ban
22  */
23 @DeprecatedProperty(names={"gossip_host", "gossip_port", "gossip_refresh", "socket_conn_timeout",
24                            "socket_read_timeout", "discovery_timeout"})
25 public class PING extends Discovery {
26 
27     /* -----------------------------------------    Properties     -------------------------------------------------- */
28 
29     @Property(description="Time (in ms) to wait for our own discovery message to be received. 0 means don't wait. If the " +
30             "discovery message is not received within discovery_timeout ms, a warning will be logged")
31     private long discovery_timeout=0L;
32 
33     /* --------------------------------------------- Fields ------------------------------------------------------ */
34 
35     protected final Promise<Boolean>   discovery_reception=new Promise<Boolean>();
36 
37 
stop()38     public void stop() {
39         super.stop();
40         discovery_reception.reset();
41     }
42 
43 
sendGetMembersRequest(String cluster_name, Promise promise, boolean return_views_only)44     public void sendGetMembersRequest(String cluster_name, Promise promise, boolean return_views_only) throws Exception{
45         //  Mcast GET_MBRS_REQ message
46         PhysicalAddress physical_addr=(PhysicalAddress)down(new Event(Event.GET_PHYSICAL_ADDRESS, local_addr));
47         List<PhysicalAddress> physical_addrs=Arrays.asList(physical_addr);
48         PingData data=new PingData(local_addr, null, false, UUID.get(local_addr), physical_addrs);
49         PingHeader hdr=new PingHeader(PingHeader.GET_MBRS_REQ, data, cluster_name);
50         hdr.return_view_only=return_views_only;
51         Message msg=new Message(null);  // mcast msg
52         msg.setFlag(Message.OOB);
53         msg.putHeader(this.id, hdr); // needs to be getName(), so we might get "MPING" !
54         sendMcastDiscoveryRequest(msg);
55     }
56 
57 
up(Event evt)58     public Object up(Event evt) {
59         if(evt.getType() == Event.MSG) {
60             Message msg=(Message)evt.getArg();
61             PingHeader hdr=(PingHeader)msg.getHeader(this.id);
62             if(hdr != null && hdr.type == PingHeader.GET_MBRS_REQ && msg.getSrc().equals(local_addr)) {
63                 discovery_reception.setResult(true);
64             }
65         }
66         return super.up(evt);
67     }
68 
sendMcastDiscoveryRequest(Message discovery_request)69     void sendMcastDiscoveryRequest(Message discovery_request) {
70         discovery_reception.reset();
71         down_prot.down(new Event(Event.MSG, discovery_request));
72         waitForDiscoveryRequestReception();
73     }
74 
75 
waitForDiscoveryRequestReception()76     protected void waitForDiscoveryRequestReception() {
77         if(discovery_timeout > 0) {
78             try {
79                 discovery_reception.getResultWithTimeout(discovery_timeout);
80             }
81             catch(TimeoutException e) {
82                 if(log.isWarnEnabled())
83                     log.warn("didn't receive my own discovery request - multicast socket might not be configured correctly");
84             }
85         }
86     }
87 }