001: // $Id: NakAckHeader.java,v 1.4 2004/07/05 14:17:15 belaban Exp $
002:
003: package org.jgroups.protocols;
004:
005: import org.jgroups.Address;
006: import org.jgroups.Header;
007: import org.jgroups.ViewId;
008:
009: import java.io.IOException;
010: import java.io.ObjectInput;
011: import java.io.ObjectOutput;
012: import java.util.Vector;
013:
014: public class NakAckHeader extends Header {
015: public static final int NAK_MSG = 1; // asynchronous msg
016: public static final int NAK_ACK_MSG = 2; // synchronous msg
017: public static final int WRAPPED_MSG = 3; // a wrapped msg, needs to be ACKed
018: public static final int RETRANSMIT_MSG = 4; // retransmit msg
019: public static final int NAK_ACK_RSP = 5; // ack to NAK_ACK_MSG, seqno contains ACKed
020: public static final int OUT_OF_BAND_MSG = 6; // out-of-band msg
021: public static final int OUT_OF_BAND_RSP = 7; // ack for out-of-band msg
022:
023: int type = 0;
024: long seqno = -1; // either reg. NAK_ACK_MSG or first_seqno in retransmissions
025: long last_seqno = -1; // used for retransmissions
026: ViewId vid = null;
027:
028: // the messages from this sender can be deleted safely (OutOfBander). Contains seqnos (Longs)
029: Vector stable_msgs = null;
030: Address sender = null; // In case of WRAPPED_MSG: the address to which an ACK has to be sent
031:
032: public NakAckHeader() {
033: }
034:
035: public NakAckHeader(int type, long seqno, ViewId vid) {
036: this .type = type;
037: this .seqno = seqno;
038: this .vid = vid;
039: }
040:
041: public long size() {
042: return 512;
043: }
044:
045: public void writeExternal(ObjectOutput out) throws IOException {
046: out.writeInt(type);
047: out.writeLong(seqno);
048: out.writeLong(last_seqno);
049: out.writeObject(vid);
050: out.writeObject(stable_msgs);
051: out.writeObject(sender);
052: }
053:
054: public void readExternal(ObjectInput in) throws IOException,
055: ClassNotFoundException {
056: type = in.readInt();
057: seqno = in.readLong();
058: last_seqno = in.readLong();
059: vid = (ViewId) in.readObject();
060: stable_msgs = (Vector) in.readObject();
061: sender = (Address) in.readObject();
062: }
063:
064: public NakAckHeader copy() {
065: NakAckHeader ret = new NakAckHeader(type, seqno, vid);
066: ret.last_seqno = last_seqno;
067: ret.stable_msgs = stable_msgs != null ? (Vector) stable_msgs
068: .clone() : null;
069: ret.sender = sender;
070: return ret;
071: }
072:
073: public static String type2Str(int t) {
074: switch (t) {
075: case NAK_MSG:
076: return "NAK_MSG";
077: case NAK_ACK_MSG:
078: return "NAK_ACK_MSG";
079: case WRAPPED_MSG:
080: return "WRAPPED_MSG";
081: case RETRANSMIT_MSG:
082: return "RETRANSMIT_MSG";
083: case NAK_ACK_RSP:
084: return "NAK_ACK_RSP";
085: case OUT_OF_BAND_MSG:
086: return "OUT_OF_BAND_MSG";
087: case OUT_OF_BAND_RSP:
088: return "OUT_OF_BAND_RSP";
089: default:
090: return "<undefined>";
091: }
092: }
093:
094: public String toString() {
095: StringBuffer ret = new StringBuffer();
096: ret.append("[NAKACK: " + type2Str(type) + ", seqno=" + seqno
097: + ", last_seqno=" + last_seqno + ", vid=" + vid);
098: if (type == WRAPPED_MSG)
099: ret.append(", sender=" + sender);
100: ret.append(']');
101:
102: return ret.toString();
103: }
104:
105: }
|