Bases: Extension
Controller for the communication handler. Can be used to send commands to the communication handler from a
protocol. Commands affect the communication between nodes, such as changing the transmission range.
Info
Every method in this class is a no-op if a communication handler is not active. This includes when the protocol
is not running on a python simulation environment.
Warning
This class is an extension and will raise an error if the protocol is not
running on a python simulation.
Source code in gradysim/simulator/extension/communication_controller.py
| class CommunicationController(Extension):
"""
Controller for the communication handler. Can be used to send commands to the communication handler from a
protocol. Commands affect the communication between nodes, such as changing the transmission range.
!!!info
Every method in this class is a no-op if a communication handler is not active. This includes when the protocol
is not running on a python simulation environment.
!!!warning
This class is an [extension][gradysim.simulator.extension] and will raise an error if the protocol is not
running on a python simulation.
"""
def __init__(self, protocol: IProtocol):
super().__init__(protocol)
if self._provider is not None:
self._communication: Optional[CommunicationHandler] = self._provider.handlers.get("communication")
if self._communication is None:
logging.warning("No communication handler detected. All commands will be no-ops.")
def set_transmission_range(self, transmission_range: float):
"""
Sets the transmission range of the protocol. This only affects the range of transmission, meaning other nodes
will receive communication messages from this node if they are within this range; but not the reverse.
No-op if no communication handler is active.
Args:
transmission_range: The new transmission range of the protocol. Must be a positive number.
Raises:
ValueError: If the transmission range is a negative number.
"""
if transmission_range < 0:
raise ValueError("Transmission range must be a positive number.")
if self._communication is None:
logging.warning("No communication handler detected, command ignored.")
return
self._communication.transmission_ranges[self._provider.get_id()] = transmission_range
|
set_transmission_range(transmission_range)
Sets the transmission range of the protocol. This only affects the range of transmission, meaning other nodes
will receive communication messages from this node if they are within this range; but not the reverse.
No-op if no communication handler is active.
Parameters:
Name |
Type |
Description |
Default |
transmission_range |
float
|
The new transmission range of the protocol. Must be a positive number.
|
required
|
Raises:
Type |
Description |
ValueError
|
If the transmission range is a negative number.
|
Source code in gradysim/simulator/extension/communication_controller.py
| def set_transmission_range(self, transmission_range: float):
"""
Sets the transmission range of the protocol. This only affects the range of transmission, meaning other nodes
will receive communication messages from this node if they are within this range; but not the reverse.
No-op if no communication handler is active.
Args:
transmission_range: The new transmission range of the protocol. Must be a positive number.
Raises:
ValueError: If the transmission range is a negative number.
"""
if transmission_range < 0:
raise ValueError("Transmission range must be a positive number.")
if self._communication is None:
logging.warning("No communication handler detected, command ignored.")
return
self._communication.transmission_ranges[self._provider.get_id()] = transmission_range
|