diff options
author | imarom <imarom@cisco.com> | 2015-08-17 11:32:59 +0300 |
---|---|---|
committer | imarom <imarom@cisco.com> | 2015-08-17 11:32:59 +0300 |
commit | 396b54ea57308890c29c1d9746f0cce32d990cf8 (patch) | |
tree | 69e09f2636538f45e513bae4595790866e56adc8 /src | |
parent | ffb38cc6153c7938f02d9eb524bf3e58f0d4336b (diff) |
draft
Diffstat (limited to 'src')
-rw-r--r-- | src/gtest/rpc_test.cpp | 12 | ||||
-rw-r--r-- | src/rpc-server/include/trex_rpc_cmd_api.h | 3 | ||||
-rw-r--r-- | src/rpc-server/include/trex_rpc_jsonrpc_v2_parser.h | 5 | ||||
-rw-r--r-- | src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp | 30 |
4 files changed, 31 insertions, 19 deletions
diff --git a/src/gtest/rpc_test.cpp b/src/gtest/rpc_test.cpp index 8cee0859..c616b7a6 100644 --- a/src/gtest/rpc_test.cpp +++ b/src/gtest/rpc_test.cpp @@ -187,8 +187,8 @@ TEST_F(RpcTest, batch_rpc_test) { {\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_sub\", \"params\": {\"x\": 22, \"y\": 17}, \"id\": \"2\"}, \ {\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_add\", \"params\": {\"x\": 22, \"y\": \"itay\"}, \"id\": \"2\"}, \ {\"foo\": \"boo\"}, \ - {\"jsonrpc\": \"2.0\", \"method\": \"foo.get\", \"params\": {\"name\": \"myself\"}, \"id\": \"5\"}, \ - {\"jsonrpc\": \"2.0\", \"method\": \"get_data\", \"id\": \"9\"} \ + {\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_sheker\", \"params\": {\"name\": \"myself\"}, \"id\": 5}, \ + {\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_add\", \"params\": {\"x\": 22, \"y\": 17} } \ ]"; resp_str = send_msg(req_str); @@ -214,5 +214,13 @@ TEST_F(RpcTest, batch_rpc_test) { // message 4 EXPECT_TRUE(response[3] == Json::Value::null); + // message 5 + EXPECT_TRUE(response[4]["jsonrpc"] == "2.0"); + EXPECT_TRUE(response[4]["id"] == 5); + EXPECT_TRUE(response[4]["error"]["code"] == -32601); + + // message 6 - no ID but a valid command + EXPECT_TRUE(response[5] == Json::Value::null); + return; } diff --git a/src/rpc-server/include/trex_rpc_cmd_api.h b/src/rpc-server/include/trex_rpc_cmd_api.h index c0416104..308e344c 100644 --- a/src/rpc-server/include/trex_rpc_cmd_api.h +++ b/src/rpc-server/include/trex_rpc_cmd_api.h @@ -40,7 +40,8 @@ public: enum rpc_cmd_rc_e { RPC_CMD_OK, RPC_CMD_PARAM_COUNT_ERR = 1, - RPC_CMD_PARAM_PARSE_ERR + RPC_CMD_PARAM_PARSE_ERR, + RPC_CMD_INTERNAL_ERR }; /** diff --git a/src/rpc-server/include/trex_rpc_jsonrpc_v2_parser.h b/src/rpc-server/include/trex_rpc_jsonrpc_v2_parser.h index 1f26b92f..3367ad6a 100644 --- a/src/rpc-server/include/trex_rpc_jsonrpc_v2_parser.h +++ b/src/rpc-server/include/trex_rpc_jsonrpc_v2_parser.h @@ -26,8 +26,6 @@ limitations under the License. #include <vector> #include <json/json.h> -class TrexRpcCommand; - /** * JSON RPC V2 parsed object * @@ -49,11 +47,8 @@ protected: /** * instance private implementation - * should provide implementation with response - * and without response value */ virtual void _execute(Json::Value &response) = 0; - virtual void _execute() = 0; Json::Value m_msg_id; bool m_respond; diff --git a/src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp b/src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp index 6fb210bd..c11c603f 100644 --- a/src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp +++ b/src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp @@ -18,6 +18,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ + #include <trex_rpc_exception_api.h> #include <trex_rpc_jsonrpc_v2_parser.h> #include <trex_rpc_cmd_api.h> @@ -28,7 +29,8 @@ limitations under the License. #include <iostream> /** - * error as described in the RFC + * error as described in the RFC + * http://www.jsonrpc.org/specification */ enum { JSONRPC_V2_ERR_PARSE = -32700, @@ -54,7 +56,8 @@ void TrexJsonRpcV2ParsedObject::execute(Json::Value &response) { response["id"] = m_msg_id; _execute(response); } else { - _execute(); + Json::Value dummy; + _execute(dummy); } } @@ -80,13 +83,13 @@ public: response["error"]["code"] = JSONRPC_V2_ERR_INVALID_PARAMS; response["error"]["message"] = "Bad paramters for method"; break; - } - } + case TrexRpcCommand::RPC_CMD_INTERNAL_ERR: + response["error"]["code"] = JSONRPC_V2_ERR_INTERNAL_ERROR; + response["error"]["message"] = "Internal Server Error"; + break; + } - virtual void _execute() { - Json::Value result; - m_cmd->run(m_params, result); } private: @@ -112,20 +115,25 @@ public: response["error"]["message"] = m_msg; } - virtual void _execute() { - /* nothing to do with no response */ - } - private: int m_code; std::string m_msg; }; +/************** JSON RPC V2 parser implementation *************/ + TrexJsonRpcV2Parser::TrexJsonRpcV2Parser(const std::string &msg) : m_msg(msg) { } +/** + * parse a batch of commands + * + * @author imarom (17-Aug-15) + * + * @param commands + */ void TrexJsonRpcV2Parser::parse(std::vector<TrexJsonRpcV2ParsedObject *> &commands) { Json::Reader reader; |