summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gtest/rpc_test.cpp28
-rw-r--r--src/rpc-server/include/trex_rpc_commands.h81
-rw-r--r--src/rpc-server/src/trex_rpc_commands.cpp29
-rw-r--r--src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp7
4 files changed, 135 insertions, 10 deletions
diff --git a/src/gtest/rpc_test.cpp b/src/gtest/rpc_test.cpp
index f80ebd5d..d8fc5389 100644
--- a/src/gtest/rpc_test.cpp
+++ b/src/gtest/rpc_test.cpp
@@ -75,28 +75,36 @@ TEST_F(RpcTest, basic_rpc_test) {
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
- EXPECT_TRUE(response["jsonrpc"] == "2.0");
- EXPECT_TRUE(response["id"] == Json::Value::null);
- EXPECT_TRUE(response["error"]["code"] == -32700);
+ EXPECT_EQ(response["jsonrpc"], "2.0");
+ EXPECT_EQ(response["id"], Json::Value::null);
+ EXPECT_EQ(response["error"]["code"], -32700);
// check bad version
req_str = "{\"jsonrpc\": \"1.5\", \"method\": \"foobar\", \"id\": \"1\"}";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
- EXPECT_TRUE(response["jsonrpc"] == "2.0");
- EXPECT_TRUE(response["id"] == "1");
- EXPECT_TRUE(response["error"]["code"] == -32600);
+ EXPECT_EQ(response["jsonrpc"], "2.0");
+ EXPECT_EQ(response["id"], "1");
+ EXPECT_EQ(response["error"]["code"], -32600);
// no method name present
- req_str = "{\"jsonrpc\": \"1.5\", \"id\": 482}";
+ req_str = "{\"jsonrpc\": \"2.0\", \"id\": 482}";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
- EXPECT_TRUE(response["jsonrpc"] == "2.0");
- EXPECT_TRUE(response["id"] == 482);
- EXPECT_TRUE(response["error"]["code"] == -32600);
+ EXPECT_EQ(response["jsonrpc"], "2.0");
+ EXPECT_EQ(response["id"], 482);
+ EXPECT_EQ(response["error"]["code"], -32600);
+ /* method does not exist */
+ req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"jfgldjlfds\", \"id\": 482}";
+ resp_str = send_msg(req_str);
+
+ EXPECT_TRUE(reader.parse(resp_str, response, false));
+ EXPECT_EQ(response["jsonrpc"], "2.0");
+ EXPECT_EQ(response["id"], 482);
+ EXPECT_EQ(response["error"]["code"], -32601);
}
TEST_F(RpcTest, batch_rpc_test) {
diff --git a/src/rpc-server/include/trex_rpc_commands.h b/src/rpc-server/include/trex_rpc_commands.h
new file mode 100644
index 00000000..4a445dac
--- /dev/null
+++ b/src/rpc-server/include/trex_rpc_commands.h
@@ -0,0 +1,81 @@
+/*
+ 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_COMMANDS_H__
+#define __TREX_RPC_COMMANDS_H__
+
+#include <unordered_map>
+#include <string>
+#include <vector>
+
+/**
+ * interface for RPC command
+ *
+ * @author imarom (13-Aug-15)
+ */
+class TrexRpcCommand {
+public:
+ TrexRpcCommand(const std::string &method_name, const std::vector<std::string> &params);
+
+ /**
+ * implemented by the derived class
+ *
+ * @author imarom (13-Aug-15)
+ */
+ virtual void run() = 0;
+
+protected:
+ std::vector<std::string> params;
+};
+
+/**
+ * holds all the commands registered
+ *
+ * @author imarom (13-Aug-15)
+ */
+class TrexRpcCommandsTable {
+
+public:
+
+ static TrexRpcCommandsTable& get_instance() {
+ static TrexRpcCommandsTable instance;
+ return instance;
+ }
+
+ void register_command(const TrexRpcCommand &command);
+
+ TrexRpcCommand * lookup(const std::string &method_name);
+
+private:
+ TrexRpcCommandsTable();
+
+ /* c++ 2011 style singleton */
+ TrexRpcCommandsTable(TrexRpcCommandsTable const&) = delete;
+ void operator=(TrexRpcCommandsTable const&) = delete;
+
+ /**
+ * holds all the registered RPC commands
+ *
+ */
+ std::unordered_map<std::string, TrexRpcCommand *> m_rpc_cmd_table;
+};
+
+#endif /* __TREX_RPC_COMMANDS_H__ */
diff --git a/src/rpc-server/src/trex_rpc_commands.cpp b/src/rpc-server/src/trex_rpc_commands.cpp
new file mode 100644
index 00000000..d7a1646a
--- /dev/null
+++ b/src/rpc-server/src/trex_rpc_commands.cpp
@@ -0,0 +1,29 @@
+/*
+ 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>
+
+TrexRpcCommandsTable::TrexRpcCommandsTable() {
+
+}
+
+TrexRpcCommand * TrexRpcCommandsTable::lookup(const std::string &method_name) {
+ return m_rpc_cmd_table[method_name];
+}
diff --git a/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp b/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp
index f28bcbd8..0e649ac5 100644
--- a/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp
+++ b/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp
@@ -20,6 +20,7 @@ limitations under the License.
*/
#include <trex_rpc_exception_api.h>
#include <trex_rpc_jsonrpc_v2.h>
+#include <trex_rpc_commands.h>
#include <json/json.h>
@@ -131,6 +132,12 @@ void TrexJsonRpcV2Parser::parse_single_request(Json::Value &request,
return;
}
+ TrexRpcCommand * rpc_cmd = TrexRpcCommandsTable::get_instance().lookup(method_name);
+ if (!rpc_cmd) {
+ commands.push_back(new JsonRpcError(msg_id, JSONRPC_V2_ERR_METHOD_NOT_FOUND, "Method not registered"));
+ return;
+ }
+
/* TODO - add commands */
}