-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.java
More file actions
129 lines (113 loc) · 3.11 KB
/
Message.java
File metadata and controls
129 lines (113 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package bn;
import java.util.Random;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.lang.System;
import java.io.InputStream;
import java.io.EOFException;
import java.lang.IllegalArgumentException;
public class Message {
private static final int DATA_READ_RETRIES = 5;
private static Random random = new Random();
private static final int HEADER_LENGTH = Long.BYTES + 5 * Integer.BYTES;
public static enum Type {
DATA,
BYE // to disconnect
};
private Type type;
private long timestamp;
private int source;
private int destination;
private int numHops;
private byte[] data;
public Message(int src, int dest, long ts, int size) {
type = Type.DATA;
data = new byte[size];
random.nextBytes(data);
source = src;
destination = dest;
numHops = 0;
timestamp = ts;
}
public Message() {
// Empty message means BYE
type = Type.BYE;
data = new byte[0];
}
// TODO add error messages
private byte[] read(InputStream inputStream, int len) throws Exception {
if (len < 0) throw new IllegalArgumentException();
byte[] data = new byte[len];
int read = 0;
int retries = 0;
while (read < len) {
int _read = inputStream.read(data, read, len - read);
if (_read < 0) {
if (++retries == DATA_READ_RETRIES) throw new EOFException("ERROR! Bad message, Expected:" + len + " Got:" + read);
}
else {
read += _read;
retries = 0;
}
}
return data;
}
public Message(InputStream inputStream) throws Exception {
byte[] header = read(inputStream, HEADER_LENGTH);
ByteBuffer bb = ByteBuffer.allocate(header.length).order(ByteOrder.BIG_ENDIAN);
bb.clear();
bb.put(header);
bb.flip(); // rewind
timestamp = bb.getLong();
type = Type.values()[bb.getInt()];
source = bb.getInt();
destination = bb.getInt();
numHops = bb.getInt();
int dataSize = bb.getInt();
data = read(inputStream, dataSize);
}
public void setTimestamp(long ts) {
timestamp = ts;
}
public void incNumHops() {
numHops++;
}
public long getTimestamp() {
return timestamp;
}
public int getNumHops() {
return numHops;
}
public int getDestination() {
return destination;
}
public int getSource() {
return source;
}
public byte[] serialize() {
// [source(int), destination(int), timestamp(long), dataSize(int), data(bytes)]
int totalLength = HEADER_LENGTH + data.length;
ByteBuffer bb = ByteBuffer.allocate(totalLength).order(ByteOrder.BIG_ENDIAN);
bb.putLong(timestamp);
bb.putInt(type.ordinal());
bb.putInt(source);
bb.putInt(destination);
bb.putInt(numHops);
bb.putInt(data.length);
if (data.length > 0) {
bb.put(data);
}
return bb.array();
}
public boolean isGoodBye() {
return type == Type.BYE;
}
public String toString() {
String str = "{type: " + type + ", timestamp: " + timestamp;
if (type != Type.BYE) {
str += ", source: " + source + ", destination: " + destination + ", num-hops: " + numHops + ", data-size: " + data.length;
}
str += "}";
return str;
}
}