added support for paramters in the python console
authorimarom <[email protected]>
Thu, 20 Aug 2015 10:51:31 +0000 (13:51 +0300)
committerimarom <[email protected]>
Thu, 20 Aug 2015 10:51:31 +0000 (13:51 +0300)
src/console/trex_console.py
src/console/trex_rpc_client.py
src/rpc-server/include/trex_rpc_cmd_api.h
src/rpc-server/src/commands/trex_rpc_cmd_test.cpp
src/rpc-server/src/commands/trex_rpc_cmds.h
src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp

index be84640..b4048f5 100755 (executable)
@@ -2,6 +2,7 @@
 # -*- coding: utf-8 -*- 
 import cmd
 import json
+import ast
 
 from trex_rpc_client import RpcClient
 import trex_status
@@ -75,10 +76,32 @@ class TrexConsole(cmd.Cmd):
     def do_rpc (self, line):
         '''\nLaunches a RPC on the server\n'''
         if line == "":
-            print "\nUsage: [method name] [param 1] ...\n"
+            print "\nUsage: [method name] [param dict as string]\n"
             return
 
-        rc, msg = self.rpc_client.invoke_rpc_method(line)
+        sp = line.split(' ', 1)
+        method = sp[0]
+
+        params = None
+        bad_parse = False
+        if len(sp) > 1:
+
+            try:
+                params = ast.literal_eval(sp[1])
+                if not isinstance(params, dict):
+                    bad_parse = True
+
+            except ValueError as e1:
+                bad_parse = True
+            except SyntaxError as e2:
+                bad_parse = True
+
+        if bad_parse:
+            print "\nValue should be a valid dict: '{0}'".format(sp[1])
+            print "\nUsage: [method name] [param dict as string]\n"
+            return
+
+        rc, msg = self.rpc_client.invoke_rpc_method(method, params)
         if rc:
             print "[SUCCESS]\n"
         else:
index f3edd25..3ec0e1f 100644 (file)
@@ -24,7 +24,7 @@ class RpcClient():
         return rc\r
 \r
     def pretty_json (self, json_str):\r
-        return json.dumps(json.loads(json_str), indent = 4, separators=(',', ': '))\r
+        return json.dumps(json.loads(json_str), indent = 4, separators=(',', ': '), sort_keys = True)\r
 \r
     def verbose_msg (self, msg):\r
         if not self.verbose:\r
@@ -38,9 +38,7 @@ class RpcClient():
         msg["jsonrpc"] = "2.0"\r
         msg["method"]  = method_name\r
 \r
-        msg["params"] = {}\r
-        for key, value in params.iteritems():\r
-            msg["params"][key] = value\r
+        msg["params"] = params\r
 \r
         msg["id"] = id\r
 \r
@@ -101,13 +99,13 @@ class RpcClient():
 \r
     def ping_rpc_server (self):\r
 \r
-        return self.invoke_rpc_method("rpc_ping", block = False)\r
+        return self.invoke_rpc_method("ping", block = False)\r
         \r
     def get_rpc_server_status (self):\r
-        return self.invoke_rpc_method("rpc_get_status")\r
+        return self.invoke_rpc_method("get_status")\r
 \r
     def query_rpc_server (self):\r
-        return self.invoke_rpc_method("rpc_get_reg_cmds")\r
+        return self.invoke_rpc_method("get_reg_cmds")\r
 \r
 \r
     def set_verbose (self, mode):\r
index 308e344..c773b15 100644 (file)
@@ -69,6 +69,20 @@ protected:
      */
     virtual rpc_cmd_rc_e _run(const Json::Value &params, Json::Value &result) = 0;
 
+    /**
+     * error generating functions
+     * 
+     */
+    void genernate_err(Json::Value &result, const std::string &msg) {
+        result["specific_err"] = msg;
+    }
+
+    void generate_err_param_count(Json::Value &result, int expected, int provided) {
+        std::stringstream ss;
+        ss << "method expects '" << expected << "' paramteres, '" << provided << "' provided";
+        genernate_err(result, ss.str());
+    }
+
     std::string m_name;
 };
 
index e9cc466..e2dc895 100644 (file)
@@ -37,11 +37,18 @@ TrexRpcCmdTestAdd::_run(const Json::Value &params, Json::Value &result) {
         
     /* validate count */
     if (params.size() != 2) {
+        generate_err_param_count(result, 2, params.size());
         return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR);
     }
 
     /* check we have all the required paramters */
