async def send_to_component(self, req): """Send a query to a component HTTP method: POST Query params: address :param req: request from webserver """ if not 'address' in req.query: return self.create_error('"address" is a required parameter!') msg = await req.json() print(msg) if not 'name' in msg: return self.create_error('name not in message!') try: component = self.resolve_component(req.query['address']) except RequestTimeoutException as rte: return self.create_error('request to {} timed out!'.format(req.query['address'])) self.log('component resolved!') # craft the request request = Message(name=msg['name']) if 'payload' in msg: request.payload = msg['payload'] # make the request try: response = component.request(request, timeout=self.REQ_TIMEOUT) except RequestTimeoutException as rte: return self.create_error('request to {} timed out!'.format(req.query['address'])) # prepare the JSON response resp = { 'name': response.name, 'success': response.success, 'payload': response.payload } # respond return web.json_response({ 'response': resp, 'success': True })
def wait(self): """Waits for a client to ask something :returns: a triple (run inputs, run config, done status) """ while True: if self.__dirty_socket: raise NoResultError() msg = Message(data=self.__socket.recv()) reply = Message(name='ack') self.__log('received {}...'.format(msg.name)) # route our reply if msg.name == 'execute': # first check if the request is valid if msg.payload is not None and 'parameters' in msg.payload and 'inputs' in msg.payload: # this is an execution, pass control back to the main loop (but needing closure) self.__log('passing execution back...') self.__dirty_socket = True return (packing.unpack_io_object( msg.payload['inputs'], defs=self.cruxfile['inputs']), self.__defaultify(msg.payload['parameters']), False) else: reply.name = 'malformed' reply.success = False self.__log.error('received malformed execution request') elif msg.name == 'get_cruxfile': reply.payload = self.cruxfile elif msg.name == 'shutdown': break else: # method called has not yet been implemented/is unknown reply.name = 'nyi' reply.success = False # skipped if there was a shutdown or execute instruction self.__socket.send(reply.pack()) self.__dirty_socket = False # if we've broken out this side of the loop, assume we're shutting down self.__log('shutting down loop...') self.__socket.send(reply.pack()) self.__dirty_socket = False return (None, None, True)