-
Notifications
You must be signed in to change notification settings - Fork 0
/
Piggybacking.py
70 lines (58 loc) · 2.53 KB
/
Piggybacking.py
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
import time
import random
class NetworkNode:
def __init__(self, name):
self.name = name
self.buffer = []
self.sequence_number = 1
self.expected_sequence_number = 1
self.timer_start_time = None
self.timeout_duration = 2.0
def send_data(self, receiver, data):
packet = {
'type': 'data',
'sequence_number': self.sequence_number,
'data': data
}
self.sequence_number += 1
self.expected_sequence_number += 1
receiver.receive_packet(packet)
# Start the timer
self.timer_start_time = time.time()
def send_ack(self, receiver):
packet = {
'type': 'ack',
'ack_number': self.expected_sequence_number - 1
}
receiver.receive_packet(packet)
def handle_timeout(self):
if self.expected_sequence_number > self.sequence_number:
print(f"{self.name} Timeout: Resending data with sequence number {self.sequence_number}")
self.send_data(network_nodes[1], f"Resent Data with Seq {self.sequence_number}")
def receive_packet(self, packet):
if packet['type'] == 'data':
print(f"{self.name} received data with sequence number {packet['sequence_number']}: {packet['data']}")
if packet['sequence_number'] == self.expected_sequence_number:
self.expected_sequence_number += 1
self.send_ack(network_nodes[1])
self.timer_start_time = None
elif packet['type'] == 'ack':
print(f"{self.name} received ACK for sequence number {packet['ack_number']}")
if packet['ack_number'] == self.sequence_number - 1:
print(f"{self.name} acknowledged data with sequence number {packet['ack_number']}")
self.timer_start_time = None
def main():
node_names = ["Sender", "Receiver"]
global network_nodes
network_nodes = [NetworkNode(name) for name in node_names]
while True:
sender_input = input("Enter the message to send (or 'exit' to quit): ")
if sender_input.lower() == 'exit':
break
network_nodes[0].send_data(network_nodes[1], sender_input)
# Check for timeout in sender
if network_nodes[0].timer_start_time is not None and \
time.time() - network_nodes[0].timer_start_time > network_nodes[0].timeout_duration:
network_nodes[0].handle_timeout()
if __name__ == "__main__":
main()