001: //$Id: RingToken.java,v 1.5 2004/09/15 17:40:59 belaban Exp $
002:
003: package org.jgroups.protocols.ring;
004:
005: import org.jgroups.Address;
006:
007: import java.io.Externalizable;
008: import java.io.IOException;
009: import java.io.ObjectInput;
010: import java.io.ObjectOutput;
011: import java.util.Collection;
012: import java.util.TreeSet;
013:
014: public class RingToken implements Externalizable {
015:
016: public static final int OPERATIONAL = 0;
017: public static final int RECOVERY = 1;
018:
019: private int type = -1;
020: private long tokenSeq;
021: private long seq;
022: private long aru;
023: private int fcc;
024: private int backlog;
025: private int windowSize;
026: private int windowThreshold;
027: private Address aruId;
028: private Collection retransmissionRequests;
029: private Collection recoveredMembers;
030:
031: public RingToken() {
032: this (OPERATIONAL);
033: }
034:
035: public RingToken(int type) {
036: if (type != OPERATIONAL && type != RECOVERY) {
037: throw new IllegalArgumentException("Illegal Ring type");
038: }
039: this .type = type;
040: initToken();
041: }
042:
043: private void initToken() {
044: retransmissionRequests = new TreeSet();
045: }
046:
047: public void setAruId(Address address) {
048: aruId = address;
049: }
050:
051: public Address getAruId() {
052: return aruId;
053: }
054:
055: public int getType() {
056: return type;
057: }
058:
059: public void setType(int type) {
060: if (type != OPERATIONAL && type != RECOVERY) {
061: throw new IllegalArgumentException("Illegal Ring type");
062: }
063: this .type = type;
064:
065: }
066:
067: public long getTokenSequence() {
068: return tokenSeq;
069: }
070:
071: public void incrementTokenSequence() {
072: tokenSeq++;
073: }
074:
075: public long getHighestSequence() {
076: return seq;
077: }
078:
079: public void setHighestSequence(long highestSequence) {
080: if (seq > highestSequence)
081: throw new IllegalArgumentException(
082: "Can not set highest sequence to be"
083: + " lower than current higest sequence "
084: + seq);
085: this .seq = highestSequence;
086: }
087:
088: public long getAllReceivedUpto() {
089: return aru;
090: }
091:
092: public void setAllReceivedUpto(long aru) {
093: this .aru = aru;
094:
095: if (aru > seq) {
096: seq = aru;
097: }
098: }
099:
100: public int getLastRoundBroadcastCount() {
101: return fcc;
102: }
103:
104: public void addLastRoundBroadcastCount(int transmitCount) {
105: this .fcc += transmitCount;
106: if (fcc < 0)
107: fcc = 0;
108: }
109:
110: public int getBacklog() {
111: return backlog;
112: }
113:
114: public void addBacklog(int back) {
115: this .backlog += back;
116:
117: if (backlog < 0)
118: backlog = 0;
119: }
120:
121: public void setWindowSize(int newSize) {
122: if (newSize < 0) {
123: windowSize = 0;
124: } else {
125: windowSize = newSize;
126: }
127: }
128:
129: public void addRecoveredMember(Address member) {
130:
131: if (recoveredMembers == null)
132: recoveredMembers = new TreeSet();
133:
134: recoveredMembers.add(member);
135: }
136:
137: public Collection getRecoveredMembers() {
138: return recoveredMembers;
139: }
140:
141: public int getWindowSize() {
142: return windowSize;
143: }
144:
145: public void setWindowThreshold(int newSize) {
146: if (newSize < 0) {
147: windowThreshold = 0;
148: } else {
149: windowThreshold = newSize;
150: }
151: }
152:
153: public int getWindowThreshold() {
154: return windowThreshold;
155: }
156:
157: public Collection getRetransmissionRequests() {
158: return retransmissionRequests;
159: }
160:
161: public void writeExternal(ObjectOutput oo) throws IOException {
162: oo.writeLong(tokenSeq);
163: oo.writeLong(seq);
164: oo.writeInt(type);
165: oo.writeLong(aru);
166: oo.writeInt(fcc);
167: oo.writeInt(backlog);
168: oo.writeInt(windowSize);
169: oo.writeInt(windowThreshold);
170: oo.writeObject(aruId);
171: oo.writeObject(retransmissionRequests);
172: oo.writeObject(recoveredMembers);
173:
174: }
175:
176: public void readExternal(ObjectInput oi) throws IOException,
177: ClassNotFoundException {
178: tokenSeq = oi.readLong();
179: seq = oi.readLong();
180: type = oi.readInt();
181: aru = oi.readLong();
182: fcc = oi.readInt();
183: backlog = oi.readInt();
184: windowSize = oi.readInt();
185: windowThreshold = oi.readInt();
186: aruId = (Address) oi.readObject();
187: retransmissionRequests = (Collection) oi.readObject();
188: recoveredMembers = (Collection) oi.readObject();
189: }
190:
191: public String toString() {
192: StringBuffer buf = new StringBuffer(200);
193: buf.append("Token[tokenSeq=").append(tokenSeq);
194: buf.append(",type=").append(type);
195: buf.append(",highestseq=").append(seq);
196: buf.append(",aru=").append(aru);
197: buf.append(",lastRoundTransmitCount=").append(fcc);
198: buf.append(",backlog=").append(backlog);
199: buf.append(",windowSize=").append(windowSize);
200: buf.append(",windowThreshold=").append(windowThreshold);
201: buf.append(",retransmissionList=").append(
202: getRetransmissionRequests());
203: buf.append(']');
204: return buf.toString();
205: }
206: }
|