001: package org.jgroups.util;
002:
003: import org.jgroups.TimeoutException;
004: import org.jgroups.View;
005: import org.jgroups.ViewId;
006:
007: import java.util.ArrayList;
008: import java.util.HashSet;
009: import java.util.Set;
010: import java.util.Vector;
011:
012: /**
013: * @author Bela Ban
014: * @version $Id: AckCollector.java,v 1.11 2006/10/30 11:19:20 belaban Exp $
015: */
016: public class AckCollector {
017: /** List<Object>: list of members from whom we haven't received an ACK yet */
018: private final java.util.List missing_acks;
019: private final Set received_acks = new HashSet();
020: private final Promise all_acks_received = new Promise();
021: private ViewId proposed_view;
022: private final Set suspected_mbrs = new HashSet();
023:
024: public AckCollector() {
025: missing_acks = new ArrayList();
026: }
027:
028: public AckCollector(ViewId v, java.util.List l) {
029: missing_acks = new ArrayList(l);
030: proposed_view = v;
031: }
032:
033: public String printMissing() {
034: synchronized (this ) {
035: return missing_acks.toString();
036: }
037: }
038:
039: public String printReceived() {
040: synchronized (this ) {
041: return received_acks.toString();
042: }
043: }
044:
045: public ViewId getViewId() {
046: return proposed_view;
047: }
048:
049: public void reset(ViewId v, java.util.List l) {
050: synchronized (this ) {
051: suspected_mbrs.clear();
052: proposed_view = v;
053: missing_acks.clear();
054: received_acks.clear();
055: if (l != null)
056: missing_acks.addAll(l);
057: missing_acks.removeAll(suspected_mbrs);
058: all_acks_received.reset();
059: }
060: }
061:
062: public int size() {
063: synchronized (this ) {
064: return missing_acks.size();
065: }
066: }
067:
068: public void ack(Object member) {
069: synchronized (this ) {
070: missing_acks.remove(member);
071: received_acks.add(member);
072: if (missing_acks.size() == 0)
073: all_acks_received.setResult(Boolean.TRUE);
074: }
075: }
076:
077: public void suspect(Object member) {
078: synchronized (this ) {
079: ack(member);
080: suspected_mbrs.add(member);
081: }
082: }
083:
084: public void unsuspect(Object member) {
085: synchronized (this ) {
086: suspected_mbrs.remove(member);
087: }
088: }
089:
090: public void handleView(View v) {
091: if (v == null)
092: return;
093: Vector mbrs = v.getMembers();
094: suspected_mbrs.retainAll(mbrs);
095: }
096:
097: public boolean waitForAllAcks() {
098: if (missing_acks.size() == 0)
099: return true;
100: Object result = all_acks_received.getResult();
101: return result != null && result instanceof Boolean
102: && ((Boolean) result).booleanValue();
103: }
104:
105: public boolean waitForAllAcks(long timeout) throws TimeoutException {
106: if (missing_acks.size() == 0)
107: return true;
108: Object result = all_acks_received.getResultWithTimeout(timeout);
109: return result != null && result instanceof Boolean
110: && ((Boolean) result).booleanValue();
111: }
112:
113: public String toString() {
114: return "missing=" + printMissing() + ", received="
115: + printReceived();
116: }
117: }
|