]> www.average.org Git - loctrkd.git/blobdiff - gps303/wsgateway.py
Rename gps303proto to zx303proto
[loctrkd.git] / gps303 / wsgateway.py
index c0a47768254bff754a2425ea1cd40ae597002a06..7dc2fcd7ff6b0c09596177364dbef0dcede3f8e7 100644 (file)
@@ -22,11 +22,12 @@ import zmq
 
 from . import common
 from .evstore import initdb, fetch
-from .gps303proto import (
+from .zx303proto import (
     GPS_POSITIONING,
     STATUS,
     WIFI_POSITIONING,
     parse_message,
+    proto_name,
 )
 from .zmsg import Bcast, topic
 
@@ -38,7 +39,10 @@ def backlog(imei: str, numback: int) -> List[Dict[str, Any]]:
     result = []
     for is_incoming, timestamp, packet in fetch(
         imei,
-        [(True, GPS_POSITIONING.PROTO), (False, WIFI_POSITIONING.PROTO)],
+        [
+            (True, proto_name(GPS_POSITIONING)),
+            (False, proto_name(WIFI_POSITIONING)),
+        ],
         numback,
     ):
         msg = parse_message(packet, is_incoming=is_incoming)
@@ -152,7 +156,11 @@ class Client:
                 e,
             )
             self.ws_data = try_http(data, self.sock.fileno(), e)
-            self.write()  # TODO this is a hack
+            # this `write` is a hack - writing _ought_ to be done at the
+            # stage when all other writes are performed. But I could not
+            # arrange it so in a logical way. Let it stay this way. The
+            # whole http server affair is a hack anyway.
+            self.write()
             log.debug("Sending HTTP response to %d", self.sock.fileno())
             msgs = None
         else:
@@ -255,7 +263,7 @@ def runserver(conf: ConfigParser) -> None:
     global htmlfile
 
     initdb(conf.get("storage", "dbfn"))
-    htmlfile = conf.get("wsgateway", "htmlfile")
+    htmlfile = conf.get("wsgateway", "htmlfile", fallback=None)
     # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?!
     zctx = zmq.Context()  # type: ignore
     zsub = zctx.socket(zmq.SUB)  # type: ignore
@@ -278,28 +286,28 @@ def runserver(conf: ConfigParser) -> None:
             for imei in neededsubs - activesubs:
                 zsub.setsockopt(
                     zmq.SUBSCRIBE,
-                    topic(GPS_POSITIONING.PROTO, True, imei),
+                    topic(proto_name(GPS_POSITIONING), True, imei),
                 )
                 zsub.setsockopt(
                     zmq.SUBSCRIBE,
-                    topic(WIFI_POSITIONING.PROTO, False, imei),
+                    topic(proto_name(WIFI_POSITIONING), False, imei),
                 )
                 zsub.setsockopt(
                     zmq.SUBSCRIBE,
-                    topic(STATUS.PROTO, True, imei),
+                    topic(proto_name(STATUS), True, imei),
                 )
             for imei in activesubs - neededsubs:
                 zsub.setsockopt(
                     zmq.UNSUBSCRIBE,
-                    topic(GPS_POSITIONING.PROTO, True, imei),
+                    topic(proto_name(GPS_POSITIONING), True, imei),
                 )
                 zsub.setsockopt(
                     zmq.UNSUBSCRIBE,
-                    topic(WIFI_POSITIONING.PROTO, False, imei),
+                    topic(proto_name(WIFI_POSITIONING), False, imei),
                 )
                 zsub.setsockopt(
                     zmq.UNSUBSCRIBE,
-                    topic(STATUS.PROTO, True, imei),
+                    topic(proto_name(STATUS), True, imei),
                 )
             activesubs = neededsubs
             log.debug("Subscribed to: %s", activesubs)
@@ -398,7 +406,9 @@ def runserver(conf: ConfigParser) -> None:
             towait &= trywrite
             towait |= morewait
     except KeyboardInterrupt:
-        pass
+        zsub.close()
+        zctx.destroy()  # type: ignore
+        tcpl.close()
 
 
 if __name__.endswith("__main__"):