docs(connector.py): add docstrings
This commit is contained in:
@@ -11,6 +11,8 @@ from judas_protocol import Message
|
|||||||
|
|
||||||
|
|
||||||
class Connector:
|
class Connector:
|
||||||
|
"""Connector class for managing TCP connection and message exchange."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
mac_address: str,
|
mac_address: str,
|
||||||
@@ -19,6 +21,14 @@ class Connector:
|
|||||||
*,
|
*,
|
||||||
on_message: Callable[[Message], None],
|
on_message: Callable[[Message], None],
|
||||||
) -> None:
|
) -> None:
|
||||||
|
"""Initialize the Connector.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
mac_address (str): The MAC address of the client.
|
||||||
|
host (str): The server host address.
|
||||||
|
port (int): The server port number.
|
||||||
|
on_message (Callable[[Message], None]): Callback for handling incoming messages.
|
||||||
|
"""
|
||||||
self.logger: lg.Logger = lg.getLogger(
|
self.logger: lg.Logger = lg.getLogger(
|
||||||
f"{__name__}.{self.__class__.__name__}"
|
f"{__name__}.{self.__class__.__name__}"
|
||||||
)
|
)
|
||||||
@@ -47,6 +57,7 @@ class Connector:
|
|||||||
self.on_message: Callable[[Message], None] = on_message
|
self.on_message: Callable[[Message], None] = on_message
|
||||||
|
|
||||||
def _send_outbound(self) -> None:
|
def _send_outbound(self) -> None:
|
||||||
|
"""Send data from the outbound buffer."""
|
||||||
while self.outbound_buffer:
|
while self.outbound_buffer:
|
||||||
try:
|
try:
|
||||||
sent = self.socket.send(self.outbound_buffer)
|
sent = self.socket.send(self.outbound_buffer)
|
||||||
@@ -63,6 +74,7 @@ class Connector:
|
|||||||
break
|
break
|
||||||
|
|
||||||
def _receive_inbound(self) -> None:
|
def _receive_inbound(self) -> None:
|
||||||
|
"""Receive data into the inbound buffer."""
|
||||||
try:
|
try:
|
||||||
data: bytes = self.socket.recv(4096)
|
data: bytes = self.socket.recv(4096)
|
||||||
if data:
|
if data:
|
||||||
@@ -76,18 +88,21 @@ class Connector:
|
|||||||
self.reconnect()
|
self.reconnect()
|
||||||
|
|
||||||
def send_hello(self) -> None:
|
def send_hello(self) -> None:
|
||||||
|
"""Send a HELLO message to the server."""
|
||||||
self.logger.debug("[*] Sending HELLO message...")
|
self.logger.debug("[*] Sending HELLO message...")
|
||||||
hello_message: bytes = Message.hello(self.mac_address).to_bytes()
|
hello_message: bytes = Message.hello(self.mac_address).to_bytes()
|
||||||
self.outbound_buffer += hello_message
|
self.outbound_buffer += hello_message
|
||||||
self._send_outbound()
|
self._send_outbound()
|
||||||
|
|
||||||
def close(self) -> None:
|
def close(self) -> None:
|
||||||
|
"""Close the connection and clean up resources."""
|
||||||
self.logger.debug("[*] Closing connection...")
|
self.logger.debug("[*] Closing connection...")
|
||||||
self.selector.unregister(self.socket)
|
self.selector.unregister(self.socket)
|
||||||
self.socket.close()
|
self.socket.close()
|
||||||
self.logger.debug("[.] Connection closed.")
|
self.logger.debug("[.] Connection closed.")
|
||||||
|
|
||||||
def reconnect(self) -> None:
|
def reconnect(self) -> None:
|
||||||
|
"""Reconnect to the server."""
|
||||||
self.logger.debug("[*] Reconnecting...")
|
self.logger.debug("[*] Reconnecting...")
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
@@ -102,6 +117,7 @@ class Connector:
|
|||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
def connect(self) -> None:
|
def connect(self) -> None:
|
||||||
|
"""Establish a connection to the server."""
|
||||||
self.logger.debug(f"Connecting to {self.host}:{self.port}...")
|
self.logger.debug(f"Connecting to {self.host}:{self.port}...")
|
||||||
connected: bool = False
|
connected: bool = False
|
||||||
delay: float = 1.0
|
delay: float = 1.0
|
||||||
@@ -122,6 +138,7 @@ class Connector:
|
|||||||
self.send_hello()
|
self.send_hello()
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
|
"""Run the main event loop."""
|
||||||
self.connect()
|
self.connect()
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
Reference in New Issue
Block a user