KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: RspList.java,v 1.4 2004/09/23 16:29:56 belaban Exp $
2

3 package org.jgroups.util;
4
5
6 import org.jgroups.Address;
7
8 import java.util.Vector JavaDoc;
9
10
11 /**
12  * Contains responses from all members. Marks faulty members.
13  * A RspList is a response list used in peer-to-peer protocols.
14  */

15 public class RspList {
16     final Vector JavaDoc rsps=new Vector JavaDoc();
17
18
19     public void reset()
20     {
21         rsps.removeAllElements();
22     }
23
24
25     public void addRsp(Address sender, Object JavaDoc retval)
26     {
27         Rsp rsp=find(sender);
28
29         if(rsp != null)
30         {
31             rsp.sender=sender; rsp.retval=retval; rsp.received=true; rsp.suspected=false;
32             return;
33         }
34         rsps.addElement(new Rsp(sender, retval));
35     }
36
37
38     public void addNotReceived(Address sender)
39     {
40         Rsp rsp=find(sender);
41
42         if(rsp == null)
43             rsps.addElement(new Rsp(sender));
44     }
45
46
47
48     public void addSuspect(Address sender)
49     {
50         Rsp rsp=find(sender);
51
52         if(rsp != null)
53         {
54             rsp.sender=sender; rsp.retval=null; rsp.received=false; rsp.suspected=true;
55             return;
56         }
57         rsps.addElement(new Rsp(sender, true));
58     }
59
60
61     public boolean isReceived(Address sender)
62     {
63         Rsp rsp=find(sender);
64
65         if(rsp ==null) return false;
66         return rsp.received;
67     }
68
69
70     public int numSuspectedMembers()
71     {
72         int num=0;
73         Rsp rsp;
74
75         for(int i=0; i < rsps.size(); i++)
76         {
77             rsp=(Rsp)rsps.elementAt(i);
78             if(rsp.wasSuspected())
79                 num++;
80         }
81         return num;
82     }
83
84
85     public Object JavaDoc getFirst()
86     {
87         return rsps.size() > 0 ? ((Rsp)rsps.elementAt(0)).getValue() : null;
88     }
89
90
91     /** Returns the results from non-suspected members that are not null. */
92     public Vector JavaDoc getResults()
93     {
94         Vector JavaDoc ret=new Vector JavaDoc();
95         Rsp rsp;
96         Object JavaDoc val;
97
98         for(int i=0; i < rsps.size(); i++)
99         {
100             rsp=(Rsp)rsps.elementAt(i);
101             if(rsp.wasReceived() && (val=rsp.getValue()) != null)
102                 ret.addElement(val);
103         }
104         return ret;
105     }
106
107
108     public Vector JavaDoc getSuspectedMembers()
109     {
110         Vector JavaDoc retval=new Vector JavaDoc();
111         Rsp rsp;
112
113         for(int i=0; i < rsps.size(); i++)
114         {
115             rsp=(Rsp)rsps.elementAt(i);
116             if(rsp.wasSuspected())
117                 retval.addElement(rsp.getSender());
118         }
119         return retval;
120     }
121
122
123     public boolean isSuspected(Address sender)
124     {
125         Rsp rsp=find(sender);
126
127         if(rsp ==null) return false;
128         return rsp.suspected;
129     }
130
131
132     public Object JavaDoc get(Address sender)
133     {
134         Rsp rsp=find(sender);
135
136         if(rsp == null) return null;
137         return rsp.retval;
138     }
139
140
141     public int size()
142     {
143         return rsps.size();
144     }
145
146     public Object JavaDoc elementAt(int i) throws ArrayIndexOutOfBoundsException JavaDoc
147     {
148         return rsps.elementAt(i);
149     }
150
151
152     public String JavaDoc toString()
153     {
154         StringBuffer JavaDoc ret=new StringBuffer JavaDoc();
155         Rsp rsp;
156
157         for(int i=0; i < rsps.size(); i++)
158         {
159             rsp=(Rsp)rsps.elementAt(i);
160             ret.append("[" + rsp + "]\n");
161         }
162         return ret.toString();
163     }
164
165
166
167
168
169     boolean contains(Address sender)
170     {
171         Rsp rsp;
172
173         for(int i=0; i < rsps.size(); i++)
174         {
175             rsp=(Rsp)rsps.elementAt(i);
176
177             if(rsp.sender != null && sender != null && rsp.sender.equals(sender))
178                 return true;
179         }
180         return false;
181     }
182
183
184     Rsp find(Address sender)
185     {
186         Rsp rsp;
187
188         for(int i=0; i < rsps.size(); i++)
189         {
190             rsp=(Rsp)rsps.elementAt(i);
191             if(rsp.sender != null && sender != null && rsp.sender.equals(sender))
192                 return rsp;
193         }
194         return null;
195     }
196
197
198
199
200 }
201
Popular Tags