KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > util > AckCollector


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 JavaDoc;
8 import java.util.HashSet JavaDoc;
9 import java.util.Set JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 /**
13  * @author Bela Ban
14  * @version $Id: AckCollector.java,v 1.11 2006/10/30 11:19:20 belaban Exp $
15  */

16 public class AckCollector {
17     /** List<Object>: list of members from whom we haven't received an ACK yet */
18     private final java.util.List JavaDoc missing_acks;
19     private final Set JavaDoc received_acks=new HashSet JavaDoc();
20     private final Promise all_acks_received=new Promise();
21     private ViewId proposed_view;
22     private final Set JavaDoc suspected_mbrs=new HashSet JavaDoc();
23
24
25     public AckCollector() {
26         missing_acks=new ArrayList JavaDoc();
27     }
28
29     public AckCollector(ViewId v, java.util.List JavaDoc l) {
30         missing_acks=new ArrayList JavaDoc(l);
31         proposed_view=v;
32     }
33
34     public String JavaDoc printMissing() {
35         synchronized(this) {
36             return missing_acks.toString();
37         }
38     }
39
40     public String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc member) {
79         synchronized(this) {
80             ack(member);
81             suspected_mbrs.add(member);
82         }
83     }
84
85     public void unsuspect(Object JavaDoc 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 JavaDoc 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 JavaDoc result=all_acks_received.getResult();
101         return result != null && result instanceof Boolean JavaDoc && ((Boolean JavaDoc)result).booleanValue();
102     }
103
104     public boolean waitForAllAcks(long timeout) throws TimeoutException {
105         if(missing_acks.size() == 0)
106             return true;
107         Object JavaDoc result=all_acks_received.getResultWithTimeout(timeout);
108         return result != null && result instanceof Boolean JavaDoc && ((Boolean JavaDoc)result).booleanValue();
109     }
110
111     public String JavaDoc toString() {
112         return "missing=" + printMissing() + ", received=" + printReceived();
113     }
114 }
115
Popular Tags