Loading poolpay/__main__.py +4 −4 Changes for poolpay/__main__.py: 4 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ from poolpay.director.Director import Director from poolpay.display.PygameDisplay import PygameDisplay from poolpay.touch.EvdevTouchScreen import EvdevTouchScreen from poolpay.ui.pygame.play import play from poolpay.wire.Message import Message from poolpay.wire.Packet import Packet from poolpay.wire.Server import Server logging.basicConfig( Loading Loading @@ -94,11 +94,11 @@ logging.info("Creating wire.Server") server = Server(paths.socket_path) def on_message_received(message: Message) -> None: logging.debug(f"Received message {message}, will do nothing.") def on_packet_received(message: Packet) -> None: logging.debug(f"Received packet {message}, will do nothing.") server.on_receive(on_message_received) server.on_receive(on_packet_received) def handle_signal(signal_number: int, stack_frame: FrameType | None) -> None: Loading poolpay/wire/Client.py +8 −8 Changes for poolpay/wire/Client.py: 8 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -6,7 +6,7 @@ from dataclasses import dataclass, field from pathlib import Path from typing import Any, Self from poolpay.wire.Message import Message from poolpay.wire.Packet import Packet @dataclass Loading @@ -22,19 +22,19 @@ class Client(AbstractContextManager): def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None: self._socket.close() def send(self, message: Message) -> Message: def send(self, packet: Packet) -> Packet: """ Send the given message to the server and returns the server's response. Send the given packet to the server and returns the server's response. """ self._socket.sendall((json.dumps(message) + "\n").encode("utf-8")) self._socket.sendall((json.dumps(packet) + "\n").encode("utf-8")) # TODO What if the response is longer than 1024 bytes? response = self._socket.recv(1024) return json.loads(response.decode("utf-8")) def on_receive_push(self, handle_message: Callable[[Message], None]) -> None: def on_receive_push(self, handle_packet: Callable[[Packet], None]) -> None: """ Register a handler that is called whenever the client receives a push message from the server (i.e a message that is not a response to a previously sent message). Register a handler that is called whenever the client receives a push packet from the server (i.e a packet that is not a response to a previously sent packet). """ # TODO Handle receiving push messages # TODO Handle receiving push packets raise NotImplementedError() poolpay/wire/Message.pydeleted 100644 → 0 +0 −3 Changes for poolpay/wire/Message.py: 0 added lines, 3 removed lines. Original line number Diff line number Diff line from typing import Any Message = dict[str, Any] poolpay/wire/Packet.py 0 → 100644 +9 −0 Changes for poolpay/wire/Packet.py: 9 added lines, 0 removed lines. Original line number Diff line number Diff line """ A packet, i.e., a "dict which can easily be turned into JSON". Keys must be strings, values can be a number of primitives, lists, or other dicts which can be easily turned into JSON. Forbidden are values which are not primitives, e.g., any sort of more complex Python objects. """ type Primitive = str | int | float | bool | None type Packet = dict[str, Primitive | Packet | list[Primitive | Packet]] poolpay/wire/Server.py +10 −10 Changes for poolpay/wire/Server.py: 10 added lines, 10 removed lines. Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ from pathlib import Path from socketserver import BaseServer, StreamRequestHandler, ThreadingUnixStreamServer from threading import Thread from poolpay.wire.Message import Message from poolpay.wire.Packet import Packet # Maps Python's BaseServer instances to our Server instances, because apparently that's the only way a request handler # can access its surroundings Loading @@ -18,22 +18,22 @@ _servers: dict[BaseServer, Server] = {} class Server: socket_path: Path _server: BaseServer = field(init=False) _handlers: list[Callable[[Message], None]] = field(default_factory=list, init=False) _handlers: list[Callable[[Packet], None]] = field(default_factory=list, init=False) class RequestHandler(StreamRequestHandler): def handle(self) -> None: # TODO What to do if line is longer than 1024 bytes? json_str = self.rfile.readline(1024).strip().decode("utf-8") data = json.loads(json_str) packet = json.loads(json_str) response = {"status": "received"} self.wfile.write((json.dumps(response) + "\n").encode("utf-8")) for handler in _servers[self.server]._handlers: handler(data) handler(packet) def __post_init__(self) -> None: # Delete the socket path, if it still exist, because it is then most likely a remnant of some untidly # Delete the socket path, if it still exists, because it is then most likely a remnant of some untidly # terminated run of the application. We could of course also just have crashed a still running run of # the application (if we started the server on the same socket twice), but this is very unlikely. self.socket_path.unlink(missing_ok=True) Loading @@ -44,17 +44,17 @@ class Server: ) _servers[self._server] = self def broadcast(self, message: Message) -> None: def broadcast(self, packet: Packet) -> None: """ Brodcast the message to all connected clients (might be none) Brodcast the packet to all connected clients (might be none) """ raise NotImplementedError() # TODO def on_receive(self, handle_message: Callable[[Message], None]) -> None: def on_receive(self, handle_packet: Callable[[Packet], None]) -> None: """ Register a handler that is called whenever this server receives a message from any of its clients. Register a handler that is called whenever this server receives a packet from any of its clients. """ self._handlers.append(handle_message) self._handlers.append(handle_packet) def start(self) -> None: """ Loading Loading
poolpay/__main__.py +4 −4 Changes for poolpay/__main__.py: 4 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ from poolpay.director.Director import Director from poolpay.display.PygameDisplay import PygameDisplay from poolpay.touch.EvdevTouchScreen import EvdevTouchScreen from poolpay.ui.pygame.play import play from poolpay.wire.Message import Message from poolpay.wire.Packet import Packet from poolpay.wire.Server import Server logging.basicConfig( Loading Loading @@ -94,11 +94,11 @@ logging.info("Creating wire.Server") server = Server(paths.socket_path) def on_message_received(message: Message) -> None: logging.debug(f"Received message {message}, will do nothing.") def on_packet_received(message: Packet) -> None: logging.debug(f"Received packet {message}, will do nothing.") server.on_receive(on_message_received) server.on_receive(on_packet_received) def handle_signal(signal_number: int, stack_frame: FrameType | None) -> None: Loading
poolpay/wire/Client.py +8 −8 Changes for poolpay/wire/Client.py: 8 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -6,7 +6,7 @@ from dataclasses import dataclass, field from pathlib import Path from typing import Any, Self from poolpay.wire.Message import Message from poolpay.wire.Packet import Packet @dataclass Loading @@ -22,19 +22,19 @@ class Client(AbstractContextManager): def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None: self._socket.close() def send(self, message: Message) -> Message: def send(self, packet: Packet) -> Packet: """ Send the given message to the server and returns the server's response. Send the given packet to the server and returns the server's response. """ self._socket.sendall((json.dumps(message) + "\n").encode("utf-8")) self._socket.sendall((json.dumps(packet) + "\n").encode("utf-8")) # TODO What if the response is longer than 1024 bytes? response = self._socket.recv(1024) return json.loads(response.decode("utf-8")) def on_receive_push(self, handle_message: Callable[[Message], None]) -> None: def on_receive_push(self, handle_packet: Callable[[Packet], None]) -> None: """ Register a handler that is called whenever the client receives a push message from the server (i.e a message that is not a response to a previously sent message). Register a handler that is called whenever the client receives a push packet from the server (i.e a packet that is not a response to a previously sent packet). """ # TODO Handle receiving push messages # TODO Handle receiving push packets raise NotImplementedError()
poolpay/wire/Message.pydeleted 100644 → 0 +0 −3 Changes for poolpay/wire/Message.py: 0 added lines, 3 removed lines. Original line number Diff line number Diff line from typing import Any Message = dict[str, Any]
poolpay/wire/Packet.py 0 → 100644 +9 −0 Changes for poolpay/wire/Packet.py: 9 added lines, 0 removed lines. Original line number Diff line number Diff line """ A packet, i.e., a "dict which can easily be turned into JSON". Keys must be strings, values can be a number of primitives, lists, or other dicts which can be easily turned into JSON. Forbidden are values which are not primitives, e.g., any sort of more complex Python objects. """ type Primitive = str | int | float | bool | None type Packet = dict[str, Primitive | Packet | list[Primitive | Packet]]
poolpay/wire/Server.py +10 −10 Changes for poolpay/wire/Server.py: 10 added lines, 10 removed lines. Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ from pathlib import Path from socketserver import BaseServer, StreamRequestHandler, ThreadingUnixStreamServer from threading import Thread from poolpay.wire.Message import Message from poolpay.wire.Packet import Packet # Maps Python's BaseServer instances to our Server instances, because apparently that's the only way a request handler # can access its surroundings Loading @@ -18,22 +18,22 @@ _servers: dict[BaseServer, Server] = {} class Server: socket_path: Path _server: BaseServer = field(init=False) _handlers: list[Callable[[Message], None]] = field(default_factory=list, init=False) _handlers: list[Callable[[Packet], None]] = field(default_factory=list, init=False) class RequestHandler(StreamRequestHandler): def handle(self) -> None: # TODO What to do if line is longer than 1024 bytes? json_str = self.rfile.readline(1024).strip().decode("utf-8") data = json.loads(json_str) packet = json.loads(json_str) response = {"status": "received"} self.wfile.write((json.dumps(response) + "\n").encode("utf-8")) for handler in _servers[self.server]._handlers: handler(data) handler(packet) def __post_init__(self) -> None: # Delete the socket path, if it still exist, because it is then most likely a remnant of some untidly # Delete the socket path, if it still exists, because it is then most likely a remnant of some untidly # terminated run of the application. We could of course also just have crashed a still running run of # the application (if we started the server on the same socket twice), but this is very unlikely. self.socket_path.unlink(missing_ok=True) Loading @@ -44,17 +44,17 @@ class Server: ) _servers[self._server] = self def broadcast(self, message: Message) -> None: def broadcast(self, packet: Packet) -> None: """ Brodcast the message to all connected clients (might be none) Brodcast the packet to all connected clients (might be none) """ raise NotImplementedError() # TODO def on_receive(self, handle_message: Callable[[Message], None]) -> None: def on_receive(self, handle_packet: Callable[[Packet], None]) -> None: """ Register a handler that is called whenever this server receives a message from any of its clients. Register a handler that is called whenever this server receives a packet from any of its clients. """ self._handlers.append(handle_message) self._handlers.append(handle_packet) def start(self) -> None: """ Loading