-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRdt.java
executable file
·310 lines (278 loc) · 10.4 KB
/
Rdt.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
/**
* Authors: Carlos Gonzalez, Nicola Pedretti
*
*/
/** Reliable Data Transport class.
*
* This class implements a reliable data transport service.
* It uses a sliding window protocol, on a packet basis,
* with selective repeat.
*
* An application layer thread provides new packet payloads to be
* sent using the provided send() method, and retrieves newly arrived
* payloads with the receive() method. Each application layer payload
* is sent as a separate UDP packet, along with a sequence number and
* a type flag that identifies a packet as a data packet or an
* acknowledgment. The sequence numbers are 15 bits.
*/
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
public class Rdt implements Runnable {
private int wSize; // protocol window
// size
private long timeout; // retransmission
// timeout in ns
private Substrate sub; // Substrate object
// for
// packet IO
// queues for communicating with source/sink
private ArrayBlockingQueue<String> fromSrc;
private ArrayBlockingQueue<String> toSnk;
// data structures to handle ack, send, receive packets
private Packet[] sendBuffer;
private long[] resendTimes;
private LinkedList<Short> resendList;
private String[] receiveBuffer;
// variables to keep track of sending and receiving buffer statuses
private short nextSequenceNumber;
private short nextExpectedPacket;
private Thread myThread; // local thread for
// this
// object
private boolean quit; // used to signal
// quitting time
/** Initialize a new Rdt object.
* @param wSize is the window size used by protocol; the sequence #
* space is twice the window size
* @param timeout is the time to wait before retransmitting
* @param sub is a reference to the Substrate object that this object
* uses to handle the socket IO
*/
Rdt(int wSize, double timeout, Substrate sub) {
this.wSize = Math.min(wSize, (1 << 14) - 1);
this.timeout = ((long) (timeout * 1000000000)); // sec to ns
this.sub = sub;
// create queues for application layer interface
fromSrc = new ArrayBlockingQueue<String>(1000, true);
toSnk = new ArrayBlockingQueue<String>(1000, true);
quit = false;
// initialize data structures to handle ack, send, receive packets
sendBuffer = new Packet[2 * wSize];
resendTimes = new long[2 * wSize];
resendList = new LinkedList<Short>();
receiveBuffer = new String[wSize];
// initialize variables to keep track of sending and receiving buffer
// statuses
nextSequenceNumber = 0;
nextExpectedPacket = 0;
}
/** Start the Rdt running. */
public void start() throws Exception {
myThread = new Thread(this);
myThread.start();
}
/** Stop the Rdt. */
public void stop() throws Exception {
quit = true;
myThread.join();
}
/** Increment sequence number, handling wrap-around.
* @param x is a sequence number
* @return next sequence number after x
*/
private short incr(short x) {
x++;
return (x < 2 * wSize ? x : 0);
}
/** Compute the difference between two sequence numbers,
* accounting for "wrap-around"
* @param x is a sequence number
* @param y is another sequence number
* @return difference, assuming x is "clockwise" from y
*/
private int diff(short x, short y) {
return (x >= y ? x - y : (x + 2 * wSize) - y);
}
/** Main thread for the Rdt object.
*
* Inserts payloads received from the application layer into
* packets, and sends them to the substrate. The packets include
* the number of packets and chars sent so far (including the
* current packet). It also takes packets received from
* the substrate and sends the extracted payloads
* up to the application layer. To ensure that packets are
* delivered reliably and in-order, using a sliding
* window protocol with the selective repeat feature.
*/
public void run() {
long t0 = System.nanoTime();
long now = 0; // current time (relative to t0)
while (!quit || (numUnAckedPackets() > 0)) {
//System.out.println("loop");
now = System.nanoTime() - t0;
if(uploadOrderedPackets()){
//System.out.println("uploadOrderedPackets called");
}else if(processIncomingPackets()){
//System.out.println("processIncomingPackets called");
}else if(resendTimedoutPackets(now)){
//System.out.println("resendTimedoutPackets called");
}else if(sendReadyPacket(now)){
//System.out.println("sendReadyPackets called");
}else
//System.out.println("sleep");
try {
Thread.sleep(1);
} catch (InterruptedException e) {
System.err.println("Rdt:run: " + "sleep exception " + e);
System.exit(1);
}
}
}
/**
* If receive buffer has a packet that can be delivered, deliver it to sink
* @return boolean true if anything is uploaded, false otherwise.
*/
private boolean uploadOrderedPackets() {
short index = (short) (nextExpectedPacket%wSize);
if(receiveBuffer[index] == null) return false;
int counter =0;
while (receiveBuffer[index] != null){
if(!toSnk.offer(receiveBuffer[index])){
nextExpectedPacket = index;
System.out.println("Maximum size of toSnk reached.");
return true;
}
counter++;
//System.out.println("uploading sequence: "+receiveBuffer[index]);
receiveBuffer[index] = null;
index = (short) (++index % wSize);
}
nextExpectedPacket = (short) ((nextExpectedPacket + counter)%(2*wSize));
return true;
}
/**
* If the substrate has an incoming packet get the packet from the substrate and process it
* @return boolean false if there is no incoming packet
*/
private boolean processIncomingPackets() {
if(!sub.incoming())
return false;
Packet rcvdPacket = sub.receive();
if (rcvdPacket.type == Packet.DATA_TYPE) {
Packet ack = new Packet();
ack.seqNum = rcvdPacket.seqNum;
ack.type = Packet.DATA_TYPE+1;
sub.send(ack);
//System.out.println("nextExpectedPacket: "+nextExpectedPacket);
//System.out.println("difference: "+diff(rcvdPacket.seqNum,nextExpectedPacket));
if(!(diff(rcvdPacket.seqNum,nextExpectedPacket)>wSize)){
//System.out.println("Adding seqNum: "+rcvdPacket.seqNum);
//System.out.println("Adding packet: "+rcvdPacket.payload);
receiveBuffer[rcvdPacket.seqNum % wSize] = rcvdPacket.payload;
}
}
else {
sendBuffer[rcvdPacket.seqNum] = null;
resendList.removeFirstOccurrence(rcvdPacket.seqNum);
}
return true;
}
/**
* Else if the resend timer has expired, re-send the oldest un-acked
* packet and reset timer
*/
public boolean resendTimedoutPackets(long now){
if(resendList.isEmpty()){
return false;
}
short seqNum = resendList.peek();
long oldSendTime = resendTimes[seqNum];
long timePassed = now - oldSendTime;
if (timePassed > timeout) {
short resentSeq = resendList.pop();
sub.send(sendBuffer[resentSeq]);
resendTimes[seqNum] = now;
resendList.add(resentSeq);
}return true;
}
/**
* if there is a message from the source waiting to be sent and the send window
* is not full and the substrate can accept a packet, create a packet containing the message
* and send it, after updating the send buffer and related data
*/
private boolean sendReadyPacket(long time) {
if(fromSrc.isEmpty() || !sub.ready())
return false;
if(!resendList.isEmpty()){
short lastUnAck = resendList.peek();
int packetsInReceiverBuff= diff(nextSequenceNumber,lastUnAck);
//System.out.println("lastUnAck: "+lastUnAck);
//System.out.println("nextExpectedPacket: "+nextExpectedPacket);
//System.out.println("pirb:"+packetsInReceiverBuff);
if(packetsInReceiverBuff >= wSize-1){
return false;
}
}
String message = fromSrc.poll();
Packet out = new Packet();
out.payload = message;
out.seqNum = nextSequenceNumber;
out.type = Packet.DATA_TYPE;
sendBuffer[nextSequenceNumber] = out;
resendTimes[nextSequenceNumber] = time;
resendList.add(nextSequenceNumber);
sub.send(out);
nextSequenceNumber = incr(nextSequenceNumber);
return true;
}
/**
* count how many unacked packets we have
* @return int number of unacked packets
*/
private int numUnAckedPackets() {
int numUnAckedPackets = 0;
for (Packet p : sendBuffer) {
if (p != null)
numUnAckedPackets++;
}
return numUnAckedPackets;
}
/** Send a message to peer.
* @param message is a string to be sent to the peer
*/
public void send(String message) {
try {
fromSrc.put(message);
} catch (Exception e) {
System.err.println("Rdt:send: put exception" + e);
System.exit(1);
}
}
/** Test if Rdt is ready to send a message.
* @return true if Rdt is ready
*/
public boolean ready() {
return fromSrc.remainingCapacity() > 0;
}
/** Get an incoming message.
* @return next message
*/
public String receive() {
String s = null;
try {
s = toSnk.take();
} catch (Exception e) {
System.err.println("Rdt:receive: take exception" + e);
System.exit(1);
}
return s;
}
/** Test for the presence of an incoming message.
* @return true if there is an incoming message
*/
public boolean incoming() {
return toSnk.size() > 0;
}
}