Detach server-client interface on RMI
authorSangwan Kwon <sangwan.kwon@samsung.com>
Thu, 2 Jan 2020 05:57:05 +0000 (14:57 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Mon, 6 Jan 2020 10:00:56 +0000 (19:00 +0900)
This is for supporting both general service and on-demand service.

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/rmi/CMakeLists.txt
src/vist/rmi/gateway.cpp
src/vist/rmi/impl/general/client.hpp [new file with mode: 0644]
src/vist/rmi/impl/general/protocol.cpp [moved from src/vist/rmi/impl/protocol.cpp with 99% similarity]
src/vist/rmi/impl/general/protocol.hpp [moved from src/vist/rmi/impl/protocol.hpp with 95% similarity]
src/vist/rmi/impl/general/server.hpp [new file with mode: 0644]
src/vist/rmi/impl/general/tests/protocol.cpp [moved from src/vist/rmi/impl/tests/protocol.cpp with 98% similarity]
src/vist/rmi/impl/general/tests/server-client.cpp [moved from src/vist/rmi/impl/tests/server-client.cpp with 94% similarity]
src/vist/rmi/impl/server.hpp
src/vist/rmi/remote.cpp

index f0c81e0..833d53c 100644 (file)
@@ -16,7 +16,7 @@ SET(TARGET vist-rmi)
 SET(${TARGET}_SRCS gateway.cpp
                                   remote.cpp
                                   message.cpp
-                                  impl/protocol.cpp)
+                                  impl/general/protocol.cpp)
 
 ADD_LIBRARY(${TARGET} SHARED ${${TARGET}_SRCS})
 SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
@@ -41,5 +41,5 @@ TARGET_LINK_LIBRARIES(${TARGET} ${TARGET_VIST_COMMON_LIB}
 FILE(GLOB RMI_TESTS "tests/*.cpp")
 ADD_VIST_TEST(${RMI_TESTS})
 
-FILE(GLOB RMI_IMPL_TESTS "impl/tests/*.cpp")
-ADD_VIST_TEST(${RMI_IMPL_TESTS})
+FILE(GLOB RMI_GENERAL_TESTS "impl/general/tests/*.cpp")
+ADD_VIST_TEST(${RMI_GENERAL_TESTS})
index e9fbdb8..1d8bc44 100644 (file)
@@ -19,6 +19,7 @@
 #include <vist/exception.hpp>
 #include <vist/rmi/message.hpp>
 #include <vist/rmi/impl/server.hpp>
+#include <vist/rmi/impl/general/server.hpp>
 
 #include <string>
 
@@ -48,7 +49,7 @@ public:
                        return reply;
                };
 
-               this->server = std::make_unique<Server>(path, dispatcher);
+               this->server = std::make_unique<general::Server>(path, dispatcher);
        }
 
        inline void start()
@@ -62,7 +63,7 @@ public:
        }
 
 private:
-       std::unique_ptr<Server> server;
+       std::unique_ptr<impl::Server> server;
 };
 
 Gateway::Gateway(const std::string& path) : pImpl(std::make_unique<Impl>(*this, path))
diff --git a/src/vist/rmi/impl/general/client.hpp b/src/vist/rmi/impl/general/client.hpp
new file mode 100644 (file)
index 0000000..920ca86
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  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
+ */
+
+#pragma once
+
+#include "protocol.hpp"
+
+#include <vist/exception.hpp>
+#include <vist/logger.hpp>
+
+namespace vist {
+namespace rmi {
+namespace impl {
+namespace general {
+
+using boost::asio::local::stream_protocol;
+
+class Client {
+public:
+       Client(const std::string& path) : socket(this->context)
+       {
+               try {
+                       this->socket.connect(Protocol::Endpoint(path));
+               }  catch(const std::exception& e) {
+                       ERROR(VIST) << "Failed to connect socket: " << e.what();
+                       std::rethrow_exception(std::current_exception());
+               }
+       }
+
+       inline Message request(Message& message)
+       {
+               return Protocol::Request(this->socket, message);
+       }
+
+private:
+       Protocol::Context context;
+       Protocol::Socket socket;
+};
+
+} // namespace general
+} // namespace impl
+} // namespace rmi
+} // namespace vist
similarity index 99%
rename from src/vist/rmi/impl/protocol.cpp
rename to src/vist/rmi/impl/general/protocol.cpp
index 70e1cb4..c80394e 100644 (file)
@@ -22,6 +22,7 @@
 namespace vist {
 namespace rmi {
 namespace impl {
+namespace general {
 
 Message Protocol::Recv(Socket& socket)
 {
@@ -141,6 +142,7 @@ void Protocol::Async::process(const Task& task)
        boost::asio::async_write(self->socket, headerBuffer, handler);
 }
 
+} // namespace general
 } // namespace impl
 } // namespace rmi
 } // namespace vist
