From: Eugene Crosser Date: Thu, 30 Jun 2022 21:47:06 +0000 (+0200) Subject: collector: get rid of more protocol specifics X-Git-Tag: 1.90~42 X-Git-Url: http://www.average.org/gitweb/?p=loctrkd.git;a=commitdiff_plain;h=037939789ccc7ed56059cf9bc7522d3cd7e840a8 collector: get rid of more protocol specifics --- diff --git a/gps303/collector.py b/gps303/collector.py index 3cb7b0b..df1a474 100644 --- a/gps303/collector.py +++ b/gps303/collector.py @@ -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, ) diff --git a/gps303/gps303proto.py b/gps303/gps303proto.py index baa9a6c..e597b2b 100755 --- a/gps303/gps303proto.py +++ b/gps303/gps303proto.py @@ -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: