summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py
diff options
context:
space:
mode:
authorDan Klein <danklein10@gmail.com>2015-12-20 00:07:44 +0200
committerDan Klein <danklein10@gmail.com>2015-12-20 00:07:44 +0200
commit4ca24cf31919870a684fe78f17c856e0d220e6d5 (patch)
treef40ab95e52adca3ac713d61eb9fa3fd0d136e4ea /scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py
parent1895d21485621c3428d045fa0f5b9daf165c8260 (diff)
parent5cef472bcdc6c0b7e20e5cc42485ed5570c10f8c (diff)
Merge branch 'master' into dan_stateless
Diffstat (limited to 'scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py')
-rwxr-xr-xscripts/automation/trex_control_plane/client_utils/jsonrpc_client.py67
1 files changed, 33 insertions, 34 deletions
diff --git a/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py b/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py
index dd208da4..bdae7bd9 100755
--- a/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py
+++ b/scripts/automation/trex_control_plane/client_utils/jsonrpc_client.py
@@ -7,8 +7,7 @@ import general_utils
import re
from time import sleep
from collections import namedtuple
-
-CmdResponse = namedtuple('CmdResponse', ['success', 'data'])
+from common.trex_types import *
class bcolors:
BLUE = '\033[94m'
@@ -33,21 +32,17 @@ class BatchMessage(object):
def invoke(self, block = False):
if not self.rpc_client.connected:
- return False, "Not connected to server"
+ return RC_ERR("Not connected to server")
msg = json.dumps(self.batch_list)
- rc, resp_list = self.rpc_client.send_raw_msg(msg, block = False)
- if len(self.batch_list) == 1:
- return CmdResponse(True, [CmdResponse(rc, resp_list)])
- else:
- return CmdResponse(rc, resp_list)
+ return self.rpc_client.send_raw_msg(msg)
# JSON RPC v2.0 client
class JsonRpcClient(object):
- def __init__ (self, default_server, default_port):
+ def __init__ (self, default_server, default_port, prn_func = None):
self.verbose = False
self.connected = False
@@ -56,6 +51,8 @@ class JsonRpcClient(object):
self.server = default_server
self.id_gen = general_utils.random_id_gen()
+ self.prn_func = prn_func
+
def get_connection_details (self):
rc = {}
rc['server'] = self.server
@@ -112,7 +109,7 @@ class JsonRpcClient(object):
def invoke_rpc_method (self, method_name, params = {}):
if not self.connected:
- return False, "Not connected to server"
+ return RC_ERR("Not connected to server")
id, msg = self.create_jsonrpc_v2(method_name, params)
@@ -130,11 +127,10 @@ class JsonRpcClient(object):
self.socket.send(msg)
break
except zmq.Again:
- sleep(0.1)
tries += 1
if tries > 10:
self.disconnect()
- return CmdResponse(False, "Failed to send message to server")
+ return RC_ERR("*** [RPC] - Failed to send message to server")
tries = 0
@@ -143,11 +139,10 @@ class JsonRpcClient(object):
response = self.socket.recv()
break
except zmq.Again:
- sleep(0.1)
tries += 1
if tries > 10:
self.disconnect()
- return CmdResponse(False, "Failed to get server response")
+ return RC_ERR("*** [RPC] - Failed to get server response")
self.verbose_msg("Server Response:\n\n" + self.pretty_json(response) + "\n")
@@ -158,36 +153,35 @@ class JsonRpcClient(object):
response_json = json.loads(response)
if isinstance(response_json, list):
- rc_list = []
+ rc_batch = RC()
for single_response in response_json:
- rc, msg = self.process_single_response(single_response)
- rc_list.append( CmdResponse(rc, msg) )
+ rc = self.process_single_response(single_response)
+ rc_batch.add(rc)
- return CmdResponse(True, rc_list)
+ return rc_batch
else:
- rc, msg = self.process_single_response(response_json)
- return CmdResponse(rc, msg)
+ return self.process_single_response(response_json)
def process_single_response (self, response_json):
if (response_json.get("jsonrpc") != "2.0"):
- return False, "Malformed Response ({0})".format(str(response_json))
+ return RC_ERR("Malformed Response ({0})".format(str(response_json)))
# error reported by server
if ("error" in response_json):
if "specific_err" in response_json["error"]:
- return False, response_json["error"]["specific_err"]
+ return RC_ERR(response_json["error"]["specific_err"])
else:
- return False, response_json["error"]["message"]
+ return RC_ERR(response_json["error"]["message"])
# if no error there should be a result
if ("result" not in response_json):
- return False, "Malformed Response ({0})".format(str(response_json))
+ return RC_ERR("Malformed Response ({0})".format(str(response_json)))
- return True, response_json["result"]
+ return RC_OK(response_json["result"])
@@ -199,11 +193,12 @@ class JsonRpcClient(object):
self.socket.close(linger = 0)
self.context.destroy(linger = 0)
self.connected = False
- return True, ""
+ return RC_OK()
else:
- return False, "Not connected to server"
+ return RC_ERR("Not connected to server")
- def connect(self, server=None, port=None):
+
+ def connect(self, server = None, port = None, prn_func = None):
if self.connected:
self.disconnect()
@@ -215,20 +210,24 @@ class JsonRpcClient(object):
# Socket to talk to server
self.transport = "tcp://{0}:{1}".format(self.server, self.port)
- print "\nConnecting To RPC Server On {0}".format(self.transport)
+ msg = "\nConnecting To RPC Server On {0}".format(self.transport)
+ if self.prn_func:
+ self.prn_func(msg)
+ else:
+ print msg
self.socket = self.context.socket(zmq.REQ)
try:
self.socket.connect(self.transport)
except zmq.error.ZMQError as e:
- return False, "ZMQ Error: Bad server or port name: " + str(e)
+ return RC_ERR("ZMQ Error: Bad server or port name: " + str(e))
- self.socket.setsockopt(zmq.SNDTIMEO, 5)
- self.socket.setsockopt(zmq.RCVTIMEO, 5)
+ self.socket.setsockopt(zmq.SNDTIMEO, 1000)
+ self.socket.setsockopt(zmq.RCVTIMEO, 1000)
self.connected = True
- return True, ""
+ return RC_OK()
def reconnect(self):
@@ -236,7 +235,7 @@ class JsonRpcClient(object):
return self.connect()
if not self.connected:
- return False, "Not connected to server"
+ return RC_ERR("Not connected to server")
# reconnect
return self.connect(self.server, self.port)