def api_call(*args, **kwargs): assert self._address assert self._response_address # lazy socket if callable(self._socket): # actual connect self._socket = self._socket() self._response = None message = { 'action': name, 'wait': self._wait, 'no_reply': self._no_reply, 'bytes': len(self._bytes), 'priority': int(self._priority), 'context': { 'event_id': self._response_address, }, 'args': { 'args': args, 'kwargs': kwargs } } message = to_json(message) logging.debug('RPCBroker >>> broker.%s', pretty_func_str(name, *args, **kwargs)) packet = ['', self._address, message] if self._bytes: packet.extend(self._bytes) self._socket.send_multipart(packet) self._response_address = None self._bytes = None self._priority = 0
def to_json(self, data): try: data = to_json(data) except ValueError: logging.warn('Cant convert to json: %s', data) raise return data
def config(agent): logging.debug('Prepare config') config = agent.net_config if not config: raise ValueError('No net config') config = Config(config) config.make_unique() if agent.config['out']: agent.config['out'].write(to_json(config)) return config
def __call__(self, *args, **kwargs): self._response = None # original message message = { 'action': self.name, 'wait': self._wait, 'no_reply': self._no_reply, 'bytes': len(self._bytes), 'priority': int(self._priority), 'args': { 'args': args, 'kwargs': kwargs } } # pack message to envelope message = { 'action': self.proxy._proxy_method, 'address': self.proxy._proxy_address, 'data': message, 'wait': self._wait, 'no_reply': self._no_reply, 'bytes': len(self._bytes), 'priority': int(self._priority), 'args': { 'args': self.proxy._proxy_args, 'kwargs': self.proxy._proxy_kwargs } } message = to_json(message) logging.debug('RPC broker proxy >>> %s', pretty_func_str('%s.%s' % ( self.proxy._proxy_method, self.name), *args, **kwargs) ) packet = ['', message] if self._bytes: packet.extend(self._bytes) self.proxy._socket.send_multipart(packet) ret = {'success': True, 'data': None} if not self._no_reply: ret = self.proxy._socket.recv_multipart() assert ret[0] == '' ret = from_json(ret[1]) self.proxy._response = ret logging.debug('RPC broker proxy %s', ret) if not ret['success']: if 'traceback' in ret and ret['traceback']: raise RPCException(ret, ret['traceback']) raise RPCException(ret, ret['error']) return ret['data']
def api_call(*args, **kwargs): self._response = None message = { 'action': name, 'args': { 'args': args, 'kwargs': kwargs } } message = to_json(message) logging.debug('RPC >>> server.%s', pretty_func_str(name, *args, **kwargs)) self._socket.send_multipart(['', message]) ret = self._socket.recv_multipart() assert ret[0] == '' ret = from_json(ret[1]) self._response = ret logging.debug('RPC %s', ret) if not ret['success']: if 'traceback' in ret and ret['traceback']: raise RPCException(ret, ret['traceback']) raise RPCException(ret, ret['error']) return ret['data']
def save(self, file_name): with open(file_name, 'w') as out: out.write(to_json(self))