Repository navigation
Expand file tree
/
Copy pathrip_algorithm.py
More file actions
51 lines (41 loc) · 1.48 KB
/
Copy pathrip_algorithm.py
File metadata and controls
51 lines (41 loc) · 1.48 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
from utils import log_event
def run_rip_round(routers):
"""
Executes ONE RIP update round.
In one round:
- Each router sends its current routing table to all neighbors
- Neighbors update their tables if a shorter path is found
Returns:
converged (bool): True if no routing table changed
updates (list): Human-readable update messages (for GUI/logs)
"""
updates_log = []
changes_count = 0
# STEP 1: Prepare all RIP messages (synchronous round simulation)
messages = []
for router_name, router in routers.items():
routing_table_snapshot = router.get_routing_table()
for neighbor in router.neighbors:
messages.append({
"sender": router_name,
"receiver": neighbor.name,
"table": routing_table_snapshot
})
# STEP 2: Process all RIP messages
for msg in messages:
sender = msg["sender"]
receiver = msg["receiver"]
table = msg["table"]
receiver_router = routers[receiver]
updated = receiver_router.receive_update(sender, table)
if updated:
changes_count += 1
message = (
f"{receiver} updated its routing table "
f"based on information received from {sender}"
)
updates_log.append(message)
log_event(message)
# STEP 3: Check convergence
converged = (changes_count == 0)
return converged, updates_log