def commandReceived(self, ssn, message): """ Process incoming commands, coming in with session and message information. ================= ========================== Parameter Description ================= ========================== ssn AMQP session object message Received AMQP message ================= ========================== Incoming messages are coming from an :class:`clacks.common.components.amqp_proxy.AMQPServiceProxy` which is providing a *reply to* queue as a return channel. The command result is written to that queue. """ # Check for id if not message.user_id: raise ValueError(C.make_error("AMQP_MESSAGE_WITHOUT_UID")) id_ = '' name = args = err = res = None try: req = loads(message.content) except ServiceRequestNotTranslatable, e: err = str(e) req = {'id': id_}
def process(self, req, environ): """ Process an incoming JSONRPC request and dispatch it thru the *CommandRegistry*. ================= ========================== Parameter Description ================= ========================== req Incoming Request environ WSGI environment ================= ========================== ``Return``: varries """ # Handle OPTIONS if req.method == 'OPTIONS': return Response( server=self.ident, allow='POST' ) if not req.method == 'POST': raise exc.HTTPMethodNotAllowed( "Only POST allowed", allow='POST').exception try: json = loads(req.body) except ValueError, e: raise ValueError(C.make_error("INVALID_JSON", data=str(e)))
def commandReceived(self, ssn, message): """ Process incoming commands, coming in with session and message information. ================= ========================== Parameter Description ================= ========================== ssn AMQP session object message Received AMQP message ================= ========================== Incoming messages are coming from an :class:`clacks.common.components.amqp_proxy.AMQPServiceProxy` which is providing a *reply to* queue as a return channel. The command result is written to that queue. """ err = None res = None id_ = "" # Check for id if not message.user_id: raise Exception("incoming message without user_id") # Check for reply_to if not message.reply_to: raise Exception("incoming message without reply_to") try: req = loads(message.content) except ServiceRequestNotTranslatable, e: err = str(e) req = {"id": id_}
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']
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']