def get(self, service, shard, method): # TODO: still lacking configurable arguments - some of these # should be GET arguments. rid = self.get_argument("__rid") arguments = self.request.arguments del arguments["__rid"] # Tornado gives for every key a list of arguments, we need # only one arguments = dict((k, decode_json(arguments[k][0])) for k in arguments) service = ServiceCoord(service, int(shard)) authorized = self.application.service.authorized_rpc(service, method, arguments) if not authorized: self.write({'status': 'not authorized'}) return if service not in self.application.service.remote_services or \ not self.application.service.remote_services[service].\ connected: self.write({'status': 'unconnected'}) return self.application.service.__responses[rid] = "wait" self.application.service.remote_services[service].__getattr__(method)( callback=WebService._default_callback, plus=rid, **arguments) self.write({'status': 'wait'})
def process_data(self, data): """Function called when a terminator is detected in the stream. It clears the buffer and decode the data. Then it asks the local service to act and in case the service wants to respond, it sends back the response. data (string): the raw string received from the remote party, to be JSON-decoded. """ # We decode the arriving data try: message = decode_json(data) except ValueError: logger.warning("Cannot understand incoming message, discarding.") return # If __method is present, someone is calling an rpc of the # local service if "__method" in message: # We initialize the data we are going to send back response = {"__data": None, "__error": None} if "__id" in message: response["__id"] = message["__id"] # Otherwise, we compute the method here and send the reply # right away. try: method_response = self.service.handle_message(message) except Exception as exception: response["__error"] = "%s: %s\n%s" % \ (exception.__class__.__name__, exception, traceback.format_exc()) method_response = None self.send_reply(response, method_response) # Otherwise, is a response to our rpc call. else: if "__id" not in message: logger.warning("Response without __id field, discarding.") return ident = message["__id"] if ident in RPCRequest.pending_requests: rpc = RPCRequest.pending_requests[ident] rpc.complete(message) else: logger.warning("No pending request with id %s found." % ident)
def get(self, service, shard, method): arguments = self.request.arguments # Tornado gives for every key a list of arguments, we need # only one arguments = dict((k, decode_json(arguments[k][0])) for k in arguments) service = ServiceCoord(service, int(shard)) if service not in self.application.service.remote_services or \ not self.application.service.remote_services[service].connected: self.write({'status': 'unconnected'}) self.finish() return # TODO - So far we have no support for synchronous calls, so I # use an Event to delay the termination of this greenlet event = Event() event.clear() self.application.service.remote_services[service].__getattr__(method)( callback=self._request_callback, plus=event, **arguments) event.wait()
def get(self, service, shard, method): arguments = self.request.arguments # Tornado gives for every key a list of arguments, we need # only one arguments = dict((k, decode_json(arguments[k][0])) for k in arguments) service = ServiceCoord(service, int(shard)) if service not in self.application.service.remote_services or \ not self.application.service.remote_services[service].\ connected: self.write({'status': 'unconnected'}) self.finish() return # TODO - So far we have no support for synchronous calls, so I # use an Event to delay the termination of this greenlet event = Event() event.clear() self.application.service.remote_services[service].__getattr__(method)( callback=self._request_callback, plus=event, **arguments) event.wait()