Update
All checks were successful
Build and Push Multi-Platform Docker Image / build-and-push (push) Successful in 16s

This commit is contained in:
Hirviturkki
2025-04-28 00:04:47 +02:00
parent 85effb4994
commit 5c851983ed

View File

@@ -20,6 +20,10 @@ STALE_TIMEOUT = 5.0
broadcast = False broadcast = False
warning = False
info = False
debug = False
async def handler(ws: websockets.WebSocketServerProtocol): async def handler(ws: websockets.WebSocketServerProtocol):
peer_id = None peer_id = None
try: try:
@@ -27,6 +31,7 @@ async def handler(ws: websockets.WebSocketServerProtocol):
try: try:
msg = json.loads(raw) msg = json.loads(raw)
except json.JSONDecodeError: except json.JSONDecodeError:
if warning:
logging.warning("Bad JSON from %s: %s", peer_id, raw) logging.warning("Bad JSON from %s: %s", peer_id, raw)
continue continue
@@ -46,6 +51,7 @@ async def handler(ws: websockets.WebSocketServerProtocol):
if target in CLIENTS: if target in CLIENTS:
await CLIENTS[target].send(raw) await CLIENTS[target].send(raw)
else: else:
if warning:
logging.warning("Target %s not found", target) logging.warning("Target %s not found", target)
elif mtype == "labels": elif mtype == "labels":
@@ -53,6 +59,7 @@ async def handler(ws: websockets.WebSocketServerProtocol):
peer_id = msg.get("id") peer_id = msg.get("id")
freqs = msg.get("freqs") freqs = msg.get("freqs")
transitions = msg.get("transitions") transitions = msg.get("transitions")
if info:
logging.info(f"Got labels from {peer_id}: {msg['labels']}") logging.info(f"Got labels from {peer_id}: {msg['labels']}")
if peer_id and isinstance(labels, list): if peer_id and isinstance(labels, list):
@@ -70,14 +77,17 @@ async def handler(ws: websockets.WebSocketServerProtocol):
if other_id != peer_id: if other_id != peer_id:
await other_ws.send(payload) await other_ws.send(payload)
count += 1 count += 1
if info:
logging.info(f"Re-broadcasted labels to {count} other clients (excluding {peer_id})") logging.info(f"Re-broadcasted labels to {count} other clients (excluding {peer_id})")
else: else:
if warning:
logging.warning("Malformed labels payload from %s: %s", peer_id, raw) logging.warning("Malformed labels payload from %s: %s", peer_id, raw)
else: else:
if warning:
logging.warning("Unknown message type from %s: %s", peer_id, mtype) logging.warning("Unknown message type from %s: %s", peer_id, mtype)
except websockets.ConnectionClosed: except websockets.ConnectionClosed:
pass pass
@@ -86,6 +96,7 @@ async def handler(ws: websockets.WebSocketServerProtocol):
CLIENTS.pop(peer_id, None) CLIENTS.pop(peer_id, None)
CLIENT_LABELS.pop(peer_id, None) CLIENT_LABELS.pop(peer_id, None)
CLIENT_LABELS_TS.pop(peer_id, None) CLIENT_LABELS_TS.pop(peer_id, None)
if info:
logging.info("Disconnected %s (%d clients left)", peer_id, len(CLIENTS)) logging.info("Disconnected %s (%d clients left)", peer_id, len(CLIENTS))
@@ -101,6 +112,7 @@ async def broadcast_labels_periodically():
# Optionally also remove their timestamp entry if you don't # Optionally also remove their timestamp entry if you don't
# want them checked again until next registration/update: # want them checked again until next registration/update:
# CLIENT_LABELS_TS.pop(pid, None) # CLIENT_LABELS_TS.pop(pid, None)
if debug:
logging.debug("Cleared labels for %s due to timeout", pid) logging.debug("Cleared labels for %s due to timeout", pid)
# 2) broadcast nested lists # 2) broadcast nested lists
@@ -113,7 +125,9 @@ async def broadcast_labels_periodically():
await asyncio.gather(*( await asyncio.gather(*(
ws.send(payload) for ws in CLIENTS.values() ws.send(payload) for ws in CLIENTS.values()
), return_exceptions=True) ), return_exceptions=True)
if debug:
logging.debug("Broadcasted labels to %d clients", len(CLIENTS)) logging.debug("Broadcasted labels to %d clients", len(CLIENTS))
if info:
logging.info(f"Broadcasted labels >>>>>> {payload}") logging.info(f"Broadcasted labels >>>>>> {payload}")
await asyncio.sleep(1) await asyncio.sleep(1)
@@ -127,6 +141,7 @@ async def main():
# start the websocket server # start the websocket server
async with websockets.serve(handler, "0.0.0.0", 8080): async with websockets.serve(handler, "0.0.0.0", 8080):
if info:
logging.info("Signalling server listening on :8080") logging.info("Signalling server listening on :8080")
await asyncio.Future() # run forever await asyncio.Future() # run forever