Module cope.link
Expand source code
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)
Classes
class Link (node1: Any, node2: Any)
-
Creates a link object between node1 and node2.
Expand source code
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)
Methods
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.
Expand source code
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)
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.
Expand source code
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.
Expand source code
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