DefaultServer

dareplane_utils.default_server.server.DefaultServer(
    port=8080,
    ip='0.0.0.0',
    nlisten=10,
    name='default_server',
    thread_stopper=stop_thread,
    proc_stopper=stop_process,
    msg_interpreter=interpret_msg,
    pcommand_map=dict(),
    current_conn=None,
    server_socket=None,
    threads=dict(),
    processes=dict(),
    is_listening=False,
    logger=get_logger(__name__),
)

A class representing a default server used and modified by other Dareplane projects. This server handles incoming TCP connections, interprets messages, and manages threads and processes.

Attributes

Name Type Description
port int The port number on which the server listens. Default is 8080.
ip str The IP address on which the server listens. Default is “0.0.0.0”.
nlisten int The maximum number of queued connections. Default is 10.
name str The name of the server. Default is “default_server”.
thread_stopper Callable A function to stop threads. Default is stop_thread.
proc_stopper Callable A function to stop processes. Default is stop_process.
msg_interpreter Callable A function to interpret messages. Default is interpret_msg.
pcommand_map dict A dictionary mapping commands to their handlers.
current_conn socket.socket | None The current active connection.
server_socket socket.socket | None The server socket.
threads dict[str, tuple[threading.Thread, threading.Event]] A dictionary of active threads and their stop events.
processes dict[str, subprocess.Popen] A dictionary of active subprocesses.
is_listening bool A flag indicating whether the server is currently listening.
logger Logger The logger for the server.

Methods

Name Description
default_msg_interpretation Interpret default messages.
handle_msg Interpret the incoming message.
init_server Initialize the server socket and set up the stop event.
msg_interpretation Interpret the message and perform book keeping if necessary
shutdown Shutdown the server and close all connections
start_listening Start the server and listen for incoming connections.

default_msg_interpretation

dareplane_utils.default_server.server.DefaultServer.default_msg_interpretation(
    msg,
)

Interpret default messages.

This method handles default commands such as stopping processes or threads, closing the server, and retrieving the list of available commands. It logs relevant information and performs the necessary actions based on the received message.

Parameters

Name Type Description Default
msg bytes The incoming message to be interpreted. required

Returns

Name Type Description
bool True if the message was a default command, False otherwise.

handle_msg

dareplane_utils.default_server.server.DefaultServer.handle_msg(msg)

Interpret the incoming message.

This method processes the incoming message, checks if it is a concatenation of multiple commands, and handles each command individually. It also logs the received message and handles any errors that occur during message processing.

Parameters

Name Type Description Default
msg bytes The incoming message to be interpreted. required

init_server

dareplane_utils.default_server.server.DefaultServer.init_server(
    stop_event=threading.Event(),
)

Initialize the server socket and set up the stop event.

This method creates a socket, sets socket options, binds the socket to the specified IP and port, and sets it to listen for incoming connections. It also initializes a threading event to control the server’s listening state.

Parameters

Name Type Description Default
stop_event threading.Event A threading event to control the server’s listening state. Default is a new threading.Event. threading.Event()

msg_interpretation

dareplane_utils.default_server.server.DefaultServer.msg_interpretation(msg)

Interpret the message and perform book keeping if necessary

shutdown

dareplane_utils.default_server.server.DefaultServer.shutdown()

Shutdown the server and close all connections

start_listening

dareplane_utils.default_server.server.DefaultServer.start_listening()

Start the server and listen for incoming connections.

This method enters a loop where it waits for incoming TCP connections. When a connection is accepted, it handles incoming messages until the server is stopped. The method also manages the server’s listening state and logs relevant information.