-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.py
66 lines (57 loc) · 2.55 KB
/
link.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
from typing import Any
import random
from .packet import COPEPacket, ReceptionReport
class Link:
def __init__(self, node1: Any, node2: Any) -> None:
"""
Creates a link object between node1 and node2.
"""
self.node1 = node1
self.node2 = node2
def transmit(self, packet: COPEPacket, source: str, timestep: int, override: bool) -> bool:
"""
Given a packet and a source MAC address, returns True iff the destination node receives the packet.
Determined based on distance between nodes and strengths of the nodes.
"""
success = random.random() < self.get_probability()
if override:
success = True
if not success:
# print('link drop')
return success
elif source == self.node1.get_mac():
self.node2.receive_cope_packet(packet, timestep)
elif source == self.node2.get_mac():
self.node1.receive_cope_packet(packet, timestep)
else:
raise ValueError(
"node must be connected to link to transmit over it")
return success
def transmit_reception_report(self, report: ReceptionReport, source: str, timestep: int, override: bool) -> bool:
"""
Given a reception report and a source MAC address, returns True iff the destination node receives the report.
Determined based on distance between nodes and strengths of the nodes.
"""
success = random.random() < self.get_probability()
if override:
success = True
if not success:
return success
elif source == self.node1.get_mac():
self.node2.receive_reception_report(report)
elif source == self.node2.get_mac():
self.node1.receive_reception_report(report)
else:
raise ValueError(
"node must be connected to link to transmit over it")
return success
def get_probability(self) -> float:
"""
Return the probability of successful transmission along this node using the formula from https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7954581, with randomness in the formula removed for ease of testing.
"""
transmit_range = min(self.node1.get_transmit_distance(),
self.node2.get_transmit_distance())
n1_x, n1_y = self.node1.get_position()
n2_x, n2_y = self.node2.get_position()
actual = ((n1_x - n2_x) ** 2 + (n1_y - n2_y) ** 2) ** 0.5
return max(1 - 0.8 * actual / transmit_range, 0)