Skip to content

Commit 213f1c8

Browse files
committed
docs: add docstrings to ManagerConsumer
1 parent 59ec7ee commit 213f1c8

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

manager/comms/new_consumer.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
WebSocket consumer module for the Robotics Application Manager (RAM).
3+
4+
Handles client connections, message processing, and communication with manager queue.
5+
"""
6+
17
import json
28
import logging
39
from queue import Queue
@@ -13,20 +19,32 @@
1319

1420

1521
class Client:
22+
"""Represents a client connected to the WebSocket server."""
23+
1624
def __init__(self, **kwargs):
25+
"""Initialize a Client instance with id, handler, and address."""
1726
self.id = kwargs["id"]
1827
self.handler = kwargs["handler"]
1928
self.address = kwargs["address"]
2029

2130

2231
class ManagerConsumer:
2332
"""
24-
Websocket server consumer for new Robotics Application Manager aka. RAM
33+
Websocket server consumer for new Robotics Application Manager aka: RAM.
34+
2535
Supports single client connection to RAM
2636
TODO: Better handling of single client connections, closing and redirecting
2737
"""
2838

2939
def __init__(self, host, port, manager_queue: Queue):
40+
"""
41+
Initialize the ManagerConsumer with host, port, and manager_queue.
42+
43+
Args:
44+
host (str): The host address for the WebSocket server.
45+
port (int): The port number for the WebSocket server.
46+
manager_queue (Queue): The queue for communication with the manager.
47+
"""
3048
self.host = host
3149
self.port = port
3250
self.server = WebsocketServer(host=host, port=port, loglevel=logging.INFO)
@@ -37,7 +55,8 @@ def __init__(self, host, port, manager_queue: Queue):
3755
ws_logger.setLevel(logging.INFO)
3856
ws_logger.handlers.clear()
3957
ws_formatter = logging.Formatter(
40-
"%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] (%(name)s) %(message)s",
58+
"%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] "
59+
"(%(name)s) %(message)s",
4160
"%H:%M:%S",
4261
)
4362
ws_console_handler = logging.StreamHandler()
@@ -51,11 +70,25 @@ def __init__(self, host, port, manager_queue: Queue):
5170
self.manager_queue = manager_queue
5271

5372
def handle_client_new(self, client, server):
73+
"""
74+
Handle a new client connection event.
75+
76+
Args:
77+
client: The client object representing the connected client.
78+
server: The WebSocket server instance.
79+
"""
5480
LogManager.logger.info(f"client connected: {client}")
5581
self.client = client
5682
self.server.deny_new_connections()
5783

5884
def handle_client_disconnect(self, client, server):
85+
"""
86+
Handle a client disconnection event.
87+
88+
Args:
89+
client: The client object representing the disconnected client.
90+
server: The WebSocket server instance.
91+
"""
5992
if client is None:
6093
return
6194
LogManager.logger.info(f"client disconnected: {client}")
@@ -70,6 +103,14 @@ def handle_client_disconnect(self, client, server):
70103
self.server.allow_new_connections()
71104

72105
def handle_message_received(self, client, server, websocket_message):
106+
"""
107+
Handle a message received from a client.
108+
109+
Args:
110+
client: The client object that sent the message.
111+
server: The WebSocket server instance.
112+
websocket_message (str): The message received from the client.
113+
"""
73114
LogManager.logger.info(
74115
f"message received length: {len(websocket_message)} from client {client}"
75116
)
@@ -90,6 +131,15 @@ def handle_message_received(self, client, server, websocket_message):
90131
raise e
91132

92133
def send_message(self, message_data, command=None):
134+
"""
135+
Send a message to the connected client.
136+
137+
Args:
138+
message_data: The message data to send, can be a ManagerConsumerMessage,
139+
ManagerConsumerMessageException, or other data.
140+
command (str, optional): The command associated with the message,
141+
used if message_data is not a ManagerConsumerMessage.
142+
"""
93143
if self.client is not None and self.server is not None:
94144
if isinstance(message_data, ManagerConsumerMessage):
95145
message = message_data
@@ -103,7 +153,9 @@ def send_message(self, message_data, command=None):
103153
self.server.send_message(self.client, str(message))
104154

105155
def start(self):
156+
"""Start the WebSocket server in a separate thread."""
106157
self.server.run_forever(threaded=True)
107158

108159
def stop(self):
160+
"""Stop the WebSocket server gracefully."""
109161
self.server.shutdown_gracefully()

0 commit comments

Comments
 (0)