X-Git-Url: http://www.average.org/gitweb/?a=blobdiff_plain;f=gps303%2Fcollector.py;h=df1a474f057e0fb68c9af477fc43c714b083de01;hb=037939789ccc7ed56059cf9bc7522d3cd7e840a8;hp=5215d7b42289762b8b8bc79b2da0de0ec2203e09;hpb=2ad23fd8e9afe8d6c46263124ecd88ea28fd3059;p=loctrkd.git diff --git a/gps303/collector.py b/gps303/collector.py index 5215d7b..df1a474 100644 --- a/gps303/collector.py +++ b/gps303/collector.py @@ -19,9 +19,8 @@ import zmq from . import common from .gps303proto import ( GPS303Conn, - StreamError, - HIBERNATION, - LOGIN, + is_goodbye_packet, + imei_from_packet, inline_response, parse_message, proto_of_message, @@ -69,16 +68,18 @@ class Client: ) return None when = time() - while True: - try: - return [ - (when, self.addr, packet) - for packet in self.stream.recv(segment) - ] - except StreamError as e: + msgs = [] + for elem in self.stream.recv(segment): + if isinstance(elem, bytes): + msgs.append((when, self.addr, elem)) + else: log.warning( - "%s from fd %d (IMEI %s)", e, self.sock.fileno(), self.imei + "%s from fd %d (IMEI %s)", + elem, + self.sock.fileno(), + self.imei, ) + return msgs def send(self, buffer: bytes) -> None: try: @@ -91,9 +92,6 @@ class Client: e, ) - def set_imei(self, imei: str) -> None: - self.imei = imei - class Clients: def __init__(self) -> None: @@ -123,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( @@ -217,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, )