The Network Interface

For convenience, all public methods and attributes of this class are exposed to the Robot class. For example

from crh_botnet import *
robot=Robot()

robot.network.set_server_address("choate-robotics-rpi-01.local") # The "legit" way
robot.set_server_address("choate-robotics-rpi-01.local") # Also valid

def setup():
    robot.network.broadcast("Hi") # What is documented

    robot.broadcast("Hi again")
    # Also valid because all methods are exposed to the robot.

    robot.shutdown()

robot.run(globals())
class crh_botnet.network.RobotNetwork(robot)

The networking interface that enable robots to communicate with each other. It provides functionalities for a robot to send a message to another robot, or to broadcast a message to every single robot in the network.

connected_robots

A list of IDs of all connected robots. It is not guaranteed to be in a particular order. Updates roughly once per 0.2 seconds.

coro

An instance of AsyncMethodsWrapper

is_connected
Returns

Whether the robot is connected to the network

Return type

bool

connect()

Connects to the network.

Returns

None

send(msg, recipient, callback=None)

Schedule a message to be sent. Note that this does not send out the message immediately. Rather, the message will be sent when the event loop is idle (while awaiting something).

Parameters
  • msg (str or Message) – The message for sending

  • recipient (int) – The ID of the recipient.

  • callback (FunctionType or None) – A callback function that takes a single argument: the Message object sent. Defaults to None.

Returns

None

broadcast(msg)

Broadcast a message to the entire robot network.

Parameters

msg (str or Message) – A message

Returns

None

disconnect()

Disconnect from the network :return:

classmethod set_server_address(address)

Set the address to the central server. The address is in format proto://addr:port. The protocol is assumed to be http if left blank.

Parameters

address (str) – The address of the central server.

Returns

None

class crh_botnet.network.AsyncMethodsWrapper(network)

This class exists purely for the purpose of scoping. All methods defined in this class are coroutines.

broadcast(msg)

This function is a a coroutine. Broadcast a message to the entire robot network.

Parameters

msg (Message or str) – The message for broadcasting

Returns

The message object broadcast (for callback purpose)

Return type

Message

disconnect()

This function is a a coroutine. Disconnect from the network.

Returns

None

poll() → List[crh_botnet.message.Message]

This function is a a coroutine.

Returns

A list of messages.

Return type

list[Message]

retrieve(msg_id: int)

This function is a coroutine.

Parameters

msg_id (int) – The message id

Returns

The retrieved message

Return type

Message

send(msg: crh_botnet.message.Message, recipient: int)

This function is a a coroutine.

Parameters
  • msg (Message or str) – The message for sending

  • recipient (int) – The ID of the recipient.

Returns

The message sent (for callback purpose)

Return type

Message