This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
177 lines (147 loc) · 5.29 KB
/
script.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
# README
#
# Author: Landon Moon, Tsebaot Meron
from datetime import datetime
import socket
import threading
from time import *
import sys
import os
# classes =====================================================================
class Router:
def __init__(self, label, IP, port):
# basic info
self.label = label
self.IP = IP
self.port = port
# connection info
self.conns = {}
self.DV = {}
# runtime info
self.updateAmt = 0
def run(self):
self.displayinit()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((self.IP, self.port))
reader = threading.Thread(target=self.reader, args=(sock,))
reader.start()
sender = threading.Thread(target=self.sender, args=(sock,))
sender.start()
# sleep until ^C is encountered
try:
while True:
sleep(1)
#run the dv over here
#self.Distance_vector()
except KeyboardInterrupt:
self.displayfinal()
print('Router shutdown.')
os._exit(1) # OS._exit reliquished the owned ports
def reader(self, sock):
while True:
try:
# recieve message
data, addr = sock.recvfrom(1024)
if addr[0] == self.IP:
continue
if addr[0] not in self.conns.keys(): #local host is weird sometimes
continue
# decode and parse
data = data.decode()
#print(f'== from:{addr[0]} ==')
for data in data.split(','):
toIP = data.split(' ')[0]
dist = int(data.split(' ')[1]) + self.conns[addr[0]]
#print(f'to:{toIP} - dist:{dist}')
# change DV values
tempDV = self.DV.copy()
matched = False
for ip, weight in tempDV.items():
# get minimum if exist
if toIP == ip:
minimum = min(weight, dist)
# if DV value is changed
if minimum != weight:
print(f'Changed DV entry: {toIP} - {tempDV[toIP]} => {toIP} - {dist}')
self.updateAmt += 1
# if ip match was found
self.DV[toIP] = minimum
matched = True
# enter entry if doesn't already exist
if not matched:
print(f'Added DV entry: {toIP} - {dist}')
self.updateAmt += 1
self.DV[toIP] = dist
except ConnectionResetError:
pass
return
def sender(self, sock):
while True:
# construct format for DV
data = ''
for dest, dist in self.DV.items():
data += f'{dest} {dist},'
data = data[:-1] # remove final comma
# send DV to all connections
for ip in self.conns.keys():
sock.sendto(data.encode(), (ip, self.port))
sleep(1)
return
def displayinit(self):
print("== label: %s IP: %s Port: %s ==" % (self.label, self.IP, self.port))
print("conns:", self.conns)
print()
return
def displayfinal(self):
print()
print("== %s %s %s ==" % (self.label, self.IP, self.port))
print('ID: 1001906270 and 1001629719')
print(f'Date and time: %s' % (datetime.now()))
print(f'Total updates: {self.updateAmt}')
print('-- final DV --')
for key, value in self.DV.items():
print(f' IP: {key} DIST: {value}')
print()
return
# functions ===================================================================
def findIP(routers, label):
for r in routers:
if r.label == label:
return r.IP
# main ========================================================================
def main():
# Command line arguments
if len(sys.argv)<2:
print("ERR. Expected:\"script.py PORT LABEL\"")
os._exit(1)
port = int(sys.argv[1])
router = sys.argv[2]
# Input config file date
configFile = open(".config", "r")
#print(configFile.read())--------------THIS PRINTS OUT CONFIG FOR ALL ROUTERS
routers = []
# creating routers
while (line:=configFile.readline().strip()) != "":
tokens = line.split()
routers.append( Router(tokens[1], tokens[0], port) )
# setting connection info
while (line:=configFile.readline().strip()) != "":
tokens = line.split()
for r in routers:
# if A -> B
if r.label == tokens[0]:
ip = findIP(routers,tokens[1])
r.conns[ip] = int(tokens[2])
# if B -> A
if r.label == tokens[1]:
ip = findIP(routers,tokens[0])
r.conns[ip] = int(tokens[2])
r.conns[r.IP] = 0
r.DV = r.conns # DV = conns initially
# activate router
for r in routers:
if r.label == router:
r.run()
return
if __name__ == "__main__":
main()