01: package org.jgroups.mux;
02:
03: import org.jgroups.Global;
04: import org.jgroups.Header;
05: import org.jgroups.util.Streamable;
06: import org.jgroups.util.Util;
07:
08: import java.io.*;
09:
10: /**
11: * Header used for multiplexing and de-multiplexing between service components on top of a Multiplexer (Channel)
12: * @author Bela Ban
13: * @version $Id: MuxHeader.java,v 1.6 2006/09/22 10:29:33 belaban Exp $
14: */
15: public class MuxHeader extends Header implements Streamable {
16: String id = null;
17:
18: /** Used for service state communication between Multiplexers */
19: ServiceInfo info;
20:
21: public MuxHeader() {
22: }
23:
24: public MuxHeader(String id) {
25: this .id = id;
26: }
27:
28: public MuxHeader(ServiceInfo info) {
29: this .info = info;
30: }
31:
32: public String getId() {
33: return id;
34: }
35:
36: public void writeExternal(ObjectOutput out) throws IOException {
37: out.writeUTF(id);
38: out.writeObject(info);
39: }
40:
41: public void readExternal(ObjectInput in) throws IOException,
42: ClassNotFoundException {
43: id = in.readUTF();
44: info = (ServiceInfo) in.readObject();
45: }
46:
47: public long size() {
48: long retval = Global.BYTE_SIZE; // presence byte in Util.writeString
49: if (id != null)
50: retval += id.length() + 2; // for UTF
51: retval += Global.BYTE_SIZE; // presence for info
52: if (info != null)
53: retval += info.size();
54: return retval;
55: }
56:
57: public void writeTo(DataOutputStream out) throws IOException {
58: Util.writeString(id, out);
59: if (info != null) {
60: out.writeBoolean(true);
61: info.writeTo(out);
62: } else {
63: out.writeBoolean(false);
64: }
65: }
66:
67: public void readFrom(DataInputStream in) throws IOException,
68: IllegalAccessException, InstantiationException {
69: id = Util.readString(in);
70: if (in.readBoolean()) {
71: info = new ServiceInfo();
72: info.readFrom(in);
73: }
74: }
75:
76: public String toString() {
77: if (id != null)
78: return id;
79: if (info != null)
80: return info.toString();
81: return "";
82: }
83: }
|