-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentSocketImpl.java
455 lines (365 loc) · 12.3 KB
/
StudentSocketImpl.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import java.net.*;
import java.io.*;
import java.util.Timer;
class StudentSocketImpl extends BaseSocketImpl {
private Demultiplexer D;
private Timer tcpTimer;
private TCPPacket lastPkt1; // last non-ack
private TCPPacket lastPkt2; // last ack
private InetAddress locAddress;
private int currentState;
private int locAck;
private int locSeq;
private int locPort;
private int count = 1;
// All of the states as integers to print transitions
private static final int LISTEN = 0;
private static final int CLOSED = 1;
private static final int SYN_SENT = 2;
private static final int SYN_RCVD = 3;
private static final int ESTABLISHED = 4;
private static final int FIN_WAIT_1 = 5;
private static final int FIN_WAIT_2 = 6;
private static final int CLOSING = 7;
private static final int CLOSE_WAIT = 8;
private static final int LAST_ACK = 9;
private static final int TIME_WAIT = 10;
StudentSocketImpl(Demultiplexer D) {
this.D = D;
currentState = CLOSED;
}
// Send Wrapper method
public void sendPacketWrapper(boolean prePack, TCPPacket thisPkt, int source, int dest, int seqNumP, int ackNumP, boolean first, boolean second, boolean third, InetAddress sendAddress){
TCPPacket synpack;
if(currentState == CLOSED && count > 0){
notifyAll();
return;
}
if(prePack){
System.out.println("XXX RESENDING PACKET");
}
count++;
if(prePack){
synpack = thisPkt;
}
else{
synpack = new TCPPacket(source, dest, seqNumP, ackNumP, first, second, third, 30, null);
}
//send the packet
TCPWrapper.send(synpack, sendAddress);
//send the packet and start a retransmission timer
if (!synpack.ackFlag || synpack.synFlag){
lastPkt1 = synpack;
createTimerTask(1000);
}
else{
lastPkt2 = synpack;
}
}
/**
* Connects this socket to the specified port number on the specified host.
*
* @param address the IP address of the remote host.
* @param port the port number.
* @exception IOException if an I/O error occurs when attempting a
* connection.
*/
public synchronized void connect(InetAddress address, int port) throws IOException{
//Initialize state and send syn packet to waiting server
locAddress = address;
count --;
localport = D.getNextAvailablePort();
D.registerConnection(address, this.localport, port, this);
sendPacketWrapper(false, lastPkt1, this.localport, port, 0, 0, false, true, false, locAddress);
currentState = changeState(CLOSED, SYN_SENT);
while (currentState != ESTABLISHED){
try{
wait();
}
catch(Exception e){
e.printStackTrace();
}
}
}
/**
* Called by Demultiplexer when a packet comes in for this connection
* @param p The packet that arrived
*/
public synchronized void receivePacket(TCPPacket p){
this.notifyAll();
if (currentState == LISTEN) {
if (!p.ackFlag && p.synFlag) {
locSeq = p.seqNum;
locAddress = p.sourceAddr;
locAck = p.ackNum;
sendPacketWrapper(false, lastPkt1, localport, p.sourcePort, locAck, locSeq+1, true, true, false, locAddress);
currentState = changeState(currentState, SYN_RCVD);
//change the socket type
try {
D.unregisterListeningSocket(localport, this);
D.registerConnection(locAddress, localport, p.sourcePort, this);
} catch (IOException e) {
e.printStackTrace();
}
}
}
else if(currentState==SYN_SENT) {
//resend if there is a dropped SYN and ACK
if (p.synFlag && p.ackFlag) {
tcpTimer.cancel();
tcpTimer = null;
locSeq = p.seqNum;
locAddress = p.sourceAddr;
locPort = p.sourcePort;
sendPacketWrapper(false, lastPkt1, localport, locPort, -2, locSeq+1, true, false, false, locAddress);
locPort = p.sourcePort;
currentState = changeState(currentState, ESTABLISHED);
}
}
else if(currentState==SYN_RCVD) {
if (p.ackFlag) {
tcpTimer.cancel(); //cancel the timer for SYN and ACK
tcpTimer = null;
locPort = p.sourcePort;
currentState = changeState(currentState, ESTABLISHED);
} else if (p.synFlag) {
sendPacketWrapper(true, lastPkt1, 0, 0, 0, 0, false, false, false, locAddress);
}
}
else if(currentState==ESTABLISHED) {
// indicated dropped ack, and resends it
if (p.finFlag) {
locSeq = p.seqNum;
locAddress = p.sourceAddr;
locPort = p.sourcePort;
sendPacketWrapper(false, lastPkt1, localport, locPort, -2, locSeq+1, true, false, false, locAddress);
currentState = changeState(currentState, CLOSE_WAIT);
} else if (p.ackFlag && p.synFlag) {
sendPacketWrapper(false, lastPkt2, localport, locPort, -2, locSeq+1, true, false, false, locAddress);
}
}
else if(currentState==FIN_WAIT_1) {
if (p.ackFlag) {
if (p.synFlag) {
sendPacketWrapper(true, lastPkt2, 0, 0, 0, 0, false, false, false, locAddress);
} else {
//ack for fin
currentState = changeState(currentState, FIN_WAIT_2);
tcpTimer.cancel();
tcpTimer = null;
}
}
if (p.finFlag) {
//change to closing state
locSeq = p.seqNum;
locAddress = p.sourceAddr;
locAck = p.ackNum;
locPort = p.sourcePort;
sendPacketWrapper(false, lastPkt1, localport, locPort, -2, locSeq+1, true, false, false, locAddress);
currentState = changeState(currentState, CLOSING);
}
}
else if(currentState==FIN_WAIT_2) {
if (p.finFlag) {
locAck = p.ackNum;
locPort = p.sourcePort;
sendPacketWrapper(false, lastPkt1, localport, locPort, -2, locSeq+1, true, false, false, locAddress);
currentState = changeState(currentState, TIME_WAIT);
createTimerTask(30000); //30 second timer
}
}
else if (currentState==LAST_ACK) {
//indicates a dropped ack
if (p.ackFlag) {
tcpTimer.cancel();
tcpTimer = null;
currentState = changeState(currentState, TIME_WAIT);
createTimerTask(30000); //wait 30 seconds
}
if (p.finFlag) {
sendPacketWrapper(true, lastPkt2, 0, 0, 0, 0, false, false, false, locAddress);
}
}
else if(currentState==CLOSE_WAIT) {
//resend the dropped ack
if (p.finFlag) {
sendPacketWrapper(true, lastPkt2, 0, 0, 0, 0, false, false, false, locAddress);
}
}
else if(currentState==TIME_WAIT) {
try {
if (p.finFlag) {
sendPacketWrapper(true, lastPkt2, 0, 0, 0, 0, false, false, false, locAddress);
}
} catch (Exception e) {
e.printStackTrace();
}
}
else if(currentState==CLOSING) {
if (p.finFlag) {
sendPacketWrapper(true, lastPkt2, 0, 0, 0, 0, false, false, false, locAddress);
}
else if (p.ackFlag) {
tcpTimer.cancel();
tcpTimer = null;
currentState = changeState(currentState, TIME_WAIT);
createTimerTask(30000);//wait 30 seconds
}
}
}
/**
* Waits for an incoming connection to arrive to connect this socket to
* Ultimately this is called by the application calling
* ServerSocket.accept(), but this method belongs to the Socket object
* that will be returned, not the listening ServerSocket.
* Note that localport is already set prior to this being called.
*/
public synchronized void acceptConnection() throws IOException {
D.registerListeningSocket(this.localport, this);
currentState = changeState(CLOSED, LISTEN);
//thread sleeps until the state is ESTABLISHED
while (currentState != ESTABLISHED) {
try {
wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public InputStream getInputStream() throws IOException {
return null;
}
public OutputStream getOutputStream() throws IOException {
return null;
}
/**
* create TCPTimerTask instance, handling tcpTimer creation
* @param delay time in milliseconds before call
*
*/
private void createTimerTask(long delay){
if(tcpTimer == null)
tcpTimer = new Timer(false);
new TCPTimerTask(tcpTimer, delay, this, null);
}
/**
* handle timer expiration (called by TCPTimerTask)
* @param ref Generic reference that can be used by the timer to return
* information.
*/
public synchronized void handleTimer(Object ref){
tcpTimer.cancel();
tcpTimer = null;
//this must run only once
if(currentState == TIME_WAIT){
try {
currentState = changeState(currentState, CLOSED);
}
catch (Exception e) {
notifyAll();
}
notifyAll();
try {
D.unregisterConnection(locAddress, localport, locPort, this);
}
catch (Exception e) {
e.printStackTrace();
}
}
//resend the packet due to an ack not being transmitted
else{
sendPacketWrapper(true, lastPkt1, 0, 0, 0, 0, false, false, false, locAddress);
}
}
private String stateToString(int s){
if (s == 0){
return "LISTEN";
}
else if (s == 1){
return "CLOSED";
}
else if (s == 2){
return "SYN_SENT";
}
else if (s == 3){
return "SYN_RCVD";
}
else if (s == 4){
return "ESTABLISHED";
}
else if (s == 5){
return "FIN_WAIT_1";
}
else if (s == 6){
return "FIN_WAIT_2";
}
else if (s == 7){
return "CLOSING";
}
else if (s == 8){
return "CLOSE_WAIT";
}
else if (s == 9){
return "LAST_ACK";
}
else if (s == 10){
return "TIME_WAIT";
}
else{
return "Invalid State";
}
}
//print out state transition
public int changeState(int before, int now) {
System.out.println("!!! " + stateToString(before) + "->" + stateToString(now));
return now;
}
public int returnState(boolean currState){
if(currState){
return currentState;
}
else{
return CLOSED;
}
}
/**
* Closes this socket.
*
* @exception IOException if an I/O error occurs when closing this socket.
*/
public synchronized void close() {
//closes the connection
sendPacketWrapper(false, lastPkt1, this.localport, this.locPort, locAck, locSeq+1, false, false, true, locAddress);
if(currentState == ESTABLISHED){
currentState = changeState(currentState, FIN_WAIT_1);
}
else if(currentState == CLOSE_WAIT){
currentState = changeState(currentState, LAST_ACK);
}
try{
//create a new thread that waits until connection closes
backgroundThread newThread = new backgroundThread(this);
newThread.run();
}
catch (Exception e){
e.printStackTrace();
}
}
}
class backgroundThread implements Runnable{
public StudentSocketImpl waitToClose;
public backgroundThread(StudentSocketImpl here) {
this.waitToClose = here;
}
public void run(){
while (waitToClose.returnState(true) != waitToClose.returnState(false)){
try {
waitToClose.wait();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}