X-Git-Url: http://www.average.org/gitweb/?a=blobdiff_plain;f=gps303%2Fwsgateway.py;h=f9f5c6a2ad936f33c9961419245c65de14414dcd;hb=c41f98b94321d35cf7f54a9c659fc36b13990f25;hp=a4a0b6d441d31d8a42800adc3e3d77eebd196547;hpb=117fd17ef103bb32940433955eba22f7fa457b99;p=loctrkd.git diff --git a/gps303/wsgateway.py b/gps303/wsgateway.py index a4a0b6d..f9f5c6a 100644 --- a/gps303/wsgateway.py +++ b/gps303/wsgateway.py @@ -1,5 +1,6 @@ """ Websocket Gateway """ +from json import loads from logging import getLogger from socket import socket, AF_INET6, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR from time import time @@ -19,11 +20,11 @@ from . import common from .zmsg import LocEvt log = getLogger("gps303/wsgateway") -htmldata = None +htmlfile = None def try_http(data, fd, e): - global htmldata + global htmlfile try: lines = data.decode().split("\r\n") request = lines[0] @@ -43,24 +44,33 @@ def try_http(data, fd, e): except ValueError: pass if op == "GET": - if htmldata is None: + if htmlfile is None: return ( f"{proto} 500 No data configured\r\n" f"Content-Type: text/plain\r\n\r\n" f"HTML data not configure on the server\r\n".encode() ) elif resource == "/": - length = len(htmldata.encode("utf-8")) - return ( - f"{proto} 200 Ok\r\n" - f"Content-Type: text/html; charset=utf-8\r\n" - f"Content-Length: {length:d}\r\n\r\n" + htmldata - ).encode("utf-8") + try: + with open(htmlfile, "rb") as fl: + htmldata = fl.read() + length = len(htmldata) + return ( + f"{proto} 200 Ok\r\n" + f"Content-Type: text/html; charset=utf-8\r\n" + f"Content-Length: {len(htmldata):d}\r\n\r\n" + ).encode("utf-8") + htmldata + except OSError: + return ( + f"{proto} 500 File not found\r\n" + f"Content-Type: text/plain\r\n\r\n" + f"HTML file could not be opened\r\n".encode() + ) else: return ( f"{proto} 404 File not found\r\n" f"Content-Type: text/plain\r\n\r\n" - f"We can only serve \"/\"\r\n".encode() + f'We can only serve "/"\r\n'.encode() ) else: return ( @@ -81,6 +91,8 @@ class Client: self.addr = addr self.ws = WSConnection(ConnectionType.SERVER) self.ws_data = b"" + self.ready = False + self.imeis = set() def close(self): log.debug("Closing fd %d", self.sock.fileno()) @@ -113,6 +125,7 @@ class Client: e, ) self.ws_data = try_http(data, self.sock.fileno(), e) + self.write() # TODO this is a hack log.debug("Sending HTTP response to %d", self.sock.fileno()) msgs = None else: @@ -122,37 +135,47 @@ class Client: log.debug("WebSocket upgrade on fd %d", self.sock.fileno()) # self.ws_data += self.ws.send(event.response()) # Why not?! self.ws_data += self.ws.send(AcceptConnection()) + self.ready = True elif isinstance(event, (CloseConnection, Ping)): log.debug("%s on fd %d", event, self.sock.fileno()) self.ws_data += self.ws.send(event.response()) elif isinstance(event, TextMessage): # TODO: save imei "subscription" log.debug("%s on fd %d", event, self.sock.fileno()) - msgs.append(event.data) + msg = loads(event.data) + msgs.append(msg) + if msg.get("type", None) == "subscribe": + self.imeis = set(msg.get("imei", [])) + log.debug( + "subs list on fd %s is %s", + self.sock.fileno(), + self.imeis, + ) else: log.warning("%s on fd %d", event, self.sock.fileno()) - if self.ws_data: # Temp hack - self.write() return msgs def wants(self, imei): + log.debug("wants %s? set is %s on fd %d", imei, self.imeis, self.sock.fileno()) return True # TODO: check subscriptions def send(self, message): - # TODO: filter only wanted imei got from the client - self.ws_data += self.ws.send(Message(data=message.json)) + if self.ready and message.imei in self.imeis: + self.ws_data += self.ws.send(Message(data=message.json)) def write(self): - try: - sent = self.sock.send(self.ws_data) - self.ws_data = self.ws_data[sent:] - except OSError as e: - log.error( - "Sending to fd %d: %s", - self.sock.fileno(), - e, - ) - self.ws_data = b"" + if self.ws_data: + try: + sent = self.sock.send(self.ws_data) + self.ws_data = self.ws_data[sent:] + except OSError as e: + log.error( + "Sending to fd %d: %s", + self.sock.fileno(), + e, + ) + self.ws_data = b"" + return bool(self.ws_data) class Clients: @@ -179,28 +202,38 @@ class Clients: result = [] for msg in msgs: log.debug("Received: %s", msg) + result.append(msg) return result def send(self, msg): - for clnt in self.by_fd.values(): + towrite = set() + for fd, clnt in self.by_fd.items(): if clnt.wants(msg.imei): clnt.send(msg) - clnt.write() + towrite.add(fd) + return towrite + + def write(self, towrite): + waiting = set() + for fd, clnt in [(fd, self.by_fd.get(fd)) for fd in towrite]: + if clnt.write(): + waiting.add(fd) + return waiting + + def subs(self): + result = set() + for clnt in self.by_fd.values(): + result |= clnt.imeis + return result def runserver(conf): - global htmldata - try: - with open( - conf.get("wsgateway", "htmlfile"), encoding="utf-8" - ) as fl: - htmldata = fl.read() - except OSError: - pass + global htmlfile + + htmlfile = conf.get("wsgateway", "htmlfile") zctx = zmq.Context() zsub = zctx.socket(zmq.SUB) zsub.connect(conf.get("lookaside", "publishurl")) - zsub.setsockopt(zmq.SUBSCRIBE, b"") tcpl = socket(AF_INET6, SOCK_STREAM) tcpl.setblocking(False) tcpl.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) @@ -211,12 +244,22 @@ def runserver(conf): poller.register(zsub, flags=zmq.POLLIN) poller.register(tcpfd, flags=zmq.POLLIN) clients = Clients() + activesubs = set() try: + towait = set() while True: + neededsubs = clients.subs() + for imei in neededsubs - activesubs: + zsub.setsockopt(zmq.SUBSCRIBE, imei.encode()) + for imei in activesubs - neededsubs: + zsub.setsockopt(zmq.UNSUBSCRIBE, imei.encode()) + activesubs = neededsubs + log.debug("Subscribed to: %s", activesubs) tosend = [] topoll = [] tostop = [] - events = poller.poll(5000) + towrite = set() + events = poller.poll() for sk, fl in events: if sk is zsub: while True: @@ -233,9 +276,15 @@ def runserver(conf): if received is None: log.debug("Client gone from fd %d", sk) tostop.append(sk) + towait.discard(fd) else: for msg in received: log.debug("Received from %d: %s", sk, msg) + towrite.add(sk) + elif fl & zmq.POLLOUT: + log.debug("Write now open for fd %d", sk) + towrite.add(sk) + towait.discard(sk) else: log.debug("Stray event: %s on socket %s", fl, sk) # poll queue consumed, make changes now @@ -243,12 +292,26 @@ def runserver(conf): poller.unregister(fd) clients.stop(fd) for zmsg in tosend: - log.debug("Sending to the client: %s", zmsg) - clients.send(zmsg) + log.debug("Sending to the clients: %s", zmsg) + towrite |= clients.send(zmsg) for clntsock, clntaddr in topoll: fd = clients.add(clntsock, clntaddr) poller.register(fd, flags=zmq.POLLIN) - # TODO: Handle write overruns (register for POLLOUT) + # Deal with actually writing the data out + trywrite = towrite - towait + morewait = clients.write(trywrite) + log.debug( + "towait %s, tried %s, still busy %s", + towait, + trywrite, + morewait, + ) + for fd in morewait - trywrite: # new fds waiting for write + poller.modify(fd, flags=zmq.POLLIN | zmq.POLLOUT) + for fd in trywrite - morewait: # no longer waiting for write + poller.modify(fd, flags=zmq.POLLIN) + towait &= trywrite + towait |= morewait except KeyboardInterrupt: pass