KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > ring > RingToken


1 //$Id: RingToken.java,v 1.5 2004/09/15 17:40:59 belaban Exp $
2

3 package org.jgroups.protocols.ring;
4
5 import org.jgroups.Address;
6
7 import java.io.Externalizable JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.ObjectInput JavaDoc;
10 import java.io.ObjectOutput JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.TreeSet JavaDoc;
13
14
15 public class RingToken implements Externalizable JavaDoc
16 {
17
18    public static final int OPERATIONAL = 0;
19    public static final int RECOVERY = 1;
20
21    private int type = -1;
22    private long tokenSeq;
23    private long seq;
24    private long aru;
25    private int fcc;
26    private int backlog;
27    private int windowSize;
28    private int windowThreshold;
29    private Address aruId;
30    private Collection JavaDoc retransmissionRequests;
31    private Collection JavaDoc recoveredMembers;
32
33    public RingToken()
34    {
35       this(OPERATIONAL);
36    }
37
38    public RingToken(int type)
39    {
40       if (type != OPERATIONAL && type != RECOVERY)
41       {
42          throw new IllegalArgumentException JavaDoc("Illegal Ring type");
43       }
44       this.type = type;
45       initToken();
46    }
47
48    private void initToken()
49    {
50       retransmissionRequests = new TreeSet JavaDoc();
51    }
52
53    public void setAruId(Address address)
54    {
55       aruId= address;
56    }
57
58    public Address getAruId()
59    {
60       return aruId;
61    }
62
63    public int getType()
64    {
65       return type;
66    }
67
68    public void setType(int type)
69    {
70       if (type != OPERATIONAL && type != RECOVERY)
71       {
72          throw new IllegalArgumentException JavaDoc("Illegal Ring type");
73       }
74       this.type = type;
75
76    }
77
78
79    public long getTokenSequence()
80    {
81       return tokenSeq;
82    }
83
84    public void incrementTokenSequence()
85    {
86       tokenSeq++;
87    }
88
89    public long getHighestSequence()
90    {
91       return seq;
92    }
93
94    public void setHighestSequence(long highestSequence)
95    {
96       if (seq > highestSequence)
97          throw new IllegalArgumentException JavaDoc("Can not set highest sequence to be" +
98                                             " lower than current higest sequence " + seq);
99       this.seq = highestSequence;
100    }
101
102    public long getAllReceivedUpto()
103    {
104       return aru;
105    }
106
107    public void setAllReceivedUpto(long aru)
108    {
109       this.aru = aru;
110
111       if (aru > seq)
112       {
113          seq = aru;
114       }
115    }
116
117    public int getLastRoundBroadcastCount()
118    {
119       return fcc;
120    }
121
122    public void addLastRoundBroadcastCount(int transmitCount)
123    {
124       this.fcc += transmitCount;
125       if (fcc < 0)
126          fcc = 0;
127    }
128
129    public int getBacklog()
130    {
131       return backlog;
132    }
133
134    public void addBacklog(int back)
135    {
136       this.backlog += back;
137
138       if (backlog < 0) backlog = 0;
139    }
140
141    public void setWindowSize(int newSize)
142    {
143       if (newSize < 0)
144       {
145          windowSize = 0;
146       }
147       else
148       {
149          windowSize = newSize;
150       }
151    }
152
153    public void addRecoveredMember(Address member)
154    {
155
156       if (recoveredMembers == null)
157          recoveredMembers = new TreeSet JavaDoc();
158
159       recoveredMembers.add(member);
160    }
161
162    public Collection JavaDoc getRecoveredMembers()
163    {
164       return recoveredMembers;
165    }
166
167    public int getWindowSize()
168    {
169       return windowSize;
170    }
171
172    public void setWindowThreshold(int newSize)
173    {
174       if (newSize < 0)
175       {
176          windowThreshold = 0;
177       }
178       else
179       {
180          windowThreshold = newSize;
181       }
182    }
183
184    public int getWindowThreshold()
185    {
186       return windowThreshold;
187    }
188
189    public Collection JavaDoc getRetransmissionRequests()
190    {
191       return retransmissionRequests;
192    }
193
194    public void writeExternal(ObjectOutput JavaDoc oo) throws IOException JavaDoc
195    {
196       oo.writeLong(tokenSeq);
197       oo.writeLong(seq);
198       oo.writeInt(type);
199       oo.writeLong(aru);
200       oo.writeInt(fcc);
201       oo.writeInt(backlog);
202       oo.writeInt(windowSize);
203       oo.writeInt(windowThreshold);
204       oo.writeObject(aruId);
205       oo.writeObject(retransmissionRequests);
206       oo.writeObject(recoveredMembers);
207
208    }
209
210    public void readExternal(ObjectInput JavaDoc oi) throws IOException JavaDoc, ClassNotFoundException JavaDoc
211    {
212       tokenSeq = oi.readLong();
213       seq = oi.readLong();
214       type = oi.readInt();
215       aru = oi.readLong();
216       fcc = oi.readInt();
217       backlog = oi.readInt();
218       windowSize = oi.readInt();
219       windowThreshold = oi.readInt();
220       aruId = (Address)oi.readObject();
221       retransmissionRequests = (Collection JavaDoc) oi.readObject();
222       recoveredMembers = (Collection JavaDoc) oi.readObject();
223    }
224
225    public String JavaDoc toString()
226    {
227       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(200);
228       buf.append("Token[tokenSeq=").append(tokenSeq);
229       buf.append(",type=").append(type);
230       buf.append(",highestseq=").append(seq);
231       buf.append(",aru=").append(aru);
232       buf.append(",lastRoundTransmitCount=").append(fcc);
233       buf.append(",backlog=").append(backlog);
234       buf.append(",windowSize=").append(windowSize);
235       buf.append(",windowThreshold=").append(windowThreshold);
236       buf.append(",retransmissionList=").append(getRetransmissionRequests());
237       buf.append(']');
238       return buf.toString();
239    }
240 }
241
Popular Tags