Exemplo n.º 1
0
 def send(self, data):
     self.log.debug("sending event: %s" % data)
     msg = Message(data)
     msg.user_id = self.__user
     try:
         return self.__sender.send(msg)
     except NotFound as e:
         self.log.critical("cannot send event: %s" % str(e))
         self.env.requestRestart()
         return False
Exemplo n.º 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")

        # 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']
Exemplo n.º 3
0
 def send(self, data):
     self.log.debug("sending event: %s" % data)
     msg = Message(data)
     msg.user_id = self.__user
     return self.__sender.send(msg)
Exemplo n.º 4
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, pylint: disable=W0612
        queue = "core"

        if AMQPServiceProxy.methods[self.__serviceAddress]:
            if not self.__serviceName in AMQPServiceProxy.methods[self.__serviceAddress]:
                raise NameError("name '%s' not defined" % self.__serviceName)

            if AMQPServiceProxy.domain:
                queue = AMQPServiceProxy.methods[self.__serviceAddress][self.__serviceName]['target']
            else:
                queue = self.__serviceAddress

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

        # No free session?
        if not self.__ssn:
            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)

        if resp['error'] != None:
            AMQPServiceProxy.worker[self.__serviceAddress][self.__worker]['locked'] = False
            raise JSONRPCException(resp['error'])

        else:
            # Look for json class hint
            if "result" in resp and \
                isinstance(resp["result"], DictType) and \
                "__jsonclass__" in resp["result"] and \
                resp["result"]["__jsonclass__"][0] == "json.ObjectFactory":

                resp = resp["result"]
                jc = resp["__jsonclass__"][1]
                del resp["__jsonclass__"]

                # Extract property presets
                data = {}
                for prop in resp:
                    data[prop] = resp[prop]

                jc.insert(0, AMQPServiceProxy(self.__serviceURL,
                    self.__serviceAddress, None, self.__conn,
                    workers=self.__workers))
                jc.append(data)
                AMQPServiceProxy.worker[self.__serviceAddress][self.__worker]['locked'] = False
                return ObjectFactory.get_instance(*jc)

            AMQPServiceProxy.worker[self.__serviceAddress][self.__worker]['locked'] = False
            return resp['result']