similarity index 95%
rename from src/vist/rmi/impl/protocol.hpp
rename to src/vist/rmi/impl/general/protocol.hpp
index eff3505..613af2c 100644 (file)
 #pragma once
 
 #include <vist/rmi/message.hpp>
-
-#include <functional>
+#include <vist/rmi/impl/server.hpp>
 
 #include <boost/asio.hpp>
 
 namespace vist {
 namespace rmi {
 namespace impl {
+namespace general {
 
 struct Protocol {
        using Acceptor = boost::asio::local::stream_protocol::acceptor;
        using Context = boost::asio::io_service;
        using Endpoint = boost::asio::local::stream_protocol::endpoint;
        using Socket = boost::asio::local::stream_protocol::socket;
-       using Task = std::function<Message(Message&)>;
 
        static Message Recv(Socket& socket);
        static void Send(Socket& socket, Message& message);
@@ -57,6 +56,7 @@ struct Protocol {
        };
 };
 
+} // namespace general
 } // namespace impl
 } // namespace rmi
 } // namespace vist
diff --git a/src/vist/rmi/impl/general/server.hpp b/src/vist/rmi/impl/general/server.hpp
new file mode 100644 (file)
index 0000000..690f9dd
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  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
+ */
+
+#pragma once
+
+#include "protocol.hpp"
+
+#include <vist/exception.hpp>
+#include <vist/logger.hpp>
+
+#include <chrono>
+#include <memory>
+#include <thread>
+
+#include <errno.h>
+#include <unistd.h>
+
+namespace vist {
+namespace rmi {
+namespace impl {
+namespace general {
+
+class Server : public impl::Server {
+public:
+       Server(const std::string& path, const Task& task) : impl::Server(path, task)
+       {
+               errno = 0;
+               if (::unlink(path.c_str()) == -1 && errno != ENOENT)
+                       THROW(ErrCode::RuntimeError) << "Failed to remove file."; 
+
+               this->acceptor = std::make_unique<Protocol::Acceptor>(this->context,
+                                                                                                                         Protocol::Endpoint(path));
+               this->accept(task);
+       }
+       virtual ~Server() = default;
+
+       Server(const Server&) = delete;
+       Server& operator=(const Server&) = delete;
+
+       Server(Server&&) = default;
+       Server& operator=(Server&&) = default;
+
+       void run() override
+       {
+               this->context.run();
+       }
+
+       void stop() override
+       {
+               this->context.stop();
+       }
+
+private:
+       void accept(const Task& task) override
+       {
+               auto asyncSession = std::make_shared<Protocol::Async>(this->context);
+               auto handler = [this, asyncSession, task](const auto& error) {
+                       DEBUG(VIST) << "New session is accepted.";
+
+                       if (error)
+                               THROW(ErrCode::RuntimeError) << error.message();
+
+                       asyncSession->dispatch(task);
+
+                       this->accept(task);
+               };
+               this->acceptor->async_accept(asyncSession->getSocket(), handler);
+       }
+
+       Protocol::Context context;
+       std::unique_ptr<Protocol::Acceptor> acceptor;
+};
+
+} // namespace general
+} // namespace impl
+} // namespace rmi
+} // namespace vist
similarity index 98%
rename from src/vist/rmi/impl/tests/protocol.cpp
rename to src/vist/rmi/impl/general/tests/protocol.cpp
index 5e03689..a67d7d5 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 #include <vist/rmi/message.hpp>
-#include <vist/rmi/impl/protocol.hpp>
+#include <vist/rmi/impl/general/protocol.hpp>
 
 #include <string>
 #include <thread>
