mirror of
https://github.com/oatpp/oatpp.git
synced 2025-03-01 17:46:02 +08:00
network: Introduce 'ConnectionProviderSwitch' (#483)
This commit is contained in:
parent
2d47208df3
commit
11e4cd1166
@ -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
|
||||
|
@ -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 */
|
||||
|
72
src/oatpp/network/ConnectionProviderSwitch.cpp
Normal file
72
src/oatpp/network/ConnectionProviderSwitch.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
|
||||
*
|
||||
* 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<ConnectionProvider>& provider)
|
||||
: m_provider(provider)
|
||||
{}
|
||||
|
||||
void ConnectionProviderSwitch::resetProvider(const std::shared_ptr<ConnectionProvider>& provider) {
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_provider = provider;
|
||||
m_properties = provider->getProperties();
|
||||
}
|
||||
|
||||
std::shared_ptr<ConnectionProvider> ConnectionProviderSwitch::getCurrentProvider() {
|
||||
|
||||
std::shared_ptr<ConnectionProvider> provider;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<data::stream::IOStream> ConnectionProviderSwitch::get() {
|
||||
return getCurrentProvider()->get();
|
||||
}
|
||||
|
||||
|
||||
oatpp::async::CoroutineStarterForResult<const provider::ResourceHandle<data::stream::IOStream>&> ConnectionProviderSwitch::getAsync() {
|
||||
return getCurrentProvider()->getAsync();
|
||||
}
|
||||
|
||||
|
||||
void ConnectionProviderSwitch::stop() {
|
||||
return getCurrentProvider()->stop();
|
||||
}
|
||||
|
||||
}}
|
85
src/oatpp/network/ConnectionProviderSwitch.hpp
Normal file
85
src/oatpp/network/ConnectionProviderSwitch.hpp
Normal file
@ -0,0 +1,85 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
|
||||
*
|
||||
* 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 <mutex>
|
||||
|
||||
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<ConnectionProvider> getCurrentProvider();
|
||||
private:
|
||||
std::shared_ptr<ConnectionProvider> m_provider;
|
||||
std::mutex m_mutex;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
ConnectionProviderSwitch() = default;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param provider
|
||||
*/
|
||||
ConnectionProviderSwitch(const std::shared_ptr<ConnectionProvider>& provider);
|
||||
|
||||
/**
|
||||
* Reset current provider.
|
||||
* @param provider
|
||||
*/
|
||||
void resetProvider(const std::shared_ptr<ConnectionProvider>& provider);
|
||||
|
||||
/**
|
||||
* Get new connection.
|
||||
* @return &id:oatpp::data::stream::IOStream;.
|
||||
*/
|
||||
provider::ResourceHandle<data::stream::IOStream> get() override;
|
||||
|
||||
/**
|
||||
* Get new connection.
|
||||
* @return &id:oatpp::data::stream::IOStream;.
|
||||
*/
|
||||
oatpp::async::CoroutineStarterForResult<const provider::ResourceHandle<data::stream::IOStream>&> getAsync() override;
|
||||
|
||||
/**
|
||||
* Stop current provider.
|
||||
*/
|
||||
void stop() override;
|
||||
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif //oatpp_network_ConnectionProviderSwitch_hpp
|
Loading…
Reference in New Issue
Block a user