draft
authorimarom <[email protected]>
Mon, 17 Aug 2015 07:45:37 +0000 (10:45 +0300)
committerimarom <[email protected]>
Mon, 17 Aug 2015 07:45:37 +0000 (10:45 +0300)
src/gtest/rpc_test.cpp
src/rpc-server/include/trex_rpc_req_resp.h
src/rpc-server/include/trex_rpc_server_api.h
src/rpc-server/src/trex_rpc_req_resp.cpp
src/rpc-server/src/trex_rpc_server.cpp

index 0141dec..8cee085 100644 (file)
@@ -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;
 };
index 511afc0..f12d054 100644 (file)
@@ -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];
 };
 
 
index f9860e1..8009137 100644 (file)
@@ -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__ */
index e533c4f..2d8f12f 100644 (file)
@@ -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) {
index 5c29b6d..2e3f97f 100644 (file)
@@ -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();
+        }
+    }
+}
+