-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSwitch.py
More file actions
80 lines (68 loc) · 3.56 KB
/
Switch.py
File metadata and controls
80 lines (68 loc) · 3.56 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
# Project 2 for OMS6250
#
# This defines a Switch that can can send and receive spanning tree
# messages to converge on a final loop free forwarding topology. This
# class is a child class (specialization) of the StpSwitch class.
from Message import *
from StpSwitch import *
class Switch(StpSwitch):
def __init__(self, idNum, topolink, neighbors):
# -self.switchID (the ID number of this switch object)
# -self.links (the list of swtich IDs connected to this switch object)
super(Switch, self).__init__(idNum, topolink, neighbors)
self.root = self.switchID
self.distance = 0
self.pathThrough = self.switchID
self.activeLinks = []
def send_initial_messages(self):
for linkID in self.links:
# Message(root, distance, origen, destination, pathThrough)
message = Message(self.switchID, 0, self.switchID, linkID, False)
self.send_message(message)
return
def process_message(self, message):
# if they go through you add them to your active links
if message.pathThrough:
if message.origen not in self.activeLinks:
self.activeLinks.append(message.origen)
# if they don't go through you and you don't go through them delete them from your active links
elif message.pathThrough == False and message.origen != self.pathThrough and message.origen in self.activeLinks:
self.activeLinks.remove(message.origen)
# if the message has a new root
if message.root < self.root:
self.root = message.root
self.distance = message.distance + 1
self.pathThrough = message.origen
if self.pathThrough not in self.activeLinks:
self.activeLinks.append(self.pathThrough)
for linkID in self.links:
message = Message(self.root, self.distance, self.switchID, linkID, self.pathThrough == linkID)
self.send_message(message)
# if the message has the same root but a shorter distance
if message.root == self.root and message.distance + 1 < self.distance:
self.distance = message.distance + 1
newPathThrough = message.origen
for linkID in self.links:
message = Message(self.root, self.distance, self.switchID, linkID, newPathThrough == linkID)
self.send_message(message)
self.activeLinks.remove(self.pathThrough)
self.pathThrough = newPathThrough
if newPathThrough not in self.activeLinks:
self.activeLinks.append(newPathThrough)
# message has the same root and same distance but the sender has a smaller switchID than current path
if message.root == self.root and message.distance + 1 == self.distance and self.pathThrough > message.origen:
newPathThrough = message.origen
for linkID in self.links:
message = Message(self.root, self.distance, self.switchID, linkID, newPathThrough == linkID)
self.send_message(message)
self.activeLinks.remove(self.pathThrough)
self.pathThrough = newPathThrough
if newPathThrough not in self.activeLinks:
self.activeLinks.append(newPathThrough)
return
def generate_logstring(self):
linkStrings = []
self.activeLinks.sort()
for linkID in self.activeLinks:
linkStrings.append(str(self.switchID) + ' - ' + str(linkID))
return ', '.join(linkStrings)