-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.py
50 lines (41 loc) · 1.56 KB
/
Network.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
"""
Communication Network Class
TR.AI.NS Project
Author: Amanda, Vinicius
"""
__all__ = ['Network']
from Train import Train
from Client import Client
from math import sqrt
class Network:
def __init__(self, simulator, log=False):
"""
The network class is responsible for masking the communication network
expected behavior in the TR.AI.NS project simulation.
"""
self.sim = simulator
self.log = log
def broadcast(self, msgStr, sender):
"""
This funcrion is responsible for delivering the desired message to its
receipients
:param msgStr: the message that is to be sent
"""
xs = sender.pos[0]
ys = sender.pos[1]
if self.log:
print("\033[93mNetwork:\033[0m Sender {}, from position ({},{})".format(sender.id, xs, ys))
d = 0
if isinstance(sender, Train):
d = self.sim.trainRange
if self.log:
print("\033[93mNetwork:\033[0m Sender is a train. Reachable distance is {} m".format(d))
elif isinstance(sender, Client):
d = self.sim.clientRange
if self.log:
print("\033[93mNetwork:\033[0m Sender is a client. Reachable distance is {} m".format(d))
for device in self.sim.devices:
if sqrt( (xs - device.pos[0])**2 + (ys - device.pos[1])**2 ) <= d:
device.receive_message(msgStr)
if self.log:
print("\033[93mNetwork:\033[0m Sent message to device {}".format(device.id))