- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectiveRepeatSender.java
More file actions
Latest commit
134 lines (131 loc) · 5.12 KB
/
Copy pathSelectiveRepeatSender.java
File metadata and controls
134 lines (131 loc) · 5.12 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
packageselectiverepeatsender;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;
importjava.net.ServerSocket;
importjava.net.Socket;
importjava.util.Scanner;
/**
*
* @author Danyal
*/
publicclassSelectiveRepeatSender
{
publicstaticvoidmain(String[] args) throwsInterruptedException
{
Scannerscan = newScanner(System.in);
intsw, sf = 0, sn = 0, m;
System.out.println("Input the value of 'm': ");
m = scan.nextInt();
sw = (int)Math.pow(2, m - 1);
inttimeout = 200;
String[] data = newString[(int)Math.pow(2,m)];
Timeout[] timers = newTimeout[sw];
for(inti = 0; i < data.length; i++)
{
data[i] = Integer.toString(i);
} //Generating string data to send.
m = (int)Math.pow(2, m);
try
{
intportnumber = 2325;
ServerSocketuservice = newServerSocket(portnumber);
System.out.println("Port number is: " + portnumber);
Socketusocket = uservice.accept();
System.out.println("accepted");
BufferedReaderinbuffer = newBufferedReader(newInputStreamReader(usocket.getInputStream()));
PrintWriteroutbuffer = newPrintWriter(usocket.getOutputStream(), true);
long[] timervals = newlong[sw];
for(inti = 0; i < timervals.length; i++)
timervals[i] = Long.MAX_VALUE;
outbuffer.println(Integer.toString(m));
System.out.println("Done writing.");
while(true)//for(int i = 0; i < data.length; i++)
{
Stringincoming = inbuffer.readLine();
if(incoming.contains("ACK"))
{
if(incoming.contains("NACK"))
{
System.out.println(incoming + " received.");
intnackno = Integer.parseInt(incoming.substring(4, incoming.length()));
if(isBetween(nackno, Integer.parseInt(data[sf%m]), Integer.parseInt(data[sn%m]), m))
{
System.out.println("Sending out " + data[nackno] + ".");
outbuffer.println(data[nackno]);
if(timers[nackno%sw]!=null)
{
System.out.println("Cancelling timer number " + nackno + ".");
timers[nackno%sw].timer.cancel();
timers[nackno%sw] = null;
}
System.out.println("Creating new timer in " + nackno);
timers[nackno%sw] = newTimeout(outbuffer, data[nackno], timeout);
}
}
else
{
System.out.println(incoming + " received.");
intackno = Integer.parseInt(incoming.substring(3, incoming.length()));
if(isBetween(ackno, Integer.parseInt(data[sf%m]), Integer.parseInt(data[sn%m]), m))
{
while(Integer.parseInt(data[sf%m]) != ackno)
{
System.out.println("Cancelling timer number " + sf%sw + ".");
timers[sf%sw].timer.cancel();
timers[sf%sw] = null;
sf++;
}
}
}
}
elseif(incoming.contains("FRAME"))
{
System.out.println("Receieved frame request packet.");
if(sn-sf >= sw)
{
System.out.println("Window size is already max. Not sending out frame.");
continue;
}
System.out.println("Sending out frame number " + sn%m);
outbuffer.println(data[sn%m]);
timers[sn%sw] = newTimeout(outbuffer, data[sn%m], timeout);
sn++;
}
elseif(incoming.equals("FINISH"))
{
System.out.println("Finished serving the client.");
break;
}
//sleep(50);
}
inbuffer.close();
outbuffer.close();
usocket.close();
uservice.close();
System.exit(0);
}
catch (IOExceptione)
{
System.out.println(e);
}
}
staticbooleanisBetween(intnum, intn1, intn2, intmod)
{
if(num == n1)
returntrue;
while(n1 != n2)
{
n1 = (n1 + 1)%mod;
if(num == n1)
returntrue;
}
returnfalse;
}
}