Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions leads/comm/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 8 additions & 3 deletions leads/comm/client/client.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions leads/comm/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions leads/comm/server/server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
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


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:
"""
Expand Down Expand Up @@ -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()
Expand Down