KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: NakAckHeader.java,v 1.4 2004/07/05 14:17:15 belaban Exp $
2

3 package org.jgroups.protocols;
4
5
6 import org.jgroups.Address;
7 import org.jgroups.Header;
8 import org.jgroups.ViewId;
9
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInput JavaDoc;
12 import java.io.ObjectOutput JavaDoc;
13 import java.util.Vector JavaDoc;
14
15
16
17 public class NakAckHeader extends Header {
18     public static final int NAK_MSG = 1; // asynchronous msg
19
public static final int NAK_ACK_MSG = 2; // synchronous msg
20
public static final int WRAPPED_MSG = 3; // a wrapped msg, needs to be ACKed
21
public static final int RETRANSMIT_MSG = 4; // retransmit msg
22
public static final int NAK_ACK_RSP = 5; // ack to NAK_ACK_MSG, seqno contains ACKed
23
public static final int OUT_OF_BAND_MSG = 6; // out-of-band msg
24
public static final int OUT_OF_BAND_RSP = 7; // ack for out-of-band msg
25

26     int type=0;
27     long seqno=-1; // either reg. NAK_ACK_MSG or first_seqno in retransmissions
28
long last_seqno=-1; // used for retransmissions
29
ViewId vid=null;
30
31     // the messages from this sender can be deleted safely (OutOfBander). Contains seqnos (Longs)
32
Vector JavaDoc stable_msgs=null;
33     Address sender=null; // In case of WRAPPED_MSG: the address to which an ACK has to be sent
34

35
36
37     public NakAckHeader() {}
38     
39
40
41     public NakAckHeader(int type, long seqno, ViewId vid) {
42     this.type=type;
43     this.seqno=seqno;
44     this.vid=vid;
45     }
46
47
48     public long size() {
49     return 512;
50     }
51
52
53     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
54     out.writeInt(type);
55     out.writeLong(seqno);
56     out.writeLong(last_seqno);
57     out.writeObject(vid);
58     out.writeObject(stable_msgs);
59     out.writeObject(sender);
60     }
61
62
63
64     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
65     type=in.readInt();
66     seqno=in.readLong();
67     last_seqno=in.readLong();
68     vid=(ViewId)in.readObject();
69     stable_msgs=(Vector JavaDoc)in.readObject();
70     sender=(Address)in.readObject();
71     }
72
73
74     public NakAckHeader copy() {
75     NakAckHeader ret=new NakAckHeader(type, seqno, vid);
76     ret.last_seqno=last_seqno;
77     ret.stable_msgs=stable_msgs != null ? (Vector JavaDoc)stable_msgs.clone() : null;
78     ret.sender=sender;
79     return ret;
80     }
81
82
83     public static String JavaDoc type2Str(int t) {
84     switch(t) {
85     case NAK_MSG: return "NAK_MSG";
86     case NAK_ACK_MSG: return "NAK_ACK_MSG";
87     case WRAPPED_MSG: return "WRAPPED_MSG";
88     case RETRANSMIT_MSG: return "RETRANSMIT_MSG";
89     case NAK_ACK_RSP: return "NAK_ACK_RSP";
90     case OUT_OF_BAND_MSG: return "OUT_OF_BAND_MSG";
91     case OUT_OF_BAND_RSP: return "OUT_OF_BAND_RSP";
92     default: return "<undefined>";
93     }
94     }
95
96     public String JavaDoc toString() {
97     StringBuffer JavaDoc ret=new StringBuffer JavaDoc();
98     ret.append("[NAKACK: " + type2Str(type) + ", seqno=" + seqno + ", last_seqno=" + last_seqno +
99            ", vid=" + vid );
100     if(type == WRAPPED_MSG)
101         ret.append(", sender=" + sender);
102     ret.append(']');
103
104     return ret.toString();
105     }
106
107 }
108
Popular Tags