001: // $Id: NakAckHeader.java,v 1.19 2006/04/05 05:37:34 belaban Exp $
002:
003: package org.jgroups.protocols.pbcast;
004:
005: import org.jgroups.Address;
006: import org.jgroups.Global;
007: import org.jgroups.Header;
008: import org.jgroups.util.Range;
009: import org.jgroups.util.Streamable;
010: import org.jgroups.util.Util;
011:
012: import java.io.*;
013:
014: public class NakAckHeader extends Header implements Streamable {
015: public static final byte MSG = 1; // regular msg
016: public static final byte XMIT_REQ = 2; // retransmit request
017: public static final byte XMIT_RSP = 3; // retransmit response (contains one or more messages)
018:
019: byte type = 0;
020: long seqno = -1; // seqno of regular message (MSG)
021: Range range = null; // range of msgs to be retransmitted (XMIT_REQ) or retransmitted (XMIT_RSP)
022: Address sender; // the original sender of the message (for XMIT_REQ)
023:
024: public NakAckHeader() {
025: }
026:
027: /**
028: * Constructor for regular messages
029: */
030: public NakAckHeader(byte type, long seqno) {
031: this .type = type;
032: this .seqno = seqno;
033: }
034:
035: /**
036: * Constructor for retransmit requests/responses (low and high define the range of msgs)
037: */
038: public NakAckHeader(byte type, long low, long high) {
039: this .type = type;
040: range = new Range(low, high);
041: }
042:
043: public NakAckHeader(byte type, long low, long high, Address sender) {
044: this (type, low, high);
045: this .sender = sender;
046: }
047:
048: public void writeExternal(ObjectOutput out) throws IOException {
049: out.writeByte(type);
050: out.writeLong(seqno);
051: if (range != null) {
052: out.writeBoolean(true); // wasn't here before, bad bug !
053: range.writeExternal(out);
054: } else
055: out.writeBoolean(false);
056: out.writeObject(sender);
057: }
058:
059: public void readExternal(ObjectInput in) throws IOException,
060: ClassNotFoundException {
061: boolean read_range;
062: type = in.readByte();
063: seqno = in.readLong();
064: read_range = in.readBoolean();
065: if (read_range) {
066: range = new Range();
067: range.readExternal(in);
068: }
069: sender = (Address) in.readObject();
070: }
071:
072: public void writeTo(DataOutputStream out) throws IOException {
073: out.writeByte(type);
074: if (type != XMIT_RSP)
075: out.writeLong(seqno);
076: Util.writeStreamable(range, out);
077: Util.writeAddress(sender, out);
078: }
079:
080: public void readFrom(DataInputStream in) throws IOException,
081: IllegalAccessException, InstantiationException {
082: type = in.readByte();
083: if (type != XMIT_RSP)
084: seqno = in.readLong();
085: range = (Range) Util.readStreamable(Range.class, in);
086: sender = Util.readAddress(in);
087: }
088:
089: public long size() {
090: // type (1 byte) + seqno (8 bytes)
091: int retval = Global.BYTE_SIZE;
092:
093: if (type != XMIT_RSP) // we don't send the seqno if this is an XMIT_RSP
094: retval += Global.LONG_SIZE;
095:
096: retval += Global.BYTE_SIZE; // presence for range
097: if (range != null)
098: retval += 2 * Global.LONG_SIZE; // 2 times 8 bytes for seqno
099: retval += Util.size(sender);
100: return retval;
101: }
102:
103: public NakAckHeader copy() {
104: NakAckHeader ret = new NakAckHeader(type, seqno);
105: ret.range = range;
106: ret.sender = sender;
107: return ret;
108: }
109:
110: public static String type2Str(byte t) {
111: switch (t) {
112: case MSG:
113: return "MSG";
114: case XMIT_REQ:
115: return "XMIT_REQ";
116: case XMIT_RSP:
117: return "XMIT_RSP";
118: default:
119: return "<undefined>";
120: }
121: }
122:
123: public String toString() {
124: StringBuffer ret = new StringBuffer();
125: ret.append("[").append(type2Str(type));
126: switch (type) {
127: case MSG: // seqno and sender
128: ret.append(", seqno=").append(seqno);
129: break;
130: case XMIT_REQ: // range and sender
131: case XMIT_RSP: // range and sender
132: if (range != null)
133: ret.append(", range=").append(range);
134: break;
135: }
136:
137: if (sender != null)
138: ret.append(", sender=").append(sender);
139: ret.append(']');
140: return ret.toString();
141: }
142:
143: }
|