-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsrouter_sender.py
118 lines (105 loc) · 4.49 KB
/
lsrouter_sender.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
import threading
import time
import logging
import math
from type import *
class LsRouterSender(threading.Thread):
def __init__(self, router_socket, routing_table, buffer ):
threading.Thread.__init__(self)
self.router_socket = router_socket
self.routing_table = routing_table
self.send = True
self.buffer = buffer
def run(self):
""" Reads data from the buffer and send it over the network"""
logging.info("Starting Sender thread.")
routing_table = self.routing_table.neighbours
router_name = self.routing_table.router_name
while(self.send):
tosend = self.buffer.pop_send() # Blocking call if no elements in Buffer
self.handle_send(tosend)
def handle_send(self, tokens):
""" Handle the sending of different tokens"""
if tokens[0] == Type.DATA:
self.send_data(tokens[1], tokens[2], tokens[3])
elif tokens[0] == Type.LSACK:
self.send_lsack(tokens)
elif tokens[0] == Type.LSP:
self.send_lsp(tokens)
elif tokens[0] == Type.LSP_ONE:
self.send_lsp_one(tokens)
elif tokens[0] == Type.HELLO:
self.send_hello(tokens)
def send_lsack(self, tokens):
""" Send LSACK to the sender of the LSP """
try:
tosend = " "
tosend = tosend.join(tokens[0:3]).strip()
addr = tokens[3]
tosend = tosend.encode('ASCII')
self.router_socket.sendto(tosend,addr)
logging.debug("LSACK sent seq# "+str(tokens[2]))
except :
logging.error("Could not send, socket error")
def send_lsp(self, tokens):
""" Forwards lsp to all neighbours"""
try:
tosend = " "
tosend=tosend.join(tokens).strip()
tosend = tosend.encode('ASCII')
for key,value in self.routing_table.neighbours.items():
if value[Field.ACTIVE]:
addr = (value[Field.HOST], int(value[Field.PORT]))
self.router_socket.sendto(tosend,addr)
logging.debug("LSP sent. seq#: "+tokens[2]+" to "+key)
# Put LSP in ack needed list
value[Field.LSPLIST][int(tokens[2])]=[time.time(), tokens]
if tokens[1] == self.routing_table.router_name:
self.routing_table.lsp_timestamp=time.time() #Update LSP timestamp only if LSP initiated by us
except:
logging.error("Could not send, socket error")
def send_lsp_one(self, tokens):
""" Send LSP to one neighbour """
try:
tosend = " "
tosend=tosend.join(tokens[2:]).strip() # Don't take the 2 first fields
tosend = tosend.encode('ASCII')
value = self.routing_table.neighbours[tokens[1]]
addr = (value[Field.HOST], int(value[Field.PORT]))
self.router_socket.sendto(tosend,addr)
logging.debug("LSP resent. seq#: "+tokens[4]+" to "+tokens[1])
# Update timestamp of LSP
value[Field.LSPLIST][int(tokens[4])][0] = time.time()
except:
logging.error("Could not send, socket error")
def send_data(self, sender, receiver, msg):
""" Forward DATA packets"""
try:
tosend = 'DATA '+sender+' '+receiver+' '+msg
if receiver in self.routing_table.table:
# Get addr
via = self.routing_table.table[receiver]
if via in self.routing_table.neighbours:
neighbour = self.routing_table.neighbours[via]
addr = (neighbour[Field.HOST], int(neighbour[Field.PORT]))
tosend = tosend.encode('ASCII')
self.router_socket.sendto(tosend, addr)
else:
logging.error("Via is not a neighbour "+via)
else:
logging.error("Unknown host "+receiver)
except:
logging.error("Could not send, socket error")
def send_hello(self, tokens):
""" Sends the hello packet"""
try:
addr=tokens[3]
""" Sends HELLO Packet to addr"""
logging.debug("Sending HELLO to "+tokens[2])
tosend = " "
tosend = tosend.join(tokens[0:3]).strip()
tosend = tosend.encode('ASCII')
# Add LSP to send buffer
self.router_socket.sendto(tosend,addr)
except :
logging.error("Could not send, socket error")