make network::virtual_::Interface obtainable by name

This commit is contained in:
lganzzzo 2019-10-16 03:24:01 +03:00
parent c9ce09635b
commit 834c09be44
9 changed files with 85 additions and 18 deletions

View File

@ -54,8 +54,6 @@ private:
std::atomic<v_int32> m_status;
oatpp::String m_port;
std::shared_ptr<ServerConnectionProvider> m_connectionProvider;
std::shared_ptr<ConnectionHandler> m_connectionHandler;

View File

@ -25,7 +25,61 @@
#include "Interface.hpp"
namespace oatpp { namespace network { namespace virtual_ {
std::recursive_mutex Interface::m_registryMutex;
std::unordered_map<oatpp::String, std::weak_ptr<Interface>> Interface::m_registry;
void Interface::registerInterface(const std::shared_ptr<Interface>& interface) {
std::lock_guard<std::recursive_mutex> 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<std::recursive_mutex> 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> Interface::obtainShared(const oatpp::String& name) {
std::lock_guard<std::recursive_mutex> lock(m_registryMutex);
auto it = m_registry.find(name);
if(it != m_registry.end()) {
return it->second.lock();
}
std::shared_ptr<Interface> interface(new Interface(name));
registerInterface(interface);
return interface;
}
void Interface::ConnectionSubmission::setSocket(const std::shared_ptr<Socket>& socket) {
{
std::lock_guard<std::mutex> lock(m_mutex);

View File

@ -29,6 +29,8 @@
#include "oatpp/core/collection/LinkedList.hpp"
#include <unordered_map>
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<oatpp::String, std::weak_ptr<Interface>> m_registry;
private:
static void registerInterface(const std::shared_ptr<Interface>& 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<std::shared_ptr<ConnectionSubmission>> 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<Interface> createShared(const oatpp::String& name) {
return std::make_shared<Interface>(name);
}
static std::shared_ptr<Interface> obtainShared(const oatpp::String& name);
/**
* Connect to interface.

View File

@ -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;

View File

@ -66,7 +66,7 @@ public:
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, virtualInterface)([] {
return oatpp::network::virtual_::Interface::createShared("virtualhost");
return oatpp::network::virtual_::Interface::obtainShared("virtualhost");
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([this] {

View File

@ -68,7 +68,7 @@ public:
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, virtualInterface)([] {
return oatpp::network::virtual_::Interface::createShared("virtualhost");
return oatpp::network::virtual_::Interface::obtainShared("virtualhost");
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([this] {

View File

@ -66,7 +66,7 @@ public:
{}
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, virtualInterface)([] {
return oatpp::network::virtual_::Interface::createShared("virtualhost");
return oatpp::network::virtual_::Interface::obtainShared("virtualhost");
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([this] {

View File

@ -61,7 +61,7 @@ public:
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, virtualInterface)([] {
return oatpp::network::virtual_::Interface::createShared("virtualhost");
return oatpp::network::virtual_::Interface::obtainShared("virtualhost");
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([this] {

View File

@ -57,7 +57,7 @@ public:
{}
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, virtualInterface)([] {
return oatpp::network::virtual_::Interface::createShared("virtualhost");
return oatpp::network::virtual_::Interface::obtainShared("virtualhost");
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([this] {