-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.java
301 lines (271 loc) · 7.63 KB
/
receiver.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
/**
* CS 438 -- MP2
* Reliable data transport over UDP
*/
import java.net.*;
import java.util.PriorityQueue;
import java.io.*;
/**
* @author Uttaresh
*/
public class receiver {
static int packet_size=0;
static int MSS=100;
//Flags:
static int DATA = 1;
static int DATAACK = 2;
static int FIN = 3;
static int FINACK = 4;
static int firstUnACKed;
static PriorityQueue<TCPSegment> packet_queue;
static InetAddress senderAddress;
static int senderPort;
static DatagramSocket receiverSocket;
/**
* Function to send one TCPSegment
*/
static void transmitSeg(int flag, int nextData){
// Create packet from function parameters
DatagramPacket packet = null;
TCPSegment segment = new TCPSegment();
segment.flag = flag;
if(flag == DATAACK){
segment.ack_no = nextData;
}
byte[] packet_data = common.objectToBytes(segment);
packet = new DatagramPacket(packet_data,packet_data.length, senderAddress, senderPort);
// Send packet over UDP
try {
receiverSocket.send(packet);
} catch (IOException e) {
// Could not send packet over UDP
e.printStackTrace();
System.exit(1);
}
}
/**
* To compile the receiver, use the following command:
* javac receiver
*
* To start receiver process, use the following command:
* java receiver <receiver-port> <loss-pattern>
* @param args
*/
public static void main(String[] args) {
/*
* Verify command line args
*/
if (args.length != 2){
System.out.println("Error. Incorrect syntax.");
System.out.println("java receiver <receiver-port> <loss-pattern>");
System.exit(1);
}
int port = 0;
try{
port = Integer.parseInt(args[0]);
}catch(NumberFormatException e){
System.out.println("Error. Receiver port must be int!");
System.out.println("java receiver <receiver-port> <loss-pattern>");
System.exit(1);
}
if (new File(args[1]).exists()==false){
System.out.println("Error. Could not find loss file.");
System.exit(1);
}
packet_size = common.SerializedPacketSize();
/* Get required loss pattern from file */
byte[] lossdata=null;
try{
RandomAccessFile lossfile = new RandomAccessFile(args[1],"r");
lossdata = new byte[(int)lossfile.length()];
lossfile.readFully(lossdata);
lossfile.close();
}catch(Exception e){
e.printStackTrace();
}
String loss_text = new String(lossdata);
// No packets received initially
long RSN=0;
int temp_num = 0;
// Read in first number to determine loss method
int loss_method=0;
int i;
for (i=0; i<loss_text.length(); i++){
if (loss_text.charAt(i) >= '0' && loss_text.charAt(i) <= '9'){
loss_method = loss_text.charAt(i) - '0';
break;
}
}
// Drop every few packets
int drop_every=0;
if (loss_method == 1){
for (i=i+1;i<loss_text.length(); i++){
if (loss_text.charAt(i)>='0'&&loss_text.charAt(i)<='9'){
temp_num = temp_num*10+(loss_text.charAt(i)-'0');
}else{
if (temp_num!=0){
drop_every = temp_num;
break;
}
}
}
}
// Create predetermined queue of packets to drop
PriorityQueue<Integer> drop_queue=null;
if (loss_method == 2){
drop_queue = new PriorityQueue<Integer>();
for (i=i+1; i<loss_text.length(); i++){
if (loss_text.charAt(i)>='0'&&loss_text.charAt(i)<='9'){
temp_num = temp_num*10+(loss_text.charAt(i)-'0');
}else{
if (temp_num!=0){
drop_queue.add(new Integer(temp_num));
temp_num = 0;
}
}
}
}
/*
* Open UDP listening socket
*/
receiverSocket = null;
try {
receiverSocket = new DatagramSocket(port);
} catch (SocketException e) {
System.out.println("Error. Could not start UDP Listening Socket");
e.printStackTrace();
System.exit(1);
}
// We will store all unwritten (out of order) packets in a priority queue
packet_queue = new PriorityQueue<TCPSegment>();
FileOutputStream f = null;
try {
f = new FileOutputStream("output.txt");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
/*
* Receive data segments from sender and process received segments
*/
firstUnACKed = 0;
boolean loopForever = true;
while (loopForever){
byte[] packet_data = new byte[packet_size];
DatagramPacket packet = new DatagramPacket(packet_data, packet_size);
try {
receiverSocket.receive(packet);
RSN++;
if (RSN==1) System.out.print("Receiving file..");
//else System.out.print(".");
if (loss_method == 1 && (RSN%drop_every==1)) continue;
if (loss_method == 2 && (drop_queue.peek()!=null)){
if (drop_queue.peek().intValue()==RSN){
drop_queue.poll().intValue();
continue;
}
}
senderAddress = packet.getAddress();
senderPort = packet.getPort();
TCPSegment this_seg = common.bytesToObject(packet_data);
//System.out.println("Seq received: " + this_seg.seq_no + " wanted: " + firstUnACKed);
if (this_seg.flag == FIN) break;
// If this segment was not received before, save it to our queue
if(packet_queue.contains(this_seg)==false){
packet_queue.add(this_seg);
}
/* If this segment is the first UnACKed segment, then remove
* it from the queue and append to file along with all consecutive
* segments available after this. Also send an ACK for the last
* continuous segment available.
*/
if (this_seg.seq_no==firstUnACKed){
TCPSegment temp;
while( (temp = packet_queue.peek()) != null ){
if (temp.seq_no<=firstUnACKed){
int size;
// Don't write trailing NULs
for (size=MSS;temp.data[size-1]==0&&size>0;size--);
f.write(packet_queue.poll().data, 0, size);
firstUnACKed += MSS;
}else break;
}
}
transmitSeg(DATAACK, firstUnACKed);
// Process DATA segment here
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Send FINACK for every FIN received.
*/
transmitSeg(FINACK, 0);
loopForever = true;
byte[] temp_data = new byte[packet_size];
DatagramPacket temp = new DatagramPacket(temp_data, packet_size);
try {
receiverSocket.setSoTimeout(2000); // Wait for 2 seconds. Enough time for our purposes
} catch (SocketException e1) {
e1.printStackTrace();
}
while (loopForever){
try {
receiverSocket.receive(temp);
if (common.bytesToObject(temp_data).flag==FIN){
// Send FINACK to server
transmitSeg(FINACK, 0);
loopForever = false;
}
}catch(SocketTimeoutException e){
break;
}catch (IOException e) {
e.printStackTrace();
}
}
/*
* Send FIN until FINACK received
*/
loopForever = true;
while(loopForever){
transmitSeg(FIN, 0);
try {
receiverSocket.setSoTimeout(500); // Are we supposed to keep a timeout calculation
} catch (SocketException e) { // JUST for this? Seems wasteful
System.out.println("Error. Could not set socket timeout");
e.printStackTrace();
}
try {
receiverSocket.receive(temp);
if (common.bytesToObject(temp_data).flag == FINACK)
loopForever = false;
}catch(SocketTimeoutException e){
continue;
}catch (IOException e) {
e.printStackTrace();
}
}
/* Close connection */
receiverSocket.close();
/*
* Print to screen and close file
*/
try {
f.close();
} catch (IOException e) {
e.printStackTrace();
}
byte[] filedata=null;
try {
RandomAccessFile fin = new RandomAccessFile("output.txt", "r");
filedata = new byte[(int)fin.length()];
fin.readFully(filedata);
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("\nFile successfully received. File contents:\n");
System.out.println(new String(filedata));
}
}