-
Notifications
You must be signed in to change notification settings - Fork 0
/
DISTANCE.PY
67 lines (59 loc) · 2.99 KB
/
DISTANCE.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
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
class DistanceVectorRoutingProtocol(DatagramProtocol):
def __init__(self, host, port):
self.host = host
self.port = port
self.routing_table = {}
self.neighbor_routers = []
def startProtocol(self):
self.transport.joinGroup("224.0.0.0")
print(f"Started routing protocol on {self.host}:{self.port}")
if self.port == 8000:
self.updateRoutingTable("A", "A", 0)
self.sendRoutingUpdate("A", "B", 2)
self.sendRoutingUpdate("A", "C", 4)
self.sendRoutingUpdate("A", "D", 7)
def sendRoutingUpdate(self, source, destination, cost):
routing_update = f"{source},{destination},{cost}"
self.transport.write(routing_update.encode(), ("224.0.0.0", self.port))
def datagramReceived(self, datagram, address):
routing_update = datagram.decode()
source, destination, cost = routing_update.split(",")
self.updateRoutingTable(source, destination, int(cost))
print(f"Received routing update from {address}: {routing_update}")
def updateRoutingTable(self, source, destination, cost):
if destination not in self.routing_table or cost < self.routing_table[destination][1]:
self.routing_table[destination] = (source, cost)
def calculateShortestPaths(self):
infinity = float("inf")
shortest_paths = {router: (infinity, None) for router in self.routing_table.keys()}
shortest_paths[self.host] = (0, self.host)
updated = True
while updated:
updated = False
for neighbor, (nh, cost) in self.routing_table.items():
for dest, (nh_dest, cost_dest) in self.routing_table.items():
if neighbor != self.host and dest != self.host:
new_cost = cost + cost_dest
if new_cost < shortest_paths[dest][0]:
shortest_paths[dest] = (new_cost, neighbor)
updated = True
self.routing_table = {dest: (nh_dest, cost_dest) for dest, (cost_dest, nh_dest) in shortest_paths.items()}
print("Routing table updated:")
print("Destination\tNext Hop\tCost")
for dest, (nh, cost) in self.routing_table.items():
print(f"{dest}\t\t{nh}\t\t{cost}")
self.broadcastRoutingTable()
def broadcastRoutingTable(self):
print("Broadcasting routing table:")
for dest, (nh, cost) in self.routing_table.items():
routing_update = f"{self.host},{dest},{cost}"
self.transport.write(routing_update.encode(), ("224.0.0.0", self.port))
if __name__ == "__main__":
host = "127.0.0.1"
port = 8000
protocol = DistanceVectorRoutingProtocol(host, port)
reactor.listenMulticast(port, protocol, listenMultiple=True)
reactor.callLater(5, protocol.calculateShortestPaths) # Delayed execution of shortest path calculation
reactor.run()