- def recv(self, fd):
- clnt = by_fd[fd]
- msg = clnt.recv()
- if isinstance(msg, LOGIN):
- self.by_imei[clnt.imei] = clnt
- return clnt.imei, msg
-
- def response(self, zmsg):
- if zmsg.imei in self.by_imei:
- clnt = self.by_imei[zmsg.imei].send(zmsg.payload)
-
-
-def runserver(opts, conf):
- zctx = zmq.Context()
- zpub = zctx.socket(zmq.PUB)
+ def recv(
+ self, fd: int
+ ) -> Optional[List[Tuple[Optional[str], float, Tuple[str, int], bytes]]]:
+ clnt = self.by_fd[fd]
+ msgs = clnt.recv()
+ if msgs is None:
+ return None
+ result = []
+ for when, peeraddr, packet in msgs:
+ 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(
+ "Orphaning fd %d with the same IMEI",
+ oldclnt.sock.fileno(),
+ )
+ oldclnt.imei = None
+ self.by_imei[clnt.imei] = clnt
+ else:
+ log.warning(
+ "Login message from %s: %s, but client imei unfilled",
+ peeraddr,
+ packet,
+ )
+ result.append((clnt.imei, when, peeraddr, packet))
+ log.debug(
+ "Received from %s (IMEI %s): %s",
+ peeraddr,
+ clnt.imei,
+ packet.hex(),
+ )
+ return result
+
+ def response(self, resp: Resp) -> None:
+ if resp.imei in self.by_imei:
+ self.by_imei[resp.imei].send(resp.packet)
+ else:
+ log.info("Not connected (IMEI %s)", resp.imei)
+
+
+def runserver(conf: ConfigParser, handle_hibernate: bool = True) -> None:
+ # Is this https://github.com/zeromq/pyzmq/issues/1627 still not fixed?!
+ zctx = zmq.Context() # type: ignore
+ zpub = zctx.socket(zmq.PUB) # type: ignore
+ zpull = zctx.socket(zmq.PULL) # type: ignore
+ oldmask = umask(0o117)