pydistsim.algorithm.node_algorithm.NodeAlgorithm
- class NodeAlgorithm(*args, **kwargs)[source]
Bases:
BaseAlgorithmBase class for distributed algorithms.
In subclass, the following functions and attributes should be defined:
Class attribute Status - subclass of StatusValues that defines all the possible statuses of the nodes STATUS must be written at the bottom after all functions are defined
Initializer: (optionally) Pass INI message to nodes that should start the algorithm and set some attributes needed by the specific algorithm
Methods
__init__add_observersalarmapply_restrictionsApply all applicable restrictions.
Block the inbox of a node, so that only messages that satisfy the filter can be processed.
Check if the algorithm's current state matches the expected initial state.
Check if the algorithm's current state matches the expected termination state.
check_restrictionsCheck if the restrictions are satisfied.
clear_observersClose the inbox of the node for a specific neighbor.
defaultDisable an alarm.
Disable all alarms set for the node.
Method used to initialize the algorithm.
Check if the distributed algorithm has come to an end or deadlock, i.e. there are no messages to pass and no alarms set.
is_initializednotify_observersOpen the inbox of the node for a specific neighbor.
receivingReset the algorithm to its initial state.
Send content to destination, with header.
Send a message to nodes listed in message's destination field.
Set an alarm for the node.
spontaneouslystepUnblock the inbox of a node, so that all messages can be processed.
Update the time of an alarm by adding a time difference.
Attributes
INITuple of statuses that nodes should have at the beginning of the algorithm.
Tuple of statuses that nodes should have at the end of the algorithm.
Tuple of restrictions that must be satisfied for the algorithm to run.
default_paramsnetworkrequired_paramsNode wrapper manager.
- NODE_WRAPPER_MANAGER_TYPE
Type of the node wrapper manager. Default is
WrapperManager.alias of
WrapperManager
- S_init = ()
Tuple of statuses that nodes should have at the beginning of the algorithm.
- S_term = ()
Tuple of statuses that nodes should have at the end of the algorithm.
- class Status(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
StatusValuesExample of StatusValues subclass that defines the possible statuses of the nodes.
- __new__(value)
- _generate_next_value_(start, count, last_values)
Return the lower-cased version of the member name.
- algorithm_restrictions = (<class 'pydistsim.restrictions.axioms.FiniteCommunicationDelays'>, <class 'pydistsim.restrictions.axioms.LocalOrientation'>)
Tuple of restrictions that must be satisfied for the algorithm to run.
- block_inbox(node: NodeAccess, filter: Callable[[Message[_NodeWrapper]], bool]) object[source]
Block the inbox of a node, so that only messages that satisfy the filter can be processed.
- Parameters:
node (NodeAccess) – The node for which the inbox is blocked.
filter (Callable[[Message], bool]) – The filter that the messages must satisfy.
- check_algorithm_initialization()[source]
Check if the algorithm’s current state matches the expected initial state.
This method depends on the correct configuration of the
S_initclass attribute of the algorithm.
- check_algorithm_termination()[source]
Check if the algorithm’s current state matches the expected termination state.
This method depends on the correct configuration of the
S_termclass attribute of the algorithm.
- close(node: NodeAccess, neighbor: NeighborLabel)[source]
Close the inbox of the node for a specific neighbor.
Implementation detail
The inbox is closed by blocking all messages that have the neighbor as the source. This is done by adding a filter to the inbox that blocks all messages that have the neighbor as the source. To be able to unblock the inbox later, the filter is stored in the node’s memory, under the key __closed_edges_filters__.
- param node:
The node for which the inbox is closed.
- type node:
NodeAccess
- param neighbor:
The neighbor for which the inbox is closed.
- type neighbor:
NeighborLabel
- disable_alarm(alarm: Alarm)[source]
Disable an alarm.
- Parameters:
alarm (Alarm) – The alarm to be disabled.
- disable_all_node_alarms(node_p: NodeAccess)[source]
Disable all alarms set for the node.
- Parameters:
node (NodeProxy) – The node for which the alarms are disabled.
- initializer()[source]
Method used to initialize the algorithm. Is always called first.
NodeAlgorithmsubclasses may want to reimplement it.Base implementation sends an INI message to the node with the lowest id and applies all restrictions.
- is_halted()[source]
Check if the distributed algorithm has come to an end or deadlock, i.e. there are no messages to pass and no alarms set.
A not-started algorithm is considered halted.
- Returns:
True if the algorithm is halted, False otherwise.
- Return type:
bool
- nwm
Node wrapper manager.
- open(node: NodeAccess, neighbor: NeighborLabel)[source]
Open the inbox of the node for a specific neighbor.
- Parameters:
node (NodeAccess) – The node for which the inbox is opened.
neighbor (NeighborLabel) – The neighbor for which the inbox is opened.
- send(node_source: NodeAccess, data: object, destination: NeighborLabel | list[NeighborLabel], header: str = None)[source]
Send content to destination, with header.
- Parameters:
node_w – Sender node
data – Content to be received by destination nodes.
destination – Destination node or list of destinations.
header – Optional, header for the message.
- send_msg(node_source: NodeAccess, message: Message[_NodeWrapper])[source]
Send a message to nodes listed in message’s destination field.
Note: Destination should be a list of nodes or one node.
Update message’s source field and inserts in node’s outbox one copy of it for each destination.
- Parameters:
node_source (NodeAccess) – The node that sends the message.
message (Message) – The message to be sent.
- set_alarm(node_p: NodeAccess, time: int, message: Message[_NodeWrapper] | None = None)[source]
Set an alarm for the node. One unit of time is one step of the algorithm. After the time has passed, the alarm will trigger and the message will be sent to the node. When will that message be processed is not guaranteed to be immediate.
Returns the alarm that was set, useful for disabling it later.
- unblock_inbox(node: NodeAccess, filter: object)[source]
Unblock the inbox of a node, so that all messages can be processed.
- Parameters:
node (NodeAccess) – The node for which the inbox is unblocked.
filter (object) – The filter that was used to block the inbox.