diff options
Diffstat (limited to 'src/console')
-rwxr-xr-x | src/console/trex_console.py | 19 | ||||
-rw-r--r-- | src/console/trex_rpc_client.py | 27 |
2 files changed, 30 insertions, 16 deletions
diff --git a/src/console/trex_console.py b/src/console/trex_console.py index 584089ae..be84640c 100755 --- a/src/console/trex_console.py +++ b/src/console/trex_console.py @@ -23,8 +23,7 @@ class TrexConsole(cmd.Cmd): rc, msg = self.rpc_client.query_rpc_server() if rc: - lst = msg.split('\n') - self.supported_rpc = [str(x) for x in lst if x] + self.supported_rpc = [str(x) for x in msg if x] # a cool hack - i stole this function and added space def completenames(self, text, *ignored): @@ -57,7 +56,11 @@ class TrexConsole(cmd.Cmd): if not rc: print "\n*** Failed to query RPC server: " + str(msg) - print "\nRPC server supports the following commands: \n\n" + msg + print "\nRPC server supports the following commands: \n\n" + for func in msg: + if func: + print func + print "\n" def do_ping (self, line): '''\npings the RPC server\n''' @@ -81,7 +84,6 @@ class TrexConsole(cmd.Cmd): else: print "[FAILED]\n" - print "Server Response:\n\n{0}\n".format(json.dumps(msg)) def complete_rpc (self, text, line, begidx, endidx): return [x for x in self.supported_rpc if x.startswith(text)] @@ -104,11 +106,10 @@ class TrexConsole(cmd.Cmd): def main (): # RPC client - try: - rpc_client = RpcClient("localhost", 5050) - rpc_client.connect() - except Exception as e: - print "\n*** " + str(e) + "\n" + rpc_client = RpcClient("localhost", 5050) + rc, msg = rpc_client.connect() + if not rc: + print "\n*** " + msg + "\n" exit(-1) # console diff --git a/src/console/trex_rpc_client.py b/src/console/trex_rpc_client.py index fe8f69a2..f3edd252 100644 --- a/src/console/trex_rpc_client.py +++ b/src/console/trex_rpc_client.py @@ -2,6 +2,7 @@ import zmq
import json
from time import sleep
+import random
class RpcClient():
@@ -22,6 +23,16 @@ class RpcClient(): return rc
+ def pretty_json (self, json_str):
+ return json.dumps(json.loads(json_str), indent = 4, separators=(',', ': '))
+
+ def verbose_msg (self, msg):
+ if not self.verbose:
+ return
+
+ print "[verbose] " + msg
+
+
def create_jsonrpc_v2 (self, method_name, params = {}, id = None):
msg = {}
msg["jsonrpc"] = "2.0"
@@ -36,10 +47,10 @@ class RpcClient(): return json.dumps(msg)
def invoke_rpc_method (self, method_name, params = {}, block = True):
- msg = self.create_jsonrpc_v2(method_name, params, id = 1)
+ id = random.randint(1, 1000)
+ msg = self.create_jsonrpc_v2(method_name, params, id = id)
- if self.verbose:
- print "\n[verbose] Sending Request To Server: " + str(msg) + "\n"
+ self.verbose_msg("Sending Request To Server:\n\n" + self.pretty_json(msg) + "\n")
if block:
self.socket.send(msg)
@@ -66,8 +77,7 @@ class RpcClient(): if not got_response:
return False, "Failed To Get Server Response"
- if self.verbose:
- print "[verbose] Server Response: " + str(response)
+ self.verbose_msg("Server Response:\n\n" + self.pretty_json(response) + "\n")
# decode
response_json = json.loads(response)
@@ -75,6 +85,9 @@ class RpcClient(): if (response_json.get("jsonrpc") != "2.0"):
return False, "Malfromed Response ({0})".format(str(response))
+ if (response_json.get("id") != id):
+ return False, "Server Replied With Bad ID ({0})".format(str(response))
+
# error reported by server
if ("error" in response_json):
return False, response_json["error"]["message"]
@@ -111,11 +124,11 @@ class RpcClient(): rc, err = self.ping_rpc_server()
if not rc:
self.context.destroy(linger = 0)
- raise Exception(err)
+ return False, err
#print "Connection Established !\n"
print "[SUCCESS]\n"
-
+ return True, ""
def __del__ (self):
print "Shutting down RPC client\n"
|