]> www.average.org Git - loctrkd.git/commitdiff
collector: get rid of more protocol specifics
authorEugene Crosser <crosser@average.org>
Thu, 30 Jun 2022 21:47:06 +0000 (23:47 +0200)
committerEugene Crosser <crosser@average.org>
Thu, 14 Jul 2022 20:36:10 +0000 (22:36 +0200)
gps303/collector.py
gps303/gps303proto.py

index 3cb7b0bda98b09a961af7f5cc834ea95ae46a76a..df1a474f057e0fb68c9af477fc43c714b083de01 100644 (file)
@@ -19,8 +19,8 @@ import zmq
 from . import common
 from .gps303proto import (
     GPS303Conn,
-    HIBERNATION,
-    LOGIN,
+    is_goodbye_packet,
+    imei_from_packet,
     inline_response,
     parse_message,
     proto_of_message,
@@ -92,9 +92,6 @@ class Client:
                 e,
             )
 
-    def set_imei(self, imei: str) -> None:
-        self.imei = imei
-
 
 class Clients:
     def __init__(self) -> None:
@@ -124,23 +121,18 @@ class Clients:
             return None
         result = []
         for when, peeraddr, packet in msgs:
-            if proto_of_message(packet) == LOGIN.PROTO:
-                msg = parse_message(packet)
-                if isinstance(msg, LOGIN):  # Can be unparseable
-                    if clnt.imei is None:
-                        clnt.imei = msg.imei
+            if clnt.imei is None:
+                imei = imei_from_packet(packet)
+                if imei is not None:
+                    log.info("LOGIN from fd %d (IMEI %s)", fd, imei)
+                    clnt.imei = imei
+                    oldclnt = self.by_imei.get(clnt.imei)
+                    if oldclnt is not None:
                         log.info(
-                            "LOGIN from fd %d (IMEI %s)",
-                            clnt.sock.fileno(),
-                            clnt.imei,
+                            "Orphaning fd %d with the same IMEI",
+                            oldclnt.sock.fileno(),
                         )
-                        oldclnt = self.by_imei.get(clnt.imei)
-                        if oldclnt is not None:
-                            log.info(
-                                "Orphaning fd %d with the same IMEI",
-                                oldclnt.sock.fileno(),
-                            )
-                            oldclnt.imei = None
+                        oldclnt.imei = None
                     self.by_imei[clnt.imei] = clnt
                 else:
                     log.warning(
@@ -218,9 +210,9 @@ def runserver(conf: ConfigParser, handle_hibernate: bool = True) -> None:
                                     packet=packet,
                                 ).packed
                             )
-                            if proto == HIBERNATION.PROTO and handle_hibernate:
+                            if is_goodbye_packet(packet) and handle_hibernate:
                                 log.debug(
-                                    "HIBERNATION from fd %d (IMEI %s)",
+                                    "Goodbye from fd %d (IMEI %s)",
                                     sk,
                                     imei,
                                 )
index baa9a6cf64ac7af7b6fcb5605de3783f02b35955..e597b2bbc564da0e24e37f39aa69b5e725b68e2b 100755 (executable)
@@ -879,6 +879,18 @@ def proto_of_message(packet: bytes) -> int:
     return packet[1]
 
 
+def imei_from_packet(packet: bytes) -> Optional[str]:
+    if proto_of_message(packet) == LOGIN.PROTO:
+        msg = parse_message(packet)
+        if isinstance(msg, LOGIN):
+            return msg.imei
+    return None
+
+
+def is_goodbye_packet(packet: bytes) -> bool:
+    return proto_of_message(packet) == HIBERNATION.PROTO
+
+
 def inline_response(packet: bytes) -> Optional[bytes]:
     proto = proto_of_message(packet)
     if proto in CLASSES: