1 """ Websocket Gateway """
3 from datetime import datetime, timezone
4 from json import dumps, loads
5 from logging import getLogger
6 from socket import socket, AF_INET6, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
8 from wsproto import ConnectionType, WSConnection
9 from wsproto.events import (
17 from wsproto.utilities import RemoteProtocolError
21 from .evstore import initdb, fetch
22 from .gps303proto import (
27 from .zmsg import Bcast, topic
29 log = getLogger("gps303/wsgateway")
33 def backlog(imei, numback):
35 for is_incoming, timestamp, packet in fetch(
37 ((True, GPS_POSITIONING.PROTO), (False, WIFI_POSITIONING.PROTO)),
40 msg = parse_message(packet, is_incoming=is_incoming)
45 datetime.fromtimestamp(timestamp).astimezone(
49 "longitude": msg.longitude,
50 "latitude": msg.latitude,
56 def try_http(data, fd, e):
59 lines = data.decode().split("\r\n")
62 op, resource, proto = request.split(" ")
64 "HTTP %s for %s, proto %s from fd %d, headers: %s",
72 pos = resource.index("?")
73 resource = resource[:pos]
79 f"{proto} 500 No data configured\r\n"
80 f"Content-Type: text/plain\r\n\r\n"
81 f"HTML data not configure on the server\r\n".encode()
85 with open(htmlfile, "rb") as fl:
87 length = len(htmldata)
90 f"Content-Type: text/html; charset=utf-8\r\n"
91 f"Content-Length: {len(htmldata):d}\r\n\r\n"
92 ).encode("utf-8") + htmldata
95 f"{proto} 500 File not found\r\n"
96 f"Content-Type: text/plain\r\n\r\n"
97 f"HTML file could not be opened\r\n".encode()
101 f"{proto} 404 File not found\r\n"
102 f"Content-Type: text/plain\r\n\r\n"
103 f'We can only serve "/"\r\n'.encode()
107 f"{proto} 400 Bad request\r\n"
108 "Content-Type: text/plain\r\n\r\n"
109 "Bad request\r\n".encode()
112 log.warning("Unparseable data from fd %d: %s", fd, data)
117 """Websocket connection to the client"""
119 def __init__(self, sock, addr):
122 self.ws = WSConnection(ConnectionType.SERVER)
128 log.debug("Closing fd %d", self.sock.fileno())
133 data = self.sock.recv(4096)
136 "Reading from fd %d: %s",
140 self.ws.receive_data(None)
142 if not data: # Client has closed connection
144 "EOF reading from fd %d",
147 self.ws.receive_data(None)
150 self.ws.receive_data(data)
151 except RemoteProtocolError as e:
153 "Websocket error on fd %d, try plain http (%s)",
157 self.ws_data = try_http(data, self.sock.fileno(), e)
158 self.write() # TODO this is a hack
159 log.debug("Sending HTTP response to %d", self.sock.fileno())
163 for event in self.ws.events():
164 if isinstance(event, Request):
165 log.debug("WebSocket upgrade on fd %d", self.sock.fileno())
166 # self.ws_data += self.ws.send(event.response()) # Why not?!
167 self.ws_data += self.ws.send(AcceptConnection())
169 elif isinstance(event, (CloseConnection, Ping)):
170 log.debug("%s on fd %d", event, self.sock.fileno())
171 self.ws_data += self.ws.send(event.response())
172 elif isinstance(event, TextMessage):
173 # TODO: save imei "subscription"
174 log.debug("%s on fd %d", event, self.sock.fileno())
175 msg = loads(event.data)
177 if msg.get("type", None) == "subscribe":
178 self.imeis = set(msg.get("imei", []))
180 "subs list on fd %s is %s",
185 log.warning("%s on fd %d", event, self.sock.fileno())
188 def wants(self, imei):
190 "wants %s? set is %s on fd %d",
195 return True # TODO: check subscriptions
197 def send(self, message):
198 if self.ready and message["imei"] in self.imeis:
199 self.ws_data += self.ws.send(Message(data=dumps(message)))
204 sent = self.sock.send(self.ws_data)
205 self.ws_data = self.ws_data[sent:]
208 "Sending to fd %d: %s",
213 return bool(self.ws_data)
220 def add(self, clntsock, clntaddr):
221 fd = clntsock.fileno()
222 log.info("Start serving fd %d from %s", fd, clntaddr)
223 self.by_fd[fd] = Client(clntsock, clntaddr)
227 clnt = self.by_fd[fd]
228 log.info("Stop serving fd %d", clnt.sock.fileno())
233 clnt = self.by_fd[fd]
238 for fd, clnt in self.by_fd.items():
239 if clnt.wants(msg["imei"]):
244 def write(self, towrite):
246 for fd, clnt in [(fd, self.by_fd.get(fd)) for fd in towrite]:
253 for clnt in self.by_fd.values():
261 initdb(conf.get("storage", "dbfn"))
262 htmlfile = conf.get("wsgateway", "htmlfile")
264 zsub = zctx.socket(zmq.SUB)
265 zsub.connect(conf.get("collector", "publishurl"))
266 tcpl = socket(AF_INET6, SOCK_STREAM)
267 tcpl.setblocking(False)
268 tcpl.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
269 tcpl.bind(("", conf.getint("wsgateway", "port")))
271 tcpfd = tcpl.fileno()
272 poller = zmq.Poller()
273 poller.register(zsub, flags=zmq.POLLIN)
274 poller.register(tcpfd, flags=zmq.POLLIN)
280 neededsubs = clients.subs()
281 for imei in neededsubs - activesubs:
284 topic(GPS_POSITIONING.PROTO, True, imei),
288 topic(WIFI_POSITIONING.PROTO, False, imei),
290 for imei in activesubs - neededsubs:
293 topic(GPS_POSITIONING.PROTO, True, imei),
297 topic(WIFI_POSITIONING.PROTO, False, imei),
299 activesubs = neededsubs
300 log.debug("Subscribed to: %s", activesubs)
305 events = poller.poll()
306 for sk, fl in events:
310 zmsg = Bcast(zsub.recv(zmq.NOBLOCK))
311 msg = parse_message(zmsg.packet, zmsg.is_incoming)
312 log.debug("Got %s with %s", zmsg, msg)
317 datetime.fromtimestamp(
319 ).astimezone(tz=timezone.utc)
321 "longitude": msg.longitude,
322 "latitude": msg.latitude,
328 clntsock, clntaddr = tcpl.accept()
329 topoll.append((clntsock, clntaddr))
330 elif fl & zmq.POLLIN:
331 received = clients.recv(sk)
333 log.debug("Client gone from fd %d", sk)
338 log.debug("Received from %d: %s", sk, msg)
339 if msg.get("type", None) == "subscribe":
340 imeis = msg.get("imei")
341 numback = msg.get("backlog", 5)
343 tosend.extend(backlog(imei, numback))
345 elif fl & zmq.POLLOUT:
346 log.debug("Write now open for fd %d", sk)
350 log.debug("Stray event: %s on socket %s", fl, sk)
351 # poll queue consumed, make changes now
353 poller.unregister(fd)
356 log.debug("Sending to the clients: %s", zmsg)
357 towrite |= clients.send(zmsg)
358 for clntsock, clntaddr in topoll:
359 fd = clients.add(clntsock, clntaddr)
360 poller.register(fd, flags=zmq.POLLIN)
361 # Deal with actually writing the data out
362 trywrite = towrite - towait
363 morewait = clients.write(trywrite)
365 "towait %s, tried %s, still busy %s",
370 for fd in morewait - trywrite: # new fds waiting for write
371 poller.modify(fd, flags=zmq.POLLIN | zmq.POLLOUT)
372 for fd in trywrite - morewait: # no longer waiting for write
373 poller.modify(fd, flags=zmq.POLLIN)
376 except KeyboardInterrupt:
380 if __name__.endswith("__main__"):
381 runserver(common.init(log))