refactor: adapt for client.id

This commit is contained in:
2026-02-28 20:57:59 +01:00
parent 1e38be5ec5
commit a5e1ba88ae
3 changed files with 13 additions and 14 deletions

View File

@@ -92,7 +92,7 @@ class BackendServer:
conn, addr = sock.accept() conn, addr = sock.accept()
self.logger.info(f"[+] Accepted connection from {addr}") self.logger.info(f"[+] Accepted connection from {addr}")
client = Client(mac_id=None, addr=addr, socket=conn) client = Client(id=None, addr=addr, socket=conn)
events = selectors.EVENT_READ | selectors.EVENT_WRITE events = selectors.EVENT_READ | selectors.EVENT_WRITE
self.selector.register(conn, events, data=client) self.selector.register(conn, events, data=client)
@@ -168,7 +168,7 @@ class BackendServer:
self._disconnect(client) self._disconnect(client)
return return
if client.mac_id is None: if client.id is None:
# expect HELLO message # expect HELLO message
try: try:
msg = Message.from_bytes(client.inbound) msg = Message.from_bytes(client.inbound)
@@ -182,22 +182,21 @@ class BackendServer:
if ( if (
msg.category == Category.CONTROL msg.category == Category.CONTROL
and msg.action == ControlAction.HELLO and msg.action == ControlAction.HELLO
and msg.payload.get("mac") is not None and msg.payload.get("id") is not None
): ):
client.mac_id = msg.payload["mac"] client.id = msg.payload["id"]
if ( if (
client.mac_id in self.clients client.id in self.clients
and self.clients[client.mac_id].status and self.clients[client.id].status == "connected"
== "connected"
): ):
old_client: Client = self.clients[client.mac_id] old_client: Client = self.clients[client.id]
self.logger.warning( self.logger.warning(
f"Client {client.mac_id} is already connected from {old_client.addr}, disconnecting old client..." f"Client {client.id} is already connected from {old_client.addr}, disconnecting old client..."
) )
self.send_close(old_client) self.send_close(old_client)
# self._disconnect(old_client) # self._disconnect(old_client)
# TODO: tell client not to reconnect # TODO: tell client not to reconnect
self.clients[client.mac_id] = client self.clients[client.id] = client
self.logger.info(f"[+] Registered new client {client}") self.logger.info(f"[+] Registered new client {client}")
else: else:
self.logger.error( self.logger.error(

View File

@@ -17,12 +17,12 @@ class Client:
"""Represents a client.""" """Represents a client."""
def __init__( def __init__(
self, mac_id: str | None, addr: tuple[str, int], socket: socket.socket self, id: str | None, addr: tuple[str, int], socket: socket.socket
) -> None: ) -> None:
"""Initialize the client. """Initialize the client.
Args: Args:
mac_id (str | None): The unique identifier for the client. id (str | None): The unique identifier for the client.
Can be None if not yet assigned. Can be None if not yet assigned.
addr (tuple[str, int]): The (IP, port) address of the client. addr (tuple[str, int]): The (IP, port) address of the client.
socket (socket.socket): The socket object for communication. socket (socket.socket): The socket object for communication.
@@ -32,7 +32,7 @@ class Client:
) )
self.logger.debug(f"Initializing Client {addr}...") self.logger.debug(f"Initializing Client {addr}...")
self.id: str | None = mac_id self.id: str | None = id
self.last_seen: float = 0.0 # unix timestanp of last inbound message self.last_seen: float = 0.0 # unix timestanp of last inbound message
self.status: ClientStatus = ClientStatus.CONNECTED self.status: ClientStatus = ClientStatus.CONNECTED

View File

@@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Details for client {{ client.id }}</title> <title>Details for client {{ client.mac_id }}</title>
</head> </head>
<body></body> <body></body>
</html> </html>