From 834c09be44e334cd638322d67de9ebee7050041e Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Wed, 16 Oct 2019 03:24:01 +0300 Subject: [PATCH 1/2] make network::virtual_::Interface obtainable by name --- src/oatpp/network/server/Server.hpp | 2 - src/oatpp/network/virtual_/Interface.cpp | 56 ++++++++++++++++++- src/oatpp/network/virtual_/Interface.hpp | 33 ++++++++--- test/oatpp/network/virtual_/InterfaceTest.cpp | 2 +- test/oatpp/web/FullAsyncClientTest.cpp | 2 +- test/oatpp/web/FullAsyncTest.cpp | 2 +- test/oatpp/web/FullTest.cpp | 2 +- test/oatpp/web/PipelineAsyncTest.cpp | 2 +- test/oatpp/web/PipelineTest.cpp | 2 +- 9 files changed, 85 insertions(+), 18 deletions(-) diff --git a/src/oatpp/network/server/Server.hpp b/src/oatpp/network/server/Server.hpp index 17ac0130..b9e817af 100644 --- a/src/oatpp/network/server/Server.hpp +++ b/src/oatpp/network/server/Server.hpp @@ -54,8 +54,6 @@ private: std::atomic m_status; - oatpp::String m_port; - std::shared_ptr m_connectionProvider; std::shared_ptr m_connectionHandler; diff --git a/src/oatpp/network/virtual_/Interface.cpp b/src/oatpp/network/virtual_/Interface.cpp index b606042b..778a0551 100644 --- a/src/oatpp/network/virtual_/Interface.cpp +++ b/src/oatpp/network/virtual_/Interface.cpp @@ -25,7 +25,61 @@ #include "Interface.hpp" namespace oatpp { namespace network { namespace virtual_ { - + +std::recursive_mutex Interface::m_registryMutex; +std::unordered_map> Interface::m_registry; + +void Interface::registerInterface(const std::shared_ptr& interface) { + + std::lock_guard lock(m_registryMutex); + + auto it = m_registry.find(interface->getName()); + if(it != m_registry.end()) { + throw std::runtime_error + ("[oatpp::network::virtual_::Interface::registerInterface()]: Error. Interface with such name already exists - '" + interface->getName()->std_str() + "'."); + } + + m_registry.insert({interface->getName(), interface}); + +} + +void Interface::unregisterInterface(const oatpp::String& name) { + + std::lock_guard lock(m_registryMutex); + + auto it = m_registry.find(name); + if(it == m_registry.end()) { + throw std::runtime_error + ("[oatpp::network::virtual_::Interface::unregisterInterface()]: Error. Interface NOT FOUND - '" + name->std_str() + "'."); + } + + m_registry.erase(it); + +} + +Interface::Interface(const oatpp::String& name) + : m_name(name) +{} + +Interface::~Interface() { + unregisterInterface(getName()); +} + +std::shared_ptr Interface::obtainShared(const oatpp::String& name) { + + std::lock_guard lock(m_registryMutex); + + auto it = m_registry.find(name); + if(it != m_registry.end()) { + return it->second.lock(); + } + + std::shared_ptr interface(new Interface(name)); + registerInterface(interface); + return interface; + +} + void Interface::ConnectionSubmission::setSocket(const std::shared_ptr& socket) { { std::lock_guard lock(m_mutex); diff --git a/src/oatpp/network/virtual_/Interface.hpp b/src/oatpp/network/virtual_/Interface.hpp index 9c103ee0..0ecc77c0 100644 --- a/src/oatpp/network/virtual_/Interface.hpp +++ b/src/oatpp/network/virtual_/Interface.hpp @@ -29,6 +29,8 @@ #include "oatpp/core/collection/LinkedList.hpp" +#include + namespace oatpp { namespace network { namespace virtual_ { /** @@ -36,6 +38,12 @@ namespace oatpp { namespace network { namespace virtual_ { * "virtual" connection is represented by &id:oatpp::network::virtual_::Socket;. */ class Interface : public oatpp::base::Countable { +private: + static std::recursive_mutex m_registryMutex; + static std::unordered_map> m_registry; +private: + static void registerInterface(const std::shared_ptr& interface); + static void unregisterInterface(const oatpp::String& name); public: /** @@ -88,24 +96,31 @@ private: std::mutex m_mutex; std::condition_variable m_condition; oatpp::collection::LinkedList> m_submissions; -public: +private: /** * Constructor. * @param name - interface name. */ - Interface(const oatpp::String& name) - : m_name(name) - {} + Interface(const oatpp::String& name); + + Interface(const Interface& other) = delete; + Interface(Interface&& other) = delete; + Interface& operator=(const Interface&) = delete; + Interface& operator=(Interface&&) = delete; + public: /** - * Create shared Interface. - * @param name - interface name. + * Destructor. + */ + ~Interface(); + + /** + * Obtain interface for given name. + * @param name - name of the interface. * @return - `std::shared_ptr` to Interface. */ - static std::shared_ptr createShared(const oatpp::String& name) { - return std::make_shared(name); - } + static std::shared_ptr obtainShared(const oatpp::String& name); /** * Connect to interface. diff --git a/test/oatpp/network/virtual_/InterfaceTest.cpp b/test/oatpp/network/virtual_/InterfaceTest.cpp index aaf70fbf..14483850 100644 --- a/test/oatpp/network/virtual_/InterfaceTest.cpp +++ b/test/oatpp/network/virtual_/InterfaceTest.cpp @@ -136,7 +136,7 @@ void InterfaceTest::onRun() { oatpp::String dataSample = "1234567890-=][poiuytrewqasdfghjkl;'/.,mnbvcxzzxcvbnm,./';lkjhgfdsaqwertyuiop][=-0987654321"; - auto interface = Interface::createShared("virtualhost"); + auto interface = Interface::obtainShared("virtualhost"); v_int32 numTasks = 100; ThreadList threadList; diff --git a/test/oatpp/web/FullAsyncClientTest.cpp b/test/oatpp/web/FullAsyncClientTest.cpp index b8e9efb0..193bf607 100644 --- a/test/oatpp/web/FullAsyncClientTest.cpp +++ b/test/oatpp/web/FullAsyncClientTest.cpp @@ -66,7 +66,7 @@ public: }()); OATPP_CREATE_COMPONENT(std::shared_ptr, virtualInterface)([] { - return oatpp::network::virtual_::Interface::createShared("virtualhost"); + return oatpp::network::virtual_::Interface::obtainShared("virtualhost"); }()); OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([this] { diff --git a/test/oatpp/web/FullAsyncTest.cpp b/test/oatpp/web/FullAsyncTest.cpp index d2f53bf4..4d49970e 100644 --- a/test/oatpp/web/FullAsyncTest.cpp +++ b/test/oatpp/web/FullAsyncTest.cpp @@ -68,7 +68,7 @@ public: }()); OATPP_CREATE_COMPONENT(std::shared_ptr, virtualInterface)([] { - return oatpp::network::virtual_::Interface::createShared("virtualhost"); + return oatpp::network::virtual_::Interface::obtainShared("virtualhost"); }()); OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([this] { diff --git a/test/oatpp/web/FullTest.cpp b/test/oatpp/web/FullTest.cpp index d7e6cbd3..f866acda 100644 --- a/test/oatpp/web/FullTest.cpp +++ b/test/oatpp/web/FullTest.cpp @@ -66,7 +66,7 @@ public: {} OATPP_CREATE_COMPONENT(std::shared_ptr, virtualInterface)([] { - return oatpp::network::virtual_::Interface::createShared("virtualhost"); + return oatpp::network::virtual_::Interface::obtainShared("virtualhost"); }()); OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([this] { diff --git a/test/oatpp/web/PipelineAsyncTest.cpp b/test/oatpp/web/PipelineAsyncTest.cpp index 725e327f..7150cc70 100644 --- a/test/oatpp/web/PipelineAsyncTest.cpp +++ b/test/oatpp/web/PipelineAsyncTest.cpp @@ -61,7 +61,7 @@ public: }()); OATPP_CREATE_COMPONENT(std::shared_ptr, virtualInterface)([] { - return oatpp::network::virtual_::Interface::createShared("virtualhost"); + return oatpp::network::virtual_::Interface::obtainShared("virtualhost"); }()); OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([this] { diff --git a/test/oatpp/web/PipelineTest.cpp b/test/oatpp/web/PipelineTest.cpp index cc69227c..c605ba32 100644 --- a/test/oatpp/web/PipelineTest.cpp +++ b/test/oatpp/web/PipelineTest.cpp @@ -57,7 +57,7 @@ public: {} OATPP_CREATE_COMPONENT(std::shared_ptr, virtualInterface)([] { - return oatpp::network::virtual_::Interface::createShared("virtualhost"); + return oatpp::network::virtual_::Interface::obtainShared("virtualhost"); }()); OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([this] { From c2902365e156ce2af9b43d6af2fec77ea8151e38 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Wed, 16 Oct 2019 03:28:42 +0300 Subject: [PATCH 2/2] API docs. Remove docs from virtual_::Interface private constructor --- src/oatpp/network/virtual_/Interface.hpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/oatpp/network/virtual_/Interface.hpp b/src/oatpp/network/virtual_/Interface.hpp index 0ecc77c0..ecd34886 100644 --- a/src/oatpp/network/virtual_/Interface.hpp +++ b/src/oatpp/network/virtual_/Interface.hpp @@ -97,10 +97,7 @@ private: std::condition_variable m_condition; oatpp::collection::LinkedList> m_submissions; private: - /** - * Constructor. - * @param name - interface name. - */ + Interface(const oatpp::String& name); Interface(const Interface& other) = delete;