1 """ Websocket Gateway """
4 from logging import getLogger
5 from socket import socket, AF_INET6, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
7 from wsproto import ConnectionType, WSConnection
8 from wsproto.events import (
16 from wsproto.utilities import RemoteProtocolError
20 from .backlog import blinit, backlog
21 from .zmsg import LocEvt
23 log = getLogger("gps303/wsgateway")
27 def try_http(data, fd, e):
30 lines = data.decode().split("\r\n")
33 op, resource, proto = request.split(" ")
35 "HTTP %s for %s, proto %s from fd %d, headers: %s",
43 pos = resource.index("?")
44 resource = resource[:pos]
50 f"{proto} 500 No data configured\r\n"
51 f"Content-Type: text/plain\r\n\r\n"
52 f"HTML data not configure on the server\r\n".encode()
56 with open(htmlfile, "rb") as fl:
58 length = len(htmldata)
61 f"Content-Type: text/html; charset=utf-8\r\n"
62 f"Content-Length: {len(htmldata):d}\r\n\r\n"
63 ).encode("utf-8") + htmldata
66 f"{proto} 500 File not found\r\n"
67 f"Content-Type: text/plain\r\n\r\n"
68 f"HTML file could not be opened\r\n".encode()
72 f"{proto} 404 File not found\r\n"
73 f"Content-Type: text/plain\r\n\r\n"
74 f'We can only serve "/"\r\n'.encode()
78 f"{proto} 400 Bad request\r\n"
79 "Content-Type: text/plain\r\n\r\n"
80 "Bad request\r\n".encode()
83 log.warning("Unparseable data from fd %d: %s", fd, data)
88 """Websocket connection to the client"""
90 def __init__(self, sock, addr):
93 self.ws = WSConnection(ConnectionType.SERVER)
99 log.debug("Closing fd %d", self.sock.fileno())
104 data = self.sock.recv(4096)
107 "Reading from fd %d: %s",
111 self.ws.receive_data(None)
113 if not data: # Client has closed connection
115 "EOF reading from fd %d",
118 self.ws.receive_data(None)
121 self.ws.receive_data(data)
122 except RemoteProtocolError as e:
124 "Websocket error on fd %d, try plain http (%s)",
128 self.ws_data = try_http(data, self.sock.fileno(), e)
129 self.write() # TODO this is a hack
130 log.debug("Sending HTTP response to %d", self.sock.fileno())
134 for event in self.ws.events():
135 if isinstance(event, Request):
136 log.debug("WebSocket upgrade on fd %d", self.sock.fileno())
137 # self.ws_data += self.ws.send(event.response()) # Why not?!
138 self.ws_data += self.ws.send(AcceptConnection())
140 elif isinstance(event, (CloseConnection, Ping)):
141 log.debug("%s on fd %d", event, self.sock.fileno())
142 self.ws_data += self.ws.send(event.response())
143 elif isinstance(event, TextMessage):
144 # TODO: save imei "subscription"
145 log.debug("%s on fd %d", event, self.sock.fileno())
146 msg = loads(event.data)
148 if msg.get("type", None) == "subscribe":
149 self.imeis = set(msg.get("imei", []))
151 "subs list on fd %s is %s",
156 log.warning("%s on fd %d", event, self.sock.fileno())
159 def wants(self, imei):
160 log.debug("wants %s? set is %s on fd %d", imei, self.imeis, self.sock.fileno())
161 return True # TODO: check subscriptions
163 def send(self, message):
164 if self.ready and message.imei in self.imeis:
165 self.ws_data += self.ws.send(Message(data=message.json))
170 sent = self.sock.send(self.ws_data)
171 self.ws_data = self.ws_data[sent:]
174 "Sending to fd %d: %s",
179 return bool(self.ws_data)
186 def add(self, clntsock, clntaddr):
187 fd = clntsock.fileno()
188 log.info("Start serving fd %d from %s", fd, clntaddr)
189 self.by_fd[fd] = Client(clntsock, clntaddr)
193 clnt = self.by_fd[fd]
194 log.info("Stop serving fd %d", clnt.sock.fileno())
199 clnt = self.by_fd[fd]
204 for fd, clnt in self.by_fd.items():
205 if clnt.wants(msg.imei):
210 def write(self, towrite):
212 for fd, clnt in [(fd, self.by_fd.get(fd)) for fd in towrite]:
219 for clnt in self.by_fd.values():
227 blinit(conf.get("storage", "dbfn"), conf.get("opencellid", "dbfn"))
228 htmlfile = conf.get("wsgateway", "htmlfile")
230 zsub = zctx.socket(zmq.SUB)
231 zsub.connect(conf.get("lookaside", "publishurl"))
232 tcpl = socket(AF_INET6, SOCK_STREAM)
233 tcpl.setblocking(False)
234 tcpl.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
235 tcpl.bind(("", conf.getint("wsgateway", "port")))
237 tcpfd = tcpl.fileno()
238 poller = zmq.Poller()
239 poller.register(zsub, flags=zmq.POLLIN)
240 poller.register(tcpfd, flags=zmq.POLLIN)
246 neededsubs = clients.subs()
247 for imei in neededsubs - activesubs:
248 zsub.setsockopt(zmq.SUBSCRIBE, imei.encode())
249 for imei in activesubs - neededsubs:
250 zsub.setsockopt(zmq.UNSUBSCRIBE, imei.encode())
251 activesubs = neededsubs
252 log.debug("Subscribed to: %s", activesubs)
257 events = poller.poll()
258 for sk, fl in events:
262 zmsg = LocEvt(zsub.recv(zmq.NOBLOCK))
267 clntsock, clntaddr = tcpl.accept()
268 topoll.append((clntsock, clntaddr))
269 elif fl & zmq.POLLIN:
270 received = clients.recv(sk)
272 log.debug("Client gone from fd %d", sk)
277 log.debug("Received from %d: %s", sk, msg)
278 if msg.get("type", None) == "subscribe":
279 imei = msg.get("imei")
280 numback = msg.get("backlog", 5)
282 tosend.extend(backlog(elem, numback))
284 elif fl & zmq.POLLOUT:
285 log.debug("Write now open for fd %d", sk)
289 log.debug("Stray event: %s on socket %s", fl, sk)
290 # poll queue consumed, make changes now
292 poller.unregister(fd)
295 log.debug("Sending to the clients: %s", zmsg)
296 towrite |= clients.send(zmsg)
297 for clntsock, clntaddr in topoll:
298 fd = clients.add(clntsock, clntaddr)
299 poller.register(fd, flags=zmq.POLLIN)
300 # Deal with actually writing the data out
301 trywrite = towrite - towait
302 morewait = clients.write(trywrite)
304 "towait %s, tried %s, still busy %s",
309 for fd in morewait - trywrite: # new fds waiting for write
310 poller.modify(fd, flags=zmq.POLLIN | zmq.POLLOUT)
311 for fd in trywrite - morewait: # no longer waiting for write
312 poller.modify(fd, flags=zmq.POLLIN)
315 except KeyboardInterrupt:
319 if __name__.endswith("__main__"):
320 runserver(common.init(log))