예제 #1
0
    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 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'})
예제 #3
0
    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

        self.application.service.remote_services[service].__getattr__(method)(
            callback=self._request_callback, plus=0, **arguments)
    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

        self.application.service.remote_services[service].__getattr__(method)(
            callback=self._request_callback, plus=0, **arguments)
예제 #5
0
    def found_terminator(self):
        """Function called when a terminator is detected in the
        stream. It clear the cache and decode the data. Then it ask
        the local service to act and in case the service wants to
        respond, it sends back the response.

        """
        data = "".join(self.data)
        self.data = []

        # We decode the arriving data
        try:
            json_length = decode_length(data[:4])
            message = decode_json(data[4:json_length + 4])
            if len(data) > json_length + 4:
                if message["__data"] is None:
                    message["__data"] = \
                        decode_binary(data[json_length + 4:])
                else:
                    message["__data"]["binary_data"] = \
                        decode_binary(data[json_length + 4:])
        except:
            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"]

            # We find the properties of the called rpc method.
            try:
                method_info = self.service.method_info(message["__method"])
                binary_response = method_info["binary_response"]
                threaded = method_info["threaded"]
            except KeyError as exception:
                response["__error"] = "%s: %s\n%s" % \
                    (exception.__class__.__name__, exception,
                     traceback.format_exc())
                binary_response = False
                method_response = None
                threaded = False

            # If the rpc method is threaded, then we start the thread
            # and return immediately.
            if threaded:
                thread = ThreadedRPC(self, message, response, binary_response)
                thread.start()
                return

            # 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())
                binary_response = False
                method_response = None
            self.send_reply(response, method_response, binary_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)
예제 #6
0
def decode_store(no_files):
    """Function to store decoded json in a dictionary"""
    global title_dict
    title_dict=decode_json(no_files)
예제 #7
0
    def found_terminator(self):
        """Function called when a terminator is detected in the
        stream. It clear the cache and decode the data. Then it ask
        the local service to act and in case the service wants to
        respond, it sends back the response.

        """
        data = "".join(self.data)
        self.data = []

        # We decode the arriving data
        try:
            json_length = decode_length(data[:4])
            message = decode_json(data[4:json_length + 4])
            if len(data) > json_length + 4:
                if message["__data"] is None:
                    message["__data"] = \
                        decode_binary(data[json_length + 4:])
                else:
                    message["__data"]["binary_data"] = \
                        decode_binary(data[json_length + 4:])
        except:
            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"]

            # We find the properties of the called rpc method.
            try:
                method_info = self.service.method_info(message["__method"])
                binary_response = method_info["binary_response"]
                threaded = method_info["threaded"]
            except KeyError as exception:
                response["__error"] = "%s: %s\n%s" % \
                    (exception.__class__.__name__, exception,
                     traceback.format_exc())
                binary_response = False
                method_response = None
                threaded = False

            # If the rpc method is threaded, then we start the thread
            # and return immediately.
            if threaded:
                thread = ThreadedRPC(self, message, response, binary_response)
                thread.start()
                return

            # 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())
                binary_response = False
                method_response = None
            self.send_reply(response, method_response, binary_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)