-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathclient.py
76 lines (60 loc) · 2.4 KB
/
client.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
import time
import sys
import Queue
from packet import Packet
class Client:
"""Client class sends periodic "traceroute" packets and returns routes that
these packets take back to the network object."""
def __init__(self, addr, allClients, sendRate, updateFunction):
"""Inititaliza parameters"""
self.addr = addr
self.allClients = allClients
self.sendRate = sendRate
self.lastTime = 0
self.link = None
self.updateFunction = updateFunction
self.sending = True
self.linkChanges = Queue.Queue()
self.keepRunning = True
def changeLink(self, change):
"""Add a link to the client.
The change argument should be a tuple ('add', link)"""
self.linkChanges.put(change)
def handlePacket(self, packet):
"""Handle receiving a packet. If it's a routing packet, ignore.
If it's a "traceroute" packet, update the network object with it's
route"""
if packet.kind == Packet.TRACEROUTE:
self.updateFunction(packet.srcAddr, packet.dstAddr, packet.route)
def sendTraceroutes(self):
"""Send "traceroute" packets to every other client in the network"""
for dstClient in self.allClients:
packet = Packet(Packet.TRACEROUTE, self.addr, dstClient)
if self.link:
self.link.send(packet, self.addr)
self.updateFunction(packet.srcAddr, packet.dstAddr, [])
def handleTime(self, timeMillisecs):
"""Send traceroute packets regularly"""
if self.sending and (timeMillisecs - self.lastTime > self.sendRate):
self.sendTraceroutes()
self.lastTime = timeMillisecs
def runClient(self):
"""Main loop of client"""
while self.keepRunning:
time.sleep(0.1)
timeMillisecs = int(round(time.time() * 1000))
try:
change = self.linkChanges.get_nowait()
if change[0] == "add":
self.link = change[1]
except Queue.Empty:
pass
if self.link:
packet = self.link.recv(self.addr)
if packet:
self.handlePacket(packet)
self.handleTime(timeMillisecs)
def lastSend(self):
"""Send one final batch of "traceroute" packets"""
self.sending = False
self.sendTraceroutes()