diff options
Diffstat (limited to 'src/console/trex_console.py')
-rwxr-xr-x | src/console/trex_console.py | 105 |
1 files changed, 90 insertions, 15 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: |