async def call(self, method, *args): """Calls a RPC method. :param str method: Method name. :param args: Method arguments. """ _logger.debug('creating request') req = self._create_request(method, args) if self._conn is None: _logger.debug("connect to {}:{}...".format(self._host, self._port)) reader, writer = await asyncio.open_connection(self._host, self._port, loop=self._loop) self._conn = Connection( reader, writer, msgpack.Unpacker(encoding=self._unpack_encoding, **self._unpack_params)) _logger.debug("Connection to {}:{} established".format( self._host, self._port)) try: _logger.debug('Sending req: {}'.format(req)) await self._conn.sendall(req, self._timeout) _logger.debug('Sending complete') except asyncio.TimeoutError as te: _logger.error("Write request to {}:{} timeout".format( self._host, self._port)) raise te except Exception as e: raise e response = None try: _logger.debug('receiving result from server') response = await self._conn.recvall(self._timeout) _logger.debug('receiving result completed') except asyncio.TimeoutError as te: _logger.error("Read request to {}:{} timeout".format( self._host, self._port)) self._conn.reader.set_exception(te) raise te except Exception as e: self._conn.reader.set_exception(e) raise e if response is None: raise IOError("Connection closed") if type(response) != tuple: logging.debug( 'Protocol error, received unexpected data: {}'.format( response)) raise RPCProtocolError('Invalid protocol') return self._parse_response(response)
def _parse_request(req): if len(req) != 4 or req[0] != MSGPACKRPC_REQUEST: raise RPCProtocolError('Invalid protocol') _, msg_id, method_name, args = req method = _methods.get(method_name) if not method: raise MethodNotFoundError("No such method {}".format(method_name)) return msg_id, method, args
def _parse_request(req): if (len(req) != 4 or req[0] != MSGPACKRPC_REQUEST): raise RPCProtocolError('Invalid protocol') (_, msg_id, method_name, args) = req method = _methods.get(method_name, None) if method is None: raise MethodNotFoundError("No such method {}".format(method_name)) return (msg_id, method, args)
def _parse_response(self, response): if (len(response) != 4 or response[0] != MSGPACKRPC_RESPONSE): raise RPCProtocolError('Invalid protocol') (_, msg_id, error, result) = response if msg_id != self._msg_id: raise RPCError('Invalid Message ID') if error: raise RPCError(str(error)) return result
async def call(self, method, *args, _close=False): """Calls a RPC method. :param str method: Method name. :param args: Method arguments. :param _close: Close the connection at the end of the request. Defaults to false """ _logger.debug('creating request') req = self._create_request(method, args) if self._conn is None or self._conn.is_closed(): await self._open_connection() try: _logger.debug('Sending req: %s', req) await self._conn.sendall(req, self._timeout) _logger.debug('Sending complete') except asyncio.TimeoutError as te: _logger.error("Write request to %s:%s timeout", *self.getpeername()) raise te except Exception as e: raise e response = None try: _logger.debug('receiving result from server') response = await self._conn.recvall(self._timeout) _logger.debug('receiving result completed') except asyncio.TimeoutError as te: _logger.error("Read request to %s:%s timeout", *self.getpeername()) self._conn.reader.set_exception(te) raise te except Exception as e: self._conn.reader.set_exception(e) raise e if response is None: raise IOError("Connection closed") if not isinstance(response, tuple): logging.debug('Protocol error, received unexpected data: %r', response) raise RPCProtocolError('Invalid protocol') if _close: self.close() return self._parse_response(response)
def _parse_request(req): if len(req) != 4 or req[0] != MSGPACKRPC_REQUEST: raise RPCProtocolError('Invalid protocol') _, msg_id, method_name, args = req _method_soup = method_name.split('.') if len(_method_soup) == 1: method = _methods.get(method_name) else: method = getattr(_class_methods.get(_method_soup[0]), _method_soup[1], None) return msg_id, method, args, method_name
def _parse_request(req): if len(req) != 6 or req[0] != MSGPACKRPC_REQUEST: raise RPCProtocolError('Invalid protocol') _, msg_id, method_name, args, timeout, streamed = req _method_soup = method_name.split('.') if len(_method_soup) == 1: method = _methods.get(method_name) else: method = getattr(_class_methods.get(_method_soup[0]), _method_soup[1]) if not method: raise MethodNotFoundError("No such method {}".format(method_name)) return msg_id, method, args, method_name, timeout, streamed
def _parse_response(self, response): if (len(response) != 4 or response[0] != MSGPACKRPC_RESPONSE): raise RPCProtocolError('Invalid protocol') (_, msg_id, error, result) = response if msg_id != self._msg_id: raise RPCError('Invalid Message ID') if error and len(error) == 2: if error[0] == 'MethodNotFoundError': raise MethodNotFoundError(error[1]) raise EnhancedRPCError(*error) elif error: raise RPCError(error) return result