-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudpclienthread.py
41 lines (30 loc) · 1.12 KB
/
udpclienthread.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
import socket
import time
import json
from routinginfo import RoutingInfo
class UDPClientThread:
"""
Runs as a thread and sends the routing table updates to the neighbours by calling send_update() of VirtualRouter
"""
__slots__ = "routing_table", "sleep_time"
def __init__(self, routing_table):
self.routing_table = routing_table
self.sleep_time = 1
def send(self, sendToIP, sendToPort):
"""
Sends the data to the server.
:param sendToIP: destination ip
:param sendToPort: destination port
:return: None
"""
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
dict_data = routing_table_to_dict(self.routing_table)
data_bytes = json.dumps(dict_data).encode('utf-8')
udp_socket.sendto(data_bytes, (sendToIP, sendToPort))
time.sleep(self.sleep_time)
def routing_table_to_dict(routing_table):
routing_table_dict = {}
for router in routing_table:
routing_table_dict[router] = RoutingInfo.to_dict(routing_table[router])
return routing_table_dict