-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectiveRepeatSender.java
More file actions
134 lines (131 loc) · 5.12 KB
/
SelectiveRepeatSender.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.
*/
package selectiverepeatsender;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author Danyal
*/
public class SelectiveRepeatSender
{
public static void main(String[] args) throws InterruptedException
{
Scanner scan = new Scanner(System.in);
int sw, sf = 0, sn = 0, m;
System.out.println("Input the value of 'm': ");
m = scan.nextInt();
sw = (int)Math.pow(2, m - 1);
int timeout = 200;
String[] data = new String[(int)Math.pow(2,m)];
Timeout[] timers = new Timeout[sw];
for(int i = 0; i < data.length; i++)
{
data[i] = Integer.toString(i);
} //Generating string data to send.
m = (int)Math.pow(2, m);
try
{
int portnumber = 2325;
ServerSocket uservice = new ServerSocket(portnumber);
System.out.println("Port number is: " + portnumber);
Socket usocket = uservice.accept();
System.out.println("accepted");
BufferedReader inbuffer = new BufferedReader(new InputStreamReader(usocket.getInputStream()));
PrintWriter outbuffer = new PrintWriter(usocket.getOutputStream(), true);
long[] timervals = new long[sw];
for(int i = 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++)
{
String incoming = inbuffer.readLine();
if(incoming.contains("ACK"))
{
if(incoming.contains("NACK"))
{
System.out.println(incoming + " received.");
int nackno = 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] = new Timeout(outbuffer, data[nackno], timeout);
}
}
else
{
System.out.println(incoming + " received.");
int ackno = 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++;
}
}
}
}
else if(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] = new Timeout(outbuffer, data[sn%m], timeout);
sn++;
}
else if(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 (IOException e)
{
System.out.println(e);
}
}
static boolean isBetween(int num, int n1, int n2, int mod)
{
if(num == n1)
return true;
while(n1 != n2)
{
n1 = (n1 + 1)%mod;
if(num == n1)
return true;
}
return false;
}
}