1 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 ; 12 13 14 public class ParticipantGmsImpl extends GmsImpl { 15 private final Vector suspected_mbrs=new Vector (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 { 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 39 public void leave(Address mbr) { 40 Address coord; 41 int max_tries=3; 42 Object 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)) { gms.becomeCoordinator(); 52 gms.getImpl().handleLeave(mbr, false); 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); } 80 } 81 82 83 public void suspect(Address mbr) { 84 handleSuspect(mbr); 85 } 86 87 88 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 113 public void handleViewChange(View new_view, Digest digest) { 114 Vector 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)) { return; 119 } 120 gms.installView(new_view, digest); 121 } 122 123 124 public void handleSuspect(Address mbr) { 125 Vector 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 )suspected_mbrs.clone(); 138 suspected_mbrs.removeAllElements(); 139 gms.becomeCoordinator(); 140 gms.castViewChange(null, null, suspects); 141 } 142 } 143 144 145 146 147 155 boolean wouldIBeCoordinator() { 156 Address new_coord=null; 157 Vector mbrs=gms.members.getMembers(); 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 178 179 } 180 | Popular Tags |