-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutgoingPacket.java
More file actions
279 lines (240 loc) · 6.2 KB
/
Copy pathOutgoingPacket.java
File metadata and controls
279 lines (240 loc) · 6.2 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import java.io.UnsupportedEncodingException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
/**
* Class for the creation of outgoing packets.
* <p>
* Data is added in a builder pattern.
*
* @author James Li
*/
class OutgoingPacket extends Packet {
private InetAddress address;
private int port;
private short opcode;
private List<Byte> tempData = new ArrayList<Byte>();
private int sectionCount = 0;
/**
* Increments section count used to keep track of how many things have been
* added to the packet for validation later.
*
* @return This object
*/
private OutgoingPacket incrSectionCount() {
++this.sectionCount;
return this;
}
/**
* Appends a single byte to the raw data of the packet.
*
* @param data
* Byte to append
* @return This object
*/
private OutgoingPacket append(byte data) {
this.tempData.add(data);
return this;
}
/**
* Appends a byte array to the raw data of the packet.
*
* @param data
* Byte array to append
* @return This object
*/
private OutgoingPacket append(byte[] data) {
for (byte b : data) {
this.append(b);
}
return this;
}
/**
* Appends a `short` value to the raw data of the packet.
*
* @param data
* `short` to append
* @return This object
*/
private OutgoingPacket append(short data) {
return this.append(Utils.shortToByteArray(data));
}
/**
* Gets the byte array representation of the data of the constructed packet.
*
* @return Byte array of packet raw data
*/
private byte[] getByteArray() {
byte[] dataArray = null;
// Stupid `Byte` is different from `byte` so we can't do a straight
// array conversion
final int dataSize = this.tempData.size();
dataArray = new byte[dataSize];
for (int i = 0; i < dataSize; ++i) {
dataArray[i] = this.tempData.get(i);
}
return dataArray;
}
/**
* Sends this packet with the constructed data and set delivery information.
*
* @return This object
* @throws Exception
*/
OutgoingPacket send() throws Exception {
// Validation will throw exception if it fails
this.validate().setupSend(this.getByteArray(), this.address, this.port).sendPacket();
return this;
}
/**
* Validates the constructed outgoing packet.
*
* @return This object if valid, otherwise will throw exception
* @throws Exception
*/
private OutgoingPacket validate() throws Exception {
// Check the opcode and perform simple section count verification for
// each type
switch (this.opcode) {
case Packet.opcode.ACK:
if (this.sectionCount == 2) {
break;
}
throw new Exception(Constants.strings.PACKET_BUILD_SECTIONS_INCORRECT);
case Packet.opcode.DATA:
if (this.sectionCount == 3) {
// We can't go over 512 bytes
if (this.tempData.size() <= Constants.maxDataPacketLength) {
break;
}
throw new Exception(Constants.strings.OUT_PACKET_DATA_CONTENT_LENGTH_EXCEEDED);
}
throw new Exception(Constants.strings.PACKET_BUILD_SECTIONS_INCORRECT);
case Packet.opcode.ERROR:
if (this.sectionCount == 4) {
break;
}
throw new Exception(Constants.strings.PACKET_BUILD_SECTIONS_INCORRECT);
case Packet.opcode.RRQ:
case Packet.opcode.WRQ:
if (this.sectionCount == 5) {
break;
}
throw new Exception(Constants.strings.PACKET_BUILD_SECTIONS_INCORRECT);
default:
throw new Exception(Constants.strings.INVALID_OPCODE);
}
// Max packet size set by the program itself - we should not overfill
// any internal buffers set at this size
if (tempData.size() > Constants.maxAnyPacketLength) {
throw new Exception(Constants.strings.OUT_PACKET_LENGTH_EXCEEDED);
}
return this;
}
/**
* Adds opcode to packet data.
* <p>
* Only for use with ACK and DATA packets.
*
* @param data
* Byte array of data
* @return This object
*/
OutgoingPacket addOpcode(short opcode) {
if (this.sectionCount == 0) {
this.opcode = opcode;
this.append(opcode).incrSectionCount();
}
return this;
}
/**
* Adds block number to packet data.
* <p>
* Only for use with ACK and DATA packets.
*
* @param data
* Byte array of data
* @return This object
*/
OutgoingPacket addBlockNumber(BlockNumber blockNumber) {
if (this.sectionCount == 1 && (this.opcode == Packet.opcode.ACK || this.opcode == Packet.opcode.DATA)) {
this.append(blockNumber.val()).incrSectionCount();
}
return this;
}
/**
* Adds raw bytes to packet data.
* <p>
* Only for use with DATA packets.
*
* @param data
* Byte array of data
* @return This object
*/
OutgoingPacket addDataBytes(byte[] data) {
if (this.sectionCount == 2 && this.opcode == Packet.opcode.DATA) {
this.append(data).incrSectionCount();
}
return this;
}
/**
* Adds string to packet data.
* <p>
* Only for use with ERROR, RRQ and WRQ packets.
*
* @param string
* String
* @return This object
*/
OutgoingPacket addString(String string) {
try {
if ((this.sectionCount == 2 && this.opcode == Packet.opcode.ERROR)
|| (((this.sectionCount == 1) || (this.sectionCount == 3))
&& ((this.opcode == Packet.opcode.RRQ) || (this.opcode == Packet.opcode.WRQ)))) {
this.append(Utils.stringToByteArray(string)).incrSectionCount();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return this;
}
/**
* Adds error code to packet data.
* <p>
* Only for use with ERROR packets.
*
* @param errcode
* Error code
* @return This object
*/
OutgoingPacket addErrorCode(short errcode) {
if (this.sectionCount == 1 && this.opcode == Packet.opcode.ERROR) {
this.append(errcode).incrSectionCount();
}
return this;
}
/**
* Adds NULL byte to packet data.
*
* @return This object
*/
OutgoingPacket addNullByte() {
return this.append((byte) 0x00).incrSectionCount();
}
/**
* Instantiates an outgoing packet instance.
*
* @param socket
* Open socket
* @param address
* Destination address
* @param port
* Destination port
*/
OutgoingPacket(DatagramSocket socket, InetAddress address, int port) {
super(socket);
this.address = address;
this.port = port;
}
}