diff options
author | imarom <imarom@cisco.com> | 2015-08-16 18:06:30 +0300 |
---|---|---|
committer | imarom <imarom@cisco.com> | 2015-08-16 18:06:30 +0300 |
commit | 509b68a5cff9503ffffe2e72a2812bb10ca0380f (patch) | |
tree | 5465c8dfda3c24d25d5e0acbcc3631e815ef2f5f /src/rpc-server | |
parent | 47eeee1f5c8c2f08b06dbb02bd1400fd246e3f8b (diff) |
draft
Diffstat (limited to 'src/rpc-server')
-rw-r--r-- | src/rpc-server/include/trex_rpc_cmd_api.h | 75 | ||||
-rw-r--r-- | src/rpc-server/include/trex_rpc_cmds_table.h (renamed from src/rpc-server/include/trex_rpc_commands.h) | 46 | ||||
-rw-r--r-- | src/rpc-server/include/trex_rpc_jsonrpc_v2.h | 1 | ||||
-rw-r--r-- | src/rpc-server/src/commands/trex_rpc_cmd_test.cpp | 84 | ||||
-rw-r--r-- | src/rpc-server/src/commands/trex_rpc_cmds.h | 46 | ||||
-rw-r--r-- | src/rpc-server/src/trex_rpc_cmds_table.cpp | 50 | ||||
-rw-r--r-- | src/rpc-server/src/trex_rpc_commands.cpp | 87 | ||||
-rw-r--r-- | src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp | 13 | ||||
-rw-r--r-- | src/rpc-server/src/trex_rpc_req_resp.cpp | 1 | ||||
-rw-r--r-- | src/rpc-server/src/trex_rpc_server.cpp | 1 |
10 files changed, 270 insertions, 134 deletions
diff --git a/src/rpc-server/include/trex_rpc_cmd_api.h b/src/rpc-server/include/trex_rpc_cmd_api.h new file mode 100644 index 00000000..c0416104 --- /dev/null +++ b/src/rpc-server/include/trex_rpc_cmd_api.h @@ -0,0 +1,75 @@ +/* + Itay Marom + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. +*/ + +#ifndef __TREX_RPC_CMD_API_H__ +#define __TREX_RPC_CMD_API_H__ + +#include <string> +#include <vector> +#include <json/json.h> + +/** + * interface for RPC command + * + * @author imarom (13-Aug-15) + */ +class TrexRpcCommand { +public: + + /** + * describe different types of rc for run() + */ + enum rpc_cmd_rc_e { + RPC_CMD_OK, + RPC_CMD_PARAM_COUNT_ERR = 1, + RPC_CMD_PARAM_PARSE_ERR + }; + + /** + * method name and params + */ + TrexRpcCommand(const std::string &method_name) : m_name(method_name) { + + } + + rpc_cmd_rc_e run(const Json::Value ¶ms, Json::Value &result) { + return _run(params, result); + } + + const std::string &get_name() { + return m_name; + } + + virtual ~TrexRpcCommand() {} + +protected: + + /** + * implemented by the dervied class + * + */ + virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result) = 0; + + std::string m_name; +}; + +#endif /* __TREX_RPC_CMD_API_H__ */ + diff --git a/src/rpc-server/include/trex_rpc_commands.h b/src/rpc-server/include/trex_rpc_cmds_table.h index ef9f5eaa..3cf67669 100644 --- a/src/rpc-server/include/trex_rpc_commands.h +++ b/src/rpc-server/include/trex_rpc_cmds_table.h @@ -19,52 +19,15 @@ See the License for the specific language governing permissions and limitations under the License. */ -#ifndef __TREX_RPC_COMMANDS_H__ -#define __TREX_RPC_COMMANDS_H__ +#ifndef __TREX_RPC_CMDS_TABLE_H__ +#define __TREX_RPC_CMDS_TABLE_H__ #include <unordered_map> #include <string> #include <vector> #include <json/json.h> -/** - * interface for RPC command - * - * @author imarom (13-Aug-15) - */ -class TrexRpcCommand { -public: - - /** - * describe different types of rc for run() - */ - enum rpc_cmd_rc_e { - RPC_CMD_OK, - RPC_CMD_PARAM_COUNT_ERR = 1, - RPC_CMD_PARAM_PARSE_ERR - }; - - /** - * method name and params - */ - TrexRpcCommand(const std::string &method_name); - - rpc_cmd_rc_e run(const Json::Value ¶ms, std::string &output); - - const std::string &get_name(); - - virtual ~TrexRpcCommand() {} - -protected: - - /** - * implemented by the dervied class - * - */ - virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, std::string &output) = 0; - - std::string m_name; -}; +class TrexRpcCommand; /** * holds all the commands registered @@ -86,6 +49,7 @@ public: private: TrexRpcCommandsTable(); + ~TrexRpcCommandsTable(); /* c++ 2011 style singleton */ TrexRpcCommandsTable(TrexRpcCommandsTable const&) = delete; @@ -98,4 +62,4 @@ private: std::unordered_map<std::string, TrexRpcCommand *> m_rpc_cmd_table; }; -#endif /* __TREX_RPC_COMMANDS_H__ */ +#endif /* __TREX_RPC_CMDS_TABLE_H__ */ diff --git a/src/rpc-server/include/trex_rpc_jsonrpc_v2.h b/src/rpc-server/include/trex_rpc_jsonrpc_v2.h index 963dab7b..a9b6faa3 100644 --- a/src/rpc-server/include/trex_rpc_jsonrpc_v2.h +++ b/src/rpc-server/include/trex_rpc_jsonrpc_v2.h @@ -37,6 +37,7 @@ class TrexJsonRpcV2ParsedObject { public: TrexJsonRpcV2ParsedObject(const Json::Value &msg_id, bool force); + virtual ~TrexJsonRpcV2ParsedObject() {} /** * main function to execute the command diff --git a/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp b/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp new file mode 100644 index 00000000..913736af --- /dev/null +++ b/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp @@ -0,0 +1,84 @@ +/* + Itay Marom + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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_cmds.h" +#include <iostream> + +using namespace std; + +/** + * add command + * + */ + +TestRpcAddMethod::TestRpcAddMethod() : TrexRpcCommand("test_rpc_add") { + +} + +TrexRpcCommand::rpc_cmd_rc_e +TestRpcAddMethod::_run(const Json::Value ¶ms, Json::Value &result) { + + const Json::Value &x = params["x"]; + const Json::Value &y = params["y"]; + + /* validate count */ + if (params.size() != 2) { + return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR); + } + + /* check we have all the required paramters */ + if (!x.isInt() || !y.isInt()) { + return (TrexRpcCommand::RPC_CMD_PARAM_PARSE_ERR); + } + + result["result"] = x.asInt() + y.asInt(); + return (RPC_CMD_OK); +} + +/** + * sub command + * + * @author imarom (16-Aug-15) + */ + +TestRpcSubMethod::TestRpcSubMethod() : TrexRpcCommand("test_rpc_sub") { + +} + +TrexRpcCommand::rpc_cmd_rc_e +TestRpcSubMethod::_run(const Json::Value ¶ms, Json::Value &result) { + + const Json::Value &x = params["x"]; + const Json::Value &y = params["y"]; + + /* validate count */ + if (params.size() != 2) { + return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR); + } + + /* check we have all the required paramters */ + if (!x.isInt() || !y.isInt()) { + return (TrexRpcCommand::RPC_CMD_PARAM_PARSE_ERR); + } + + result["result"] = x.asInt() - y.asInt(); + return (RPC_CMD_OK); +} + diff --git a/src/rpc-server/src/commands/trex_rpc_cmds.h b/src/rpc-server/src/commands/trex_rpc_cmds.h new file mode 100644 index 00000000..ea6e1081 --- /dev/null +++ b/src/rpc-server/src/commands/trex_rpc_cmds.h @@ -0,0 +1,46 @@ +/* + Itay Marom + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. +*/ + +#ifndef __TREX_RPC_CMD_H__ +#define __TREX_RPC_CMD_H__ + +#include <trex_rpc_cmd_api.h> +#include <json/json.h> + +/* all the RPC commands decl. goes here */ + +/******************* test section ************/ +class TestRpcAddMethod : public TrexRpcCommand { +public: + TestRpcAddMethod(); +protected: + virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); +}; + +class TestRpcSubMethod : public TrexRpcCommand { +public: + TestRpcSubMethod(); +protected: + virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); +}; + +/**************** test section end *************/ +#endif /* __TREX_RPC_CMD_H__ */ diff --git a/src/rpc-server/src/trex_rpc_cmds_table.cpp b/src/rpc-server/src/trex_rpc_cmds_table.cpp new file mode 100644 index 00000000..4bc1ef29 --- /dev/null +++ b/src/rpc-server/src/trex_rpc_cmds_table.cpp @@ -0,0 +1,50 @@ +/* + Itay Marom + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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_cmds_table.h> +#include <iostream> + +#include "commands/trex_rpc_cmds.h" + +using namespace std; + +/************* table related methods ***********/ +TrexRpcCommandsTable::TrexRpcCommandsTable() { + /* add the test command (for gtest) */ + register_command(new TestRpcAddMethod()); + register_command(new TestRpcSubMethod()); +} + +TrexRpcCommandsTable::~TrexRpcCommandsTable() { + for (auto cmd : m_rpc_cmd_table) { + delete cmd.second; + } +} + +TrexRpcCommand * TrexRpcCommandsTable::lookup(const string &method_name) { + return m_rpc_cmd_table[method_name]; +} + + +void TrexRpcCommandsTable::register_command(TrexRpcCommand *command) { + + m_rpc_cmd_table[command->get_name()] = command; +} + diff --git a/src/rpc-server/src/trex_rpc_commands.cpp b/src/rpc-server/src/trex_rpc_commands.cpp deleted file mode 100644 index 8547384a..00000000 --- a/src/rpc-server/src/trex_rpc_commands.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - Itay Marom - Cisco Systems, Inc. -*/ - -/* -Copyright (c) 2015-2015 Cisco Systems, Inc. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -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_commands.h> -#include <iostream> -#include <sstream> - -using namespace std; - -/***************** commands **********/ -class TestRpcAddRpcMethod : public TrexRpcCommand { -public: - - TestRpcAddRpcMethod() : TrexRpcCommand("test_rpc_add") { - } - - virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, std::string &output) { - const Json::Value &x = params["x"]; - const Json::Value &y = params["y"]; - - /* validate count */ - if (params.size() != 2) { - return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR); - } - - /* check we have all the required paramters */ - if (!x.isInt() || !y.isInt()) { - return (TrexRpcCommand::RPC_CMD_PARAM_PARSE_ERR); - } - - std::stringstream ss; - ss << (x.asInt() + y.asInt()); - output = ss.str(); - - return (RPC_CMD_OK); - } -}; - -/*************** generic command methods *********/ - -TrexRpcCommand::TrexRpcCommand(const string &method_name) : m_name(method_name) { -} - -TrexRpcCommand::rpc_cmd_rc_e -TrexRpcCommand::run(const Json::Value ¶ms, std::string &output) { - return _run(params, output); -} - -const string & TrexRpcCommand::get_name() { - return m_name; -} - - -/************* table related methods ***********/ -TrexRpcCommandsTable::TrexRpcCommandsTable() { - /* add the test command (for gtest) */ - register_command(new TestRpcAddRpcMethod()); - - TrexRpcCommand *cmd = m_rpc_cmd_table["test_rpc_add"]; -} - -TrexRpcCommand * TrexRpcCommandsTable::lookup(const string &method_name) { - return m_rpc_cmd_table[method_name]; -} - - -void TrexRpcCommandsTable::register_command(TrexRpcCommand *command) { - - m_rpc_cmd_table[command->get_name()] = command; -} diff --git a/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp b/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp index 2eec599a..53811d70 100644 --- a/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp +++ b/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp @@ -20,7 +20,8 @@ limitations under the License. */ #include <trex_rpc_exception_api.h> #include <trex_rpc_jsonrpc_v2.h> -#include <trex_rpc_commands.h> +#include <trex_rpc_cmd_api.h> +#include <trex_rpc_cmds_table.h> #include <json/json.h> @@ -65,13 +66,13 @@ public: } virtual void _execute(Json::Value &response) { - std::string output; + Json::Value result; - TrexRpcCommand::rpc_cmd_rc_e rc = m_cmd->run(m_params, output); + TrexRpcCommand::rpc_cmd_rc_e rc = m_cmd->run(m_params, result); switch (rc) { case TrexRpcCommand::RPC_CMD_OK: - response["result"] = output; + response["result"] = result["result"]; break; case TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR: @@ -84,8 +85,8 @@ public: } virtual void _execute() { - std::string output; - m_cmd->run(m_params, output); + Json::Value result; + m_cmd->run(m_params, result); } private: diff --git a/src/rpc-server/src/trex_rpc_req_resp.cpp b/src/rpc-server/src/trex_rpc_req_resp.cpp index 7acd1e09..e533c4f2 100644 --- a/src/rpc-server/src/trex_rpc_req_resp.cpp +++ b/src/rpc-server/src/trex_rpc_req_resp.cpp @@ -99,6 +99,7 @@ void TrexRpcServerReqRes::handle_request(const std::string &request) { Json::Value single_response; command->execute(single_response); + delete command; response[index++] = single_response; diff --git a/src/rpc-server/src/trex_rpc_server.cpp b/src/rpc-server/src/trex_rpc_server.cpp index 3f6f5107..5c29b6d7 100644 --- a/src/rpc-server/src/trex_rpc_server.cpp +++ b/src/rpc-server/src/trex_rpc_server.cpp @@ -92,6 +92,7 @@ void TrexRpcServerInterface::stop() { /* hold until thread has joined */ m_thread->join(); + delete m_thread; } bool TrexRpcServerInterface::is_running() { |