def func(address, node_id, *args): time_keeper = TimeKeeper() msgID = sha1(os.urandom(32)).digest() assert len(node_id) == 20 time_keeper.start_clock() data = umsgpack.packb([str(name), node_id, args]) time_keeper.stop_clock("time_data_pack") if len(data) > self.max_packet_size: msg = "Total length of function name and arguments cannot exceed 8K" raise MalformedMessage(msg) txdata = b'\x00' + msgID + data if self.noisy: log.msg("calling remote function %s on %s (msgid %s)" % (name, address, b64encode(msgID))) time_keeper.start_clock() self.transport.write(txdata, address) time_keeper.stop_clock("time_write_socket") # self.log.debug("[BENCH] LOW RPC SEND TIMES %s -> %s " % (str(name), time_keeper.get_summary())) d = defer.Deferred() timeout = reactor.callLater(self._waitTimeout, self._timeout, msgID) self._outstanding[msgID] = (d, timeout) return d
def _acceptRequest(self, msgID, data, address): if not isinstance(data, list) or len(data) != 3: raise MalformedMessage("Could not read packet: %s" % data) funcname, node_id, args = data f = getattr(self, "rpc_%s" % funcname, None) if f is None or not callable(f): msgargs = (self.__class__.__name__, funcname) log.err("%s has no callable method rpc_%s; ignoring request" % msgargs) return d = defer.maybeDeferred(f, address, node_id, *args) d.addCallback(self._sendResponse, msgID, address)
def func(address, *args): msgID = sha1(os.urandom(32)).digest() data = umsgpack.packb([str(name), args]) if len(data) > 8192: msg = "Total length of function name and arguments cannot exceed 8K" raise MalformedMessage(msg) txdata = b'\x00%s%s' % (msgID, data) if self.noisy: log.msg("calling remote function %s on %s (msgid %s)" % (name, address, b64encode(msgID))) self.transport.write(txdata, address) d = defer.Deferred() timeout = reactor.callLater(self._waitTimeout, self._timeout, msgID) self._outstanding[msgID] = (d, timeout) return d
def _acceptResponse(self, msgID, data, address): if not isinstance(data, list) or len(data) != 4: raise MalformedMessage("Could not read packet: %s" % data) msgargs = (b64encode(msgID), address) if msgID not in self._outstanding: log.err("received unknown message %s from %s; ignoring" % msgargs) return if self.noisy: log.msg("received response for message id %s from %s" % msgargs) d, timeout = self._outstanding[msgID] timeout.cancel() del self._outstanding[msgID] node_id, signature, ser_pub, response = data if not self._check_resp_ok(address, node_id, msgID, signature, ser_pub): return d.callback((True, response))
async def _accept_request(self, msg_id, data, address): if not isinstance(data, list) or len(data) != 2: raise MalformedMessage("Could not read packet: %s" % data) funcname, args = data func = getattr(self, "rpc_%s" % funcname, None) if func is None or not callable(func): msgargs = (self.__class__.__name__, funcname) LOG.warning("%s has no callable method " "rpc_%s; ignoring request", *msgargs) return if not asyncio.iscoroutinefunction(func): func = asyncio.coroutine(func) response = await func(address, *args) LOG.debug("sending response %s for msg id %s to %s", response, b64encode(msg_id), address) txdata = b'\x01' + msg_id + umsgpack.packb(response) self.transport.sendto(txdata, address)
def func(address, *args): msgID = sha1(os.urandom(32)).digest() data = umsgpack.packb([name, args]) if len(data) > 8192: raise MalformedMessage("Total length of function " "name and arguments cannot exceed 8K") txdata = b'\x00' + msgID + data log.debug("calling remote function %s on %s (msgid %s)", name, address, b64encode(msgID)) self.transport.sendto(txdata, address) loop = asyncio.get_event_loop() if hasattr(loop, 'create_future'): f = loop.create_future() else: f = asyncio.Future() timeout = loop.call_later(self._waitTimeout, self._timeout, msgID) self._outstanding[msgID] = (f, timeout) return f
def func(address, node_id, *args): msgID = sha1(os.urandom(32)).digest() assert len(node_id) == 20 (my_ip, my_port) = self.get_address() signature, timestamp = sign_msg(my_ip, my_port, self.private_key) ser_pub = serialize_pub_key(self.public_key) data = umsgpack.packb([str(name), node_id, timestamp, signature, ser_pub, args]) if len(data) > self.max_packet_size: msg = "Total length of function name and arguments cannot exceed 8K" raise MalformedMessage(msg) txdata = b'\x00' + msgID + data if self.noisy: log.msg("calling remote function %s on %s (msgid %s)" % (name, address, b64encode(msgID))) self.transport.write(txdata, address) d = defer.Deferred() timeout = reactor.callLater(self._waitTimeout, self._timeout, msgID) self._outstanding[msgID] = (d, timeout) return d