Skip to content

arbitrator

Classes

TransportArbitrator

TransportArbitrator(sharedTransport=None, codec=None)

Shares a transport between a server and multiple clients.

Source code in heartkit/backends/erpc/arbitrator.py
def __init__(self, sharedTransport=None, codec=None):
    self._transport = sharedTransport
    self._codec = codec
    self._pending_clients = {}
    self._lock = threading.Lock()

Functions

prepare_client_receive
prepare_client_receive(requestContext: RequestContext) -> int

Add a client request to the client list.

This call is made by the client thread prior to sending the invocation to the server. It Ensures that the transport arbitrator has the client's response message buffer ready in case it sees the response before the client even has a chance to call client_receive().

Parameters:

  • requestContext
    (RequestContext) –

    description

Returns:

  • int ( int ) –

    A token value to be passed to client_receive().

Source code in heartkit/backends/erpc/arbitrator.py
def prepare_client_receive(self, requestContext: RequestContext) -> int:
    """Add a client request to the client list.

    This call is made by the client thread prior to sending the invocation to the server. It
    Ensures that the transport arbitrator has the client's response message buffer ready in
    case it sees the response before the client even has a chance to call client_receive().

    Args:
        requestContext (client.RequestContext): _description_

    Returns:
        int: A token value to be passed to client_receive().
    """

    # Create pending client info.
    info = ClientInfo()
    info.event = threading.Event()

    # Add this client to the pending clients dict.
    try:
        self._lock.acquire()
        self._pending_clients[requestContext.sequence] = info
    finally:
        self._lock.release()

    return requestContext.sequence
client_receive
client_receive(token: int) -> bytearray

Receive method for the client.

Blocks until the a reply message is received with the expected sequence number that is associated with @a token. The client must have called prepare_client_receive() previously.

Parameters:

  • token
    (int) –

    The token previously returned by prepare_client_receive().

Returns:

  • bytearray ( bytearray ) –

    containing the received message.

Source code in heartkit/backends/erpc/arbitrator.py
def client_receive(self, token: int) -> bytearray:
    """Receive method for the client.

    Blocks until the a reply message is received with the expected sequence number that is
    associated with @a token. The client must have called prepare_client_receive() previously.

    Args:
        token (int): The token previously returned by prepare_client_receive().

    Returns:
        bytearray: containing the received message.
    """
    try:
        # Look up our client info.
        try:
            self._lock.acquire()
            client = self._pending_clients[token]
        finally:
            self._lock.release()

        # Wait for the reply to be received.
        client.event.wait()

        # Remove this client from the pending clients dict.
        try:
            self._lock.acquire()
            del self._pending_clients[token]
        finally:
            self._lock.release()

        # Return the received message.
        return client.msg
    except KeyError:
        pass