001: package org.jgroups.tests.perf;
002:
003: import org.jgroups.util.Streamable;
004: import org.jgroups.util.Util;
005:
006: import java.io.*;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.Map;
010:
011: /**
012: * Data sent around between members
013: * @author Bela Ban Jan 22
014: * @author 2004
015: * @version $Id: Data.java,v 1.9 2005/09/02 11:30:43 belaban Exp $
016: */
017: public class Data implements Streamable {
018: final static byte DISCOVERY_REQ = 1;
019: final static byte DISCOVERY_RSP = 2;
020: final static byte DATA = 3;
021: final static byte RESULTS = 4; // sent when a receiver has received all messages
022: final static byte FINAL_RESULTS = 5; // sent when a sender is done
023:
024: public Data() {
025: ;
026: }
027:
028: public Data(byte type) {
029: this .type = type;
030: }
031:
032: byte type = 0;
033: byte[] payload = null; // used with DATA
034: boolean sender = false; // used with DISCOVERY_RSP
035: long num_msgs = 0; // used with DISCOVERY_RSP
036: MemberInfo result = null; // used with RESULTS
037: Map results = null; // used with final results
038:
039: public int getType() {
040: return type;
041: }
042:
043: public void writeTo(DataOutputStream out) throws IOException {
044: out.writeByte(type);
045: if (payload != null) {
046: out.writeBoolean(true);
047: out.writeInt(payload.length);
048: out.write(payload, 0, payload.length);
049: } else
050: out.writeBoolean(false);
051: out.writeBoolean(sender);
052: out.writeLong(num_msgs);
053:
054: Util.writeStreamable(result, out);
055:
056: if (results != null) {
057: out.writeBoolean(true);
058: out.writeInt(results.size());
059: Map.Entry entry;
060: Object key;
061: MemberInfo val;
062: for (Iterator it = results.entrySet().iterator(); it
063: .hasNext();) {
064: entry = (Map.Entry) it.next();
065: key = entry.getKey();
066: val = (MemberInfo) entry.getValue();
067: try {
068: Util.writeObject(key, out);
069: } catch (Exception e) {
070: IOException ex = new IOException(
071: "failed to write object " + key);
072: ex.initCause(e);
073: throw ex;
074: }
075: Util.writeStreamable(val, out);
076: }
077: } else
078: out.writeBoolean(false);
079: }
080:
081: public void readFrom(DataInputStream in) throws IOException,
082: IllegalAccessException, InstantiationException {
083: type = in.readByte();
084: if (in.readBoolean()) {
085: int length = in.readInt();
086: payload = new byte[length];
087: in.read(payload, 0, length);
088: }
089: sender = in.readBoolean();
090: num_msgs = in.readLong();
091:
092: result = (MemberInfo) Util.readStreamable(MemberInfo.class, in);
093:
094: if (in.readBoolean()) {
095: int length = in.readInt();
096: results = new HashMap(length);
097: Object key;
098: MemberInfo val;
099: for (int i = 0; i < length; i++) {
100: try {
101: key = Util.readObject(in);
102: } catch (Exception e) {
103: IOException ex = new IOException(
104: "failed to read key");
105: ex.initCause(e);
106: throw ex;
107: }
108: val = (MemberInfo) Util.readStreamable(
109: MemberInfo.class, in);
110: results.put(key, val);
111: }
112: }
113: }
114:
115: public void writeExternal(ObjectOutput out) throws IOException {
116: out.writeByte(type);
117: if (payload != null) {
118: out.writeInt(payload.length);
119: out.write(payload, 0, payload.length);
120: } else {
121: out.writeInt(0);
122: }
123: out.writeBoolean(sender);
124: out.writeLong(num_msgs);
125: if (results != null) {
126: out.writeBoolean(true);
127: out.writeObject(results);
128: } else
129: out.writeBoolean(false);
130: }
131:
132: public void readExternal(ObjectInput in) throws IOException,
133: ClassNotFoundException {
134: type = in.readByte();
135: int len = in.readInt();
136: if (len > 0) {
137: payload = new byte[len];
138: in.readFully(payload, 0, payload.length);
139: }
140: sender = in.readBoolean();
141: num_msgs = in.readLong();
142: boolean results_available = in.readBoolean();
143: if (results_available)
144: results = (Map) in.readObject();
145: }
146:
147: public String toString() {
148: StringBuffer sb = new StringBuffer();
149: sb.append('[');
150: switch (type) {
151: case DISCOVERY_REQ:
152: sb.append("DISCOVERY_REQ");
153: break;
154: case DISCOVERY_RSP:
155: sb.append("DISCOVERY_RSP");
156: break;
157: case DATA:
158: sb.append("DATA");
159: break;
160: case RESULTS:
161: sb.append("RESULTS");
162: break;
163: case FINAL_RESULTS:
164: sb.append("FINAL_RESULTS");
165: break;
166: default:
167: sb.append("<unknown>");
168: break;
169: }
170: sb.append("] ");
171: return sb.toString();
172: }
173: }
|