001: // $Id: ParticipantGmsImpl.java,v 1.9.10.1 2007/04/27 08:03:51 belaban Exp $
002:
003: package org.jgroups.protocols;
004:
005: import org.jgroups.Address;
006: import org.jgroups.View;
007: import org.jgroups.ViewId;
008: import org.jgroups.blocks.GroupRequest;
009: import org.jgroups.blocks.MethodCall;
010:
011: import java.util.Vector;
012:
013: public class ParticipantGmsImpl extends GmsImpl {
014: boolean leaving = false;
015: boolean received_final_view = false;
016: final Object leave_mutex = new Object();
017: final Vector suspected_mbrs = new Vector(5);
018: final Vector new_mbrs = new Vector(11);
019:
020: public ParticipantGmsImpl(GMS g) {
021: gms = g;
022: init();
023: }
024:
025: final public void init() {
026: leaving = false;
027: received_final_view = false;
028: suspected_mbrs.removeAllElements();
029: new_mbrs.removeAllElements();
030: if (gms.mbrs != null)
031: for (int i = 0; i < gms.mbrs.size(); i++)
032: new_mbrs.addElement(gms.mbrs.elementAt(i));
033: }
034:
035: public void join(Address mbr) {
036: wrongMethod("join");
037: }
038:
039: /**
040: Loop: determine coord. If coord is me --> handleLeave(). Else send handleLeave() to coord until
041: success.
042: */
043: public void leave(Address mbr) {
044: Address coord;
045:
046: if (mbr.equals(gms.local_addr))
047: leaving = true;
048:
049: while ((coord = gms.determineCoordinator()) != null
050: && !received_final_view) {
051: synchronized (leave_mutex) {
052: if (gms.local_addr.equals(coord)) { // I'm the coordinator
053: gms.becomeCoordinator();
054: gms.handleLeave(mbr, false); // regular leave
055: } else {
056: try {
057:
058: if (log.isInfoEnabled())
059: log.info("sending LEAVE request to "
060: + coord);
061: MethodCall call = new MethodCall("handleLeave",
062: new Object[] { mbr, Boolean.FALSE },
063: new String[] { Address.class.getName(),
064: boolean.class.getName() });
065: gms.callRemoteMethod(coord, call,
066: GroupRequest.GET_NONE, 0); // asynchronous
067: } catch (Exception e) {
068: }
069: }
070:
071: try {
072: leave_mutex.wait(gms.leave_timeout);
073: } catch (Exception e) {
074: }
075: }
076: }
077: gms.becomeClient();
078: }
079:
080: public void suspect(Address mbr) {
081: handleSuspect(mbr);
082: }
083:
084: public void merge(Vector other_coords) {
085: wrongMethod("merge");
086: }
087:
088: public boolean handleJoin(Address mbr) {
089: wrongMethod("handleJoin");
090: return false;
091: }
092:
093: public void handleLeave(Address mbr, boolean suspected) {
094: wrongMethod("handleLeave");
095: }
096:
097: /**
098: If we are leaving, we have to wait for the view change (last msg in the current view) that
099: excludes us before we can leave.
100: */
101: public void handleViewChange(ViewId new_view, Vector mbrs) {
102: if (log.isInfoEnabled())
103: log.info("mbrs are " + mbrs);
104:
105: suspected_mbrs.removeAllElements();
106: new_mbrs.removeAllElements();
107: if (mbrs != null)
108: for (int i = 0; i < mbrs.size(); i++)
109: new_mbrs.addElement(mbrs.elementAt(i));
110:
111: if (leaving) {
112: if (mbrs != null && mbrs.contains(gms.local_addr)) {
113: if (log.isWarnEnabled())
114: log
115: .warn("received view in which I'm still a member, cannot quit yet");
116: gms.installView(new_view, mbrs);
117: } else {
118: synchronized (leave_mutex) {
119: received_final_view = true;
120: leave_mutex.notifyAll();
121: }
122: }
123: return;
124: }
125: gms.installView(new_view, mbrs);
126: }
127:
128: // Only coordinators Handle Merge requests.
129: public View handleMerge(ViewId other_view, Vector other_members) {
130: wrongMethod("handleMerge");
131: return null;
132: }
133:
134: public void handleSuspect(Address mbr) {
135: Vector suspects;
136:
137: if (mbr == null)
138: return;
139: if (!suspected_mbrs.contains(mbr))
140: suspected_mbrs.addElement(mbr);
141: new_mbrs.removeElement(mbr);
142:
143: if (log.isInfoEnabled())
144: log.info("suspected mbr=" + mbr + "\nsuspected_mbrs="
145: + suspected_mbrs + "\nnew_mbrs=" + new_mbrs);
146:
147: if (new_mbrs.size() > 0
148: && new_mbrs.elementAt(0).equals(gms.local_addr)) {
149:
150: if (log.isInfoEnabled())
151: log.info("suspected mbr=" + mbr + ", members are "
152: + gms.mbrs + ", coord=" + gms.local_addr
153: + ": I'm the new coord !");
154:
155: suspects = (Vector) suspected_mbrs.clone();
156: suspected_mbrs.removeAllElements();
157: new_mbrs.removeAllElements();
158: gms.becomeCoordinator();
159: gms.castViewChange(null, null, suspects);
160: }
161: }
162:
163: }
|