KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > perf > Data


1 package org.jgroups.tests.perf;
2
3 import java.io.Externalizable JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.ObjectInput JavaDoc;
6 import java.io.ObjectOutput JavaDoc;
7 import java.util.HashMap JavaDoc;
8
9 /**
10  * Data sent around between members
11  * @author Bela Ban Jan 22
12  * @author 2004
13  * @version $Id: Data.java,v 1.6 2004/07/05 14:15:21 belaban Exp $
14  */

15 public class Data implements Externalizable JavaDoc {
16     final static int DISCOVERY_REQ = 1;
17     final static int DISCOVERY_RSP = 2;
18     final static int DATA = 3;
19     final static int DONE = 4; // sent when a sender is done
20
final static int RESULTS = 5; // sent when a receiver has received all messages
21

22     public Data() {
23         ;
24     }
25
26     public Data(int type) {
27         this.type=type;
28     }
29
30     int type=0;
31     byte[] payload=null; // used with DATA
32
boolean sender=false; // used with DISCOVERY_RSP
33
long num_msgs=0; // used with DISCOVERY_RSP
34
HashMap JavaDoc results=null; // used with RESULTS
35

36     public int getType() {
37         return type;
38     }
39
40     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
41         out.writeInt(type);
42         if(payload != null) {
43             out.writeInt(payload.length);
44             out.write(payload, 0, payload.length);
45         }
46         else {
47             out.writeInt(0);
48         }
49         out.writeBoolean(sender);
50         out.writeLong(num_msgs);
51         if(results != null) {
52             out.writeBoolean(true);
53             out.writeObject(results);
54         }
55         else
56             out.writeBoolean(false);
57     }
58
59     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
60         type=in.readInt();
61         int len=in.readInt();
62         if(len > 0) {
63             payload=new byte[len];
64             in.readFully(payload, 0, payload.length);
65         }
66         sender=in.readBoolean();
67         num_msgs=in.readLong();
68         boolean results_available=in.readBoolean();
69         if(results_available)
70             results=(HashMap JavaDoc)in.readObject();
71     }
72
73 // public byte[] write() throws Exception {
74
// byte[] retval=null;
75
// switch(type) {
76
// case DISCOVERY_REQ: return new byte[]{DISCOVERY_REQ};
77
// case DISCOVERY_RSP:
78
// byte[] tmp=Util.objectToByteBuffer(new Long(num_msgs));
79
// retval=new byte[tmp.length +1];
80
// retval[0]=DISCOVERY_RSP;
81
// System.arraycopy(tmp, 0, retval, 1, tmp.length);
82
// return retval;
83
// case DATA:
84
// retval=new byte[payload.length +1];
85
// retval[0]=DATA;
86
// System.arraycopy(payload, 0, retval, 1, payload.length);
87
// return retval;
88
// default: return new byte[]{(byte)type};
89
// }
90
// }
91
//
92
// public void read(byte[] buf) throws Exception {
93
// type=buf[0];
94
// switch(type) {
95
// case DISCOVERY_REQ:
96
// break;
97
// case DISCOVERY_RSP:
98
// break;
99
// case DATA:
100
// payload=new byte[buf.length-1];
101
// System.arraycopy(buf, 1, payload, 0, buf.length -1);
102
// break;
103
// default:
104
// break;
105
// }
106
// }
107

108     public String JavaDoc toString() {
109         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
110         sb.append('[');
111         switch(type) {
112             case DISCOVERY_REQ: sb.append("DISCOVERY_REQ"); break;
113             case DISCOVERY_RSP: sb.append("DISCOVERY_RSP"); break;
114             case DATA: sb.append("DATA"); break;
115             case DONE: sb.append("DONE"); break;
116             case RESULTS: sb.append("RESULTS"); break;
117             default: sb.append("<unknown>"); break;
118         }
119         sb.append("] ");
120         return sb.toString();
121     }
122 }
123
Popular Tags