-
Notifications
You must be signed in to change notification settings - Fork 17
/
Receiver.java
87 lines (55 loc) · 2.2 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
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.ArrayList;
public class Receiver {
// Probability of ACK loss
public static final double PROBABILITY = 0.1;
public static void main(String[] args) throws Exception{
DatagramSocket fromSender = new DatagramSocket(9876);
// 83 is the base size (in bytes) of a serialized RDTPacket object
byte[] receivedData = new byte[Sender.MSS + 83];
int waitingFor = 0;
ArrayList<RDTPacket> received = new ArrayList<RDTPacket>();
boolean end = false;
while(!end){
System.out.println("Waiting for packet");
// Receive packet
DatagramPacket receivedPacket = new DatagramPacket(receivedData, receivedData.length);
fromSender.receive(receivedPacket);
// Unserialize to a RDTPacket object
RDTPacket packet = (RDTPacket) Serializer.toObject(receivedPacket.getData());
System.out.println("Packet with sequence number " + packet.getSeq() + " received (last: " + packet.isLast() + " )");
if(packet.getSeq() == waitingFor && packet.isLast()){
waitingFor++;
received.add(packet);
System.out.println("Last packet received");
end = true;
}else if(packet.getSeq() == waitingFor){
waitingFor++;
received.add(packet);
System.out.println("Packed stored in buffer");
}else{
System.out.println("Packet discarded (not in order)");
}
// Create an RDTAck object
RDTAck ackObject = new RDTAck(waitingFor);
// Serialize
byte[] ackBytes = Serializer.toBytes(ackObject);
DatagramPacket ackPacket = new DatagramPacket(ackBytes, ackBytes.length, receivedPacket.getAddress(), receivedPacket.getPort());
// Send with some probability of loss
if(Math.random() > PROBABILITY){
fromSender.send(ackPacket);
}else{
System.out.println("[X] Lost ack with sequence number " + ackObject.getPacket());
}
System.out.println("Sending ACK to seq " + waitingFor + " with " + ackBytes.length + " bytes");
}
// Print the data received
System.out.println(" ------------ DATA ---------------- ");
for(RDTPacket p : received){
for(byte b: p.getData()){
System.out.print((char) b);
}
}
}
}