summaryrefslogtreecommitdiffstats
path: root/src/rpc-server
diff options
context:
space:
mode:
authorimarom <imarom@cisco.com>2015-08-20 10:36:53 +0300
committerimarom <imarom@cisco.com>2015-08-20 10:36:53 +0300
commitda56afe25e71f4dc65ae4669889eec5b8fc43afc (patch)
tree6f4a1782297cf43c5123ea5b0424348af3c58dca /src/rpc-server
parent2a4d1ac17610d15c65d6337306ffeda04ab29bef (diff)
draft
Diffstat (limited to 'src/rpc-server')
-rw-r--r--src/rpc-server/include/trex_rpc_server_api.h44
-rw-r--r--src/rpc-server/src/commands/trex_rpc_cmd_test.cpp8
-rw-r--r--src/rpc-server/src/trex_rpc_req_resp_server.cpp15
-rw-r--r--src/rpc-server/src/trex_rpc_server.cpp33
-rw-r--r--src/rpc-server/src/trex_rpc_server_mock.cpp3
5 files changed, 89 insertions, 14 deletions
diff --git a/src/rpc-server/include/trex_rpc_server_api.h b/src/rpc-server/include/trex_rpc_server_api.h
index bb455be2..6bb81c73 100644
--- a/src/rpc-server/include/trex_rpc_server_api.h
+++ b/src/rpc-server/include/trex_rpc_server_api.h
@@ -68,7 +68,7 @@ private:
class TrexRpcServerInterface {
public:
- TrexRpcServerInterface(const TrexRpcServerConfig &cfg);
+ TrexRpcServerInterface(const TrexRpcServerConfig &cfg, const std::string &name);
virtual ~TrexRpcServerInterface();
/**
@@ -84,22 +84,42 @@ public:
void stop();
/**
+ * set verbose on or off
+ *
+ */
+ void set_verbose(bool verbose);
+
+ /**
* return TRUE if server is active
*
*/
bool is_running();
+ /**
+ * is the server verbose or not
+ *
+ */
+ bool is_verbose();
+
protected:
/**
* instances implement this
*
*/
- virtual void _rpc_thread_cb() = 0;
- virtual void _stop_rpc_thread() = 0;
+ virtual void _rpc_thread_cb() = 0;
+ virtual void _stop_rpc_thread() = 0;
+
+ /**
+ * prints a verbosed message (if enabled)
+ *
+ */
+ void verbose_msg(const std::string &msg);
TrexRpcServerConfig m_cfg;
bool m_is_running;
+ bool m_is_verbose;
std::thread *m_thread;
+ std::string m_name;
};
/**
@@ -116,16 +136,30 @@ public:
TrexRpcServer(const TrexRpcServerConfig &req_resp_cfg);
~TrexRpcServer();
+ /**
+ * starts the RPC server
+ *
+ * @author imarom (19-Aug-15)
+ */
void start();
+
+ /**
+ * stops the RPC server
+ *
+ * @author imarom (19-Aug-15)
+ */
void stop();
+ void set_verbose(bool verbose);
+
static const std::string &get_server_uptime() {
return s_server_uptime;
}
private:
- std::vector<TrexRpcServerInterface *> m_servers;
- static const std::string s_server_uptime;
+ std::vector<TrexRpcServerInterface *> m_servers;
+ bool m_verbose;
+ static const std::string s_server_uptime;
};
#endif /* __TREX_RPC_SERVER_API_H__ */
diff --git a/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp b/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp
index f2d4121e..e9cc4665 100644
--- a/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp
+++ b/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp
@@ -95,7 +95,6 @@ TrexRpcCmdPing::_run(const Json::Value &params, Json::Value &result) {
TrexRpcCommand::rpc_cmd_rc_e
TrexRpcCmdGetReg::_run(const Json::Value &params, Json::Value &result) {
vector<string> cmds;
- stringstream ss;
/* validate count */
if (params.size() != 0) {
@@ -104,11 +103,14 @@ TrexRpcCmdGetReg::_run(const Json::Value &params, Json::Value &result) {
TrexRpcCommandsTable::get_instance().query(cmds);
+
+ Json::Value test = Json::arrayValue;
for (auto cmd : cmds) {
- ss << cmd << "\n";
+ test.append(cmd);
}
- result["result"] = ss.str();
+ result["result"] = test;
+
return (RPC_CMD_OK);
}
diff --git a/src/rpc-server/src/trex_rpc_req_resp_server.cpp b/src/rpc-server/src/trex_rpc_req_resp_server.cpp
index e40f5554..7484758d 100644
--- a/src/rpc-server/src/trex_rpc_req_resp_server.cpp
+++ b/src/rpc-server/src/trex_rpc_req_resp_server.cpp
@@ -34,7 +34,7 @@ limitations under the License.
* ZMQ based request-response server
*
*/
-TrexRpcServerReqRes::TrexRpcServerReqRes(const TrexRpcServerConfig &cfg) : TrexRpcServerInterface(cfg) {
+TrexRpcServerReqRes::TrexRpcServerReqRes(const TrexRpcServerConfig &cfg) : TrexRpcServerInterface(cfg, "req resp") {
/* ZMQ is not thread safe - this should be outside */
m_context = zmq_ctx_new();
}
@@ -84,6 +84,9 @@ void TrexRpcServerReqRes::_rpc_thread_cb() {
/* transform it to a string */
std::string request((const char *)m_msg_buffer, msg_size);
+
+ verbose_msg("Server Received: " + request);
+
handle_request(request);
}
@@ -128,14 +131,16 @@ void TrexRpcServerReqRes::handle_request(const std::string &request) {
}
/* write the JSON to string and sever on ZMQ */
- std::string reponse_str;
+ std::string response_str;
if (response.size() == 1) {
- reponse_str = writer.write(response[0]);
+ response_str = writer.write(response[0]);
} else {
- reponse_str = writer.write(response);
+ response_str = writer.write(response);
}
- zmq_send(m_socket, reponse_str.c_str(), reponse_str.size(), 0);
+ verbose_msg("Server Replied: " + response_str);
+
+ zmq_send(m_socket, response_str.c_str(), response_str.size(), 0);
}
diff --git a/src/rpc-server/src/trex_rpc_server.cpp b/src/rpc-server/src/trex_rpc_server.cpp
index 139614e5..366bfc5b 100644
--- a/src/rpc-server/src/trex_rpc_server.cpp
+++ b/src/rpc-server/src/trex_rpc_server.cpp
@@ -24,11 +24,13 @@ limitations under the License.
#include <unistd.h>
#include <zmq.h>
#include <sstream>
+#include <iostream>
/************** RPC server interface ***************/
-TrexRpcServerInterface::TrexRpcServerInterface(const TrexRpcServerConfig &cfg) : m_cfg(cfg) {
+TrexRpcServerInterface::TrexRpcServerInterface(const TrexRpcServerConfig &cfg, const std::string &name) : m_cfg(cfg), m_name(name) {
m_is_running = false;
+ m_is_verbose = false;
}
TrexRpcServerInterface::~TrexRpcServerInterface() {
@@ -37,6 +39,14 @@ TrexRpcServerInterface::~TrexRpcServerInterface() {
}
}
+void TrexRpcServerInterface::verbose_msg(const std::string &msg) {
+ if (!m_is_verbose) {
+ return;
+ }
+
+ std::cout << "[verbose][" << m_name << "] " << msg << "\n";
+}
+
/**
* starts a RPC specific server
*
@@ -45,6 +55,8 @@ TrexRpcServerInterface::~TrexRpcServerInterface() {
void TrexRpcServerInterface::start() {
m_is_running = true;
+ verbose_msg("Starting RPC Server");
+
m_thread = new std::thread(&TrexRpcServerInterface::_rpc_thread_cb, this);
if (!m_thread) {
throw TrexRpcException("unable to create RPC thread");
@@ -54,14 +66,27 @@ void TrexRpcServerInterface::start() {
void TrexRpcServerInterface::stop() {
m_is_running = false;
+ verbose_msg("Attempting To Stop RPC Server");
+
/* call the dynamic type class stop */
_stop_rpc_thread();
/* hold until thread has joined */
m_thread->join();
+
+ verbose_msg("Server Stopped");
+
delete m_thread;
}
+void TrexRpcServerInterface::set_verbose(bool verbose) {
+ m_is_verbose = verbose;
+}
+
+bool TrexRpcServerInterface::is_verbose() {
+ return m_is_verbose;
+}
+
bool TrexRpcServerInterface::is_running() {
return m_is_running;
}
@@ -120,3 +145,9 @@ void TrexRpcServer::stop() {
}
}
+void TrexRpcServer::set_verbose(bool verbose) {
+ for (auto server : m_servers) {
+ server->set_verbose(verbose);
+ }
+}
+
diff --git a/src/rpc-server/src/trex_rpc_server_mock.cpp b/src/rpc-server/src/trex_rpc_server_mock.cpp
index b01fff90..fd4f051c 100644
--- a/src/rpc-server/src/trex_rpc_server_mock.cpp
+++ b/src/rpc-server/src/trex_rpc_server_mock.cpp
@@ -60,6 +60,9 @@ int main(int argc, char *argv[]) {
/* init the RPC server */
rpc.start();
+ cout << "Setting Server To Full Verbose\n\n";
+ rpc.set_verbose(true);
+
cout << "Server Started\n\n";
while (true) {