-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.py
More file actions
250 lines (194 loc) · 8.15 KB
/
Copy pathNetwork.py
File metadata and controls
250 lines (194 loc) · 8.15 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
class Node:
edges = []
DVs = [ ]
def __init__(self, id):
self.id = str(id)
self.RT = []
self.adjecencyList = []
self.updated = False
self.dvCount = 0
def __str__(self):
return "Node " + self.id
# rich comparison overload
def __lt__(self, other):
return int(self.id) < int(other.id)
def printRT(self):
out_string = ''
for row in self.RT:
out_string += ( f'{str(row.get("node")):>5}' + '|' +
f'{str(row.get("cost")):>6}' + '|' +
f'{str(row.get("nextHop")):>7}' + '\n' )
return out_string
def appendEntry(self, dest, cost, nextHop):
self.RT.append({"node": int(dest), "cost": int(cost), "nextHop": int(nextHop)})
def appendAdjacency(self, dest):
self.adjecencyList.append(int(dest))
def generateDVs(self):
for neighbor in self.adjecencyList:
self.DVs.append( {"source": self.id, "dest": neighbor, "DVs": self.RT[:]} )
def handleDVs(self):
if self.DVs: # check to see if the buffer is empty
for packet in self.DVs[:]: # changes to the packet will occur to old and new lists
if str(self.id) == str(packet["dest"]):
self.searchRT(packet["DVs"], packet["source"])
self.DVs.remove(packet)
self.dvCount += 1
def searchRT(self, table, sender):
cost = 0
for row in self.RT:
if str(row["node"]) == str(sender):
cost = int(row["cost"])
break
# Compare my DV to the RT
for entry in table:
found = False
for line in self.RT:
# if im in the list
if str(entry["node"]) == str(self.id):
found = True
continue
# if there is another instance of this node
if str(entry["node"]) == str(line["node"]):
found = True
if (int(entry["cost"]) + int(cost)) < line["cost"]:
self.updateRT(line["node"], (int(entry["cost"]) + int(cost)), sender)
self.updated = True
found = True
elif (int(entry["cost"]) + int(cost)) == line["cost"]:
self.updateRT(line["node"], (int(entry["cost"]) + int(cost)), sender)
found = True
# else: add the route to the table
if not found:
self.appendEntry(entry["node"], (entry["cost"] + cost), sender)
self.updated = True
def updateRT(self, node, cost, nextHop):
for entry in self.RT:
if str(entry["node"]) == str(node):
entry["cost"] = cost
entry["nextHop"] = nextHop
def isUpdated(self):
return self.updated
def receivePacket(self, src, dest, nodes):
if str(self.id) == str(dest):
self.edges.append(self.id)
return
else:
for entry in self.RT:
if str(entry["node"]) == str(dest):
self.forwardPacket(src, dest, int(entry["nextHop"]), nodes)
self.edges.append(self.id)
def forwardPacket(self, src, dest, nextHop, nodes):
for node in nodes:
if str(node.id) == str(nextHop):
node.receivePacket(src, dest, nodes)
class Network:
nodeList = []
lastNode = []
def __init__(self, fileName, fileData ):
self.numRounds = 0
self.fileName = fileName
self.fileData = fileData
self.convergence = False
self.roundNumber = 0
self.route = ""
def sendPacket(self, src, dest):
nodes = self.nodeList[:] # changes the contents of the list that both the
# original list and new list
self.nodeList[src].receivePacket(src, dest, nodes)
def getLastConverged(self):
#keep track of last updated node
last = self.nodeList[0]
for node in self.nodeList:
if node.updated:
last = node
return last.id
def returnRoutes(self):
length = len(self.nodeList[0].edges)
route = ""
for index in range(0, length):
if index != length-1:
route += "Node {0} to ".format(self.nodeList[0].edges[(length - 1) - index])
else:
route += "Node {0}".format(self.nodeList[0].edges[(length - 1) - index] +'\n\n')
return route
def createNodes(self):
nodes = []
for line in self.fileData:
src = int(line["node1"])
dest = int(line["node2"])
cost = int(line["cost"])
if src not in nodes:
newNode = Node(src)
newNode.appendEntry(dest, cost, dest)
newNode.appendAdjacency(dest)
self.nodeList.append(newNode)
nodes.append(src)
elif src in nodes:
for index, r in enumerate(self.nodeList):
if int(r.id) == src:
self.nodeList[index].appendEntry(dest, cost, dest)
self.nodeList[index].appendAdjacency(dest)
if dest not in nodes:
newNode = Node(dest)
newNode.appendEntry(src, cost, src)
newNode.appendAdjacency(src)
self.nodeList.append(newNode)
nodes.append(dest)
elif dest in nodes:
for index, r in enumerate(self.nodeList):
if int(r.id) == dest:
self.nodeList[index].appendEntry(src, cost, src)
self.nodeList[index].appendAdjacency(src)
def beginRounds(self, rounds):
self.nodeList.sort ( )
round = 0
while round < rounds:
if not self.convergence:
for node in self.nodeList:
node.generateDVs()
for node in self.nodeList:
node.handleDVs()
update = False
for node in self.nodeList:
if node.isUpdated():
update = True
if update:
self.convergence = False
else:
self.convergence = True
self.lastNode.append(self.getLastConverged())
for node in self.nodeList:
node.updated = False
round += 1
else:
print("Only needed " + str(round) + " rounds to converge")
break
self.numRounds = round
total = 0
for node in self.nodeList:
total += node.dvCount
if not self.convergence:
print("Error: Insufficient Rounds to convergence.")
else:
if self.fileName == "topology1.txt":
self.route = "\nNode 0 receives packet for node 3 via, "
self.sendPacket(src = 0, dest = 3)
elif self.fileName == "topology2.txt":
self.route = "\nNode 0 receives packet for node 7 via, "
self.sendPacket(src = 0, dest = 7)
elif self.fileName == "topology3.txt":
self.route = "\nNode 0 receives packet for node 23 via, "
self.sendPacket(src = 0, dest = 23)
with open ( 'outputfile.txt' , 'a' ) as outFile :
outFile.write( self.fileName + "\n"
+ "Then number of rounds till covergence is "
+ str(self.numRounds) + ".\n"
+ "The total Distance Vector Sent is "
+ str(total) + ", and the last converged node is "
+ str(self.lastNode[len(self.lastNode) - 2]) + ".")
def printResult( self ):
with open('outputfile.txt', 'a') as outFile:
for node in self.nodeList :
outFile.write ( "\nNode " + str ( node.id ) + '\n' )
outFile.write ( "Dest | cost | source\n" )
outFile.write( node.printRT ( ))