]> www.average.org Git - loctrkd.git/blobdiff - loctrkd/collector.py
Update changelog for 2.00 release
[loctrkd.git] / loctrkd / collector.py
index 6fc606a14ecabcbe41c6aba94c69ca7ce5bc55f4..216134e57850d96d762286f23db43cafd9ea78c7 100644 (file)
@@ -14,10 +14,11 @@ from socket import (
 )
 from struct import pack
 from time import time
-from typing import Any, cast, Dict, List, Optional, Set, Tuple, Union
+from typing import Any, Dict, List, Optional, Set, Tuple, Union
 import zmq
 
 from . import common
+from .protomodule import ProtoModule
 from .zmsg import Bcast, Resp
 
 log = getLogger("loctrkd/collector")
@@ -25,46 +26,6 @@ log = getLogger("loctrkd/collector")
 MAXBUFFER: int = 4096
 
 
-class ProtoModule:
-    class Stream:
-        def recv(self, segment: bytes) -> List[Union[bytes, str]]:
-            ...
-
-        def close(self) -> bytes:
-            ...
-
-    @staticmethod
-    def enframe(buffer: bytes, imei: Optional[str] = None) -> bytes:
-        ...
-
-    @staticmethod
-    def probe_buffer(buffer: bytes) -> bool:
-        ...
-
-    @staticmethod
-    def parse_message(packet: bytes, is_incoming: bool = True) -> Any:
-        ...
-
-    @staticmethod
-    def inline_response(packet: bytes) -> Optional[bytes]:
-        ...
-
-    @staticmethod
-    def is_goodbye_packet(packet: bytes) -> bool:
-        ...
-
-    @staticmethod
-    def imei_from_packet(packet: bytes) -> Optional[str]:
-        ...
-
-    @staticmethod
-    def proto_of_message(packet: bytes) -> str:
-        ...
-
-
-pmods: List[ProtoModule] = []
-
-
 class Client:
     """Connected socket to the terminal plus buffer and metadata"""
 
@@ -83,7 +44,7 @@ class Client:
         else:
             rest = b""
         if rest:
-            log.warning(
+            log.info(
                 "%d bytes in buffer on close: %s", len(rest), rest[:64].hex()
             )
 
@@ -107,11 +68,9 @@ class Client:
             )
             return None
         if self.stream is None:
-            for pmod in pmods:
-                if pmod.probe_buffer(segment):
-                    self.pmod = pmod
-                    self.stream = pmod.Stream()
-                    break
+            self.pmod = common.probe_pmod(segment)
+            if self.pmod is not None:
+                self.stream = self.pmod.Stream()
         if self.stream is None:
             log.info(
                 "unrecognizable %d bytes of data %s from fd %d",
@@ -126,7 +85,7 @@ class Client:
             if isinstance(elem, bytes):
                 msgs.append((when, self.addr, elem))
             else:
-                log.warning(
+                log.info(
                     "%s from fd %d (IMEI %s)",
                     elem,
                     self.sock.fileno(),
@@ -217,11 +176,6 @@ class Clients:
 
 
 def runserver(conf: ConfigParser, handle_hibernate: bool = True) -> None:
-    global pmods
-    pmods = [
-        cast(ProtoModule, import_module("." + modnm, __package__))
-        for modnm in conf.get("collector", "protocols").split(",")
-    ]
     # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?!
     zctx = zmq.Context()  # type: ignore
     zpub = zctx.socket(zmq.PUB)  # type: ignore
@@ -269,6 +223,7 @@ def runserver(conf: ConfigParser, handle_hibernate: bool = True) -> None:
                             zpub.send(
                                 Bcast(
                                     proto=proto,
+                                    pmod=pmod.PMODNAME,
                                     imei=imei,
                                     when=when,
                                     peeraddr=peeraddr,
@@ -301,8 +256,9 @@ def runserver(conf: ConfigParser, handle_hibernate: bool = True) -> None:
                         Bcast(
                             is_incoming=False,
                             proto=rpmod.proto_of_message(zmsg.packet),
-                            when=zmsg.when,
+                            pmod=pmod.PMODNAME,
                             imei=zmsg.imei,
+                            when=zmsg.when,
                             packet=zmsg.packet,
                         ).packed
                     )