-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacket.java
174 lines (144 loc) · 4.77 KB
/
Packet.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package udp3;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* Packet represents a simulated network packet.
* As we don't have unsigned types in Java, we can achieve this by using a larger type.
*/
public class Packet {
public static final int MIN_LEN = 11;
public static final int MAX_LEN = 11 + 1024;
private final int type;
private final long sequenceNumber;
private final InetAddress peerAddress;
private final int peerPort;
private final byte[] payload;
public Packet(int type, long sequenceNumber, InetAddress peerAddress, int peerPort, byte[] payload) {
this.type = type;
this.sequenceNumber = sequenceNumber;
this.peerAddress = peerAddress;
this.peerPort = peerPort;
this.payload = payload;
}
public int getType() {
return type;
}
public long getSequenceNumber() {
return sequenceNumber;
}
public InetAddress getPeerAddress() {
return peerAddress;
}
public int getPeerPort() {
return peerPort;
}
public byte[] getPayload() {
return payload;
}
/**
* Creates a builder from the current packet.
* It's used to create another packet by re-using some parts of the current packet.
*/
public Builder toBuilder(){
return new Builder()
.setType(type)
.setSequenceNumber(sequenceNumber)
.setPeerAddress(peerAddress)
.setPortNumber(peerPort)
.setPayload(payload);
}
/**
* Writes a raw presentation of the packet to byte buffer.
* The order of the buffer should be set as BigEndian.
*/
private void write(ByteBuffer buf) {
buf.put((byte) type);
buf.putInt((int) sequenceNumber);
buf.put(peerAddress.getAddress());
buf.putShort((short) peerPort);
buf.put(payload);
}
/**
* Create a byte buffer in BigEndian for the packet.
* The returned buffer is flipped and ready for get operations.
*/
public ByteBuffer toBuffer() {
ByteBuffer buf = ByteBuffer.allocate(MAX_LEN).order(ByteOrder.BIG_ENDIAN);
write(buf);
buf.flip();
return buf;
}
/**
* Returns a raw representation of the packet.
*/
public byte[] toBytes() {
ByteBuffer buf = toBuffer();
byte[] raw = new byte[buf.remaining()];
buf.get(raw);
return raw;
}
/**
* fromBuffer creates a packet from the given ByteBuffer in BigEndian.
*/
public static Packet fromBuffer(ByteBuffer buf) throws IOException {
if (buf.limit() < MIN_LEN || buf.limit() > MAX_LEN) {
throw new IOException("Invalid length");
}
Builder builder = new Builder();
builder.setType(Byte.toUnsignedInt(buf.get()));
builder.setSequenceNumber(Integer.toUnsignedLong(buf.getInt()));
byte[] host = new byte[]{buf.get(), buf.get(), buf.get(), buf.get()};
builder.setPeerAddress(Inet4Address.getByAddress(host));
builder.setPortNumber(Short.toUnsignedInt(buf.getShort()));
byte[] payload = new byte[buf.remaining()];
buf.get(payload);
builder.setPayload(payload);
return builder.create();
}
/**
* fromBytes creates a packet from the given array of bytes.
*/
public static Packet fromBytes(byte[] bytes) throws IOException {
ByteBuffer buf = ByteBuffer.allocate(MAX_LEN).order(ByteOrder.BIG_ENDIAN);
buf.put(bytes);
buf.flip();
return fromBuffer(buf);
}
@Override
public String toString() {
return String.format("#%d peer=%s:%d, size=%d", sequenceNumber, peerAddress, peerPort, payload.length);
}
public static class Builder {
private int type;
private long sequenceNumber;
private InetAddress peerAddress;
private int portNumber;
private byte[] payload;
public Builder setType(int type) {
this.type = type;
return this;
}
public Builder setSequenceNumber(long sequenceNumber) {
this.sequenceNumber = sequenceNumber;
return this;
}
public Builder setPeerAddress(InetAddress peerAddress) {
this.peerAddress = peerAddress;
return this;
}
public Builder setPortNumber(int portNumber) {
this.portNumber = portNumber;
return this;
}
public Builder setPayload(byte[] payload) {
this.payload = payload;
return this;
}
public Packet create() {
return new Packet(type, sequenceNumber, peerAddress, portNumber, payload);
}
}
}