KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > pbcast > ParticipantGmsImpl


1 // $Id: ParticipantGmsImpl.java,v 1.9 2004/09/23 16:29:38 belaban Exp $
2

3 package org.jgroups.protocols.pbcast;
4
5 import org.jgroups.Address;
6 import org.jgroups.Event;
7 import org.jgroups.Message;
8 import org.jgroups.View;
9 import org.jgroups.util.Promise;
10
11 import java.util.Vector JavaDoc;
12
13
14 public class ParticipantGmsImpl extends GmsImpl {
15     private final Vector JavaDoc suspected_mbrs=new Vector JavaDoc(11);
16     private final Promise leave_promise=new Promise();
17
18
19     public ParticipantGmsImpl(GMS g) {
20         gms=g;
21     }
22
23
24     public void init() throws Exception JavaDoc {
25         super.init();
26         suspected_mbrs.removeAllElements();
27         leave_promise.reset();
28     }
29
30     public void join(Address mbr) {
31         wrongMethod("join");
32     }
33
34
35     /**
36      * Loop: determine coord. If coord is me --> handleLeave().
37      * Else send handleLeave() to coord until success
38      */

39     public void leave(Address mbr) {
40         Address coord;
41         int max_tries=3;
42         Object JavaDoc result;
43
44         leave_promise.reset();
45
46         if(mbr.equals(gms.local_addr))
47             leaving=true;
48
49         while((coord=gms.determineCoordinator()) != null && max_tries-- > 0) {
50             if(gms.local_addr.equals(coord)) { // I'm the coordinator
51
gms.becomeCoordinator();
52                 gms.getImpl().handleLeave(mbr, false); // regular leave
53
return;
54             }
55
56             if(log.isDebugEnabled()) log.debug("sending LEAVE request to " + coord);
57             sendLeaveMessage(coord, mbr);
58             synchronized(leave_promise) {
59                 result=leave_promise.getResult(gms.leave_timeout);
60                 if(result != null)
61                     break;
62             }
63         }
64         gms.becomeClient();
65     }
66
67
68     public void handleJoinResponse(JoinRsp join_rsp) {
69         wrongMethod("handleJoinResponse");
70     }
71
72     public void handleLeaveResponse() {
73         if(leave_promise == null) {
74             if(log.isErrorEnabled()) log.error("leave_promise is null");
75             return;
76         }
77         synchronized(leave_promise) {
78             leave_promise.setResult(Boolean.TRUE); // unblocks thread waiting in leave()
79
}
80     }
81
82
83     public void suspect(Address mbr) {
84         handleSuspect(mbr);
85     }
86
87
88     /** Removes previously suspected member from list of currently suspected members */
89     public void unsuspect(Address mbr) {
90         if(mbr != null)
91             suspected_mbrs.remove(mbr);
92     }
93
94
95     public JoinRsp handleJoin(Address mbr) {
96         wrongMethod("handleJoin");
97         return null;
98     }
99
100
101     public void handleLeave(Address mbr, boolean suspected) {
102         wrongMethod("handleLeave");
103     }
104
105
106     /**
107      * If we are leaving, we have to wait for the view change (last msg in the current view) that
108      * excludes us before we can leave.
109      * @param new_view The view to be installed
110      * @param digest If view is a MergeView, digest contains the seqno digest of all members and has to
111      * be set by GMS
112      */

113     public void handleViewChange(View new_view, Digest digest) {
114         Vector JavaDoc mbrs=new_view.getMembers();
115          if(log.isDebugEnabled()) log.debug("view=" + new_view);
116         suspected_mbrs.removeAllElements();
117         if(leaving && !mbrs.contains(gms.local_addr)) { // received a view in which I'm not member: ignore
118
return;
119         }
120         gms.installView(new_view, digest);
121     }
122
123
124     public void handleSuspect(Address mbr) {
125         Vector JavaDoc suspects=null;
126
127         if(mbr == null) return;
128         if(!suspected_mbrs.contains(mbr))
129             suspected_mbrs.addElement(mbr);
130         
131         if(log.isDebugEnabled()) log.debug("suspected mbr=" + mbr + ", suspected_mbrs=" + suspected_mbrs);
132
133         if(wouldIBeCoordinator()) {
134             if(log.isDebugEnabled()) log.debug("suspected mbr=" + mbr + "), members are " +
135                     gms.members + ", coord=" + gms.local_addr + ": I'm the new coord !");
136
137             suspects=(Vector JavaDoc)suspected_mbrs.clone();
138             suspected_mbrs.removeAllElements();
139             gms.becomeCoordinator();
140             gms.castViewChange(null, null, suspects);
141         }
142     }
143
144
145     /* ---------------------------------- Private Methods --------------------------------------- */
146
147     /**
148      * Determines whether this member is the new coordinator given a list of suspected members. This is
149      * computed as follows: the list of currently suspected members (suspected_mbrs) is removed from the current
150      * membership. If the first member of the resulting list is equals to the local_addr, then it is true,
151      * otherwise false. Example: own address is B, current membership is {A, B, C, D}, suspected members are {A,
152      * D}. The resulting list is {B, C}. The first member of {B, C} is B, which is equal to the
153      * local_addr. Therefore, true is returned.
154      */

155     boolean wouldIBeCoordinator() {
156         Address new_coord=null;
157         Vector JavaDoc mbrs=gms.members.getMembers(); // getMembers() returns a *copy* of the membership vector
158

159         for(int i=0; i < suspected_mbrs.size(); i++)
160             mbrs.removeElement(suspected_mbrs.elementAt(i));
161
162         if(mbrs.size() < 1) return false;
163         new_coord=(Address)mbrs.elementAt(0);
164         return gms.local_addr.equals(new_coord);
165     }
166
167
168     void sendLeaveMessage(Address coord, Address mbr) {
169         Message msg=new Message(coord, null, null);
170         GMS.GmsHeader hdr=new GMS.GmsHeader(GMS.GmsHeader.LEAVE_REQ, mbr);
171
172         msg.putHeader(gms.getName(), hdr);
173         gms.passDown(new Event(Event.MSG, msg));
174     }
175
176
177     /* ------------------------------ End of Private Methods ------------------------------------ */
178
179 }
180
Popular Tags