KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > pbcast > NakAckHeader


1 // $Id: NakAckHeader.java,v 1.10 2005/04/20 11:13:18 belaban Exp $
2

3 package org.jgroups.protocols.pbcast;
4
5
6 import org.jgroups.Header;
7 import org.jgroups.Global;
8 import org.jgroups.util.Range;
9 import org.jgroups.util.Streamable;
10 import org.jgroups.util.Util;
11
12 import java.io.*;
13
14
15 public class NakAckHeader extends Header implements Streamable {
16     public static final byte MSG=1; // regular msg
17
public static final byte XMIT_REQ=2; // retransmit request
18
public static final byte XMIT_RSP=3; // retransmit response (contains one or more messages)
19

20
21     byte type=0;
22     long seqno=-1; // seqno of regular message (MSG)
23
Range range=null; // range of msgs to be retransmitted (XMIT_REQ) or retransmitted (XMIT_RSP)
24

25
26     public NakAckHeader() {
27     }
28
29
30     /**
31      * Constructor for regular messages
32      */

33     public NakAckHeader(byte type, long seqno) {
34         this.type=type;
35         this.seqno=seqno;
36     }
37
38     /**
39      * Constructor for retransmit requests/responses (low and high define the range of msgs)
40      */

41     public NakAckHeader(byte type, long low, long high) {
42         this.type=type;
43         range=new Range(low, high);
44     }
45
46
47     public long size() {
48         // type (1 byte) + seqno (8 bytes) + presence (1 byte)
49
int retval=Global.BYTE_SIZE + Global.LONG_SIZE + Global.BYTE_SIZE;
50         if(range != null)
51             retval+= (2 * Global.LONG_SIZE); // presence byte, plus 2 times 8 bytes for seqno
52
return retval;
53     }
54
55
56     public void writeExternal(ObjectOutput out) throws IOException {
57         out.writeByte(type);
58         out.writeLong(seqno);
59         if(range != null) {
60             out.writeBoolean(true); // wasn't here before, bad bug !
61
range.writeExternal(out);
62         }
63         else
64             out.writeBoolean(false);
65     }
66
67
68     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
69         boolean read_range;
70         type=in.readByte();
71         seqno=in.readLong();
72         read_range=in.readBoolean();
73         if(read_range) {
74             range=new Range();
75             range.readExternal(in);
76         }
77     }
78
79     public void writeTo(DataOutputStream out) throws IOException {
80         out.writeByte(type);
81         out.writeLong(seqno);
82         Util.writeStreamable(range, out);
83     }
84
85     public void readFrom(DataInputStream in) throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
86         type=in.readByte();
87         seqno=in.readLong();
88         range=(Range)Util.readStreamable(Range.class, in);
89     }
90
91
92     public NakAckHeader copy() {
93         NakAckHeader ret=new NakAckHeader(type, seqno);
94         ret.range=range;
95         return ret;
96     }
97
98
99     public static String JavaDoc type2Str(byte t) {
100         switch(t) {
101             case MSG:
102                 return "MSG";
103             case XMIT_REQ:
104                 return "XMIT_REQ";
105             case XMIT_RSP:
106                 return "XMIT_RSP";
107             default:
108                 return "<undefined>";
109         }
110     }
111
112
113     public String JavaDoc toString() {
114         StringBuffer JavaDoc ret=new StringBuffer JavaDoc();
115         ret.append("[NAKACK: ").append(type2Str(type)).append(", seqno=").append(seqno);
116         ret.append(", range=").append(range);
117         ret.append(']');
118         return ret.toString();
119     }
120
121
122 }
123
Popular Tags