diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gtest/rpc_test.cpp | 6 | ||||
-rw-r--r-- | src/rpc-server/include/trex_rpc_req_resp.h | 10 | ||||
-rw-r--r-- | src/rpc-server/include/trex_rpc_server_api.h | 60 | ||||
-rw-r--r-- | src/rpc-server/src/trex_rpc_req_resp.cpp | 8 | ||||
-rw-r--r-- | src/rpc-server/src/trex_rpc_server.cpp | 86 |
5 files changed, 98 insertions, 72 deletions
diff --git a/src/gtest/rpc_test.cpp b/src/gtest/rpc_test.cpp index 0141dec4..8cee0859 100644 --- a/src/gtest/rpc_test.cpp +++ b/src/gtest/rpc_test.cpp @@ -31,7 +31,9 @@ using namespace std; class RpcTest : public testing::Test { virtual void SetUp() { - m_rpc = new TrexRpcServerArray(TrexRpcServerArray::RPC_PROT_TCP, 5050); + TrexRpcServerConfig cfg = TrexRpcServerConfig(TrexRpcServerConfig::RPC_PROT_TCP, 5050); + + m_rpc = new TrexRpcServer(cfg); m_rpc->start(); m_context = zmq_ctx_new (); @@ -57,7 +59,7 @@ public: return string(buffer, len); } - TrexRpcServerArray *m_rpc; + TrexRpcServer *m_rpc; void *m_context; void *m_socket; }; diff --git a/src/rpc-server/include/trex_rpc_req_resp.h b/src/rpc-server/include/trex_rpc_req_resp.h index 511afc02..f12d0540 100644 --- a/src/rpc-server/include/trex_rpc_req_resp.h +++ b/src/rpc-server/include/trex_rpc_req_resp.h @@ -32,7 +32,7 @@ limitations under the License. class TrexRpcServerReqRes : public TrexRpcServerInterface { public: - TrexRpcServerReqRes(TrexRpcServerArray::protocol_type_e protocol, uint16_t port); + TrexRpcServerReqRes(const TrexRpcServerConfig &cfg); protected: void _rpc_thread_cb(); @@ -41,10 +41,10 @@ protected: private: void handle_request(const std::string &request); - static const int RPC_MAX_MSG_SIZE = 2048; - void *m_context; - void *m_socket; - uint8_t m_msg_buffer[RPC_MAX_MSG_SIZE]; + static const int RPC_MAX_MSG_SIZE = 2048; + void *m_context; + void *m_socket; + uint8_t m_msg_buffer[RPC_MAX_MSG_SIZE]; }; diff --git a/src/rpc-server/include/trex_rpc_server_api.h b/src/rpc-server/include/trex_rpc_server_api.h index f9860e14..8009137f 100644 --- a/src/rpc-server/include/trex_rpc_server_api.h +++ b/src/rpc-server/include/trex_rpc_server_api.h @@ -27,36 +27,37 @@ limitations under the License. #include <thread> #include <string> #include <stdexcept> - #include <trex_rpc_exception_api.h> -/* forward decl. of class */ class TrexRpcServerInterface; /** - * servers array + * defines a configuration of generic RPC server * - * @author imarom (12-Aug-15) + * @author imarom (17-Aug-15) */ -class TrexRpcServerArray { +class TrexRpcServerConfig { public: - /** - * different types the RPC server supports - */ - enum protocol_type_e { + + enum rpc_prot_e { RPC_PROT_TCP }; - TrexRpcServerArray(protocol_type_e protocol, uint16_t port); - ~TrexRpcServerArray(); + TrexRpcServerConfig(rpc_prot_e protocol, uint16_t port) : m_protocol(protocol), m_port(port) { - void start(); - void stop(); + } + + uint16_t get_port() { + return m_port; + } + + rpc_prot_e get_protocol() { + return m_protocol; + } private: - std::vector<TrexRpcServerInterface *> m_servers; - protocol_type_e m_protocol; - uint16_t m_port; + rpc_prot_e m_protocol; + uint16_t m_port; }; /** @@ -67,7 +68,7 @@ private: class TrexRpcServerInterface { public: - TrexRpcServerInterface(TrexRpcServerArray::protocol_type_e protocol, uint16_t port); + TrexRpcServerInterface(const TrexRpcServerConfig &cfg); virtual ~TrexRpcServerInterface(); /** @@ -93,13 +94,32 @@ protected: * instances implement this * */ - virtual void _rpc_thread_cb() = 0; + virtual void _rpc_thread_cb() = 0; virtual void _stop_rpc_thread() = 0; - TrexRpcServerArray::protocol_type_e m_protocol; - uint16_t m_port; + TrexRpcServerConfig m_cfg; bool m_is_running; std::thread *m_thread; }; +/** + * TREX RPC server + * may contain serveral types of RPC servers + * + * @author imarom (12-Aug-15) + */ +class TrexRpcServer { +public: + + /* currently only request response server config is required */ + TrexRpcServer(const TrexRpcServerConfig &req_resp_cfg); + ~TrexRpcServer(); + + void start(); + void stop(); + +private: + std::vector<TrexRpcServerInterface *> m_servers; +}; + #endif /* __TREX_RPC_SERVER_API_H__ */ diff --git a/src/rpc-server/src/trex_rpc_req_resp.cpp b/src/rpc-server/src/trex_rpc_req_resp.cpp index e533c4f2..2d8f12fa 100644 --- a/src/rpc-server/src/trex_rpc_req_resp.cpp +++ b/src/rpc-server/src/trex_rpc_req_resp.cpp @@ -31,7 +31,7 @@ limitations under the License. #include <json/json.h> -TrexRpcServerReqRes::TrexRpcServerReqRes(TrexRpcServerArray::protocol_type_e protocol, uint16_t port) : TrexRpcServerInterface(protocol, port) { +TrexRpcServerReqRes::TrexRpcServerReqRes(const TrexRpcServerConfig &cfg) : TrexRpcServerInterface(cfg) { /* ZMQ is not thread safe - this should be outside */ m_context = zmq_ctx_new(); } @@ -42,15 +42,15 @@ void TrexRpcServerReqRes::_rpc_thread_cb() { // Socket to talk to clients m_socket = zmq_socket (m_context, ZMQ_REP); - switch (m_protocol) { - case TrexRpcServerArray::RPC_PROT_TCP: + switch (m_cfg.get_protocol()) { + case TrexRpcServerConfig::RPC_PROT_TCP: ss << "tcp://*:"; break; default: throw TrexRpcException("unknown protocol for RPC"); } - ss << m_port; + ss << m_cfg.get_port(); int rc = zmq_bind (m_socket, ss.str().c_str()); if (rc != 0) { diff --git a/src/rpc-server/src/trex_rpc_server.cpp b/src/rpc-server/src/trex_rpc_server.cpp index 5c29b6d7..2e3f97f9 100644 --- a/src/rpc-server/src/trex_rpc_server.cpp +++ b/src/rpc-server/src/trex_rpc_server.cpp @@ -25,54 +25,17 @@ limitations under the License. #include <zmq.h> #include <sstream> -/************** RPC server array *************/ - -TrexRpcServerArray::TrexRpcServerArray(protocol_type_e protocol, uint16_t prot) { - - /* add the request response server */ - m_servers.push_back(new TrexRpcServerReqRes(protocol, prot)); -} - -TrexRpcServerArray::~TrexRpcServerArray() { - - /* make sure they are all stopped */ - TrexRpcServerArray::stop(); - - for (auto server : m_servers) { - delete server; - } -} - -/** - * start the server array - * - */ -void TrexRpcServerArray::start() { - for (auto server : m_servers) { - server->start(); - } -} - -/** - * stop the server array - * - */ -void TrexRpcServerArray::stop() { - for (auto server : m_servers) { - if (server->is_running()) { - server->stop(); - } - } -} /************** RPC server interface ***************/ -TrexRpcServerInterface::TrexRpcServerInterface(TrexRpcServerArray::protocol_type_e protocol, uint16_t port) : m_protocol(protocol), m_port(port) { +TrexRpcServerInterface::TrexRpcServerInterface(const TrexRpcServerConfig &cfg) : m_cfg(cfg) { m_is_running = false; } TrexRpcServerInterface::~TrexRpcServerInterface() { - + if (m_is_running) { + stop(); + } } void TrexRpcServerInterface::start() { @@ -99,3 +62,44 @@ bool TrexRpcServerInterface::is_running() { return m_is_running; } + +/************** RPC server *************/ + +TrexRpcServer::TrexRpcServer(const TrexRpcServerConfig &req_resp_cfg) { + + /* add the request response server */ + m_servers.push_back(new TrexRpcServerReqRes(req_resp_cfg)); +} + +TrexRpcServer::~TrexRpcServer() { + + /* make sure they are all stopped */ + stop(); + + for (auto server : m_servers) { + delete server; + } +} + +/** + * start the server array + * + */ +void TrexRpcServer::start() { + for (auto server : m_servers) { + server->start(); + } +} + +/** + * stop the server array + * + */ +void TrexRpcServer::stop() { + for (auto server : m_servers) { + if (server->is_running()) { + server->stop(); + } + } +} + |