Make Client to abstract class
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 6 Jan 2020 05:24:43 +0000 (14:24 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Mon, 13 Jan 2020 03:51:29 +0000 (12:51 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/rmi/gateway.cpp
src/vist/rmi/impl/client.hpp
src/vist/rmi/impl/general/client.hpp
src/vist/rmi/impl/general/protocol.cpp
src/vist/rmi/impl/general/protocol.hpp
src/vist/rmi/impl/general/server.hpp
src/vist/rmi/impl/ondemand/client.hpp
src/vist/rmi/impl/ondemand/server.hpp
src/vist/rmi/impl/server.hpp
src/vist/rmi/remote.cpp

index 1d8bc44..72768c7 100644 (file)
@@ -21,6 +21,7 @@
 #include <vist/rmi/impl/server.hpp>
 #include <vist/rmi/impl/general/server.hpp>
 
+#include <memory>
 #include <string>
 
 namespace vist {
@@ -63,7 +64,7 @@ public:
        }
 
 private:
-       std::unique_ptr<impl::Server> server;
+       std::unique_ptr<interface::Server> server;
 };
 
 Gateway::Gateway(const std::string& path) : pImpl(std::make_unique<Impl>(*this, path))
index c9186e8..d673c64 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2019-present 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/exception.hpp>
-#include <vist/logger.hpp>
+#include <string>
 
 namespace vist {
 namespace rmi {
 namespace impl {
-
-using boost::asio::local::stream_protocol;
+namespace interface {
 
 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;
+       Client(const std::string&) {}
+       virtual ~Client() = default;
+
+       Client(const Client&) = delete;
+       Client& operator=(const Client&) = delete;
+
+       Client(Client&&) = default;
+       Client& operator=(Client&&) = default;
+
+       virtual Message request(Message& message) = 0;
 };
 
+} // namespace interface
 } // namespace impl
 } // namespace rmi
 } // namespace vist
index 920ca86..1ef64c2 100644 (file)
@@ -16,7 +16,8 @@
 
 #pragma once
 
-#include "protocol.hpp"
+#include <vist/rmi/impl/client.hpp>
+#include <vist/rmi/impl/general/protocol.hpp>
 
 #include <vist/exception.hpp>
 #include <vist/logger.hpp>
