From b3d65ecbe84de1b1c6e4d7ab3a0cca6a117fe55c Mon Sep 17 00:00:00 2001 From: Terry Fu Date: Mon, 15 Jul 2024 20:41:56 +0800 Subject: [PATCH] Support Customizing the Separators for Servers and Clients (#289) * Supported specifying the separators in the constructors of `Server` and `Client`. (#288) * Supported passing the separators to the constructors. (#288) --- leads/comm/client/__init__.py | 5 +++-- leads/comm/client/client.py | 11 ++++++++--- leads/comm/server/__init__.py | 5 +++-- leads/comm/server/server.py | 10 +++++++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/leads/comm/client/__init__.py b/leads/comm/client/__init__.py index f64178ef..acce0ee9 100644 --- a/leads/comm/client/__init__.py +++ b/leads/comm/client/__init__.py @@ -2,14 +2,15 @@ from leads.comm.prototype import Callback -def create_client(port: int = 16900, callback: Callback = Callback()) -> Client: +def create_client(port: int = 16900, callback: Callback = Callback(), separator: bytes = b";") -> Client: """ Create a client service. :param port: the port to which the client connects :param callback: the callback methods + :param separator: the separator that splits messages into sentences :return: the client service """ - return Client(port, callback) + return Client(port, callback, separator) def start_client(server_address: str, target: Client = create_client(), parallel: bool = False) -> Client: diff --git a/leads/comm/client/client.py b/leads/comm/client/client.py index 967bde5b..0e605dc6 100644 --- a/leads/comm/client/client.py +++ b/leads/comm/client/client.py @@ -1,13 +1,17 @@ from typing import override as _override -from leads.comm.prototype import Entity, Connection +from leads.comm.prototype import Entity, Connection, Callback class Client(Entity): """ You should use `create_client()` and `start_client()` instead of directly calling any method. """ - _connection: Connection | None = None + + def __init__(self, port: int, callback: Callback, separator: bytes) -> None: + super().__init__(port, callback) + self._connection: Connection | None = None + self._separator: bytes = separator @_override def run(self, server_address: str) -> None: @@ -17,7 +21,8 @@ def run(self, server_address: str) -> None: """ self._callback.on_initialize(self) self._socket.connect((server_address, self._port)) - self._callback.on_connect(self, connection := Connection(self, self._socket, (server_address, self._port))) + self._callback.on_connect(self, connection := Connection(self, self._socket, (server_address, self._port), + separator=self._separator)) self._connection = connection self._stage(connection) diff --git a/leads/comm/server/__init__.py b/leads/comm/server/__init__.py index 8505aff1..81a7830f 100644 --- a/leads/comm/server/__init__.py +++ b/leads/comm/server/__init__.py @@ -2,14 +2,15 @@ from leads.comm.server.server import Server -def create_server(port: int = 16900, callback: Callback = Callback()) -> Server: +def create_server(port: int = 16900, callback: Callback = Callback(), separator: bytes = b";") -> Server: """ Create a server service. :param port: the port on which the server listens :param callback: the callback methods + :param separator: the separator that splits messages into sentences :return: the server service """ - return Server(port, callback) + return Server(port, callback, separator) def start_server(target: Server = create_server(), parallel: bool = False) -> Server: diff --git a/leads/comm/server/server.py b/leads/comm/server/server.py index 8a3526b6..abe93d1f 100644 --- a/leads/comm/server/server.py +++ b/leads/comm/server/server.py @@ -1,7 +1,7 @@ from threading import Thread as _Thread from typing import override as _override -from leads.comm.prototype import Entity, Connection +from leads.comm.prototype import Entity, Connection, Callback from leads.os import _thread_flags @@ -9,7 +9,11 @@ class Server(Entity): """ You should use `create_server()` and `start_server()` instead of directly calling any method. """ - _connections: list[Connection] = [] + + def __init__(self, port: int, callback: Callback, separator: bytes) -> None: + super().__init__(port, callback) + self._connections: list[Connection] = [] + self._separator: bytes = separator def num_connections(self) -> int: """ @@ -39,7 +43,7 @@ def run(self, max_connection: int = 1) -> None: self._callback.on_initialize(self) while _thread_flags.active: socket, address = self._socket.accept() - self._callback.on_connect(self, connection := Connection(self, socket, address, + self._callback.on_connect(self, connection := Connection(self, socket, address, separator=self._separator, on_close=lambda c: self.remove_connection(c))) self._connections.append(connection) _Thread(target=self._stage, args=(connection,), daemon=True).start()