surrortg.network

Classes

surrortg.network.ge_api_client

surrortg.network.message_router

This module implements different types of message routing strategies.

class InputBinding(dev: surrortg.inputs.input.Input, admin: bool)

Bases: object

dev: Input
admin: bool
class SeatStatus(enabled: bool, clientType: str)

Bases: object

enabled: bool
clientType: str
class MessageRouter

Bases: object

Basic message router that routes a command to the correct input.

Messages are routed to inputs based on input type and id. Following message format is expected:

{
    payload:
    {
        type:    <input type>,
        id:      <input id>,
        command: <input specific command object>
    }
}

Additional fields may be present and will be ignored.

async handle_message(msg, seat, is_admin_msg)

Routes a message according to input type and id.

Reads type and id from message and lookups corresponding route from registered routes.

If route is not found or message is malformed, warning is logged and nothing is done.

Parameters
  • msg (dict) – Message from game engine

  • seat (int) – Robot seat

  • is_admin_msg (bool) – Defines if the message is an admin message

register_input(dev_id, dev, admin)

Registers a callback for route

Parameters
  • dev_id (str) – Input device id

  • dev (Input) – Input device

  • admin (bool) – Describes if the input is for admin use only

trigger_watchdog_reset(seat)

Resets inputs and clears watchdog for given seat immediately

Parameters

seat (int) – Robot seat

class MultiSeatMessageRouter(robot_log_handler)

Bases: object

Routes messages to correct seat based on sender.

Messages are routed to seats (message routers) based on the sender. Following message format is expected:

{
    event: <event>
    dst: <recipient id>
    src: <sender id> (optional)
    seat: <robot seat> (optional)
    payload: (optional)
        {
            type:    <input type>,
            id:      <input id>,
            command: <input specific command object>
        }
    isAdmin: <boolean> (optional)
}

Additional fields may be present and will be ignored.

src field value gameEngine is a special value meaning that our game engine has sent the message. If gameEngine wants to send a message to specific seat, seat field can be used for that.

Parameters

robot_log_handler (function) – functions that handles robot logs

async handle_message(msg)

Handles a message.

This function either
  1. Handles newPeer/peerLeft events by modifying routing.

  2. Forwards a game engine message to seat using seat field.

  3. Forwards a peer message using src field and current routing.

  4. Drops a message and logs warning if none of the above apply.

register_input(dev_id, dev, admin=False)

Registers a new routing

Parameters
  • dev_id (str) – Input device id

  • dev (Input) – Input device

  • admin (bool, optional) – Describes if the input is for admin use only, defaults to False

async handle_routing_messages(msg)

Handle routing related game engine messages

Parameters

msg (dict) – Message from game engine

Returns

‘True’ if the message was handled, ‘False’ otherwise

Return type

bool

set_enabled_all(enabled)

Sets message routing states for all seats

Parameters

enabled (bool) – State to set the routings to

set_enabled_seat(seat, enabled)

Sets message routing states for the specified seat

Parameters
  • seat (int) – Robot seat

  • enabled (bool) – State to set the routings to

get_all_seats()

Returns all the seats that have routings set

Returns

All the seats that have routings set

Return type

list[int]

surrortg.network.socket_handler

exception MessageValidationError

Bases: Exception

class Message(event: str, dst: str, src: Union[str, NoneType] = None, seat: Union[int, NoneType] = 0, payload: Union[Dict[Any, Any], NoneType] = <factory>, isAdmin: Union[bool, NoneType] = False)

Bases: object

event: str
dst: str
src: Optional[str] = None
seat: Optional[int] = 0
payload: Optional[Dict[Any, Any]]
isAdmin: Optional[bool] = False
classmethod from_dict(dictionary)
to_dict()
class LocalSocketHandler(socket_name, message_handler)

Bases: object

Handles sending and receiving messages from unix local socket.

Messages are forwarded to the message router passed when constructing a LocalSocketHandler.

async run()
async do_receive()

Selects socket and reads it when ready

async connect()

Connects to the socket in an infinite loop with sleep

async send(msg)
shutdown()
class SocketHandler(url, query={}, message_callbacks=[], response_callbacks={}, socketio_connect_callback=<function SocketHandler.<lambda>>, socketio_logging_level=30)

Bases: object

Handles messaging through socketio and optional local socket

Parameters
  • url (String) – socketio connection url

  • query (Dict, optional) – socketio query parameters, defaults to {}

  • socketio_logging_level (Int/None, optional) – both socketio and engineio logging level, None disables all logging, defaults to logging.WARNING

async run()
async send_socketio(event, dst, seat, src=None, payload={}, callback=None)
send_socketio_threadsafe(event, dst, seat, src=None, payload={}, callback=None)
async send_local(event, dst, seat, src=None, payload={})
send_local_threadsafe(event, dst, seat, src=None, payload={})
async shutdown()

Shuts down SocketHandler gracefully with logging

surrortg.network.test_server

send_game_event(sid)