KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jgroups.protocols;
4
5 import org.jgroups.Address;
6 import org.jgroups.View;
7 import org.jgroups.ViewId;
8 import org.jgroups.blocks.GroupRequest;
9 import org.jgroups.blocks.MethodCall;
10
11 import java.util.Vector JavaDoc;
12
13
14 public class ParticipantGmsImpl extends GmsImpl {
15     boolean leaving=false;
16     boolean received_final_view=false;
17     final Object JavaDoc leave_mutex=new Object JavaDoc();
18     final Vector JavaDoc suspected_mbrs=new Vector JavaDoc(5);
19     final Vector JavaDoc new_mbrs=new Vector JavaDoc(11);
20
21
22     public ParticipantGmsImpl(GMS g) {
23         gms=g;
24         init();
25     }
26
27
28     public void init() {
29         leaving=false;
30         received_final_view=false;
31         suspected_mbrs.removeAllElements();
32         new_mbrs.removeAllElements();
33         if(gms.members != null)
34             for(int i=0; i < gms.members.size(); i++)
35                 new_mbrs.addElement(gms.members.elementAt(i));
36     }
37
38
39     public void join(Address mbr) {
40         wrongMethod("join");
41     }
42
43
44     /**
45      Loop: determine coord. If coord is me --> handleLeave(). Else send handleLeave() to coord until
46      success.
47      */

48     public void leave(Address mbr) {
49         Address coord;
50         Boolean JavaDoc retval=null;
51
52         if(mbr.equals(gms.local_addr))
53             leaving=true;
54
55
56         while((coord=gms.determineCoordinator()) != null && !received_final_view) {
57             synchronized(leave_mutex) {
58                 if(gms.local_addr.equals(coord)) { // I'm the coordinator
59
gms.becomeCoordinator();
60                     gms.handleLeave(mbr, false); // regular leave
61
}
62                 else {
63                     try {
64
65                             if(log.isInfoEnabled()) log.info("sending LEAVE request to " + coord);
66                         MethodCall call = new MethodCall("handleLeave", new Object JavaDoc[] {mbr, Boolean.FALSE},
67                             new String JavaDoc[] {Address.class.getName(), boolean.class.getName()});
68                         gms.callRemoteMethod(coord, call, GroupRequest.GET_NONE, 0); // asynchronous
69
}
70                     catch(Exception JavaDoc e) {
71                     }
72                 }
73
74                 try {
75                     leave_mutex.wait(gms.leave_timeout);
76                 }
77                 catch(Exception JavaDoc e) {
78                 }
79             }
80         }
81         gms.becomeClient();
82     }
83
84
85     public void suspect(Address mbr) {
86         handleSuspect(mbr);
87     }
88
89
90     public void merge(Vector JavaDoc other_coords) {
91         wrongMethod("merge");
92     }
93
94
95     public boolean handleJoin(Address mbr) {
96         wrongMethod("handleJoin");
97         return false;
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      */

110     public void handleViewChange(ViewId new_view, Vector JavaDoc mbrs) {
111          if(log.isInfoEnabled()) log.info("mbrs are " + mbrs);
112
113         suspected_mbrs.removeAllElements();
114         new_mbrs.removeAllElements();
115         if(mbrs != null)
116             for(int i=0; i < mbrs.size(); i++)
117                 new_mbrs.addElement(mbrs.elementAt(i));
118
119         if(leaving) {
120             if(mbrs.contains(gms.local_addr)) {
121                 if(log.isWarnEnabled()) log.warn("received view in " +
122                                                                     "which I'm still a member, cannot quit yet");
123                 gms.installView(new_view, mbrs);
124             }
125             else {
126                 synchronized(leave_mutex) {
127                     received_final_view=true;
128                     leave_mutex.notifyAll();
129                 }
130             }
131             return;
132         }
133         gms.installView(new_view, mbrs);
134     }
135
136
137     // Only coordinators Handle Merge requests.
138
public View handleMerge(ViewId other_view, Vector JavaDoc other_members) {
139         wrongMethod("handleMerge");
140         return null;
141     }
142
143     public void handleSuspect(Address mbr) {
144         Vector JavaDoc suspects=null;
145
146         if(mbr == null) return;
147         if(!suspected_mbrs.contains(mbr))
148             suspected_mbrs.addElement(mbr);
149         new_mbrs.removeElement(mbr);
150
151
152             if(log.isInfoEnabled()) log.info("suspected mbr=" + mbr +
153                                                              "\nsuspected_mbrs=" + suspected_mbrs + "\nnew_mbrs=" + new_mbrs);
154
155         if(new_mbrs.size() > 0 && new_mbrs.elementAt(0).equals(gms.local_addr)) {
156
157                 if(log.isInfoEnabled()) log.info("suspected mbr=" + mbr + ", members are " + gms.members +
158                            ", coord=" + gms.local_addr + ": I'm the new coord !");
159
160             suspects=(Vector JavaDoc) suspected_mbrs.clone();
161             suspected_mbrs.removeAllElements();
162             new_mbrs.removeAllElements();
163             gms.becomeCoordinator();
164             gms.castViewChange(null, null, suspects);
165         }
166     }
167
168
169 }
170
Popular Tags