-    if (!x.isInt() || !y.isInt()) {
+    if (!x.isInt()) {
+        genernate_err(result, "'x' is either missing or not an integer");
+        return (TrexRpcCommand::RPC_CMD_PARAM_PARSE_ERR);
+    }
+
+    if (!y.isInt()) {
+        genernate_err(result, "'y' is either missing or not an integer");
         return (TrexRpcCommand::RPC_CMD_PARAM_PARSE_ERR);
     }
 
@@ -62,6 +69,7 @@ TrexRpcCmdTestSub::_run(const Json::Value &params, Json::Value &result) {
         
     /* validate count */
     if (params.size() != 2) {
+        generate_err_param_count(result, 2, params.size());
         return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR);
     }
 
@@ -82,6 +90,7 @@ TrexRpcCmdPing::_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);
     }
 
@@ -98,6 +107,7 @@ TrexRpcCmdGetReg::_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);
     }
 
index 0778b75..e37e1cd 100644 (file)
@@ -35,7 +35,7 @@ limitations under the License.
  */
 class TrexRpcCmdTestAdd : public TrexRpcCommand {
 public:
-     TrexRpcCmdTestAdd() : TrexRpcCommand("rpc_test_add") {}
+     TrexRpcCmdTestAdd() : TrexRpcCommand("test_add") {}
 protected:
      virtual rpc_cmd_rc_e _run(const Json::Value &params, Json::Value &result);
 };
@@ -46,7 +46,7 @@ protected:
  */
 class TrexRpcCmdTestSub : public TrexRpcCommand {
 public:
-     TrexRpcCmdTestSub() : TrexRpcCommand("rpc_test_sub") {} ;
+     TrexRpcCmdTestSub() : TrexRpcCommand("test_sub") {} ;
 protected:
      virtual rpc_cmd_rc_e _run(const Json::Value &params, Json::Value &result);
 };
@@ -57,7 +57,7 @@ protected:
  */
 class TrexRpcCmdPing : public TrexRpcCommand {
 public:
-     TrexRpcCmdPing() : TrexRpcCommand("rpc_ping") {};
+     TrexRpcCmdPing() : TrexRpcCommand("ping") {};
 protected:
      virtual rpc_cmd_rc_e _run(const Json::Value &params, Json::Value &result);
 };
@@ -68,7 +68,7 @@ protected:
  */
 class TrexRpcCmdGetReg : public TrexRpcCommand {
 public:
-     TrexRpcCmdGetReg() : TrexRpcCommand("rpc_get_reg_cmds") {};
+     TrexRpcCmdGetReg() : TrexRpcCommand("get_reg_cmds") {};
 protected:
      virtual rpc_cmd_rc_e _run(const Json::Value &params, Json::Value &result);
 };
@@ -79,7 +79,7 @@ protected:
  */
 class TrexRpcCmdGetStatus : public TrexRpcCommand {
 public:
-     TrexRpcCmdGetStatus() : TrexRpcCommand("rpc_get_status") {};
+     TrexRpcCmdGetStatus() : TrexRpcCommand("get_status") {};
 protected:
      virtual rpc_cmd_rc_e _run(const Json::Value &params, Json::Value &result);
 };
index c11c603..be1eb2f 100644 (file)
@@ -80,13 +80,15 @@ public:
 
         case TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR:
         case TrexRpcCommand::RPC_CMD_PARAM_PARSE_ERR:
-            response["error"]["code"]     = JSONRPC_V2_ERR_INVALID_PARAMS;
-            response["error"]["message"]  = "Bad paramters for method";
+            response["error"]["code"]          = JSONRPC_V2_ERR_INVALID_PARAMS;
+            response["error"]["message"]       = "Bad paramters for method";
+            response["error"]["specific_err"]  = result["specific_err"];
             break;
 
         case TrexRpcCommand::RPC_CMD_INTERNAL_ERR:
-            response["error"]["code"]     = JSONRPC_V2_ERR_INTERNAL_ERROR;
-            response["error"]["message"]  = "Internal Server Error";
+            response["error"]["code"]          = JSONRPC_V2_ERR_INTERNAL_ERROR;
+            response["error"]["message"]       = "Internal Server Error";
+            response["error"]["specific_err"]  = result["specific_err"];
             break;
         }