-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStopAndWaitARQ.java
136 lines (111 loc) · 4.21 KB
/
StopAndWaitARQ.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
// Contributed by - Anuj Das ( GC University, Silchar - @ Department of Computer Science )
// 2. Simulate and implement Stop And Wait ARQ protocol for noisy channel.
import java.util.Random;
class StopAndWaitARQ {
// Constants for the program
private static final int MESSAGE_LENGTH = 10;
private static final int TIMEOUT = 1000;
private static final double ERROR_RATE = 0.1;
// Variables for the program
private int sequenceNumber;
private boolean waitingForAck;
private boolean corrupted;
public StopAndWaitARQ() {
// Initialize variables
sequenceNumber = 0;
waitingForAck = false;
corrupted = false;
}
public void send() {
// Set the waiting flag and send the message
waitingForAck = true;
System.out.println("Sending message with sequence number: " + sequenceNumber);
sendMessage(sequenceNumber);
// Start the timeout timer
startTimer(TIMEOUT);
}
public void receive(int receivedSeqNum) {
// If the message is corrupted, ignore it
if (isCorrupted()) {
return;
}
// If we are waiting for an ACK and the received message is the ACK we were expecting,
// clear the waiting flag and send the next message
if (waitingForAck && receivedSeqNum == sequenceNumber) {
waitingForAck = false;
sequenceNumber = (sequenceNumber + 1) % 2;
send();
}
}
public void timeout() {
// If the timeout occurs while we are waiting for an ACK, resend the message
if (waitingForAck) {
send();
}
}
private void sendMessage(int seqNum) {
// Simulate sending the message over the channel
// In a real implementation, you would send the message through a network connection
System.out.println("Simulating sending message with sequence number: " + seqNum);
}
private void startTimer(int timeout) {
// Simulate starting the timeout timer
// In a real implementation, you would start a timer that would call the timeout() method
// after the specified number of milliseconds
System.out.println("Simulating starting timeout timer for " + timeout + "ms");
}
private boolean isCorrupted() {
// Simulate a noisy channel by randomly corrupting some of the messages
Random rand = new Random();
double r = rand.nextDouble();
if (r < ERROR_RATE) {
System.out.println("Message Corrupted!");
return true;
}
return false;
}
public static void main(String[] args) {
StopAndWaitARQ sender = new StopAndWaitARQ();
sender.send();
// Simulate receiving messages in a separate thread
Thread receiver = new Thread(() -> {
// Simulate receiving messages and sending ACKs
Random rand = new Random();
while (true) {
try {
// Sleep for a random amount of time before receiving the next message
Thread.sleep(rand.nextInt(2 * TIMEOUT));
// Simulate receiving a message
int seqNum = rand.nextInt(2);
System.out.println("Received message with sequence number: " + seqNum);
sender.receive(seqNum);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
receiver.start();
// Simulate timeouts in a separate thread
Thread timer = new Thread(() -> {
// Simulate timeouts
Random rand = new Random();
while (true) {
try {
// Sleep for a random amount of time before the next timeout
Thread.sleep(rand.nextInt(2 * TIMEOUT));
// Call the timeout method
sender.timeout();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
timer.start();
}
}
/*
This program simulates the stop-and-wait protocol for a noisy channel by sending messages, receiving ACKs or corrupted messages, and
handling timeouts. In a real implementation, you would send the messages and receive ACKs through a network connection, and use a timer
to handle timeouts. However, this program uses simulated methods to send messages, start timers, and receive messages to make it easier
to test and understand the stop-and-wait protocol.
*/