1 package org.jgroups.util; 2 3 import org.jgroups.TimeoutException; 4 import org.jgroups.View; 5 import org.jgroups.ViewId; 6 7 import java.util.ArrayList ; 8 import java.util.HashSet ; 9 import java.util.Set ; 10 import java.util.Vector ; 11 12 16 public class AckCollector { 17 18 private final java.util.List missing_acks; 19 private final Set received_acks=new HashSet (); 20 private final Promise all_acks_received=new Promise(); 21 private ViewId proposed_view; 22 private final Set suspected_mbrs=new HashSet (); 23 24 25 public AckCollector() { 26 missing_acks=new ArrayList (); 27 } 28 29 public AckCollector(ViewId v, java.util.List l) { 30 missing_acks=new ArrayList (l); 31 proposed_view=v; 32 } 33 34 public String printMissing() { 35 synchronized(this) { 36 return missing_acks.toString(); 37 } 38 } 39 40 public String printReceived() { 41 synchronized(this) { 42 return received_acks.toString(); 43 } 44 } 45 46 public ViewId getViewId() { 47 return proposed_view; 48 } 49 50 public void reset(ViewId v, java.util.List l) { 51 synchronized(this) { 52 suspected_mbrs.clear(); 53 proposed_view=v; 54 missing_acks.clear(); 55 received_acks.clear(); 56 if(l != null) 57 missing_acks.addAll(l); 58 missing_acks.removeAll(suspected_mbrs); 59 all_acks_received.reset(); 60 } 61 } 62 63 public int size() { 64 synchronized(this) { 65 return missing_acks.size(); 66 } 67 } 68 69 public void ack(Object member) { 70 synchronized(this) { 71 missing_acks.remove(member); 72 received_acks.add(member); 73 if(missing_acks.size() == 0) 74 all_acks_received.setResult(Boolean.TRUE); 75 } 76 } 77 78 public void suspect(Object member) { 79 synchronized(this) { 80 ack(member); 81 suspected_mbrs.add(member); 82 } 83 } 84 85 public void unsuspect(Object member) { 86 synchronized(this) { 87 suspected_mbrs.remove(member); 88 } 89 } 90 91 public void handleView(View v) { 92 if(v == null) return; 93 Vector mbrs=v.getMembers(); 94 suspected_mbrs.retainAll(mbrs); 95 } 96 97 public boolean waitForAllAcks() { 98 if(missing_acks.size() == 0) 99 return true; 100 Object result=all_acks_received.getResult(); 101 return result != null && result instanceof Boolean && ((Boolean )result).booleanValue(); 102 } 103 104 public boolean waitForAllAcks(long timeout) throws TimeoutException { 105 if(missing_acks.size() == 0) 106 return true; 107 Object result=all_acks_received.getResultWithTimeout(timeout); 108 return result != null && result instanceof Boolean && ((Boolean )result).booleanValue(); 109 } 110 111 public String toString() { 112 return "missing=" + printMissing() + ", received=" + printReceived(); 113 } 114 } 115 | Popular Tags |