Repository navigation
Expand file tree
/
Copy pathpacketswitchingmultir.py
More file actions
103 lines (84 loc) · 3.36 KB
/
Copy pathpacketswitchingmultir.py
File metadata and controls
103 lines (84 loc) · 3.36 KB
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
import random
import time
senderPackets = []
receiverPackets = []
r1 = []
r2 = []
r3 = []
r4 = []
message = ""
parity = 0
#input a message to send
message = input("Input a message: ")
#Go through each character of the message and turn it into a packet [header,payload,trailer]
#In each packet we will record the packet number which is the current list length, the character as payload, and the parity 'bit' which is the current length modulus 2 which alternates 0 or 1
for char in message:
#[packet number, payload, parity check]
senderPackets.append([len(senderPackets), char, len(senderPackets)%2])
print("Packets to be sent:", senderPackets)
loopCount = 0
while bool(senderPackets + r1 + r2 + r3 + r4):
#send each packet in the senderPackets list
if len(senderPackets) > 0:
#get and remove the first packet from the list
nextPacket = senderPackets.pop(0)
#choose a random router to send to
router = random.choice([r1,r2])
#add packet to the router list
print("Packet number", nextPacket[0], "with payload", nextPacket[1], "sent")
router.append(nextPacket)
if len(r1) > 0:
randomPacket = random.choice(r1)
#choose a random router to send to
router = random.choice([r2,r3,r4])
#add packet to the router list
print("Packet number", randomPacket[0], "with payload", randomPacket[1], "sent")
router.append(randomPacket)
r1.remove(randomPacket)
if len(r2) > 0:
randomPacket = random.choice(r2)
#choose a random router to send to
router = random.choice([r1,r3,r4 ])
#add packet to the router list
print("Packet number", randomPacket[0], "with payload", randomPacket[1], "sent")
router.append(randomPacket)
r2.remove(randomPacket)
if len(r3) > 0:
randomPacket = random.choice(r3)
#choose a random router to send to
router = random.choice([r2,r4,receiverPackets])
#add packet to the router list
print("Packet number", randomPacket[0], "with payload", randomPacket[1], "sent")
router.append(randomPacket)
r3.remove(randomPacket)
if len(r4) > 0:
randomPacket = random.choice(r4)
#choose a random router to send to
router = random.choice([r2,r3,receiverPackets])
#add packet to the router list
print("Packet number", randomPacket[0], "with payload", randomPacket[1], "sent")
router.append(randomPacket)
r4.remove(randomPacket)
loopCount += 1
time.sleep((0.1))
print("----\nFinished loop", loopCount, ":" , "\nSender:", senderPackets, "\nR1:", r1, "\nR2:", r2, "\nR3:", r3, "\nR4:", r4)
#output unsorted packets at receiver for checking
print("Unsorted packets received at receiver:")
for packet in receiverPackets:
print(packet)
#this returns the packet number so we can sort
def packetNumber(e):
return e[0]
#sort the packets by the packet number
receiverPackets.sort(key=packetNumber)
receiverMessage = ""
parity = 0
#reconstruct the message and also check for parity in message (should alternate between 1 and 0)
for packet in receiverPackets:
if parity % 2 == packet[2]:
print("Parity check for packet passed!")
parity += 1
receiverMessage += packet[1]
time.sleep(0.05)
print("------------")
print("Message received after", loopCount, "iterations :", receiverMessage)