KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: PingHeader.java,v 1.10 2005/04/26 15:22:13 belaban Exp $
2

3 package org.jgroups.protocols;
4
5 import org.jgroups.Header;
6 import org.jgroups.Global;
7 import org.jgroups.util.Streamable;
8 import org.jgroups.util.Util;
9
10 import java.io.*;
11
12
13 public class PingHeader extends Header implements Streamable {
14     public static final byte GET_MBRS_REQ=1; // arg = null
15
public static final byte GET_MBRS_RSP=2; // arg = PingRsp(local_addr, coord_addr)
16

17     public byte type=0;
18     public PingRsp arg=null;
19
20     public PingHeader() {
21     } // for externalization
22

23     public PingHeader(byte type, PingRsp arg) {
24         this.type=type;
25         this.arg=arg;
26     }
27
28     public long size() {
29         long retval=Global.BYTE_SIZE *2; // type and presence
30
if(arg != null) {
31             retval+=arg.size();
32         }
33         return retval;
34     }
35
36     public String JavaDoc toString() {
37         return "[PING: type=" + type2Str(type) + ", arg=" + arg + ']';
38     }
39
40     String JavaDoc type2Str(byte t) {
41         switch(t) {
42             case GET_MBRS_REQ:
43                 return "GET_MBRS_REQ";
44             case GET_MBRS_RSP:
45                 return "GET_MBRS_RSP";
46             default:
47                 return "<unkown type (" + t + ")>";
48         }
49     }
50
51
52     public void writeExternal(ObjectOutput out) throws IOException {
53         out.writeByte(type);
54         out.writeObject(arg);
55     }
56
57
58     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
59         type=in.readByte();
60         arg=(PingRsp)in.readObject();
61     }
62
63     public void writeTo(DataOutputStream outstream) throws IOException {
64         outstream.writeByte(type);
65         Util.writeStreamable(arg, outstream);
66     }
67
68     public void readFrom(DataInputStream instream) throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
69         type=instream.readByte();
70         arg=(PingRsp)Util.readStreamable(PingRsp.class, instream);
71     }
72 }
73
Popular Tags