Exemple #1
0
    def __call__(self, *args, **kwargs):
        if len(kwargs) > 0 and len(args) > 0:
            raise JSONRPCException("JSON-RPC does not support positional and keyword arguments at the same time")

        # Default to 'core' queue
        queue = "core" #@UnusedVariable

        if self.__methods:
            if not self.__serviceName in self.__methods:
                raise NameError("name '%s' not defined" % self.__serviceName)

            if self.__domain:
                queue = self.__methods[self.__serviceName]['target'] #@UnusedVariable
            else:
                queue = self.__serviceAddress #@UnusedVariable

        # Find free session for requested queue
        found = False
        for sess, dsc in self.__worker[self.__serviceAddress].iteritems():
            if not dsc['locked']:
                self.__ssn = dsc['ssn']
                self.__sender = dsc['sender']
                self.__receiver = dsc['receiver']
                self.__sess = sess
                dsc['locked'] = True
                found = True
                break

        # No free session?
        if not found:
            raise AMQPException('no free session - increase workers')

        # Send
        if len(kwargs):
            postdata = dumps({"method": self.__serviceName, 'params': kwargs, 'id': 'jsonrpc'})
        else:
            postdata = dumps({"method": self.__serviceName, 'params': args, 'id': 'jsonrpc'})

        message = Message(postdata)
        message.user_id = self.__URL['user']
        message.reply_to = 'reply-%s' % self.__ssn.name

        self.__sender.send(message)

        # Get response
        respdata = self.__receiver.fetch()
        resp = loads(respdata.content)
        self.__ssn.acknowledge(respdata)

        self.__worker[self.__serviceAddress][self.__sess]['locked'] = False

        if resp['error'] != None:
            raise JSONRPCException(resp['error'])

        return resp['result']
Exemple #2
0
    def __call__(self, *args, **kwargs):
        if len(kwargs) > 0 and len(args) > 0:
            raise JSONRPCException("JSON-RPC does not support positional and keyword arguments at the same time")

        if len(kwargs):
            postdata = dumps({"method": self.__serviceName, 'params': kwargs, 'id': 'jsonrpc'})
        else:
            postdata = dumps({"method": self.__serviceName, 'params': args, 'id': 'jsonrpc'})

        if self.__mode == 'POST':
            respdata = self.__opener.open(self.__serviceURL, postdata).read()
        else:
            respdata = self.__opener.open(self.__serviceURL + "?" + quote(postdata)).read()

        resp = loads(respdata)
        if resp['error'] != None:
            raise JSONRPCException(resp['error'])

        return resp['result']
Exemple #3
0
 def send_message(self, message_type, message):
     self.write_message(dumps([message_type, message]))
Exemple #4
0
                # Remove current sid if present
                if 'REMOTE_SESSION' in environ and sid in self.__session:
                    del self.__session[sid]

                result = False
                self.log.error("login failed for user '%s'" % user)
                raise exc.HTTPUnauthorized(
                    "Login failed",
                    allow='POST').exception

            return Response(
                server=self.ident,
                content_type='application/json',
                charset='utf8',
                body=dumps(dict(result=result,
                                error=None,
                                id=jid)))

        # Don't let calls pass beyond this point if we've no valid session ID
        if not environ.get('REMOTE_SESSION') in self.__session:
            self.log.error("blocked unauthenticated call of method '%s'" % method)
            raise exc.HTTPUnauthorized(
                    "Please use the login method to authorize yourself.",
                    allow='POST').exception

        # Remove remote session on logout
        if method == 'logout':

            # Remove current sid if present
            if 'REMOTE_SESSION' in environ and environ.get('REMOTE_SESSION') in self.__session:
                del self.__session[environ.get('REMOTE_SESSION')]