def call(self, method, *args, **kwargs): if self._connection is not None: rpc_request = pyjsonrpc.create_request_dict(method, *args, **kwargs) self.dt_call = datetime.datetime.utcnow() self.response = None #self.correlation_id = str(uuid.uuid4()) # UUID4 (random) self.correlation_id = rpc_request["id"] # get request_id from dict rpc_request = self.enc_decoder.encode(rpc_request) # dict -> str #rpc_request = self.enc_decoder.encode(rpc_request) + "bad" # badly formated JSON (for test) logging.debug(" [->] Sending request to queue %r" % self._queue) logging.debug("request: %r" % rpc_request) logging.debug("correlation_id: %r" % self.correlation_id) logging.debug("reply_to: %r" % self._callback_queue) self._channel.basic_publish(exchange='', routing_key=self._queue, properties=pika.BasicProperties( reply_to = self._callback_queue, correlation_id = self.correlation_id, ), body=rpc_request) while self.response is None: self._connection.process_data_events() logging.debug(" [<-] Got response on queue %r" % self._callback_queue) logging.debug("response: %r" % (self.response)) logging.debug("rpc execution delay: %s" % (self.dt_response-self.dt_call)) return(self.enc_decoder.decode(self.response)) else: raise(MyException("No connection"))
def main(terminal_id, message, drop_table, create_table, no_insert): # connect to our database DB.connect() if drop_table: JSON_RPC.drop_table(fail_silently=True) return if create_table: JSON_RPC.create_table(fail_silently=True) #JSON_RPC.select().where(True).delete_instance() terminal_id = terminal_id #request = {} #request_id = random_alphanumeric_id(10) #request["jsonrpc"] = "2.0" #request["method"] = "Comment" #request["params"] = ["Hello Eiffel with request_id='%s' @ %s" % (request_id, datetime.datetime.utcnow())] #request["id"] = request_id #request = json.dumps(request) #request = '{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": '+ str(request_id) + '}' request = pyjsonrpc.create_request_dict("Comment", "%s@ %s" % (message, datetime.datetime.utcnow())) request_id = request["id"] request = json.dumps(request) response = "" json_rpc = JSON_RPC( terminal_id=terminal_id, request_id = request_id, request=request, response=response, was_received=False, was_executed=False ) if not no_insert: #json_rpc.save() json_rpc.save(force_insert=True)
def main(terminal_id, message, drop_table, create_table, no_insert): # connect to our database DB.connect() if drop_table: JSON_RPC.drop_table(fail_silently=True) return if create_table: JSON_RPC.create_table(fail_silently=True) #JSON_RPC.select().where(True).delete_instance() terminal_id = terminal_id #request = {} #request_id = random_alphanumeric_id(10) #request["jsonrpc"] = "2.0" #request["method"] = "Comment" #request["params"] = ["Hello Eiffel with request_id='%s' @ %s" % (request_id, datetime.datetime.utcnow())] #request["id"] = request_id #request = json.dumps(request) #request = '{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": '+ str(request_id) + '}' request = pyjsonrpc.create_request_dict( "Comment", "%s@ %s" % (message, datetime.datetime.utcnow())) request_id = request["id"] request = json.dumps(request) response = "" json_rpc = JSON_RPC(terminal_id=terminal_id, request_id=request_id, request=request, response=response, was_received=False, was_executed=False) if not no_insert: #json_rpc.save() json_rpc.save(force_insert=True)
def _call_sendtodb(self, method, *args, **kwargs): """ Send a request with its arguments to database JSON RPC request is created using pyjsonrpc Parameters ---------- method : method to call *args : list of arguments **kwargs : keyword arguments (dict) Returns ------- conn : database connection """ if self._terminal_id is None: raise(MyException("terminal_id wasn't set. Use use_terminal method)")) #logging.info("method: %s" % method) #logging.info("args: %s" % args) #logging.info("kwargs: %s" % kwargs) request = pyjsonrpc.create_request_dict(method, *args, **kwargs) request_id = request["id"] request = json.dumps(request) #reply_to = amqp_queue_random() reply_to = amqp_queue_from_request_id(request_id) #UglyFix (because of MySQL ArrayFetch issue) logging.debug(" [->] Sending request") logging.debug("request: %s" % request) logging.debug("request_id: %s" % request_id) logging.debug("reply_to: %s" % reply_to) self._insert_rpc(request_id, request, reply_to) return(request_id)
def _call_sendtodb(self, method, *args, **kwargs): """ Send a request with its arguments to database JSON RPC request is created using pyjsonrpc Parameters ---------- method : method to call *args : list of arguments **kwargs : keyword arguments (dict) Returns ------- conn : database connection """ if self._terminal_id is None: raise (MyException("terminal_id wasn't set. Use use_terminal method)")) # logging.info("method: %s" % method) # logging.info("args: %s" % args) # logging.info("kwargs: %s" % kwargs) request = pyjsonrpc.create_request_dict(method, *args, **kwargs) request_id = request["id"] request = json.dumps(request) # reply_to = amqp_queue_random() reply_to = amqp_queue_from_request_id(request_id) # UglyFix (because of MySQL ArrayFetch issue) logging.debug(" [->] Sending request") logging.debug("request: %s" % request) logging.debug("request_id: %s" % request_id) logging.debug("reply_to: %s" % reply_to) self._insert_rpc(request_id, request, reply_to) return request_id
#!/usr/bin/env python # coding: utf-8 # BEGIN --- required only for testing, remove in real world code --- BEGIN import os import sys THISDIR = os.path.dirname(os.path.abspath(__file__)) APPDIR = os.path.abspath(os.path.join(THISDIR, os.path.pardir, os.path.pardir)) sys.path.insert(0, APPDIR) # END --- required only for testing, remove in real world code --- END import pyjsonrpc rpc_client = pyjsonrpc.HttpClient("http://localhost:8080") # Example with *multiple calls* in one request methods = [ pyjsonrpc.create_request_dict("add", 1, 2), pyjsonrpc.create_request_dict("add", 3, 4) ] print rpc_client.call(methods) # Example with *multiple notifications* (no response) in one request notifications = [ pyjsonrpc.create_request_dict("add", 1, 2), pyjsonrpc.create_request_dict("add", 3, 4) ] rpc_client.notify(notifications)
# coding: utf-8 # BEGIN --- required only for testing, remove in real world code --- BEGIN import os import sys THISDIR = os.path.dirname(os.path.abspath(__file__)) APPDIR = os.path.abspath(os.path.join(THISDIR, os.path.pardir, os.path.pardir)) sys.path.insert(0, APPDIR) # END --- required only for testing, remove in real world code --- END import pyjsonrpc rpc_client = pyjsonrpc.HttpClient(url = "http://localhost:8080") # Example with *multiple calls* in one request methods = [ pyjsonrpc.create_request_dict("addxxxxx", 1, 2), # ERROR pyjsonrpc.create_request_dict("add", 3, 4), pyjsonrpc.create_request_dict("add", 5, 6) ] print rpc_client.call(methods) # Example with *multiple notifications* (no response) in one request notifications = [ pyjsonrpc.create_request_dict("addxxxxx", 1, 2), # ERROR pyjsonrpc.create_request_dict("add", 3, 4), pyjsonrpc.create_request_dict("add", 5, 6) ] rpc_client.notify(notifications)