001: package org.jgroups.mux;
002:
003: import org.jgroups.Address;
004: import org.jgroups.Global;
005: import org.jgroups.util.Streamable;
006: import org.jgroups.util.Util;
007:
008: import java.io.*;
009:
010: /**
011: * Class used for service state communication between Multiplexers
012: * @author Bela Ban
013: * @version $Id: ServiceInfo.java,v 1.4 2006/10/05 08:06:21 belaban Exp $
014: */
015: public class ServiceInfo implements Externalizable, Streamable {
016: public static final byte STATE_REQ = 1;
017: public static final byte STATE_RSP = 2;
018: public static final byte SERVICE_UP = 3;
019: public static final byte SERVICE_DOWN = 4;
020: public static final byte LIST_SERVICES_RSP = 5; // list of services available on a given node (available in 'state')
021:
022: byte type = 0;
023: String service = null;
024: Address host = null;
025: byte[] state = null;
026:
027: public ServiceInfo() {
028: }
029:
030: public ServiceInfo(byte type, String service, Address host,
031: byte[] state) {
032: this .type = type;
033: this .service = service;
034: this .host = host;
035: this .state = state;
036: }
037:
038: public void writeExternal(ObjectOutput out) throws IOException {
039: out.writeByte(type);
040: out.writeUTF(service);
041: out.writeObject(host);
042: if (state != null) {
043: out.writeInt(state.length);
044: out.write(state);
045: } else {
046: out.writeInt(0);
047: }
048: }
049:
050: public void readExternal(ObjectInput in) throws IOException,
051: ClassNotFoundException {
052: type = in.readByte();
053: service = in.readUTF();
054: host = (Address) in.readObject();
055: int len = in.readInt();
056: if (len > 0) {
057: state = new byte[len];
058: in.readFully(state, 0, len);
059: }
060: }
061:
062: public long size() {
063: long retval = Global.BYTE_SIZE; // type
064: retval += Global.BYTE_SIZE; // presence byte for service
065: if (service != null)
066: retval += service.length() + 2;
067: retval += Util.size(host);
068: retval += Global.INT_SIZE; // length of state
069: if (state != null)
070: retval += state.length;
071: return retval;
072: }
073:
074: public void writeTo(DataOutputStream out) throws IOException {
075: out.writeByte(type);
076: Util.writeString(service, out);
077: Util.writeAddress(host, out);
078: if (state != null) {
079: out.writeInt(state.length);
080: out.write(state, 0, state.length);
081: } else {
082: out.writeInt(0);
083: }
084: }
085:
086: public void readFrom(DataInputStream in) throws IOException,
087: IllegalAccessException, InstantiationException {
088: type = in.readByte();
089: service = Util.readString(in);
090: host = Util.readAddress(in);
091: int len = in.readInt();
092: if (len > 0) {
093: state = new byte[len];
094: in.readFully(state, 0, len);
095: }
096: }
097:
098: public String toString() {
099: switch (type) {
100: case STATE_REQ:
101: return "STATE_REQ";
102: case STATE_RSP:
103: String tmp = "STATE_RSP (";
104: if (state == null)
105: tmp += state;
106: else
107: tmp += state.length;
108: tmp += ")";
109: return tmp;
110: case SERVICE_UP:
111: return "SERVICE_UP(" + service + "," + host + ")";
112: case SERVICE_DOWN:
113: return "SERVICE_DOWN(" + service + "," + host + ")";
114: case LIST_SERVICES_RSP:
115: String services = null;
116: try {
117: services = Util.objectFromByteBuffer(state).toString();
118: } catch (Exception e) {
119: }
120: return "LIST_SERVICES_RSP(" + services + ")";
121: default:
122: return "n/a";
123: }
124: }
125:
126: public static String typeToString(int t) {
127: switch (t) {
128: case STATE_REQ:
129: return "STATE_REQ";
130: case STATE_RSP:
131: return "STATE_RSP";
132: case SERVICE_UP:
133: return "SERVICE_UP";
134: case SERVICE_DOWN:
135: return "SERVICE_DOWN";
136: case LIST_SERVICES_RSP:
137: return "LIST_SERVICES_RSP";
138: default:
139: return "n/a";
140: }
141: }
142: }
|