# -*- coding: utf-8 -*-
import cmd
import json
+import ast
from trex_rpc_client import RpcClient
import trex_status
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:
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
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
\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
*/
virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, 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;
};
/* 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);
}
/* validate count */
if (params.size() != 2) {
+ generate_err_param_count(result, 2, params.size());
return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR);
}
/* validate count */
if (params.size() != 0) {
+ generate_err_param_count(result, 0, params.size());
return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR);
}
/* validate count */
if (params.size() != 0) {
+ generate_err_param_count(result, 0, params.size());
return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR);
}
*/
class TrexRpcCmdTestAdd : public TrexRpcCommand {
public:
- TrexRpcCmdTestAdd() : TrexRpcCommand("rpc_test_add") {}
+ TrexRpcCmdTestAdd() : TrexRpcCommand("test_add") {}
protected:
virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result);
};
*/
class TrexRpcCmdTestSub : public TrexRpcCommand {
public:
- TrexRpcCmdTestSub() : TrexRpcCommand("rpc_test_sub") {} ;
+ TrexRpcCmdTestSub() : TrexRpcCommand("test_sub") {} ;
protected:
virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result);
};
*/
class TrexRpcCmdPing : public TrexRpcCommand {
public:
- TrexRpcCmdPing() : TrexRpcCommand("rpc_ping") {};
+ TrexRpcCmdPing() : TrexRpcCommand("ping") {};
protected:
virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result);
};
*/
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 ¶ms, Json::Value &result);
};
*/
class TrexRpcCmdGetStatus : public TrexRpcCommand {
public:
- TrexRpcCmdGetStatus() : TrexRpcCommand("rpc_get_status") {};
+ TrexRpcCmdGetStatus() : TrexRpcCommand("get_status") {};
protected:
virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result);
};
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;
}