KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > mux > ServiceInfo


1 package org.jgroups.mux;
2
3 import org.jgroups.Address;
4 import org.jgroups.Global;
5 import org.jgroups.util.Streamable;
6 import org.jgroups.util.Util;
7
8 import java.io.*;
9
10 /**
11  * Class used for service state communication between Multiplexers
12  * @author Bela Ban
13  * @version $Id: ServiceInfo.java,v 1.4 2006/10/05 08:06:21 belaban Exp $
14  */

15 public class ServiceInfo implements Externalizable, Streamable {
16     public static final byte STATE_REQ = 1;
17     public static final byte STATE_RSP = 2;
18     public static final byte SERVICE_UP = 3;
19     public static final byte SERVICE_DOWN = 4;
20     public static final byte LIST_SERVICES_RSP = 5; // list of services available on a given node (available in 'state')
21

22     byte type=0;
23     String JavaDoc service=null;
24     Address host=null;
25     byte[] state=null;
26
27
28     public ServiceInfo() {
29     }
30
31     public ServiceInfo(byte type, String JavaDoc service, Address host, byte[] state) {
32         this.type=type;
33         this.service=service;
34         this.host=host;
35         this.state=state;
36     }
37
38
39     public void writeExternal(ObjectOutput out) throws IOException {
40           out.writeByte(type);
41           out.writeUTF(service);
42           out.writeObject(host);
43           if(state != null) {
44               out.writeInt(state.length);
45               out.write(state);
46           }
47           else {
48               out.writeInt(0);
49           }
50       }
51
52       public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
53           type=in.readByte();
54           service=in.readUTF();
55           host=(Address)in.readObject();
56           int len=in.readInt();
57           if(len > 0) {
58               state=new byte[len];
59               in.readFully(state, 0, len);
60           }
61       }
62
63
64       public long size() {
65           long retval=Global.BYTE_SIZE; // type
66
retval+=Global.BYTE_SIZE; // presence byte for service
67
if(service != null)
68               retval+=service.length() +2;
69           retval+=Util.size(host);
70           retval+=Global.INT_SIZE; // length of state
71
if(state != null)
72               retval+=state.length;
73           return retval;
74       }
75
76       public void writeTo(DataOutputStream out) throws IOException {
77           out.writeByte(type);
78           Util.writeString(service, out);
79           Util.writeAddress(host, out);
80           if(state != null) {
81               out.writeInt(state.length);
82               out.write(state, 0, state.length);
83           }
84           else {
85               out.writeInt(0);
86           }
87       }
88
89       public void readFrom(DataInputStream in) throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
90           type=in.readByte();
91           service=Util.readString(in);
92           host=Util.readAddress(in);
93           int len=in.readInt();
94           if(len > 0) {
95               state=new byte[len];
96               in.readFully(state, 0, len);
97           }
98       }
99
100
101
102     public String JavaDoc toString() {
103         switch(type) {
104             case STATE_REQ: return "STATE_REQ";
105             case STATE_RSP:
106                 String JavaDoc tmp="STATE_RSP (";
107                 if(state == null)
108                     tmp+=state;
109                 else
110                     tmp+=state.length;
111                 tmp+=")";
112                 return tmp;
113             case SERVICE_UP: return "SERVICE_UP(" + service + "," + host + ")";
114             case SERVICE_DOWN: return "SERVICE_DOWN(" + service + "," + host + ")";
115             case LIST_SERVICES_RSP:
116                 String JavaDoc services=null;
117                 try {
118                     services=Util.objectFromByteBuffer(state).toString();
119                 }
120                 catch(Exception JavaDoc e) {
121                 }
122                 return "LIST_SERVICES_RSP(" + services + ")";
123             default: return "n/a";
124         }
125     }
126
127     public static String JavaDoc typeToString(int t) {
128         switch(t) {
129             case STATE_REQ: return "STATE_REQ";
130             case STATE_RSP: return "STATE_RSP";
131             case SERVICE_UP: return "SERVICE_UP";
132             case SERVICE_DOWN: return "SERVICE_DOWN";
133             case LIST_SERVICES_RSP: return "LIST_SERVICES_RSP";
134             default: return "n/a";
135         }
136     }
137 }
138
Popular Tags