@@ -28,9 +29,9 @@ namespace general {
 
 using boost::asio::local::stream_protocol;
 
-class Client {
+class Client : public interface::Client {
 public:
-       Client(const std::string& path) : socket(this->context)
+       Client(const std::string& path) : interface::Client(path), socket(this->context)
        {
                try {
                        this->socket.connect(Protocol::Endpoint(path));
@@ -40,7 +41,7 @@ public:
                }
        }
 
-       inline Message request(Message& message)
+       Message request(Message& message) override
        {
                return Protocol::Request(this->socket, message);
        }
index c80394e..2a9d25e 100644 (file)
@@ -65,7 +65,7 @@ Message Protocol::Request(Socket& socket, Message& message)
        return Protocol::Recv(socket);
 }
 
-void Protocol::Async::dispatch(const Task& task)
+void Protocol::Async::dispatch(const interface::Task& task)
 {
        auto self = shared_from_this();
        const auto& header = boost::asio::buffer(&this->message.header,
@@ -99,7 +99,7 @@ void Protocol::Async::dispatch(const Task& task)
        boost::asio::async_read(self->socket, header, handler);
 }
 
-void Protocol::Async::process(const Task& task)
+void Protocol::Async::process(const interface::Task& task)
 {
        bool raised = false;
        std::string errMsg;
index 613af2c..6690bf4 100644 (file)
@@ -42,8 +42,8 @@ struct Protocol {
        class Async : public std::enable_shared_from_this<Async> {
        public:
                explicit Async(Context& context) : socket(context) {}
-               void dispatch(const Task& task);
-               void process(const Task& task);
+               void dispatch(const interface::Task& task);
+               void process(const interface::Task& task);
 
                inline Socket& getSocket()
                {
index 690f9dd..0a6f898 100644 (file)
@@ -21,9 +21,7 @@
 #include <vist/exception.hpp>
 #include <vist/logger.hpp>
 
-#include <chrono>
 #include <memory>
-#include <thread>
 
 #include <errno.h>
 #include <unistd.h>
@@ -33,9 +31,9 @@ namespace rmi {
 namespace impl {
 namespace general {
 
-class Server : public impl::Server {
+class Server : public interface::Server {
 public:
-       Server(const std::string& path, const Task& task) : impl::Server(path, task)
+       Server(const std::string& path, const interface::Task& task) : interface::Server(path, task)
        {
                errno = 0;
                if (::unlink(path.c_str()) == -1 && errno != ENOENT)
@@ -64,7 +62,7 @@ public:
        }
 
 private:
-       void accept(const Task& task) override
+       void accept(const interface::Task& task) override
        {
                auto asyncSession = std::make_shared<Protocol::Async>(this->context);
                auto handler = [this, asyncSession, task](const auto& error) {
index eb5073b..555d3ec 100644 (file)
@@ -17,6 +17,7 @@
 #pragma once
 
 #include <vist/logger.hpp>
+#include <vist/rmi/impl/client.hpp>
 #include <vist/rmi/impl/ondemand/connection.hpp>
 
 namespace vist {
@@ -24,14 +25,15 @@ namespace rmi {
 namespace impl {
 namespace ondemand {
 
-class Client {
+class Client : public interface::Client {
 public:
-       Client(const std::string& path) : connection(path)
+       Client(const std::string& path) : interface::Client(path), connection(path)
        {
-               DEBUG(VIST) << "Success to connect to : " << path << " by fd[" << connection.getFd() << "]";
+               DEBUG(VIST) << "Success to connect to : " << path
+                                       << " by fd[" << connection.getFd() << "]";
        }
 
-       inline Message request(Message& message)
+       Message request(Message& message) override
        {
                return this->connection.request(message);
        }
index 20b2fba..bb8b5d9 100644 (file)
@@ -35,9 +35,11 @@ namespace rmi {
 namespace impl {
 namespace ondemand {
 
-class Server : public impl::Server {
+class Server : public interface::Server {
 public:
-       Server(const std::string& path, const Task& task) : impl::Server(path, task), socket(path)
+       Server(const std::string& path, const interface::Task& task) :
+               interface::Server(path, task),
+               socket(path)
        {
                this->accept(task);
        }
@@ -62,7 +64,7 @@ public:
        }
 
 private:
-       void accept(const Task& task) override
+       void accept(const interface::Task& task) override
        {
                auto handler = [this, task]() {
                        DEBUG(VIST) << "New session is accepted.";
index 3e5d590..948d0c0 100644 (file)
 #include <vist/rmi/message.hpp>
 
 #include <functional>
+#include <string>
 
 namespace vist {
 namespace rmi {
 namespace impl {
+namespace interface {
 
 using Task = std::function<Message(Message&)>;
 
 class Server {
 public:
-       Server(const std::string&, const Task&) {}
+       explicit Server(const std::string&, const Task&) {}
        virtual ~Server() = default;
 
        Server(const Server&) = delete;
@@ -44,6 +46,7 @@ private:
        virtual void accept(const Task& task) = 0;
 };
 
+} // namespace interface
 } // namespace impl
 } // namespace rmi
 } // namespace vist
index 575a987..8d3b722 100644 (file)
 
 #include "remote.hpp"
 
+#include <vist/rmi/impl/client.hpp>
 #include <vist/rmi/impl/general/client.hpp>
 
-#include <string>
+#include <memory>
 #include <mutex>
+#include <string>
 
 namespace vist {
 namespace rmi {
 
-using namespace vist::rmi::impl::general;
+using namespace vist::rmi::impl;
 
 class Remote::Impl {
 public:
-       explicit Impl(const std::string& path) : client(path)
+       explicit Impl(const std::string& path) : client(std::make_unique<general::Client>(path))
        {
        }
 
        Message request(Message& message)
        {
-               return this->client.request(message);
+               return this->client->request(message);
        }
 
 private:
-       Client client;
+       std::unique_ptr<interface::Client> client;
 };
 
 Remote::Remote(const std::string& path) : pImpl(new Impl(path))