def execute_rpc(self, method, data, callback=None, plus=None): """Method to send an RPC request to the remote service. The message sent to the remote service is of this kind: {"__method": <name of the requested method> "__data": {"<name of first arg>": <value of first arg, ... } "__id": <16 letters random ID> } The __id field is put by the pre_execute method of RPCRequest. method (string): the name of the method to call. data (object): the object to pass to the remote method. callback (function): method to call with the RPC response. plus (object): additional object to be passed to the callback. return (bool/dict): False if the remote service is not connected; in a non-yielded call True if it is connected; in a yielded call, a dictionary with fields 'completed', 'data', and 'error'. """ # Try to connect, or fail. if not self.connected: self.connect_remote_service() if not self.connected: return False # We start building the request message message = {} message["__method"] = method message["__data"] = data # And we remember that we need to wait for a reply request = RPCRequest(message, self.service, callback, plus) message = request.pre_execute() # We encode the request and send it try: json_message = encode_json(message) except ValueError: msg = "Cannot send request of method %s because of " \ "encoding error." % method request.complete({"__error": msg}) return ret = self._push_right(json_message) if not ret: msg = "Transfer interrupted" request.complete({"__error": msg}) return return True
def execute_rpc(self, method, data, callback=None, plus=None): """Method to send an RPC request to the remote service. The message sent to the remote service is of this kind: {"__method": <name of the requested method> "__data": {"<name of first arg>": <value of first arg, ... } "__id": <32-digit hex-encoded UUID> } The __id field is put by the pre_execute method of RPCRequest. method (string): the name of the method to call. data (object): the object to pass to the remote method. callback (function): method to call with the RPC response. plus (object): additional object to be passed to the callback. return (bool|dict): False if the remote service is not connected; in a non-yielded call True if it is connected; in a yielded call, a dictionary with fields 'completed', 'data', and 'error'. """ # Try to connect, or fail. if not self.connected: self.connect_remote_service() if not self.connected: return False # We start building the request message message = {} message["__method"] = method message["__data"] = data # And we remember that we need to wait for a reply request = RPCRequest(message, self.service, callback, plus) message = request.pre_execute() # We encode the request and send it try: json_message = encode_json(message) except ValueError: msg = "Cannot send request of method %s because of " \ "encoding error." % method request.complete({"__error": msg}) return ret = self._push_right(json_message) if not ret: msg = "Transfer interrupted" request.complete({"__error": msg}) return return True
def send_reply(self, response, method_response): """Send back a reply to an rpc call. response (dict): the metadata of the reply. method_response (object): the actual returned value. """ response["__data"] = method_response try: json_message = encode_json(response) except ValueError as error: logger.warning("Cannot send response because of " + "encoding error. %s" % repr(error)) return self._push_right(json_message)