Skip to content

Utils

This module provides a socket server and client for managing connections in a distributed system.

ATTRIBUTE DESCRIPTION
BYTE_SIZE

size of the byte.

TYPE: int

CLASS DESCRIPTION
SocketServer

Class for handling socket server operations.

SocketClient

Class for handling socket client operations.

FUNCTION DESCRIPTION
create_listen_socket_callback

Create a callback function for listening to socket data.

create_socket_user

Create a Socket client instance.

create_socket_worker

Create a Socket worker instance.

create_socket_hub

Create a Socket hub instance.

Documentation last updated: 2025-06-11

SocketClient

start_client staticmethod

start_client(
    host: str,
    port: int,
    controller: Controller,
    relay: bool = False,
    *,
    terminate: Event | None = None,
)

Starts the client

PARAMETER DESCRIPTION
host

the host

TYPE: str

port

the port

TYPE: int

controller

the controller

TYPE: Controller

relay

flag to indicate relay. Defaults to False.

TYPE: bool DEFAULT: False

terminate

the termination event. Defaults to None.

TYPE: Event | None DEFAULT: None

SocketServer

handle_client staticmethod

handle_client(
    client_socket: socket,
    client_addr: str,
    controller: Controller,
    client_role: str | None = None,
    *,
    terminate: Event | None = None,
)

Handles communication with a single client

PARAMETER DESCRIPTION
client_socket

the client socket

TYPE: socket

client_addr

the client address

TYPE: str

controller

the controller

TYPE: Controller

client_role

the client role. Defaults to None.

TYPE: str | None DEFAULT: None

terminate

the termination event. Defaults to None.

TYPE: Event | None DEFAULT: None

start_server staticmethod

start_server(
    host: str,
    port: int,
    controller: Controller,
    *,
    n_connections: int = 5,
    terminate: Event | None = None,
)

Starts the server

PARAMETER DESCRIPTION
host

the host

TYPE: str

port

the port

TYPE: int

controller

the controller

TYPE: Controller

n_connections

the number of connections. Defaults to 5.

TYPE: int DEFAULT: 5

terminate

the termination event. Defaults to None

TYPE: Event | None DEFAULT: None

create_listen_socket_callback

create_listen_socket_callback(
    client_socket: socket, relay: bool
) -> Callable[[Any], str]

Create a callback function for listening to socket data.

PARAMETER DESCRIPTION
client_socket

the client socket

TYPE: socket

relay

flag to indicate if the socket is a relay

TYPE: bool

RETURNS DESCRIPTION
Callable[[Any], str]

Callable[[Any], str]: a function that listens for incoming data on the socket

create_socket_hub

create_socket_hub(
    host: str,
    port: int,
    address: str | None = None,
    relay: bool = True,
) -> tuple[Controller, dict[str, Any]]

Create a Socket client instance.

PARAMETER DESCRIPTION
host

the host address

TYPE: str

port

the port number

TYPE: int

address

the address to set for the controller. Defaults to None.

TYPE: str | None DEFAULT: None

relay

whether to relay messages. Defaults to True.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
tuple[Controller, dict[str, Any]]

tuple[Controller, dict[str,Any]]: a tuple containing the controller and a dictionary with termination event and thread information.

create_socket_user

create_socket_user(
    host: str,
    port: int,
    address: str | None = None,
    relay: bool = True,
) -> tuple[Controller, dict[str, Any]]

Create a Socket client instance.

PARAMETER DESCRIPTION
host

the host address

TYPE: str

port

the port number

TYPE: int

address

the address to set for the controller. Defaults to None.

TYPE: str | None DEFAULT: None

relay

whether to relay messages. Defaults to True.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
tuple[Controller, dict[str, Any]]

tuple[Controller, dict[str,Any]]: a tuple containing the controller and a dictionary with termination event and thread information.

create_socket_worker

create_socket_worker(
    host: str,
    port: int,
    address: str | None = None,
    relay: bool = True,
) -> tuple[Controller, dict[str, Any]]

Create a Socket client instance.

PARAMETER DESCRIPTION
host

the host address

TYPE: str

port

the port number

TYPE: int

address

the address to set for the controller. Defaults to None.

TYPE: str | None DEFAULT: None

relay

whether to relay messages. Defaults to True.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
tuple[Controller, dict[str, Any]]

tuple[Controller, dict[str,Any]]: a tuple containing the controller and a dictionary with termination event and thread information.