41 Commits

Author SHA1 Message Date
d7b136851b feat(client_details.html): add temporary initial telemetry display 2026-03-08 20:38:59 +01:00
7e9a9e6eed feat(backend_server.py): add initial_telemetry support 2026-03-08 20:38:32 +01:00
6ed03ab74d feat(client.py): add Client.initial_telemetry property 2026-03-08 20:38:01 +01:00
40c08d0169 feat(initial_handler.py): add handler for TELEMETRY/INTIIAL msgs 2026-03-08 20:37:26 +01:00
3d13d24116 build(uv.lock): update judas_protocol to 0.9.1 2026-03-05 22:29:48 +01:00
28b57b6964 build(uv.lock): update judas_protocol to 0.9.0 2026-03-05 21:26:34 +01:00
github-actions[bot]
6a024bbfc6 chore(release): 0.6.0 2026-03-05 19:59:49 +00:00
e5ca1066fd Merge pull request 'chore(release): 0.6.0' (#14) from release/0.6.0 into main
Reviewed-on: #14
2026-03-05 19:59:05 +00:00
61be674258 chore: remove cache/ directory 2026-03-05 20:52:41 +01:00
72d51b451f feat(backend_server.py): add notice to known_clients.yaml 2026-03-05 20:52:18 +01:00
efbf99f356 refactor(backend_server.py): if known_clients.yaml not present, call _save_known_clients() 2026-03-05 20:51:53 +01:00
f46d27164b chore(.gitignore): ignore config/known_clients.yaml as it's generated automatically 2026-03-05 20:38:26 +01:00
f13f243b5b chore(__main__.py): set logging level for werkzeug to WARNING 2026-03-05 13:14:08 +01:00
fbb75c263c fix(backend_server.py): fix double-connection handling 2026-03-05 13:05:48 +01:00
0e7c948668 feat(config/): add config/ directory 2026-03-04 09:23:54 +01:00
35899d3668 refactor(backend_server.py): move known_clients.yaml to config/ 2026-03-04 09:23:21 +01:00
88d349090e fix(backend_server.py): fix double disconnect if client.inbound empty 2026-03-03 22:20:36 +01:00
332238b403 chore(__init__.py): correct version 2026-03-03 21:55:56 +01:00
e54cc479b5 chore(handler/__init__.py): add AckHandler to __all__ 2026-03-03 20:56:18 +01:00
97fc17fbb3 feat(hello_handler.py): remove client from pending_hello if recv'd HELLO 2026-03-03 20:55:39 +01:00
f5b14fc610 feat(backend_server.py): add timeout on HELLO 2026-03-03 20:55:39 +01:00
bf1ad0ead0 feat(ack_handler.py): add handling for ACKs 2026-03-03 20:55:39 +01:00
a9bace8aca feat(backend_server.py): add ACK_TIMEOUT constant 2026-03-03 20:42:16 +01:00
c88e39c735 feat(backend_server.py): track message ACKs and resend if no ACK recv'd within 5 seconds 2026-03-03 20:39:53 +01:00
e308a07dab fix(backend_server.py): call _initialize_handlers() on init 2026-03-03 20:38:55 +01:00
dafe418916 feat(backend_server.py): add warning if received an unknown message (no handler) 2026-03-03 20:18:24 +01:00
c64a258243 build(uv.lock): update judas_protocol to 0.8.0 2026-03-03 19:34:32 +01:00
ead2224066 fix(backend_server.py): do not disconnect a client if Exception raised on msg handling 2026-03-03 18:56:34 +01:00
ee381414a9 chore(backend_server.py): remove redundant HELLO msg handling 2026-03-03 18:55:45 +01:00
0ed478a88e feat(backend_server.py): implement message handling 2026-03-03 18:55:24 +01:00
6446fe883c fix(backend_server.py): check if client to disconnect has an open socket 2026-03-03 18:54:45 +01:00
ec58a5257a chore(handler/__init__.py): add module init 2026-03-03 18:53:05 +01:00
c952413d91 feat(hello_handler.py): add HELLO message handler 2026-03-03 18:52:39 +01:00
882c8780e1 feat(base_handler.py): add BaseHandler class for message handling 2026-03-03 18:52:13 +01:00
d3f68d3baf chore(backend/__init__.py): add Client and ClientStatus to __all__ 2026-03-03 17:46:51 +01:00
62acc4b181 style(client.py): correct property typing 2026-03-03 17:46:06 +01:00
faecc38261 feat(client_status.py): move ClientStatus enum to own module 2026-03-03 17:45:01 +01:00
3eb681e233 refactor(backend_server.py): move loading known clients to its own method 2026-03-03 17:43:19 +01:00
bda10a6248 chore(cache/): add cache/ directory 2026-03-03 17:41:48 +01:00
fa2da207a9 refactor(backend_server.py): refactor calls to Message class constructors after protocol changes 2026-03-01 20:17:05 +01:00
f41a7774ec build(uv.lock): update judas_protocol to 0.7.0 2026-03-01 20:14:03 +01:00
17 changed files with 444 additions and 102 deletions

3
.gitignore vendored
View File

@@ -45,3 +45,6 @@ logs/
# Sphinx
docs/_build/
docs/ref/modules/
# known clients
config/known_clients.yaml

View File

@@ -2,6 +2,59 @@
All notable changes to this project will be documented in this file.
## [0.6.0] - 2026-03-05
### Bug Fixes
- [`fbb75c2`](https://github.com/pufereq/template-repo/commit/fbb75c263c13726d7e1c3fa28fb4e8f1f8cf0a17) **backend_server.py**: fix double-connection handling
- [`88d3490`](https://github.com/pufereq/template-repo/commit/88d349090e6e3605800656cc3e85779bf4b417c3) **backend_server.py**: fix double disconnect if client.inbound empty
- [`e308a07`](https://github.com/pufereq/template-repo/commit/e308a07dabcf20d9b080ce12a8928760dcde3057) **backend_server.py**: call `_initialize_handlers()` on init
- [`ead2224`](https://github.com/pufereq/template-repo/commit/ead22240660ff9f723c36ee35fa5918c33a0c66d) **backend_server.py**: do not disconnect a client if Exception raised on msg handling
- [`6446fe8`](https://github.com/pufereq/template-repo/commit/6446fe883cea4268992efc9f97cdb493af416576) **backend_server.py**: check if client to disconnect has an open socket
### Features
- [`72d51b4`](https://github.com/pufereq/template-repo/commit/72d51b451fc11ebe2f38d5e50bf263f9f441b03c) **backend_server.py**: add notice to known_clients.yaml
- [`0e7c948`](https://github.com/pufereq/template-repo/commit/0e7c9486683be4ceb91203109986f0b9fc83eb8b) **config/**: add `config/` directory
- [`97fc17f`](https://github.com/pufereq/template-repo/commit/97fc17fbb3ed5d681a4f5a0d488b7a2b09ab35df) **hello_handler.py**: remove client from `pending_hello` if recv'd HELLO
- [`f5b14fc`](https://github.com/pufereq/template-repo/commit/f5b14fc6109e83730c4b13aaecf6f4314c0136a4) **backend_server.py**: add timeout on HELLO
- [`bf1ad0e`](https://github.com/pufereq/template-repo/commit/bf1ad0ead0e4fc35c20d0d747cca05388b223d35) **ack_handler.py**: add handling for ACKs
- [`a9bace8`](https://github.com/pufereq/template-repo/commit/a9bace8acaab07e6e5d02155a9966c6381ad107e) **backend_server.py**: add `ACK_TIMEOUT` constant
- [`c88e39c`](https://github.com/pufereq/template-repo/commit/c88e39c7355b6f8c982198e6e56da2d09d17c61f) **backend_server.py**: track message ACKs and resend if no ACK recv'd within 5 seconds
- [`dafe418`](https://github.com/pufereq/template-repo/commit/dafe418916485258694949538eda151d7da33695) **backend_server.py**: add warning if received an unknown message (no handler)
- [`0ed478a`](https://github.com/pufereq/template-repo/commit/0ed478a88efa46f2f28b24371ad68010ecbf88d0) **backend_server.py**: implement message handling
- [`c952413`](https://github.com/pufereq/template-repo/commit/c952413d912b7a5d2000bc48c11a4d9274533caf) **hello_handler.py**: add HELLO message handler
- [`882c878`](https://github.com/pufereq/template-repo/commit/882c8780e15ec827fac1854cf7419b71d9a41a4f) **base_handler.py**: add `BaseHandler` class for message handling
- [`faecc38`](https://github.com/pufereq/template-repo/commit/faecc382610daf890c90074809085560931fd178) **client_status.py**: move `ClientStatus` enum to own module
### Miscellaneous Tasks
- [`61be674`](https://github.com/pufereq/template-repo/commit/61be674258dcce8a5b54efddb7d1d856c6cba9bc) remove `cache/` directory
- [`f46d271`](https://github.com/pufereq/template-repo/commit/f46d27164b79107abe405b221c9d424add55a723) **.gitignore**: ignore `config/known_clients.yaml` as it's generated automatically
- [`f13f243`](https://github.com/pufereq/template-repo/commit/f13f243b5b389d84acaa4ab933a234a00c6a981c) **__main__.py**: set logging level for werkzeug to WARNING
- [`332238b`](https://github.com/pufereq/template-repo/commit/332238b403782a08a8974b9267071f8d0ff5f006) **__init__.py**: correct version
- [`e54cc47`](https://github.com/pufereq/template-repo/commit/e54cc479b5f6bb3fb887803122fea0c551e27ee2) **handler/__init__.py**: add `AckHandler` to `__all__`
- [`ee38141`](https://github.com/pufereq/template-repo/commit/ee381414a931ed9d7562410e08c9de5cdc7b94e7) **backend_server.py**: remove redundant HELLO msg handling
- [`ec58a52`](https://github.com/pufereq/template-repo/commit/ec58a5257a3007a6edd502eb3967d92a9b652358) **handler/__init__.py**: add module init
- [`d3f68d3`](https://github.com/pufereq/template-repo/commit/d3f68d3baff8b903be125b0da7a88b415cd56bac) **backend/__init__.py**: add `Client` and `ClientStatus` to `__all__`
- [`bda10a6`](https://github.com/pufereq/template-repo/commit/bda10a6248478e99dfb42d8c605996cafdaf1e6e) **cache/**: add cache/ directory
### Refactor
- [`efbf99f`](https://github.com/pufereq/template-repo/commit/efbf99f356c20fbd48937f1c4e8df0a2d57f0651) **backend_server.py**: if known_clients.yaml not present, call `_save_known_clients()`
- [`35899d3`](https://github.com/pufereq/template-repo/commit/35899d366823c2878330eb06dc703f2965f61c3f) **backend_server.py**: move `known_clients.yaml` to `config/`
- [`3eb681e`](https://github.com/pufereq/template-repo/commit/3eb681e233fa151c2476581854c947bdc77b4900) **backend_server.py**: move loading known clients to its own method
- [`fa2da20`](https://github.com/pufereq/template-repo/commit/fa2da207a9cf3d90cdbfb5977033fee21d449528) **backend_server.py**: refactor calls to Message class constructors after protocol changes
### Styling
- [`62acc4b`](https://github.com/pufereq/template-repo/commit/62acc4b181cd359557909fa94f0cb0e6a4109255) **client.py**: correct property typing
### Build
- [`c64a258`](https://github.com/pufereq/template-repo/commit/c64a2582439e5ef1ab68bb9c588f7c4644bb4e07) **uv.lock**: update judas_protocol to 0.8.0
- [`f41a777`](https://github.com/pufereq/template-repo/commit/f41a7774ec5cb4997005ac8e31ed7bdc3541a8d6) **uv.lock**: update judas_protocol to 0.7.0
## [0.5.0] - 2026-02-28
### Features

0
config/.gitkeep Normal file
View File

View File

@@ -4,7 +4,7 @@ build-backend = "uv_build"
[project]
name = "judas_server"
version = "0.5.0"
version = "0.6.0"
description = "The backbone of the remote PC fleet management system."
readme = "README.md"
authors = []

View File

@@ -1 +1 @@
__version__: str = "0.1.0"
__version__: str = "0.5.0"

View File

@@ -12,6 +12,8 @@ if __name__ == "__main__":
format="%(asctime)s : [%(levelname)s] : %(threadName)s : %(name)s :: %(message)s",
)
lg.getLogger("werkzeug").setLevel(lg.WARNING)
ladygaga_logger = lg.getLogger(f"{__name__}.LAGA_DYGA")
ladygaga_logger.info(LADY_GAGA)

View File

@@ -1,3 +1,5 @@
from .backend_server import BackendServer
from .client import Client
from .client_status import ClientStatus
__all__ = ["BackendServer"]
__all__ = ["BackendServer", "Client", "ClientStatus"]

View File

@@ -6,16 +6,28 @@ import selectors
import socket
import threading
import time
from typing import TYPE_CHECKING, Any, Final
from judas_protocol.types import TelemetryAction
import yaml
from typing import Any
from judas_protocol import Category, ControlAction, Message
from judas_server.backend.client import Client, ClientStatus
from judas_server.backend.handler.hello_handler import HelloHandler
from judas_server.backend.handler.telemetry.initial_handler import (
InitialTelemetryHandler,
)
if TYPE_CHECKING:
from typing import Callable
from judas_protocol import ActionType
class BackendServer:
ACK_TIMEOUT: Final[float] = 5.0 # seconds
HELLO_TIMEOUT: Final[float] = 3.0 # seconds
def __init__(self, host: str = "0.0.0.0", port: int = 3692) -> None:
"""Initialize the backend server.
@@ -28,27 +40,6 @@ class BackendServer:
)
self.logger.debug("Initializing Server...")
self.known_clients: dict[str, dict[str, str | float]] = {}
try:
with open("cache/known_clients.yaml", "r") as f:
self.known_clients = (
yaml.safe_load(f).get("known_clients", {}) or {}
)
self.logger.debug(
f"Loaded known clients: {self.known_clients}"
)
self.logger.info(
f"Loaded {len(self.known_clients)} known clients"
)
except FileNotFoundError:
self.logger.warning(
"known_clients.yaml not found, creating empty known clients list"
)
with open("cache/known_clients.yaml", "w") as f:
yaml.safe_dump({"known_clients": {}}, f)
except Exception as e:
self.logger.error(f"Error loading known clients: {e}")
self.selector = selectors.DefaultSelector()
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(
@@ -63,20 +54,93 @@ class BackendServer:
self.clients: dict[str, Client] = {}
if self.known_clients:
for client_id in self.known_clients:
client = Client(id=client_id, addr=None, socket=None)
client.status = ClientStatus.OFFLINE
client.last_seen = float(
self.known_clients[client_id].get("last_seen", 0.0)
)
self.clients[client_id] = client
self.known_clients: dict[str, dict[str, str | float]] = {}
self.known_clients = self._load_known_clients()
self.message_handlers: dict[
tuple[Category, ActionType], Callable[[Client, Message], None]
] = {}
self._initialize_handlers()
self.pending_acks: list[tuple[Client, Message, float]] = []
self.pending_hello: dict[Client, float] = {}
self.running: bool = False
def _initialize_handlers(self) -> None:
"""Initialize message handlers."""
hello_handler = HelloHandler(self)
initial_telemetry_handler = InitialTelemetryHandler(self)
self.message_handlers[(Category.CONTROL, ControlAction.HELLO)] = (
hello_handler.handle
)
self.message_handlers[
(Category.TELEMETRY, TelemetryAction.INITIAL)
] = initial_telemetry_handler.handle
def _load_known_clients(self) -> dict[str, dict[str, str | float]]:
"""Load the list of known clients from a YAML file and validate."""
known_clients: dict[str, dict[str, str | float]] = {}
try:
with open("config/known_clients.yaml", "r") as f:
data = yaml.safe_load(f)
if not isinstance(data, dict):
raise ValueError("YAML root must be a dict")
known_clients = data.get("known_clients", {}) or {}
if not isinstance(known_clients, dict):
raise ValueError("'known_clients' must be a dict")
for client_id, client_data in known_clients.items():
if not isinstance(client_data, dict):
raise ValueError(
f"Client {client_id} data must be a dict"
)
last_seen = client_data.get("last_seen", 0.0)
if not isinstance(last_seen, (float, int)):
raise ValueError(
f"Client {client_id} 'last_seen' must be a float or int"
)
self.logger.debug(f"Loaded known clients: {known_clients}")
self.logger.info(f"Loaded {len(known_clients)} known clients")
for client_id in known_clients:
client = Client(id=client_id, addr=None, socket=None)
client.status = ClientStatus.OFFLINE
client.last_seen = float(
known_clients[client_id].get("last_seen", 0.0)
)
client.initial_telemetry = known_clients[client_id].get( # type: ignore
"initial_telemetry", None
)
self.clients[client_id] = client
except FileNotFoundError:
self.logger.warning(
"known_clients.yaml not found, creating empty known clients list"
)
self._save_known_clients()
except Exception as e:
self.logger.error(f"Error loading known clients: {e}")
raise
return known_clients
def _save_known_clients(self) -> None:
"""Save the list of known clients to a YAML file."""
with open("cache/known_clients.yaml", "w") as f:
with open("config/known_clients.yaml", "w") as f:
f.write(
"# This file is automatically generated by BackendServer.\n"
+ "# Do not edit manually.\n"
+ f"# Generated at: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}\n\n"
)
yaml.safe_dump({"known_clients": self.known_clients}, f)
self.logger.debug("Saved known clients")
@@ -99,6 +163,22 @@ class BackendServer:
)
time.sleep(1)
def send(self, client: Client, msg: Message) -> None:
"""Send a message to a client.
Args:
client (Client): The client to send the message to.
msg (Message): The message to send.
"""
msg_bytes: bytes = msg.to_bytes()
self.logger.info(
f"[>] Sending message {msg.id} to {client}, category: {msg.category}, action: {msg.action}, ack_required: {msg.ack_required}"
)
self.logger.debug(f"[>] Message bytes: {msg_bytes!r}")
if msg.ack_required:
self.pending_acks.append((client, msg, time.time()))
client.outbound += msg_bytes
def send_ack(self, client: Client, target_id: str) -> None:
"""Send an ACK message to a client.
@@ -106,9 +186,9 @@ class BackendServer:
client (Client): The client to send the ACK to.
target_id (str): The id of the ACK'd message.
"""
ack: bytes = Message.ack(target_id=target_id).to_bytes()
ack: Message = Message.Control.ack(target_id=target_id)
self.logger.info(f"[>] Sending ACK to {client}")
client.outbound += ack
self.send(client, ack)
def send_close(self, client: Client) -> None:
"""Send a CLOSE message to a client.
@@ -116,9 +196,9 @@ class BackendServer:
Args:
client (Client): The client to send the CLOSE message to.
"""
close_msg: bytes = Message.close().to_bytes()
close_msg: Message = Message.Control.close()
self.logger.info(f"[>] Sending CLOSE to {client}")
client.outbound += close_msg
self.send(client, close_msg)
def _accept_connection(self, sock: socket.socket) -> None:
"""Accept a new client connection.
@@ -134,6 +214,8 @@ class BackendServer:
events = selectors.EVENT_READ | selectors.EVENT_WRITE
self.selector.register(conn, events, data=client)
self.pending_hello[client] = time.time()
self.logger.info(f"[+] Registered client {client}, HELLO pending...")
def _disconnect(self, client: Client) -> None:
@@ -144,6 +226,12 @@ class BackendServer:
"""
self.logger.info(f"[-] Disconnecting {client}...")
if client.socket is None or client.socket._closed:
self.logger.warning(
f"Client {client} has no socket, nothing to disconnect."
)
return
try:
self.selector.unregister(client.socket)
except Exception as e:
@@ -202,51 +290,6 @@ class BackendServer:
try:
if mask & selectors.EVENT_READ:
self._receive_inbound(sock, client)
if not client.inbound:
self._disconnect(client)
return
if client.id is None:
# expect HELLO message
try:
msg = Message.from_bytes(client.inbound)
except Exception as e:
self.logger.error(
f"Failed to parse HELLO message from {client}: {e}"
)
self._disconnect(client)
return
if (
msg.category == Category.CONTROL
and msg.action == ControlAction.HELLO
and msg.payload.get("id") is not None
):
client.id = msg.payload["id"]
if (
client.id in self.clients
and self.clients[client.id].status == "connected"
):
old_client: Client = self.clients[client.id]
self.logger.warning(
f"Client {client.id} is already connected from {old_client.addr}, disconnecting old client..."
)
self.send_close(old_client)
self.clients[client.id] = client
self.known_clients[client.id] = {
"last_seen": client.last_seen
}
self._save_known_clients()
client.status = ClientStatus.ONLINE
self.logger.info(f"[+] Registered new client {client}")
else:
self.logger.error(
f"Expected HELLO message from {client}, got {msg}"
)
self._disconnect(client)
return
while b"\n" in client.inbound:
line, client.inbound = client.inbound.split(b"\n", 1)
@@ -256,13 +299,40 @@ class BackendServer:
try:
msg = Message.from_bytes(line)
self.logger.info(f"[.] Parsed message {msg.id}")
if client.id is None:
self.logger.debug(
f"Client {client} has no ID, expecting HELLO message..."
)
if (
msg.category != Category.CONTROL
or msg.action != ControlAction.HELLO
):
self.logger.warning(
f"First message from {client} must be HELLO, disconnecting..."
)
self._disconnect(client)
continue
handler: Callable[[Client, Message], None] | None = (
self.message_handlers.get(
(msg.category, msg.action), None
)
)
if handler is not None:
handler(client, msg)
else:
self.logger.warning(
f"No handler for message {msg.id} with category {msg.category} and action {msg.action}"
)
continue
if msg.ack_required:
self.send_ack(client, target_id=msg.id)
except Exception as e:
self.logger.error(
f"Failed to parse message from {client}: {e}"
)
self._disconnect(client)
return
if mask & selectors.EVENT_WRITE and client.outbound:
@@ -302,6 +372,25 @@ class BackendServer:
and now - client.last_seen > 60 * 60 * 24 # 24 hours
):
self.clients[client.id].status = ClientStatus.STALE
# check pending ACKs
for client, msg, timestamp in self.pending_acks[:]:
if time.time() - timestamp > self.ACK_TIMEOUT:
self.logger.warning(
f"ACK timeout for message {msg.id} to {client}, resending..."
)
self.send(client, msg)
self.pending_acks.remove((client, msg, timestamp))
# check pending HELLOs
for client, timestamp in list(self.pending_hello.items()):
if time.time() - timestamp > self.HELLO_TIMEOUT:
self.logger.warning(
f"HELLO timeout for {client}, disconnecting..."
)
self._disconnect(client)
del self.pending_hello[client]
time.sleep(0.001) # prevent 100% CPU usage
except Exception as e:
@@ -322,4 +411,5 @@ class BackendServer:
"addr": client.addr,
"last_seen": client.last_seen,
"status": client.status,
"initial_telemetry": client.initial_telemetry,
}

View File

@@ -5,24 +5,20 @@ from __future__ import annotations
import logging as lg
import socket
from enum import Enum
import time
from typing import Any
class ClientStatus(str, Enum):
"""Enumeration of client connection statuses."""
ONLINE = "online"
PENDING = "pending"
OFFLINE = "offline"
STALE = "stale"
from judas_server.backend.client_status import ClientStatus
class Client:
"""Represents a client."""
def __init__(
self, id: str | None, addr: tuple[str, int], socket: socket.socket
self,
id: str | None,
addr: tuple[str, int] | None,
socket: socket.socket | None,
) -> None:
"""Initialize the client.
@@ -41,13 +37,17 @@ class Client:
self.last_seen: float = 0.0 # unix timestanp of last inbound message
self.status: ClientStatus = ClientStatus.PENDING
self.socket: socket.socket = socket
self.addr: tuple[str, int] = addr
self.socket: socket.socket | None = socket
self.addr: tuple[str, int] | None = addr
self.inbound: bytes = b""
self.outbound: bytes = b""
self.initial_telemetry: dict[str, Any] | None = None
def __str__(self) -> str:
return f"Client({self.id} ({self.addr[0]}:{self.addr[1]}))"
if self.addr:
return f"Client({self.id} ({self.addr[0]}:{self.addr[1]}))"
return f"Client({self.id} (not connected))"
def __repr__(self) -> str:
return f"Client({self.id}, {self.addr})"
@@ -55,6 +55,11 @@ class Client:
def disconnect(self) -> None:
"""Disconnect the client and close the socket."""
self.logger.debug(f"Disconnecting Client {self}...")
if self.socket is None:
self.logger.warning(
f"Client {self} not connected, nothing to disconnect."
)
return
try:
self.socket.close()
except Exception as e:

View File

@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
from enum import Enum
class ClientStatus(str, Enum):
"""Enumeration of client connection statuses."""
ONLINE = "online"
PENDING = "pending"
OFFLINE = "offline"
STALE = "stale"

View File

@@ -0,0 +1,5 @@
from .base_handler import BaseHandler
from .hello_handler import HelloHandler
from .ack_handler import AckHandler
__all__ = ["BaseHandler", "HelloHandler", "AckHandler"]

View File

@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
from typing import TYPE_CHECKING
from .base_handler import BaseHandler
if TYPE_CHECKING:
from judas_protocol import Message
from judas_server.backend import BackendServer, Client
class AckHandler(BaseHandler):
def __init__(self, backend_server: BackendServer) -> None:
super().__init__(backend_server)
def handle(self, client: Client, message: Message) -> None:
pending_acks = self.backend_server.pending_acks
if message.id in pending_acks:
del pending_acks[message.id]
self.logger.debug(
f"[*] Received ACK for message {message.id} from {client}."
)
else:
self.logger.warning(
f"[!] Received ACK for unknown (or ACK'd) message {message.id} from {client}."
)

View File

@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
from __future__ import annotations
import logging as lg
from typing import TYPE_CHECKING
from judas_server.backend.client import Client
if TYPE_CHECKING:
from judas_protocol import Message
from judas_server.backend import BackendServer
class BaseHandler:
"""BaseHandler is the base class for all message handlers in the backend server.
It defines the interface for handling messages and provides common functionality for all handlers.
"""
def __init__(self, backend_server: BackendServer) -> None:
"""Initialize the BaseHandler with a reference to the backend server.
Args:
backend_server (BackendServer): The backend server instance that this handler belongs to.
"""
self.logger: lg.Logger = lg.getLogger(
f"{__name__}.{self.__class__.__name__}"
)
self.backend_server: BackendServer = backend_server
def handle(self, client: Client, message: Message) -> None:
"""Handle a message from a client.
This method must be implemented by subclasses to define the specific handling logic for different message types.
Args:
client (Client): The client that sent the message.
message (Message): The message to be handled.
"""
raise NotImplementedError("handle() must be implemented by subclasses")

View File

@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
from __future__ import annotations
from typing import TYPE_CHECKING, override
from judas_protocol import Category, ControlAction, Message
from judas_server.backend.client import ClientStatus
from judas_server.backend.handler import BaseHandler
if TYPE_CHECKING:
from judas_server.backend.backend_server import BackendServer
from judas_server.backend.client import Client
class HelloHandler(BaseHandler):
def __init__(self, backend_server: BackendServer) -> None:
super().__init__(backend_server)
@override
def handle(self, client: Client, message: Message) -> None:
if client.id is not None:
return
if (
message.category != Category.CONTROL
or message.action != ControlAction.HELLO
):
self.logger.error(
f"Expected HELLO message from {client}, got {message}, disconnecting client..."
)
self.backend_server._disconnect(client)
return
if message.payload.get("id") is None:
self.logger.error(
f"HELLO message from {client} missing 'id' field, disconnecting client..."
)
self.backend_server._disconnect(client)
return
client.id = message.payload["id"]
# check if client already connected, if so disconnect old client and register new one
if (
client.id in self.backend_server.clients
and self.backend_server.clients[client.id].status
== ClientStatus.ONLINE
):
old_client: Client = self.backend_server.clients[client.id]
self.backend_server.logger.warning(
f"Client {client.id} is already connected from {old_client.addr}, disconnecting old client..."
)
self.backend_server.send_close(old_client)
return
self.backend_server.clients[client.id] = client # type: ignore
self.backend_server.known_clients[client.id] = { # type: ignore
"last_seen": client.last_seen
}
del self.backend_server.pending_hello[client]
self.backend_server._save_known_clients()
client.status = ClientStatus.ONLINE
self.logger.info(f"[+] Registered new client {client}")

View File

@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""Initial telemetry handler."""
from __future__ import annotations
from typing import TYPE_CHECKING
from judas_protocol import Message
from judas_server.backend.handler.base_handler import BaseHandler
if TYPE_CHECKING:
from judas_server.backend import BackendServer
from judas_server.backend.client import Client
class InitialTelemetryHandler(BaseHandler):
"""Handles the initial telemetry message from a client."""
def __init__(self, backend_server: BackendServer) -> None:
"""Initialize the handler."""
super().__init__(backend_server)
def handle(self, client: Client, message: Message) -> None:
"""Handle the initial telemetry message."""
self.logger.debug(
f"Handling initial telemetry message from {client}..."
)
client.initial_telemetry = message.payload
self.backend_server.known_clients[client.id]["initial_telemetry"] = ( # type: ignore
message.payload
)

View File

@@ -18,6 +18,10 @@
<strong>Last Seen:</strong>
<span id="last-seen">{{ client.last_seen }}</span>
</p>
<p>
<strong>Initial:</strong>
<pre>{{ client.initial_telemetry | tojson(indent=2) }}</pre>
</p>
</body>
<script>

6
uv.lock generated
View File

@@ -358,12 +358,12 @@ wheels = [
[[package]]
name = "judas-protocol"
version = "0.6.0"
source = { git = "https://gitea.pufereq.pl/judas/judas_protocol.git#d16c1914ba343aed300f1c5fae0201370c3274de" }
version = "0.9.1"
source = { git = "https://gitea.pufereq.pl/judas/judas_protocol.git#085c34f232f95313d66db48a7d17bc25c92a35ae" }
[[package]]
name = "judas-server"
version = "0.5.0"
version = "0.6.0"
source = { editable = "." }
dependencies = [
{ name = "flask" },