]> www.average.org Git - loctrkd.git/commitdiff
collector: prevent two active clients share IMEI
authorEugene Crosser <crosser@average.org>
Thu, 14 Jul 2022 20:15:58 +0000 (22:15 +0200)
committerEugene Crosser <crosser@average.org>
Thu, 14 Jul 2022 20:15:58 +0000 (22:15 +0200)
gps303/collector.py

index 9d694b0ae4c83180a7a718aa21be5daa4e7fe6f4..ce41cb57b1c91cdf7b3d1f656a1fe98057a672b7 100644 (file)
@@ -108,15 +108,6 @@ class Client:
             if len(packet) < 2:  # frameend comes too early
                 log.warning("Packet too short: %s", packet)
                 break
-            if proto_of_message(packet) == LOGIN.PROTO:
-                msg = parse_message(packet)
-                if isinstance(msg, LOGIN):  # Can be unparseable
-                    self.imei = msg.imei
-                    log.info(
-                        "LOGIN from fd %d (IMEI %s)",
-                        self.sock.fileno(),
-                        self.imei,
-                    )
             msgs.append((when, self.addr, packet))
         return msgs
 
@@ -160,8 +151,23 @@ class Clients:
             return None
         result = []
         for when, peeraddr, packet in msgs:
-            if proto_of_message(packet) == LOGIN.PROTO:  # Could do blindly...
-                if clnt.imei:
+            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
+                        log.info(
+                            "LOGIN from fd %d (IMEI %s)",
+                            clnt.sock.fileno(),
+                            clnt.imei,
+                        )
+                        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
                     self.by_imei[clnt.imei] = clnt
                 else:
                     log.warning(