From 11e4cd1166f51c3af079a356b9c99a0b4cb77df5 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Wed, 20 Oct 2021 01:22:08 +0300 Subject: [PATCH] network: Introduce 'ConnectionProviderSwitch' (#483) --- src/CMakeLists.txt | 2 + src/oatpp/network/ConnectionProvider.hpp | 6 +- .../network/ConnectionProviderSwitch.cpp | 72 ++++++++++++++++ .../network/ConnectionProviderSwitch.hpp | 85 +++++++++++++++++++ 4 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 src/oatpp/network/ConnectionProviderSwitch.cpp create mode 100644 src/oatpp/network/ConnectionProviderSwitch.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ccba2cd5..a011984d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -174,6 +174,8 @@ add_library(oatpp oatpp/network/ConnectionPool.hpp oatpp/network/ConnectionProvider.cpp oatpp/network/ConnectionProvider.hpp + oatpp/network/ConnectionProviderSwitch.cpp + oatpp/network/ConnectionProviderSwitch.hpp oatpp/network/Server.cpp oatpp/network/Server.hpp oatpp/network/Url.cpp diff --git a/src/oatpp/network/ConnectionProvider.hpp b/src/oatpp/network/ConnectionProvider.hpp index 8df1b113..9bf59fb7 100644 --- a/src/oatpp/network/ConnectionProvider.hpp +++ b/src/oatpp/network/ConnectionProvider.hpp @@ -22,8 +22,8 @@ * ***************************************************************************/ -#ifndef oatpp_netword_ConnectionsProvider_hpp -#define oatpp_netword_ConnectionsProvider_hpp +#ifndef oatpp_network_ConnectionProvider_hpp +#define oatpp_network_ConnectionProvider_hpp #include "oatpp/core/data/share/MemoryLabel.hpp" #include "oatpp/core/data/stream/Stream.hpp" @@ -68,4 +68,4 @@ class ClientConnectionProvider : virtual public ConnectionProvider { }} -#endif /* oatpp_netword_ConnectionsProvider_hpp */ +#endif /* oatpp_network_ConnectionProvider_hpp */ diff --git a/src/oatpp/network/ConnectionProviderSwitch.cpp b/src/oatpp/network/ConnectionProviderSwitch.cpp new file mode 100644 index 00000000..8c49bb78 --- /dev/null +++ b/src/oatpp/network/ConnectionProviderSwitch.cpp @@ -0,0 +1,72 @@ +/*************************************************************************** + * + * Project _____ __ ____ _ _ + * ( _ ) /__\ (_ _)_| |_ _| |_ + * )(_)( /(__)\ )( (_ _)(_ _) + * (_____)(__)(__)(__) |_| |_| + * + * + * Copyright 2018-present, Leonid Stryzhevskyi + * + * 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 "ConnectionProviderSwitch.hpp" + +namespace oatpp { namespace network { + +ConnectionProviderSwitch::ConnectionProviderSwitch(const std::shared_ptr& provider) + : m_provider(provider) +{} + +void ConnectionProviderSwitch::resetProvider(const std::shared_ptr& provider) { + std::lock_guard lock(m_mutex); + m_provider = provider; + m_properties = provider->getProperties(); +} + +std::shared_ptr ConnectionProviderSwitch::getCurrentProvider() { + + std::shared_ptr provider; + + { + std::lock_guard lock(m_mutex); + provider = m_provider; + } + + if(!provider) { + const char* const msg = "Error. Can't provide connection. There is no provider set."; + OATPP_LOGE(TAG, msg) + throw std::runtime_error(std::string(TAG) + ": " + msg); + } + + return provider; + +} + +provider::ResourceHandle ConnectionProviderSwitch::get() { + return getCurrentProvider()->get(); +} + + +oatpp::async::CoroutineStarterForResult&> ConnectionProviderSwitch::getAsync() { + return getCurrentProvider()->getAsync(); +} + + +void ConnectionProviderSwitch::stop() { + return getCurrentProvider()->stop(); +} + +}} diff --git a/src/oatpp/network/ConnectionProviderSwitch.hpp b/src/oatpp/network/ConnectionProviderSwitch.hpp new file mode 100644 index 00000000..acc7ea4e --- /dev/null +++ b/src/oatpp/network/ConnectionProviderSwitch.hpp @@ -0,0 +1,85 @@ +/*************************************************************************** + * + * Project _____ __ ____ _ _ + * ( _ ) /__\ (_ _)_| |_ _| |_ + * )(_)( /(__)\ )( (_ _)(_ _) + * (_____)(__)(__)(__) |_| |_| + * + * + * Copyright 2018-present, Leonid Stryzhevskyi + * + * 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. + * + ***************************************************************************/ + +#ifndef oatpp_network_ConnectionProviderSwitch_hpp +#define oatpp_network_ConnectionProviderSwitch_hpp + +#include "ConnectionProvider.hpp" +#include + +namespace oatpp { namespace network { + +/** + * ConnectionProviderSwitch can be used to hot-change connection providers. + * Ex.: to hot-reload server certificate. + */ +class ConnectionProviderSwitch : public ServerConnectionProvider, public ClientConnectionProvider { +private: + static constexpr const char* TAG = "[oatpp::network::ConnectionProviderSwitch::getCurrentProvider()]"; +private: + std::shared_ptr getCurrentProvider(); +private: + std::shared_ptr m_provider; + std::mutex m_mutex; +public: + + /** + * Default constructor. + */ + ConnectionProviderSwitch() = default; + + /** + * Constructor. + * @param provider + */ + ConnectionProviderSwitch(const std::shared_ptr& provider); + + /** + * Reset current provider. + * @param provider + */ + void resetProvider(const std::shared_ptr& provider); + + /** + * Get new connection. + * @return &id:oatpp::data::stream::IOStream;. + */ + provider::ResourceHandle get() override; + + /** + * Get new connection. + * @return &id:oatpp::data::stream::IOStream;. + */ + oatpp::async::CoroutineStarterForResult&> getAsync() override; + + /** + * Stop current provider. + */ + void stop() override; + +}; + +}} + +#endif //oatpp_network_ConnectionProviderSwitch_hpp