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