From a2cde7e447e5da0b5158a35b756977b2e767a996 Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Wed, 15 Jan 2020 14:06:19 +0900 Subject: [PATCH] Remove boost dependency from rmi Signed-off-by: Sangwan Kwon --- src/vist/rmi/CMakeLists.txt | 20 +-- src/vist/rmi/gateway.cpp | 14 +- src/vist/rmi/impl/client.hpp | 31 ++-- src/vist/rmi/impl/{ondemand => }/connection.cpp | 2 - src/vist/rmi/impl/{ondemand => }/connection.hpp | 4 +- src/vist/rmi/impl/{ondemand => }/eventfd.cpp | 2 - src/vist/rmi/impl/{ondemand => }/eventfd.hpp | 2 - src/vist/rmi/impl/general/client.hpp | 57 ------- src/vist/rmi/impl/general/protocol.cpp | 148 ------------------ src/vist/rmi/impl/general/protocol.hpp | 62 -------- src/vist/rmi/impl/general/server.hpp | 91 ----------- src/vist/rmi/impl/general/tests/protocol.cpp | 173 --------------------- src/vist/rmi/impl/{ondemand => }/mainloop.cpp | 2 - src/vist/rmi/impl/{ondemand => }/mainloop.hpp | 4 +- src/vist/rmi/impl/ondemand/client.hpp | 48 ------ src/vist/rmi/impl/ondemand/server.hpp | 108 ------------- src/vist/rmi/impl/server.hpp | 84 ++++++++-- src/vist/rmi/impl/{ondemand => }/socket.cpp | 2 - src/vist/rmi/impl/{ondemand => }/socket.hpp | 2 - .../rmi/impl/{ondemand => }/systemd-socket.hpp | 4 +- .../rmi/impl/{ondemand => }/tests/connection.cpp | 8 +- .../rmi/impl/{ondemand => }/tests/mainloop.cpp | 6 +- .../rmi/impl/{general => }/tests/server-client.cpp | 19 +-- src/vist/rmi/impl/{ondemand => }/tests/socket.cpp | 4 +- src/vist/rmi/remote.cpp | 17 +- src/vist/service/vistd.cpp | 12 +- 26 files changed, 132 insertions(+), 794 deletions(-) rename src/vist/rmi/impl/{ondemand => }/connection.cpp (97%) rename src/vist/rmi/impl/{ondemand => }/connection.hpp (94%) rename src/vist/rmi/impl/{ondemand => }/eventfd.cpp (97%) rename src/vist/rmi/impl/{ondemand => }/eventfd.hpp (96%) delete mode 100644 src/vist/rmi/impl/general/client.hpp delete mode 100644 src/vist/rmi/impl/general/protocol.cpp delete mode 100644 src/vist/rmi/impl/general/protocol.hpp delete mode 100644 src/vist/rmi/impl/general/server.hpp delete mode 100644 src/vist/rmi/impl/general/tests/protocol.cpp rename src/vist/rmi/impl/{ondemand => }/mainloop.cpp (98%) rename src/vist/rmi/impl/{ondemand => }/mainloop.hpp (96%) delete mode 100644 src/vist/rmi/impl/ondemand/client.hpp delete mode 100644 src/vist/rmi/impl/ondemand/server.hpp rename src/vist/rmi/impl/{ondemand => }/socket.cpp (98%) rename src/vist/rmi/impl/{ondemand => }/socket.hpp (98%) rename src/vist/rmi/impl/{ondemand => }/systemd-socket.hpp (92%) rename src/vist/rmi/impl/{ondemand => }/tests/connection.cpp (92%) rename src/vist/rmi/impl/{ondemand => }/tests/mainloop.cpp (95%) rename src/vist/rmi/impl/{general => }/tests/server-client.cpp (86%) rename src/vist/rmi/impl/{ondemand => }/tests/socket.cpp (96%) diff --git a/src/vist/rmi/CMakeLists.txt b/src/vist/rmi/CMakeLists.txt index d06d07d..329367e 100644 --- a/src/vist/rmi/CMakeLists.txt +++ b/src/vist/rmi/CMakeLists.txt @@ -1,4 +1,4 @@ -# 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. @@ -16,16 +16,14 @@ SET(TARGET vist-rmi) SET(${TARGET}_SRCS gateway.cpp remote.cpp message.cpp - impl/general/protocol.cpp - impl/ondemand/socket.cpp - impl/ondemand/eventfd.cpp - impl/ondemand/mainloop.cpp - impl/ondemand/connection.cpp) + impl/socket.cpp + impl/eventfd.cpp + impl/mainloop.cpp + impl/connection.cpp) ADD_LIBRARY(${TARGET} SHARED ${${TARGET}_SRCS}) SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") TARGET_LINK_LIBRARIES(${TARGET} ${TARGET_VIST_COMMON_LIB} - boost_system pthread) INSTALL(TARGETS ${TARGET} DESTINATION ${CMAKE_INSTALL_LIBDIR} @@ -39,14 +37,10 @@ INSTALL(TARGETS ${TARGET} ADD_LIBRARY(${TARGET}-static STATIC ${${TARGET}_SRCS}) TARGET_LINK_LIBRARIES(${TARGET} ${TARGET_VIST_COMMON_LIB} - boost_system pthread) FILE(GLOB RMI_TESTS "tests/*.cpp") ADD_VIST_TEST(${RMI_TESTS}) -FILE(GLOB RMI_GENERAL_TESTS "impl/general/tests/*.cpp") -ADD_VIST_TEST(${RMI_GENERAL_TESTS}) - -FILE(GLOB RMI_ONDEMAND_TESTS "impl/ondemand/tests/*.cpp") -ADD_VIST_TEST(${RMI_ONDEMAND_TESTS}) +FILE(GLOB RMI_IMPL_TESTS "impl/tests/*.cpp") +ADD_VIST_TEST(${RMI_IMPL_TESTS}) diff --git a/src/vist/rmi/gateway.cpp b/src/vist/rmi/gateway.cpp index 5ecc9ec..1bbb640 100644 --- a/src/vist/rmi/gateway.cpp +++ b/src/vist/rmi/gateway.cpp @@ -19,8 +19,6 @@ #include #include #include -#include -#include #include #include @@ -51,15 +49,7 @@ public: return reply; }; - switch (type) { - case ServiceType::OnDemand: - this->server = std::make_unique(path, dispatcher); - break; - case ServiceType::General: /// fall through - default: - this->server = std::make_unique(path, dispatcher); - break; - } + this->server = std::make_unique(path, dispatcher, type); } inline void start(int timeout, std::function stopper) @@ -73,7 +63,7 @@ public: } private: - std::unique_ptr server; + std::unique_ptr server; }; Gateway::Gateway(const std::string& path, ServiceType type) : diff --git a/src/vist/rmi/impl/client.hpp b/src/vist/rmi/impl/client.hpp index d673c64..e244621 100644 --- a/src/vist/rmi/impl/client.hpp +++ b/src/vist/rmi/impl/client.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-present 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. @@ -16,28 +16,31 @@ #pragma once -#include +#include +#include +#include namespace vist { namespace rmi { namespace impl { -namespace interface { class Client { public: - 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; + Client(const std::string& path) : connection(path) + { + DEBUG(VIST) << "Success to connect to : " << path + << " by fd[" << connection.getFd() << "]"; + } + + Message request(Message& message) + { + return this->connection.request(message); + } + +private: + Connection connection; }; -} // namespace interface } // namespace impl } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/impl/ondemand/connection.cpp b/src/vist/rmi/impl/connection.cpp similarity index 97% rename from src/vist/rmi/impl/ondemand/connection.cpp rename to src/vist/rmi/impl/connection.cpp index 75e346a..eeaf761 100644 --- a/src/vist/rmi/impl/ondemand/connection.cpp +++ b/src/vist/rmi/impl/connection.cpp @@ -23,7 +23,6 @@ namespace vist { namespace rmi { namespace impl { -namespace ondemand { Connection::Connection(Socket&& socket) noexcept : socket(std::move(socket)) { @@ -69,7 +68,6 @@ int Connection::getFd(void) const noexcept return this->socket.getFd(); } -} // namespace ondemand } // namespace impl } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/impl/ondemand/connection.hpp b/src/vist/rmi/impl/connection.hpp similarity index 94% rename from src/vist/rmi/impl/ondemand/connection.hpp rename to src/vist/rmi/impl/connection.hpp index 080391d..5da2651 100644 --- a/src/vist/rmi/impl/ondemand/connection.hpp +++ b/src/vist/rmi/impl/connection.hpp @@ -17,7 +17,7 @@ #pragma once #include -#include +#include #include #include @@ -25,7 +25,6 @@ namespace vist { namespace rmi { namespace impl { -namespace ondemand { class Connection { public: @@ -58,7 +57,6 @@ private: unsigned int sequence = 0; }; -} // namespace ondemand } // namespace impl } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/impl/ondemand/eventfd.cpp b/src/vist/rmi/impl/eventfd.cpp similarity index 97% rename from src/vist/rmi/impl/ondemand/eventfd.cpp rename to src/vist/rmi/impl/eventfd.cpp index e3ec163..e902369 100644 --- a/src/vist/rmi/impl/ondemand/eventfd.cpp +++ b/src/vist/rmi/impl/eventfd.cpp @@ -27,7 +27,6 @@ namespace vist { namespace rmi { namespace impl { -namespace ondemand { EventFD::EventFD(unsigned int initval, int flags) : fd(::eventfd(initval, flags)) @@ -62,7 +61,6 @@ int EventFD::getFd(void) const noexcept return this->fd; } -} // namespace ondemand } // namespace impl } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/impl/ondemand/eventfd.hpp b/src/vist/rmi/impl/eventfd.hpp similarity index 96% rename from src/vist/rmi/impl/ondemand/eventfd.hpp rename to src/vist/rmi/impl/eventfd.hpp index c017fe3..9dc529a 100644 --- a/src/vist/rmi/impl/ondemand/eventfd.hpp +++ b/src/vist/rmi/impl/eventfd.hpp @@ -21,7 +21,6 @@ namespace vist { namespace rmi { namespace impl { -namespace ondemand { class EventFD final { public: @@ -43,7 +42,6 @@ private: int fd; }; -} // namespace ondemand } // namespace impl } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/impl/general/client.hpp b/src/vist/rmi/impl/general/client.hpp deleted file mode 100644 index 1ef64c2..0000000 --- a/src/vist/rmi/impl/general/client.hpp +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 -#include - -#include -#include - -namespace vist { -namespace rmi { -namespace impl { -namespace general { - -using boost::asio::local::stream_protocol; - -class Client : public interface::Client { -public: - Client(const std::string& path) : interface::Client(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()); - } - } - - Message request(Message& message) override - { - return Protocol::Request(this->socket, message); - } - -private: - Protocol::Context context; - Protocol::Socket socket; -}; - -} // namespace general -} // namespace impl -} // namespace rmi -} // namespace vist diff --git a/src/vist/rmi/impl/general/protocol.cpp b/src/vist/rmi/impl/general/protocol.cpp deleted file mode 100644 index 2a9d25e..0000000 --- a/src/vist/rmi/impl/general/protocol.cpp +++ /dev/null @@ -1,148 +0,0 @@ -/* - * 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 - */ - -#include "protocol.hpp" - -#include -#include - -namespace vist { -namespace rmi { -namespace impl { -namespace general { - -Message Protocol::Recv(Socket& socket) -{ - DEBUG(VIST) << "Socket read event occured."; - Message::Header header; - const auto& headerBuffer = boost::asio::buffer(&header, sizeof(Message::Header)); - auto readen = boost::asio::read(socket, headerBuffer); - if (readen != sizeof(Message::Header)) - THROW(ErrCode::ProtocolBroken) << "Failed to receive message header."; - - Message content(header); - const auto& contentBuffer = boost::asio::buffer(content.getBuffer()); - readen = boost::asio::read(socket, contentBuffer); - if (readen != content.size()) - THROW(ErrCode::ProtocolBroken) << "Failed to receive message content."; - - content.disclose(content.signature); - - return content; -} - -void Protocol::Send(Socket& socket, Message& message) -{ - DEBUG(VIST) << "Socket write event occured."; - const auto& headerBuffer = boost::asio::buffer(&message.header, - sizeof(Message::Header)); - auto written = boost::asio::write(socket, headerBuffer); - if (written != sizeof(Message::Header)) - THROW(ErrCode::ProtocolBroken) << "Failed to send message header."; - - const auto& contentBuffer = boost::asio::buffer(message.getBuffer(), message.size()); - written = boost::asio::write(socket, contentBuffer); - if (written != message.size()) - THROW(ErrCode::ProtocolBroken) << "Failed to send message content."; -} - -Message Protocol::Request(Socket& socket, Message& message) -{ - Protocol::Send(socket, message); - return Protocol::Recv(socket); -} - -void Protocol::Async::dispatch(const interface::Task& task) -{ - auto self = shared_from_this(); - const auto& header = boost::asio::buffer(&this->message.header, - sizeof(Message::Header)); - auto handler = [self, task](const auto& error, std::size_t size) { - if (error) { - if (error == boost::asio::error::eof) { - DEBUG(VIST) << "Socket EoF event occured."; - return; - } else { - THROW(ErrCode::RuntimeError) << error.message(); - } - } - - if (size != sizeof(Message::Header)) - THROW(ErrCode::ProtocolBroken) << error.message(); - - /// Resize message buffer to revieved header length. - self->message.resize(self->message.size()); - - const auto& contentBuffer = boost::asio::buffer(self->message.getBuffer()); - auto readen = boost::asio::read(self->socket, contentBuffer); - if (readen != self->message.size()) - THROW(ErrCode::ProtocolBroken) << "Failed to receive message content." - << readen << ", " << self->message.size(); - - self->message.disclose(self->message.signature); - self->process(task); - }; - - boost::asio::async_read(self->socket, header, handler); -} - -void Protocol::Async::process(const interface::Task& task) -{ - bool raised = false; - std::string errMsg; - auto onError = [&raised, &errMsg](const std::string& message) { - ERROR(VIST) << "Failed to process task: " << message; - raised = true; - errMsg = message; - }; - - try { - /// Process dispatched task. - auto result = task(message); - this->message = result; - } catch (const vist::Exception& e) { - onError(e.what()); - } catch (const std::exception& e) { - onError(e.what()); - } - - if (raised) - this->message = Message(Message::Type::Error, errMsg); - - auto self = shared_from_this(); - const auto& headerBuffer = boost::asio::buffer(&this->message.header, - sizeof(Message::Header)); - auto handler = [self, task](const auto& error, std::size_t size) { - if (error || size != sizeof(Message::Header)) - THROW(ErrCode::ProtocolBroken) << "Failed to send message header: " - << error.message(); - - const auto& contentBuffer = boost::asio::buffer(self->message.getBuffer()); - auto written = boost::asio::write(self->socket, contentBuffer); - if (written != self->message.size()) - THROW(ErrCode::ProtocolBroken) << "Failed to send message content."; - - /// Re-dispatch for next request. - self->dispatch(task); - }; - - boost::asio::async_write(self->socket, headerBuffer, handler); -} - -} // namespace general -} // namespace impl -} // namespace rmi -} // namespace vist diff --git a/src/vist/rmi/impl/general/protocol.hpp b/src/vist/rmi/impl/general/protocol.hpp deleted file mode 100644 index 6690bf4..0000000 --- a/src/vist/rmi/impl/general/protocol.hpp +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 -#include - -#include - -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; - - static Message Recv(Socket& socket); - static void Send(Socket& socket, Message& message); - - static Message Request(Socket& socket, Message& message); - - /// Passing shared_from_this() to lambda() guarantees - /// that the lambda() always refer to a live object. - class Async : public std::enable_shared_from_this { - public: - explicit Async(Context& context) : socket(context) {} - void dispatch(const interface::Task& task); - void process(const interface::Task& task); - - inline Socket& getSocket() - { - return this->socket; - } - - private: - Message message; - Socket socket; - }; -}; - -} // 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 deleted file mode 100644 index 05d256e..0000000 --- a/src/vist/rmi/impl/general/server.hpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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 -#include - -#include - -#include -#include - -namespace vist { -namespace rmi { -namespace impl { -namespace general { - -class Server : public interface::Server { -public: - Server(const std::string& path, const interface::Task& task) : interface::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(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; - - /// boost-based service does not support timeout and stopper. - void run(int timeout = -1, interface::Stopper stopper = nullptr) override - { - (void) timeout; - (void) stopper; - this->context.run(); - } - - void stop() override - { - this->context.stop(); - } - -private: - void accept(const interface::Task& task) override - { - auto asyncSession = std::make_shared(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 acceptor; -}; - -} // namespace general -} // namespace impl -} // namespace rmi -} // namespace vist diff --git a/src/vist/rmi/impl/general/tests/protocol.cpp b/src/vist/rmi/impl/general/tests/protocol.cpp deleted file mode 100644 index a67d7d5..0000000 --- a/src/vist/rmi/impl/general/tests/protocol.cpp +++ /dev/null @@ -1,173 +0,0 @@ -/* - * 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 - */ - -#include -#include - -#include -#include - -#include -#include - -#include - -using namespace vist::rmi; -using namespace vist::rmi::impl::general; -using boost::asio::local::stream_protocol; - -namespace { - -/// Request variables -std::string requestSignature = "request signature"; -int request1 = 100; -bool request2 = true; -std::string request3 = "request argument"; - -/// Response variables -std::string responseSignature = "response signature"; -int response1 = 300; -bool response2 = false; -std::string response3 = "response argument"; - -} // anonymous namespace - -TEST(ProtocolTests, sync_server_sync_client) -{ - std::string sockPath = "vist-test.sock"; - ::unlink(sockPath.c_str()); - - /// Server configuration - boost::asio::io_service context; - stream_protocol::acceptor acceptor(context, stream_protocol::endpoint(sockPath)); - stream_protocol::socket sock(context); - - auto handler = [&](const auto& error) { - EXPECT_EQ(error, boost::system::errc::success); - - Message request = Protocol::Recv(sock); - EXPECT_EQ(request.signature, requestSignature); - - int recv1; - bool recv2; - std::string recv3; - request.disclose(recv1, recv2, recv3); - EXPECT_EQ(request1, recv1); - EXPECT_EQ(request2, recv2); - EXPECT_EQ(request3, recv3); - - Message reply(Message::Type::Reply, responseSignature); - reply.enclose(response1, response2, response3); - Protocol::Send(sock, reply); - }; - acceptor.async_accept(sock, handler); - - auto serverThread = std::thread([&]() { - context.run(); - }); - - { /// Client configuration - boost::asio::io_service context; - stream_protocol::socket sock(context); - sock.connect(stream_protocol::endpoint(sockPath)); - - Message request(Message::Type::MethodCall, requestSignature); - request.enclose(request1, request2, request3); - - auto reply = Protocol::Request(sock, request); - EXPECT_EQ(reply.signature, responseSignature); - - int recv1; - bool recv2; - std::string recv3; - reply.disclose(recv1, recv2, recv3); - EXPECT_EQ(response1, recv1); - EXPECT_EQ(response2, recv2); - EXPECT_EQ(response3, recv3); - } - - context.stop(); - - if (serverThread.joinable()) - serverThread.join(); -} - -TEST(ProtocolTests, async_server_sync_client) -{ - std::string sockPath = "vist-test.sock"; - ::unlink(sockPath.c_str()); - - /// Server configuration - boost::asio::io_service context; - stream_protocol::acceptor acceptor(context, stream_protocol::endpoint(sockPath)); - - auto async = std::make_shared(context); - auto handler = [&](const auto& error) { - EXPECT_EQ(error, boost::system::errc::success); - auto task = [&](auto& message) -> Message { - EXPECT_EQ(message.signature, requestSignature); - - int recv1; - bool recv2; - std::string recv3; - message.disclose(recv1, recv2, recv3); - EXPECT_EQ(request1, recv1); - EXPECT_EQ(request2, recv2); - EXPECT_EQ(request3, recv3); - - Message reply(Message::Type::Reply, responseSignature); - reply.enclose(response1, response2, response3); - return reply; - }; - - async->dispatch(task); - }; - acceptor.async_accept(async->getSocket(), handler); - - auto serverThread = std::thread([&]() { - context.run(); - }); - - { /// Client configuration - boost::asio::io_service context; - stream_protocol::socket sock(context); - sock.connect(stream_protocol::endpoint(sockPath)); - - Message request(Message::Type::MethodCall, requestSignature); - request.enclose(request1, request2, request3); - - auto requestClosure = [&]() { - auto reply = Protocol::Request(sock, request); - EXPECT_EQ(reply.signature, responseSignature); - - int recv1; - bool recv2; - std::string recv3; - reply.disclose(recv1, recv2, recv3); - EXPECT_EQ(response1, recv1); - EXPECT_EQ(response2, recv2); - EXPECT_EQ(response3, recv3); - }; - - for (int i = 0; i < 3; i++) - requestClosure(); - } - - context.stop(); - - if (serverThread.joinable()) - serverThread.join(); -} diff --git a/src/vist/rmi/impl/ondemand/mainloop.cpp b/src/vist/rmi/impl/mainloop.cpp similarity index 98% rename from src/vist/rmi/impl/ondemand/mainloop.cpp rename to src/vist/rmi/impl/mainloop.cpp index 613632b..da08828 100644 --- a/src/vist/rmi/impl/ondemand/mainloop.cpp +++ b/src/vist/rmi/impl/mainloop.cpp @@ -27,7 +27,6 @@ namespace vist { namespace rmi { namespace impl { -namespace ondemand { Mainloop::Mainloop() : epollFd(::epoll_create1(EPOLL_CLOEXEC)), @@ -168,7 +167,6 @@ void Mainloop::stop(void) this->wakeupSignal.send(); } -} // namespace ondemand } // namespace impl } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/impl/ondemand/mainloop.hpp b/src/vist/rmi/impl/mainloop.hpp similarity index 96% rename from src/vist/rmi/impl/ondemand/mainloop.hpp rename to src/vist/rmi/impl/mainloop.hpp index 8ce748c..67ad856 100644 --- a/src/vist/rmi/impl/ondemand/mainloop.hpp +++ b/src/vist/rmi/impl/mainloop.hpp @@ -16,7 +16,7 @@ #pragma once -#include +#include #include #include @@ -32,7 +32,6 @@ namespace vist { namespace rmi { namespace impl { -namespace ondemand { class Mainloop { public: @@ -83,7 +82,6 @@ private: std::array<::epoll_event, MAX_EVENTS> events; }; -} // namespace ondemand } // namespace impl } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/impl/ondemand/client.hpp b/src/vist/rmi/impl/ondemand/client.hpp deleted file mode 100644 index 555d3ec..0000000 --- a/src/vist/rmi/impl/ondemand/client.hpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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. - * 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 -#include -#include - -namespace vist { -namespace rmi { -namespace impl { -namespace ondemand { - -class Client : public interface::Client { -public: - Client(const std::string& path) : interface::Client(path), connection(path) - { - DEBUG(VIST) << "Success to connect to : " << path - << " by fd[" << connection.getFd() << "]"; - } - - Message request(Message& message) override - { - return this->connection.request(message); - } - -private: - Connection connection; -}; - -} // namespace ondemand -} // namespace impl -} // namespace rmi -} // namespace vist diff --git a/src/vist/rmi/impl/ondemand/server.hpp b/src/vist/rmi/impl/ondemand/server.hpp deleted file mode 100644 index 9b90015..0000000 --- a/src/vist/rmi/impl/ondemand/server.hpp +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2018-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. - * 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 -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include - -namespace vist { -namespace rmi { -namespace impl { -namespace ondemand { - -class Server : public interface::Server { -public: - Server(const std::string& path, const interface::Task& task) : - interface::Server(path, task), - socket(SystemdSocket::Create(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(int timeout = -1, interface::Stopper stopper = nullptr) override - { - this->mainloop.run(timeout, stopper); - } - - void stop(void) override - { - this->mainloop.removeHandler(this->socket.getFd()); - this->mainloop.stop(); - } - -private: - void accept(const interface::Task& task) override - { - auto handler = [this, task]() { - DEBUG(VIST) << "New session is accepted."; - - auto connection = std::make_shared(this->socket.accept()); - auto onRead = [connection, task]() { - Message request = connection->recv(); - DEBUG(VIST) << "Session header: " << request.signature; - - try { - Message reply = task(request); - connection->send(reply); - } catch (const std::exception& e) { - ERROR(VIST) << e.what(); - Message reply = Message(Message::Type::Error, e.what()); - connection->send(reply); - } - }; - - auto onClose = [this, connection]() { - DEBUG(VIST) << "Connection closed. fd: " << connection->getFd(); - this->mainloop.removeHandler(connection->getFd()); - }; - - this->mainloop.addHandler(connection->getFd(), - std::move(onRead), std::move(onClose)); - }; - - INFO(VIST) << "Ready for new connection."; - this->mainloop.addHandler(this->socket.getFd(), std::move(handler)); - } - - Socket socket; - Mainloop mainloop; -}; - -} // namespace ondemand -} // namespace impl -} // namespace rmi -} // namespace vist diff --git a/src/vist/rmi/impl/server.hpp b/src/vist/rmi/impl/server.hpp index cb56f90..e8b4604 100644 --- a/src/vist/rmi/impl/server.hpp +++ b/src/vist/rmi/impl/server.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2018-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. @@ -16,22 +16,47 @@ #pragma once -#include +#include +#include +#include +#include +#include -#include +#include +#include + +#include +#include +#include #include +#include +#include namespace vist { namespace rmi { namespace impl { -namespace interface { using Task = std::function; using Stopper = std::function; +using ServiceType = Gateway::ServiceType; class Server { public: - explicit Server(const std::string&, const Task&) {} + Server(const std::string& path, const Task& task, ServiceType type = ServiceType::General) + { + switch (type) { + case ServiceType::OnDemand: + this->socket = std::make_unique(SystemdSocket::Create(path)); + break; + case ServiceType::General: /// fall through + default: + this->socket = std::make_unique(path); + break; + } + + this->accept(task); + } + virtual ~Server() = default; Server(const Server&) = delete; @@ -40,14 +65,55 @@ public: Server(Server&&) = default; Server& operator=(Server&&) = default; - virtual void run(int timeout = -1, Stopper stopper = nullptr) = 0; - virtual void stop() = 0; + void run(int timeout = -1, Stopper stopper = nullptr) + { + this->mainloop.run(timeout, stopper); + } + + void stop(void) + { + this->mainloop.removeHandler(this->socket->getFd()); + this->mainloop.stop(); + } private: - virtual void accept(const Task& task) = 0; + void accept(const Task& task) + { + auto handler = [this, task]() { + DEBUG(VIST) << "New session is accepted."; + + auto connection = std::make_shared(this->socket->accept()); + auto onRead = [connection, task]() { + Message request = connection->recv(); + DEBUG(VIST) << "Session header: " << request.signature; + + try { + Message reply = task(request); + connection->send(reply); + } catch (const std::exception& e) { + ERROR(VIST) << e.what(); + Message reply = Message(Message::Type::Error, e.what()); + connection->send(reply); + } + }; + + auto onClose = [this, connection]() { + DEBUG(VIST) << "Connection closed. fd: " << connection->getFd(); + this->mainloop.removeHandler(connection->getFd()); + }; + + this->mainloop.addHandler(connection->getFd(), + std::move(onRead), std::move(onClose)); + }; + + INFO(VIST) << "Ready for new connection."; + this->mainloop.addHandler(this->socket->getFd(), std::move(handler)); + } + + std::unique_ptr socket; + Mainloop mainloop; }; -} // namespace interface } // namespace impl } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/impl/ondemand/socket.cpp b/src/vist/rmi/impl/socket.cpp similarity index 98% rename from src/vist/rmi/impl/ondemand/socket.cpp rename to src/vist/rmi/impl/socket.cpp index 110e3fc..d304545 100644 --- a/src/vist/rmi/impl/ondemand/socket.cpp +++ b/src/vist/rmi/impl/socket.cpp @@ -29,7 +29,6 @@ namespace vist { namespace rmi { namespace impl { -namespace ondemand { namespace { @@ -153,7 +152,6 @@ int Socket::getFd(void) const noexcept return this->fd; } -} // namespace ondemand } // namespace impl } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/impl/ondemand/socket.hpp b/src/vist/rmi/impl/socket.hpp similarity index 98% rename from src/vist/rmi/impl/ondemand/socket.hpp rename to src/vist/rmi/impl/socket.hpp index 993fe90..0fa6757 100644 --- a/src/vist/rmi/impl/ondemand/socket.hpp +++ b/src/vist/rmi/impl/socket.hpp @@ -28,7 +28,6 @@ namespace vist { namespace rmi { namespace impl { -namespace ondemand { class Socket { public: @@ -93,7 +92,6 @@ void Socket::recv(T *buffer, const std::size_t size) const } } -} // namespace ondemand } // namespace impl } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/impl/ondemand/systemd-socket.hpp b/src/vist/rmi/impl/systemd-socket.hpp similarity index 92% rename from src/vist/rmi/impl/ondemand/systemd-socket.hpp rename to src/vist/rmi/impl/systemd-socket.hpp index d952dd0..38dd2d2 100644 --- a/src/vist/rmi/impl/ondemand/systemd-socket.hpp +++ b/src/vist/rmi/impl/systemd-socket.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2020-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. @@ -26,7 +26,6 @@ namespace vist { namespace rmi { namespace impl { -namespace ondemand { class SystemdSocket { public: @@ -51,7 +50,6 @@ public: } }; -} // namespace ondemand } // namespace impl } // namespace rmi } // namespace vist diff --git a/src/vist/rmi/impl/ondemand/tests/connection.cpp b/src/vist/rmi/impl/tests/connection.cpp similarity index 92% rename from src/vist/rmi/impl/ondemand/tests/connection.cpp rename to src/vist/rmi/impl/tests/connection.cpp index f5f2297..7e79624 100644 --- a/src/vist/rmi/impl/ondemand/tests/connection.cpp +++ b/src/vist/rmi/impl/tests/connection.cpp @@ -15,9 +15,9 @@ */ #include -#include -#include -#include +#include +#include +#include #include #include @@ -25,7 +25,7 @@ #include using namespace vist::rmi; -using namespace vist::rmi::impl::ondemand; +using namespace vist::rmi::impl; TEST(ConnectionTests, socket_communication) { diff --git a/src/vist/rmi/impl/ondemand/tests/mainloop.cpp b/src/vist/rmi/impl/tests/mainloop.cpp similarity index 95% rename from src/vist/rmi/impl/ondemand/tests/mainloop.cpp rename to src/vist/rmi/impl/tests/mainloop.cpp index 435b5d1..6119ffe 100644 --- a/src/vist/rmi/impl/ondemand/tests/mainloop.cpp +++ b/src/vist/rmi/impl/tests/mainloop.cpp @@ -14,8 +14,8 @@ * limitations under the License */ -#include -#include +#include +#include #include #include @@ -24,7 +24,7 @@ #include using namespace vist::rmi; -using namespace vist::rmi::impl::ondemand; +using namespace vist::rmi::impl; TEST(MainloopTests, single) { diff --git a/src/vist/rmi/impl/general/tests/server-client.cpp b/src/vist/rmi/impl/tests/server-client.cpp similarity index 86% rename from src/vist/rmi/impl/general/tests/server-client.cpp rename to src/vist/rmi/impl/tests/server-client.cpp index eae2494..a657ed6 100644 --- a/src/vist/rmi/impl/general/tests/server-client.cpp +++ b/src/vist/rmi/impl/tests/server-client.cpp @@ -15,19 +15,16 @@ */ #include -#include -#include +#include +#include #include #include -#include - #include using namespace vist::rmi; -using namespace vist::rmi::impl::general; -using boost::asio::local::stream_protocol; +using namespace vist::rmi::impl; namespace { @@ -45,10 +42,9 @@ std::string response3 = "response argument"; } // anonymous namespace -#ifndef TIZEN -TEST(ServerClientTests, server) +TEST(ServerClientTests, not_ondemand) { - std::string sockPath = "vist-test.sock"; + std::string sockPath = "./vist-test.sock"; auto task = [&](Message& message) -> Message { EXPECT_EQ(message.signature, requestSignature); @@ -66,9 +62,9 @@ TEST(ServerClientTests, server) return reply; }; - Server server(sockPath, task); + Server server(sockPath, task); auto serverThread = std::thread([&]() { - server.run(); + server.run(); }); { /// Client configuration @@ -101,4 +97,3 @@ TEST(ServerClientTests, server) if (serverThread.joinable()) serverThread.join(); } -#endif diff --git a/src/vist/rmi/impl/ondemand/tests/socket.cpp b/src/vist/rmi/impl/tests/socket.cpp similarity index 96% rename from src/vist/rmi/impl/ondemand/tests/socket.cpp rename to src/vist/rmi/impl/tests/socket.cpp index 86e0eaa..2437e3a 100644 --- a/src/vist/rmi/impl/ondemand/tests/socket.cpp +++ b/src/vist/rmi/impl/tests/socket.cpp @@ -14,7 +14,7 @@ * limitations under the License */ -#include +#include #include #include @@ -24,7 +24,7 @@ #include -using namespace vist::rmi::impl::ondemand; +using namespace vist::rmi::impl; TEST(SocketTests, socket_read_write) { diff --git a/src/vist/rmi/remote.cpp b/src/vist/rmi/remote.cpp index 9f8923e..033adea 100644 --- a/src/vist/rmi/remote.cpp +++ b/src/vist/rmi/remote.cpp @@ -18,12 +18,6 @@ #include -#ifdef TIZEN -#include -#else -#include -#endif - #include #include #include @@ -35,22 +29,17 @@ using namespace vist::rmi::impl; class Remote::Impl { public: - explicit Impl(const std::string& path) + explicit Impl(const std::string& path) : client(path) { -#ifdef TIZEN - this->client = std::make_unique(path); -#else - this->client = std::make_unique(path); -#endif } Message request(Message& message) { - return this->client->request(message); + return this->client.request(message); } private: - std::unique_ptr client; + Client client; }; Remote::Remote(const std::string& path) : pImpl(new Impl(path)) diff --git a/src/vist/service/vistd.cpp b/src/vist/service/vistd.cpp index b078d97..4fb1566 100644 --- a/src/vist/service/vistd.cpp +++ b/src/vist/service/vistd.cpp @@ -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. @@ -39,9 +39,10 @@ void Vistd::start() { INFO(VIST) << "Vistd daemon starts."; - rmi::Gateway::ServiceType type = rmi::Gateway::ServiceType::General; #ifdef TIZEN - type = rmi::Gateway::ServiceType::OnDemand; + rmi::Gateway::ServiceType type = rmi::Gateway::ServiceType::OnDemand; +#else + rmi::Gateway::ServiceType type = rmi::Gateway::ServiceType::General; #endif rmi::Gateway gateway(SOCK_ADDR, type); @@ -49,8 +50,13 @@ void Vistd::start() auto& pm = policy::PolicyManager::Instance(); +#ifdef TIZEN /// Shutdown service if timeout is occured without activated admin gateway.start(3000, [&pm]() -> bool { return !pm.isActivated(); }); +#else + (void)pm; + gateway.start(); +#endif INFO(VIST) << "Vistd daemon stopped."; } -- 2.7.4