def register(self, aliases, port): self.logger.info("registering on %s:%s", self.ip, self.port) sock = socket.socket(self.sock_family, socket.SOCK_DGRAM) try: if self.bcast: sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True) data = brine.dump(("RPYC", "REGISTER", (aliases, port))) sock.sendto(data, (self.ip, self.port)) tmax = time.time() + self.timeout while time.time() < tmax: sock.settimeout(tmax - time.time()) try: data, address = sock.recvfrom(MAX_DGRAM_SIZE) rip, rport = address[:2] except socket.timeout: self.logger.warn("no registry acknowledged") return False if rport != self.port: continue try: reply = brine.load(data) except Exception: continue if reply == "OK": self.logger.info("registry %s:%s acknowledged", rip, rport) return True else: self.logger.warn("no registry acknowledged") return False finally: sock.close()
def test_brine(): x = ("he", 7, u"llo", 8, (), 900, None, True, Ellipsis, 18.2, 18.2j + 13, slice(1, 2, 3), frozenset([5, 6, 7]), NotImplemented, (1,2)) assert dumpable(x) y = dump(x) z = load(y) assert x == z
def register(self, aliases, port): self.logger.info("registering on %s:%s", self.ip, self.port) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(self.timeout) data = brine.dump(("RPYC", "REGISTER", (aliases, port))) try: try: sock.connect((self.ip, self.port)) sock.send(data) except (socket.error, socket.timeout): self.logger.warn("could not connect to registry") return False try: data = sock.recv(MAX_DGRAM_SIZE) except socket.timeout: self.logger.warn("registry did not acknowledge") return False try: reply = brine.load(data) except Exception: self.logger.warn("received corrupted data from registry") return False if reply == "OK": self.logger.info("registry %s:%s acknowledged", self.ip, self.port) return True finally: sock.close()
def _dispatch(self, data): msg, seq, args = brine.load(data) if msg == consts.MSG_REQUEST: self._dispatch_request(seq, args) elif msg == consts.MSG_REPLY: self._dispatch_reply(seq, args) elif msg == consts.MSG_EXCEPTION: self._dispatch_exception(seq, args) else: raise ValueError("invalid message type: %r" % (msg,))
def test_brine_2(self): if is_py3k: exec('''x = (b"he", 7, "llo", 8, (), 900, None, True, Ellipsis, 18.2, 18.2j + 13, slice(1, 2, 3), frozenset([5, 6, 7]), NotImplemented, (1,2))''', globals()) else: exec('''x = ("he", 7, u"llo", 8, (), 900, None, True, Ellipsis, 18.2, 18.2j + 13, slice(1, 2, 3), frozenset([5, 6, 7]), NotImplemented, (1,2))''') self.assertTrue(brine.dumpable(x)) y = brine.dump(x) z = brine.load(y) self.assertEqual(x, z)
def _dispatch(self, data): msg, seq, args = brine.load(data) if msg == consts.MSG_REQUEST: self._dispatch_request(seq, args) elif msg == consts.MSG_REPLY: obj = self._unbox(args) self._request_callbacks.pop(seq)(False, obj) elif msg == consts.MSG_EXCEPTION: obj = self._unbox_exc(args) self._request_callbacks.pop(seq)(True, obj) else: raise ValueError("invalid message type: %r" % (msg,))
def _dispatch(self, data): # serving---dispatch? msg, seq, args = brine.load(data) if msg == consts.MSG_REQUEST: self._dispatch_request(seq, args) elif msg == consts.MSG_REPLY: obj = self._unbox(args) self._request_callbacks.pop(seq)(False, obj) elif msg == consts.MSG_EXCEPTION: obj = self._unbox_exc(args) self._request_callbacks.pop(seq)(True, obj) else: raise ValueError("invalid message type: %r" % (msg, ))
def discover(self, name): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) with closing(sock): sock.settimeout(self.timeout) data = brine.dump(("RPYC", "QUERY", (name,))) sock.connect((self.ip, self.port)) sock.send(data) try: data = sock.recv(MAX_DGRAM_SIZE) except (socket.error, socket.timeout): servers = () else: servers = brine.load(data) return servers
def discover(self, name): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) with closing(sock): sock.settimeout(self.timeout) data = brine.dump(("RPYC", "QUERY", (name, ))) sock.connect((self.ip, self.port)) sock.send(data) try: data = sock.recv(MAX_DGRAM_SIZE) except (socket.error, socket.timeout): servers = () else: servers = brine.load(data) return servers
def test_brine_2(self): if is_py3k: exec( '''x = (b"he", 7, "llo", 8, (), 900, None, True, Ellipsis, 18.2, 18.2j + 13, slice(1, 2, 3), frozenset([5, 6, 7]), NotImplemented, (1,2))''', globals()) else: exec( '''x = ("he", 7, u"llo", 8, (), 900, None, True, Ellipsis, 18.2, 18.2j + 13, slice(1, 2, 3), frozenset([5, 6, 7]), NotImplemented, (1,2))''' ) self.assertTrue(brine.dumpable(x)) # noqa y = brine.dump(x) # noqa z = brine.load(y) self.assertEqual(x, z) # noqa
def discover(self, name): sock = socket.socket(self.sock_family, socket.SOCK_DGRAM) with closing(sock): if self.bcast: sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True) data = brine.dump(("RPYC", "QUERY", (name,))) sock.sendto(data, (self.ip, self.port)) sock.settimeout(self.timeout) try: data, _ = sock.recvfrom(MAX_DGRAM_SIZE) except (socket.error, socket.timeout): servers = () else: servers = brine.load(data) return servers
def discover(self, name): sock = socket.socket(self.sock_family, socket.SOCK_DGRAM) with closing(sock): if self.bcast: sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True) data = brine.dump(("RPYC", "QUERY", (name, ))) sock.sendto(data, (self.ip, self.port)) sock.settimeout(self.timeout) try: data, _ = sock.recvfrom(MAX_DGRAM_SIZE) except (socket.error, socket.timeout): servers = () else: servers = brine.load(data) return servers
def discover(self, name): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) if self.bcast: sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True) data = brine.dump(("RPYC", "QUERY", (name, ))) sock.sendto(data, (self.ip, self.port)) sock.settimeout(self.timeout) try: try: data, addrinfo = sock.recvfrom(MAX_DGRAM_SIZE) except (socket.error, socket.timeout): servers = () else: servers = brine.load(data) finally: sock.close() return servers
def _dispatch(self, data): msg, seq, args = brine.load(data) if msg == consts.MSG_REQUEST: try: # note: we're acquiring a shared lock here since we want network event handlers to be dispatched # atomically w.r.t. the local game state and engne state #framework.tickmodule.engine_lock.acquire() self._dispatch_request(seq, args) finally: #framework.tickmodule.engine_lock.release() pass #self._dispatch_request(seq, args) elif msg == consts.MSG_REPLY: self._dispatch_reply(seq, args) elif msg == consts.MSG_EXCEPTION: self._dispatch_exception(seq, args) else: raise ValueError("invalid message type: %r" % (msg,))
def discover(self, name): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) if self.bcast: sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True) data = brine.dump(("RPYC", "QUERY", (name,))) sock.sendto(data, (self.ip, self.port)) sock.settimeout(self.timeout) try: try: data, addrinfo = sock.recvfrom(MAX_DGRAM_SIZE) except (socket.error, socket.timeout): servers = () else: servers = brine.load(data) finally: sock.close() return servers
def _dispatch(self, data): msg, seq, args = brine.load(data) if msg == consts.MSG_REQUEST: try: # note: we're acquiring a shared lock here since we want network event handlers to be dispatched # atomically w.r.t. the local game state and engne state #framework.tickmodule.engine_lock.acquire() self._dispatch_request(seq, args) finally: #framework.tickmodule.engine_lock.release() pass #self._dispatch_request(seq, args) elif msg == consts.MSG_REPLY: self._dispatch_reply(seq, args) elif msg == consts.MSG_EXCEPTION: self._dispatch_exception(seq, args) else: raise ValueError("invalid message type: %r" % (msg, ))
def _dispatch(self, data): """tracing only""" got1 = time.time() try: return super()._dispatch(data) finally: got2 = time.time() msg, seq, args = brine.load(data) sent = self.timings.pop(seq, got1) if msg == consts.MSG_REQUEST: handler, args = args str_handler = f":req={_msg_to_name['HANDLE'][handler]}" else: str_handler = "" str_args = ( self.__to_text(args).replace("\r", "").replace("\n", "\tNEWLINE\t") ) with self.logLock: for logfile in self.logfiles: with open(logfile, "a") as out: out.write( f"recv:timing={got1 - sent}+{got2 - got1}:msg={_msg_to_name['MSG'][msg]}:seq={seq}{str_handler}:args={str_args}\n" )
def _work(self): while self.active: try: data, addrinfo = self._recv() except (socket.error, socket.timeout): continue try: magic, cmd, args = brine.load(data) except Exception: continue if magic != "RPYC": self.logger.warn("invalid magic: %r", magic) continue cmdfunc = getattr(self, "cmd_%s" % (cmd.lower(),), None) if not cmdfunc: self.logger.warn("unknown command: %r", cmd) continue try: reply = cmdfunc(addrinfo[0], *args) except Exception: self.logger.exception("error executing function") else: self._send(brine.dump(reply), addrinfo)
def _work(self): while self.active: try: data, addrinfo = self._recv() except (socket.error, socket.timeout): continue try: magic, cmd, args = brine.load(data) except Exception: continue if magic != "RPYC": self.logger.warn("invalid magic: %r", magic) continue cmdfunc = getattr(self, "cmd_%s" % (cmd.lower(), ), None) if not cmdfunc: self.logger.warn("unknown command: %r", cmd) continue try: reply = cmdfunc(addrinfo[0], *args) except Exception: self.logger.exception('error executing function') else: self._send(brine.dump(reply), addrinfo)