-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenereate_topology_txt.py
More file actions
78 lines (62 loc) · 2.18 KB
/
genereate_topology_txt.py
File metadata and controls
78 lines (62 loc) · 2.18 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
import json
import os
import sys
TOPOLOGY_TXT_PATH = "rss/build/topology.txt"
TOPOLOGY_JSON_PATH = "topology.json"
def start_mode():
# Delete topology.txt if it exists
if os.path.exists(TOPOLOGY_TXT_PATH):
os.remove(TOPOLOGY_TXT_PATH)
# Load JSON
with open(TOPOLOGY_JSON_PATH, 'r') as f:
data = json.load(f)
# Write topology.txt
with open(TOPOLOGY_TXT_PATH, 'w') as out:
for link in data.get("links", []):
src = link.get("src")
dst = link.get("dst")
bandwidth = link.get("bandwidth") or 0
latency = link.get("latency") or 0
jitter = link.get("jitter") or 0
packet_loss = link.get("packet_loss") or 0
out.write(f"{src} {dst} {bandwidth} {latency} {jitter} {packet_loss}\n")
print("✅ topology.txt recreated.")
def change_mode(args):
if len(args) != 6:
print("❌ Usage for 'change': python manage_topology.py change <src> <dst> <bandwidth> <latency> <jitter> <packet_loss>")
return
src, dst, bandwidth, latency, jitter, packet_loss = args
updated = False
lines = []
# Read current lines
with open(TOPOLOGY_TXT_PATH, 'r') as f:
for line in f:
parts = line.strip().split()
if len(parts) < 6:
lines.append(line)
continue
if parts[0] == src and parts[1] == dst:
lines.append(f"{src} {dst} {bandwidth} {latency} {jitter} {packet_loss}\n")
updated = True
else:
lines.append(line)
if not updated:
print(f"⚠️ Link {src} -> {dst} not found. No changes made.")
return
# Write updated lines back
with open(TOPOLOGY_TXT_PATH, 'w') as f:
f.writelines(lines)
print(f"✅ Updated link {src} -> {dst}.")
def main():
if len(sys.argv) < 2:
print("❌ Usage: python manage_topology.py <start|change> [...]")
return
mode = sys.argv[1]
if mode == "start":
start_mode()
elif mode == "change":
change_mode(sys.argv[2:])
else:
print("❌ Unknown mode. Use 'start' or 'change'.")
if __name__ == "__main__":
main()