summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/console/trex_console.py105
-rw-r--r--src/console/trex_rpc_client.py76
-rw-r--r--src/rpc-server/src/commands/trex_rpc_cmd_general.cpp1
3 files changed, 149 insertions, 33 deletions
diff --git a/src/console/trex_console.py b/src/console/trex_console.py
index b4048f5b..bd2af92a 100755
--- a/src/console/trex_console.py
+++ b/src/console/trex_console.py
@@ -12,7 +12,13 @@ class TrexConsole(cmd.Cmd):
def __init__(self, rpc_client):
cmd.Cmd.__init__(self)
- self.prompt = "TRex > "
+
+ rc, msg = rpc_client.connect()
+ if not rc:
+ print "\n*** " + msg
+ self.prompt = "Trex (offline) > "
+ else:
+ self.prompt = "TRex > "
self.intro = "\n-=TRex Console V1.0=-\n"
self.intro += "\nType 'help' or '?' for supported actions\n"
@@ -33,7 +39,7 @@ class TrexConsole(cmd.Cmd):
# set verbose on / off
def do_verbose (self, line):
- '''\nshows or set verbose mode\nusage: verbose [on/off]\n'''
+ '''shows or set verbose mode\n'''
if line == "":
print "\nverbose is " + ("on\n" if self.verbose else "off\n")
@@ -52,10 +58,12 @@ class TrexConsole(cmd.Cmd):
# query the server for registered commands
def do_query_server(self, line):
- '''\nquery the RPC server for supported remote commands\n'''
+ '''query the RPC server for supported remote commands\n'''
+
rc, msg = self.rpc_client.query_rpc_server()
if not rc:
- print "\n*** Failed to query RPC server: " + str(msg)
+ print "\n*** " + msg + "\n"
+ return
print "\nRPC server supports the following commands: \n\n"
for func in msg:
@@ -64,19 +72,30 @@ class TrexConsole(cmd.Cmd):
print "\n"
def do_ping (self, line):
- '''\npings the RPC server\n'''
+ '''Pings the RPC server\n'''
+
print "\n-> Pinging RPC server"
rc, msg = self.rpc_client.ping_rpc_server()
if rc:
print "[SUCCESS]\n"
else:
- print "[FAILED]\n"
+ print "\n*** " + msg + "\n"
+
+ def do_reconnect (self, line):
+ '''Reconnects to the server\n'''
+ rc, msg = self.rpc_client.reconnect()
+ if rc:
+ print "[SUCCESS]\n"
+ else:
+ print "\n*** " + msg + "\n"
def do_rpc (self, line):
- '''\nLaunches a RPC on the server\n'''
+ '''Launches a RPC on the server\n'''
+
if line == "":
print "\nUsage: [method name] [param dict as string]\n"
+ print "Example: rpc test_add {'x': 12, 'y': 17}\n"
return
sp = line.split(' ', 1)
@@ -99,41 +118,97 @@ class TrexConsole(cmd.Cmd):
if bad_parse:
print "\nValue should be a valid dict: '{0}'".format(sp[1])
print "\nUsage: [method name] [param dict as string]\n"
+ print "Example: rpc test_add {'x': 12, 'y': 17}\n"
return
rc, msg = self.rpc_client.invoke_rpc_method(method, params)
if rc:
- print "[SUCCESS]\n"
+ print "\nServer Response:\n\n" + json.dumps(msg) + "\n"
else:
- print "[FAILED]\n"
+ print "\n*** " + msg + "\n"
+ #print "Please try 'reconnect' to reconnect to server"
def complete_rpc (self, text, line, begidx, endidx):
return [x for x in self.supported_rpc if x.startswith(text)]
def do_status (self, line):
- '''\nShows a graphical console\n'''
+ '''Shows a graphical console\n'''
self.do_verbose('off')
trex_status.show_trex_status(self.rpc_client)
def do_quit(self, line):
- '''\nexit the client\n'''
+ '''exit the client\n'''
return True
+ def do_disconnect (self, line):
+ '''Disconnect from the server\n'''
+ if not self.rpc_client.is_connected():
+ print "Not connected to server\n"
+ return
+
+ rc, msg = self.rpc_client.disconnect()
+ if rc:
+ print "[SUCCESS]\n"
+ else:
+ print msg + "\n"
+
+ def postcmd(self, stop, line):
+ if self.rpc_client.is_connected():
+ self.prompt = "TRex > "
+ else:
+ self.prompt = "TRex (offline) > "
+
+ return stop
+
def default(self, line):
print "'{0}' is an unrecognized command. type 'help' or '?' for a list\n".format(line)
+ def do_help (self, line):
+ '''Shows This Help Screen\n'''
+ if line:
+ try:
+ func = getattr(self, 'help_' + line)
+ except AttributeError:
+ try:
+ doc = getattr(self, 'do_' + line).__doc__
+ if doc:
+ self.stdout.write("%s\n"%str(doc))
+ return
+ except AttributeError:
+ pass
+ self.stdout.write("%s\n"%str(self.nohelp % (line,)))
+ return
+ func()
+ return
+
+ print "\nSupported Console Commands:"
+ print "----------------------------\n"
+
+ cmds = [x[3:] for x in self.get_names() if x.startswith("do_")]
+ for cmd in cmds:
+ if cmd == "EOF":
+ continue
+
+ try:
+ doc = getattr(self, 'do_' + cmd).__doc__
+ if doc:
+ help = str(doc)
+ else:
+ help = "*** Undocumented Function ***\n"
+ except AttributeError:
+ help = "*** Undocumented Function ***\n"
+
+ print "{:<30} {:<30}".format(cmd + " - ", help)
+
+
# aliasing
do_exit = do_EOF = do_q = do_quit
def main ():
# RPC client
rpc_client = RpcClient("localhost", 5050)
- rc, msg = rpc_client.connect()
- if not rc:
- print "\n*** " + msg + "\n"
- exit(-1)
# console
try:
diff --git a/src/console/trex_rpc_client.py b/src/console/trex_rpc_client.py
index 3ec0e1f6..2ce44afb 100644
--- a/src/console/trex_rpc_client.py
+++ b/src/console/trex_rpc_client.py
@@ -6,15 +6,13 @@ import random
class RpcClient():
- def __init__ (self, server, port):
- self.context = zmq.Context()
-
- self.port = port
- self.server = server
- # Socket to talk to server
- self.transport = "tcp://{0}:{1}".format(server, port)
-
+ def __init__ (self, default_server, default_port):
self.verbose = False
+ self.connected = False
+
+ # default values
+ self.port = default_port
+ self.server = default_server
def get_connection_details (self):
rc = {}
@@ -44,7 +42,17 @@ class RpcClient():
return json.dumps(msg)
- def invoke_rpc_method (self, method_name, params = {}, block = True):
+ def invoke_rpc_method (self, method_name, params = {}, block = False):
+ rc, msg = self._invoke_rpc_method(method_name, params, block)
+ if not rc:
+ self.disconnect()
+
+ return rc, msg
+
+ def _invoke_rpc_method (self, method_name, params = {}, block = False):
+ if not self.connected:
+ return False, "Not connected to server"
+
id = random.randint(1, 1000)
msg = self.create_jsonrpc_v2(method_name, params, id = id)
@@ -56,7 +64,7 @@ class RpcClient():
try:
self.socket.send(msg, flags = zmq.NOBLOCK)
except zmq.error.ZMQError:
- return False, "Failed To Get Server Response"
+ return False, "Failed To Get Send Message"
got_response = False
@@ -64,13 +72,13 @@ class RpcClient():
response = self.socket.recv()
got_response = True
else:
- for i in xrange(0 ,5):
+ for i in xrange(0 ,10):
try:
response = self.socket.recv(flags = zmq.NOBLOCK)
got_response = True
break
except zmq.error.Again:
- sleep(0.1)
+ sleep(0.2)
if not got_response:
return False, "Failed To Get Server Response"
@@ -88,7 +96,7 @@ class RpcClient():
# error reported by server
if ("error" in response_json):
- return False, response_json["error"]["message"]
+ return True, response_json["error"]["message"]
# if no error there should be a result
if ("result" not in response_json):
@@ -111,23 +119,55 @@ class RpcClient():
def set_verbose (self, mode):
self.verbose = mode
- def connect (self):
+ def disconnect (self):
+ if self.connected:
+ self.socket.close(linger = 0)
+ self.context.destroy(linger = 0)
+ self.connected = False
+ return True, ""
+ else:
+ return False, "Not connected to server"
+
+ def connect (self, server = None, port = None):
+ if self.connected:
+ self.disconnect()
+
+ self.context = zmq.Context()
+
+ self.server = (server if server else self.server)
+ self.port = (port if port else self.port)
+
+ # 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)
self.socket = self.context.socket(zmq.REQ)
self.socket.connect(self.transport)
+ self.connected = True
+
# ping the server
rc, err = self.ping_rpc_server()
if not rc:
- self.context.destroy(linger = 0)
- return False, err
+ self.disconnect()
+ return rc, err
- #print "Connection Established !\n"
- print "[SUCCESS]\n"
return True, ""
+ def reconnect (self):
+ # connect using current values
+ return self.connect()
+
+ if not self.connected:
+ return False, "Not connected to server"
+
+ # reconnect
+ return self.connect(self.server, self.port)
+
+ def is_connected (self):
+ return self.connected
+
def __del__ (self):
print "Shutting down RPC client\n"
self.context.destroy(linger = 0)
diff --git a/src/rpc-server/src/commands/trex_rpc_cmd_general.cpp b/src/rpc-server/src/commands/trex_rpc_cmd_general.cpp
index 581f3a02..193ce8db 100644
--- a/src/rpc-server/src/commands/trex_rpc_cmd_general.cpp
+++ b/src/rpc-server/src/commands/trex_rpc_cmd_general.cpp
@@ -33,6 +33,7 @@ TrexRpcCmdGetStatus::_run(const Json::Value &params, Json::Value &result) {
/* validate count */
if (params.size() != 0) {
+ generate_err_param_count(result, 0, params.size());
return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR);
}