-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetReader.py
180 lines (148 loc) · 5.14 KB
/
NetReader.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import sys
import socket
import struct
try:
from mininet.topo import Topo
class dhcpTopo(Topo):
def __init__(self, blocking = False, link_opts = {}, xmlFilename = "/home/comhghall/Final-Year-Project/resources/netanim_topo.xml", **opts):
Topo.__init__(self, **opts)
hostIDs = []
switchIDs = []
hostCount = 0
switchCount = 0
routerId = 0
with open(xmlFilename, 'r') as file:
data = file.readlines()
for i in range(len(data)):
if data[i].startswith("<nu") and data[i].count('r="255"'):
#host
hostCount += 1
name = "h" + str(hostCount)
host = self.addHost(name)
hostIDs.append(int(data[i - 1][10]))
elif data[i].startswith("<nu") and data[i].count('g="255"'):
#switch
switchCount += 1
routerId = int(data[i - 1][10])
name = "s" + str(switchCount)
print "Adding switch ", name
switch = self.addSwitch(name, blocking = blocking)
switchIDs.append(int(data[i - 1][10]))
elif data[i].startswith("<nu") and data[i].count('b="255"'):
#switch
switchCount += 1
routerId = int(data[i - 1][10])
name = "r" + str(switchCount)
print "Adding router ", name
switch = self.addSwitch(name, blocking = blocking)
switchIDs.append(int(data[i - 1][10]))
#Need to wait until all hosts and switches have been added before
# adding links. Need to find proper way of getting host and switch ids.
links = []
for i in range(len(data)):
if data[i].startswith("<link"):
#link
indexOfEnd = data[i].index(">")
optionString = data[i][indexOfEnd + 1:]
optionList = optionString.split()
bw = optionList[0]
delay = optionList[1]
loss = optionList[2]
fromID = int(data[i][14])
toID = int(data[i][23])
for j in range(len(hostIDs)):
if hostIDs[j] == fromID:
#hostCount += 1
fromNode = str("h" + str(j + 1))
elif hostIDs[j] == toID:
#hostCount += 1
toNode = str("h" + str(j + 1))
for k in range(len(switchIDs)):
if switchIDs[k] == fromID:
#switchCount += 1
if switchIDs[k] == routerId:
fromNode = str("r" + str(k + 1))
else:
fromNode = str("s" + str(k + 1))
elif switchIDs[k] == toID:
#switchCount += 1
if switchIDs[k] == routerId:
toNode = str("r" + str(k + 1))
else:
toNode = str("s" + str(k + 1))
print "Adding link ", fromNode, " -> ", toNode, " bw: ", bw, " delay: ", delay, " loss: ", loss
link = fromNode + " " + toNode + "\n"
links.append(str(link))
with open("/home/comhghall/Final-Year-Project/resources/linkMap.txt", 'w') as file:
for i in range(len(links)):
file.writelines(links[i])
file.close()
link_opts['bw'] = float(bw)
link_opts['delay'] = float(delay)
link_opts['loss'] = float(loss)
self.addLink(fromNode, toNode, **link_opts)
class basicTopo(Topo):
def __init__(self, blocking = False, link_opts = {}, xmlFilename = "/home/comhghall/Final-Year-Project/resources/netanim_topo.xml", **opts):
Topo.__init__(self, **opts)
hostIDs = []
switchIDs = []
hostCount = 0
switchCount = 0
with open(xmlFilename, 'r') as file:
data = file.readlines()
for i in range(len(data)):
if data[i].startswith("<nu") and data[i].count('r="255"'):
#host
hostCount += 1
name = "h" + str(hostCount)
host = self.addHost(name)
hostIDs.append(int(data[i - 1][10]))
elif data[i].startswith("<nu") and data[i].count('g="255"'):
#switch
switchCount += 1
name = "s" + str(switchCount)
switch = self.addSwitch(name, blocking = blocking)
switchIDs.append(int(data[i - 1][10]))
hostCount = 0
switchCount = 1 #hardcoded for testing
#Need to wait until all hosts and switches have been added before
# adding links. Need to find proper way of getting host and switch ids.
links = []
for i in range(len(data)):
if data[i].startswith("<link"):
#link
indexOfEnd = data[i].index(">")
optionString = data[i][indexOfEnd + 1:]
optionList = optionString.split()
bw = optionList[0]
delay = optionList[1]
loss = optionList[2]
fromID = int(data[i][14])
toID = int(data[i][23])
for j in range(len(hostIDs)):
if hostIDs[j] == fromID:
#hostCount += 1
fromNode = str("h" + str(j + 1))
elif hostIDs[j] == toID:
#hostCount += 1
toNode = str("h" + str(j + 1))
for k in range(len(switchIDs)):
if switchIDs[k] == fromID:
#switchCount += 1
fromNode = str("s" + str(k + 1))
elif switchIDs[k] == toID:
#switchCount += 1
toNode = str("s" + str(k + 1))
print "Adding link ", fromNode, " -> ", toNode, " bw: ", bw, " delay: ", delay, " loss: ", loss
link = fromNode + " " + toNode + "\n"
links.append(str(link))
link_opts['bw'] = float(bw)
link_opts['delay'] = float(delay)
link_opts['loss'] = float(loss)
self.addLink(fromNode, toNode, **link_opts)
with open("/home/comhghall/Final-Year-Project/resources/linkMap.txt", 'w') as file:
for i in range(len(links)):
file.writelines(links[i])
file.close()
except ImportError:
pass