@@ -26,7 +26,7 @@
 #include <unistd.h>
 
 using namespace vist::rmi;
-using namespace vist::rmi::impl;
+using namespace vist::rmi::impl::general;
 using boost::asio::local::stream_protocol;
 
 namespace {
@@ -15,8 +15,8 @@
  */
 
 #include <vist/rmi/message.hpp>
-#include <vist/rmi/impl/server.hpp>
-#include <vist/rmi/impl/client.hpp>
+#include <vist/rmi/impl/general/server.hpp>
+#include <vist/rmi/impl/general/client.hpp>
 
 #include <string>
 #include <thread>
@@ -26,7 +26,7 @@
 #include <gtest/gtest.h>
 
 using namespace vist::rmi;
-using namespace vist::rmi::impl;
+using namespace vist::rmi::impl::general;
 using boost::asio::local::stream_protocol;
 
 namespace {
index 098e69d..3e5d590 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
 
 #pragma once
 
-#include "protocol.hpp"
+#include <vist/rmi/message.hpp>
 
-#include <vist/exception.hpp>
-#include <vist/logger.hpp>
-
-#include <chrono>
-#include <memory>
-#include <thread>
-
-#include <errno.h>
-#include <unistd.h>
+#include <functional>
 
 namespace vist {
 namespace rmi {
 namespace impl {
 
+using Task = std::function<Message(Message&)>;
+
 class Server {
 public:
-       Server(const std::string& path, const Protocol::Task& task)
-       {
-               errno = 0;
-               if (::unlink(path.c_str()) == -1 && errno != ENOENT)
-                       THROW(ErrCode::RuntimeError) << "Failed to remove file."; 
-
-               this->acceptor = std::make_unique<Protocol::Acceptor>(this->context,
-                                                                                                                         Protocol::Endpoint(path));
-               this->accept(task);
-       }
-
-       inline void accept(const Protocol::Task& task)
-       {
-               auto asyncSession = std::make_shared<Protocol::Async>(this->context);
-               auto handler = [this, asyncSession, task](const auto& error) {
-                       DEBUG(VIST) << "New session is accepted.";
-
-                       if (error)
-                               THROW(ErrCode::RuntimeError) << error.message();
-
-                       asyncSession->dispatch(task);
+       Server(const std::string&, const Task&) {}
+       virtual ~Server() = default;
 
-                       this->accept(task);
-               };
-               this->acceptor->async_accept(asyncSession->getSocket(), handler);
-       }
+       Server(const Server&) = delete;
+       Server& operator=(const Server&) = delete;
 
-       inline void run()
-       {
-               this->context.run();
-       }
+       Server(Server&&) = default;
+       Server& operator=(Server&&) = default;
 
-       inline void stop()
-       {
-               this->context.stop();
-       }
+       virtual void run() = 0;
+       virtual void stop() = 0;
 
 private:
-       Protocol::Context context;
-       std::unique_ptr<Protocol::Acceptor> acceptor;
+       virtual void accept(const Task& task) = 0;
 };
 
 } // namespace impl
index d2ff1e6..575a987 100644 (file)
@@ -16,7 +16,7 @@
 
 #include "remote.hpp"
 
-#include <vist/rmi/impl/client.hpp>
+#include <vist/rmi/impl/general/client.hpp>
 
 #include <string>
 #include <mutex>
@@ -24,7 +24,7 @@
 namespace vist {
 namespace rmi {
 
-using namespace vist::rmi::impl;
+using namespace vist::rmi::impl::general;
 
 class Remote::Impl {
 public: