AdcGenericNetServer, AdcDeviceNetServer, AdcDeviceNetServer::Session and

AdcDeviceNetServerASIO classes now have template parameter 'IdentT' (type
of identificator)
This commit is contained in:
Timur A. Fatkhullin 2024-11-05 18:02:28 +03:00
parent 1794de6acd
commit 4a20eecc02
6 changed files with 251 additions and 181 deletions

View File

@ -270,4 +270,11 @@ using adc_common_duration_t = adc_duration_common_type_t<std::chrono::nanosecond
std::chrono::years>;
// concept for hashable types
template <typename T>
concept adc_hashable_c = requires(T t) {
{ std::hash<T>{}(t) } -> std::convertible_to<std::size_t>;
};
} // namespace adc::traits

View File

@ -105,10 +105,12 @@ namespace adc
{
class AdcDeviceNetServer : public AdcGenericNetServer
template <typename IdentT = std::string>
class AdcDeviceNetServer : public AdcGenericNetServer<IdentT>
{
public:
using typename AdcGenericNetServer<IdentT>::server_ident_t;
// type for serialized data (attr/command ID, attr values etc...)
typedef std::vector<char> serialized_t;
@ -206,29 +208,23 @@ protected:
std::unordered_map<void*, DeviceWrapper> _devices;
public:
template <interfaces::adc_netservice_c NetServiceT>
class Session : public std::enable_shared_from_this<Session<NetServiceT>>
template <interfaces::adc_netservice_c NetServiceT, traits::adc_hashable_c SessionIdentT = std::string>
class Session : public std::enable_shared_from_this<Session<NetServiceT, SessionIdentT>>
{
public:
typedef std::string netsession_ident_t;
typedef SessionIdentT netsession_ident_t;
typedef NetServiceT netservice_t;
typedef AdcDeviceNetServer* netsession_ctx_t;
typedef std::vector<char> message_t;
template <traits::adc_input_char_range R>
Session(R&& id, netservice_t srv, AdcDeviceNetServer* srv_ptr)
: _ident(),
Session(const netsession_ident_t& id, netservice_t srv, AdcDeviceNetServer* srv_ptr)
: _ident(id),
_netService(std::move(srv)),
_serverPtr(srv_ptr),
_bindDevice(srv_ptr->_devices.size() ? &srv_ptr->_devices.begin()->second
: &AdcDeviceNetServer::nullDevice)
{
if constexpr (std::is_array_v<std::remove_cvref_t<R>>) {
_ident = id;
} else {
_ident = netsession_ident_t(id.begin(), id.end());
}
}
netsession_ident_t ident() const
@ -368,10 +364,7 @@ public:
// using AdcGenericNetServer::AdcGenericNetServer;
template <traits::adc_input_char_range R>
AdcDeviceNetServer(const R& id) : AdcGenericNetServer(id), _devices()
{
}
AdcDeviceNetServer(const server_ident_t& id) : AdcGenericNetServer<IdentT>(id), _devices() {}
virtual ~AdcDeviceNetServer() = default;

View File

@ -224,20 +224,13 @@ protected:
/* very generic network server */
template <typename IdentT = std::string>
class AdcGenericNetServer : public AdcPosixGenericDaemon, public AdcNetSessionManager
{
public:
typedef std::string server_ident_t;
typedef IdentT server_ident_t;
template <traits::adc_input_char_range R>
AdcGenericNetServer(const R& id) : _serverIdent()
{
if constexpr (std::is_array_v<std::remove_cvref_t<R>>) {
_serverIdent = id;
} else {
_serverIdent = server_ident_t{id.begin(), id.end()};
}
}
AdcGenericNetServer(const server_ident_t& id) : _serverIdent(id) {}
AdcGenericNetServer(const AdcGenericNetServer&) = delete;
@ -289,6 +282,7 @@ public:
// It must be assumed that this is asynchronous operation!!!
template <interfaces::adc_netsession_c SessionT, typename... AcceptorCtorArgTs>
void start(SessionT::netsession_ident_t id, SessionT::netsession_ctx_t sess_ctx, AcceptorCtorArgTs&&... ctor_args)
requires traits::adc_hashable_c<typename SessionT::netsession_ident_t>
{
if (!_isListening<SessionT>[this][id]) {
auto acceptor = std::make_shared<typename SessionT::netservice_t::acceptor_t>(
@ -340,7 +334,7 @@ protected:
// inline static std::unordered_map<const AdcGenericNetServer*, bool> _isListening{};
template <interfaces::adc_netsession_c SessionT>
inline static std::unordered_map<const AdcGenericNetServer*,
std::map<const typename SessionT::netsession_ident_t, bool>>
std::unordered_map<typename SessionT::netsession_ident_t, bool>>
_isListening{};
std::vector<std::function<void(const AdcGenericNetServer*)>> _stopListenFunc;
@ -367,34 +361,16 @@ protected:
};
template <interfaces::adc_netservice_c NetServiceT>
class AdcAbstractNetServer
namespace interfaces
{
public:
struct ServerEvents {
std::function<void(NetServiceT*)> onOpen;
std::function<void(NetServiceT*, std::error_code)> onClose;
std::function<void(NetServiceT*, std::span<const char>)> onData;
std::function<void(NetServiceT*, std::error_code)> onError;
};
AdcAbstractNetServer(ServerEvents&& events) : _serverEvents(std::move(events)) {}
template <typename... AcceptorCtorArgTs>
void listen(const typename NetServiceT::endpoint_t& endpoint, AcceptorCtorArgTs&&... ctor_args)
{
auto acceptor =
std::make_shared<typename NetServiceT::acceptor_t>(endpoint, std::forward<AcceptorCtorArgTs>(ctor_args)...);
}
void stop() {}
protected:
ServerEvents _serverEvents;
typename NetServiceT::acceptor_t _acceptor;
template <typename T>
concept adc_generic_netserver_c = requires {
typename T::server_ident_t;
requires std::derived_from<T, adc::AdcGenericNetServer<typename T::server_ident_t>>;
};
} // namespace interfaces
} // namespace adc

View File

@ -11,87 +11,19 @@ ABSTRACT DEVICE COMPONENTS LIBRARY
#ifdef USE_SPDLOG_LIBRARY
#include <spdlog/sinks/null_sink.h>
#include "../common/adc_spdlog.h"
#include "../common/adc_traits.h"
#include "adc_netserver.h"
namespace adc
namespace adc::spdlog
{
template <traits::adc_netserver_session_c SessionT>
class AdcNetServerSessionSpdlogDecorator : public AdcSpdlogGenericMarkDecorator<SessionT>
{
protected:
using base_t = AdcSpdlogGenericMarkDecorator<SessionT>;
std::string _sessionID;
public:
using base_t::logDebug;
using base_t::logInfo;
using base_t::logMethodCalling;
// using SessionT::sessionIdent;
using typename SessionT::session_ident_t;
static_assert(traits::formattable<session_ident_t>, "NETWORK SESSION IDENT TYPE MUST BE A FORMATTABLE ONE!!!");
template <typename... ImplCtorArgTs>
AdcNetServerSessionSpdlogDecorator(std::shared_ptr<spdlog::logger> logger, ImplCtorArgTs&&... ctor_args)
: base_t("ADC NETSERVER SESSION", logger, std::forward<ImplCtorArgTs>(ctor_args)...)
{
logMethodCalling(__PRETTY_FUNCTION__);
fmt::format_to(std::back_inserter(_sessionID), "{}", SessionT::sessionIdent());
this->setPattern(base_t::constructPattern(_sessionID));
logDebug("Creating network server session with ID = [{}] (ADDR = {})", _sessionID, this->_thisAddress);
logDebug("Use logger with ", this->loggerInfo());
}
virtual ~AdcNetServerSessionSpdlogDecorator()
{
logMethodCalling(__PRETTY_FUNCTION__);
logDebug("Deleting network server session with ID = [{}] (ADDR = {})", _sessionID, this->_thisAddress);
};
virtual session_ident_t sessionIdent() const override
{
logMethodCalling(__PRETTY_FUNCTION__);
return SessionT::sessionIdent();
};
virtual void start() override
{
logMethodCalling(__PRETTY_FUNCTION__);
logInfo("Starting network server session with ID = [{}]", _sessionID);
SessionT::start();
logInfo("Network server session [{}] is started", _sessionID);
};
virtual void stop() override
{
logMethodCalling(__PRETTY_FUNCTION__);
logInfo("Stopping network server session with ID = [{}]", _sessionID);
SessionT::stop();
logInfo("Network server session [{}] is stopped", _sessionID);
};
};
template <traits::adc_netserver_c ServerT>
class AdcNetServerSpdlogDecorator : public AdcSpdlogGenericMarkDecorator<ServerT>
template <interfaces::adc_generic_netserver_c ServerT>
requires traits::formattable<typename ServerT::server_ident_t>
class AdcGenericNetServerSpdlog : public AdcSpdlogGenericMarkDecorator<ServerT>
{
protected:
using base_t = AdcSpdlogGenericMarkDecorator<ServerT>;
@ -99,6 +31,8 @@ protected:
std::string _serverID;
public:
using typename ServerT::server_ident_t;
using base_t::logCritical;
using base_t::logDebug;
using base_t::logError;
@ -106,51 +40,44 @@ public:
using base_t::logMsg;
using base_t::logWarn;
using typename ServerT::server_ident_t;
static_assert(traits::formattable<server_ident_t>, "NETWORK SERVER IDENT TYPE MUST BE A FORMATTABLE ONE!!!");
template <typename... ImplCtorArgTs>
AdcNetServerSpdlogDecorator(std::shared_ptr<spdlog::logger> logger, ImplCtorArgTs&&... ctor_args)
: base_t("ADC NETSERVER", logger, std::forward<ImplCtorArgTs>(ctor_args)...)
template <traits::adc_input_char_range R>
AdcGenericNetServerSpdlog(const server_ident_t& id,
const R& mark = std::string_view("ADC GENERIC NETSERVER"),
std::shared_ptr<::spdlog::logger> logger = ::spdlog::null_logger_mt("NULL_LOGGER"))
: base_t(mark, logger, id)
{
this->logMethodCalling(__PRETTY_FUNCTION__);
fmt::format_to(std::back_inserter(_serverID), "{}", ServerT::serverIdent());
fmt::format_to(std::back_inserter(_serverID), "{}", ServerT::_serverIdent);
this->setPattern(base_t::constructPattern(_serverID));
logDebug("Creating network server with ID = [{}] (ADDR = {})", _serverID, this->_thisAddress);
logDebug("Creating ADC-library generic network server with ID = [{}] (ADDR = {})", _serverID,
this->_thisAddress);
logDebug("Use logger with ", this->loggerInfo());
}
virtual ~AdcNetServerSpdlogDecorator()
virtual server_ident_t ident() const
{
this->logMethodCalling(__PRETTY_FUNCTION__);
logDebug("Deleting network server with ID = [{}] (ADDR = {})", _serverID, this->_thisAddress);
return ServerT::ident();
}
virtual server_ident_t serverIdent() const
template <interfaces::adc_netsession_c SessionT, typename... AcceptorCtorArgTs>
void start(SessionT::netsession_ident_t id, SessionT::netsession_ctx_t sess_ctx, AcceptorCtorArgTs&&... ctor_args)
{
this->logMethodCalling(__PRETTY_FUNCTION__);
return ServerT::serverIdent();
if constexpr (traits::formattable<typename SessionT::netsession_ident_t>) {
logInfo("Start accepting client connections for server session ID: {} ...", id);
} else { // here session ID has hashable type (see AdcGenericNetServer)
logInfo("Start accepting client connections for server session ID hash: {} ...", std::hash<SessionT>(id));
}
ServerT::start(std::move(id), std::move(sess_ctx), std::forward<AcceptorCtorArgTs>(ctor_args)...);
}
virtual void start()
{
this->logMethodCalling(__PRETTY_FUNCTION__);
logInfo("Starting network server");
ServerT::start();
logInfo("Network server is started");
};
virtual void stop()
{
this->logMethodCalling(__PRETTY_FUNCTION__);
@ -160,9 +87,166 @@ public:
ServerT::stop();
logInfo("Network server is stopped");
};
}
template <interfaces::adc_netsession_c SessionT>
bool isListening(const typename SessionT::netsession_ident_t& id) const
{
this->logMethodCalling(__PRETTY_FUNCTION__);
ServerT::_isListening(id);
}
protected:
};
} // namespace adc
// template <traits::adc_netserver_session_c SessionT>
// class AdcNetServerSessionSpdlogDecorator : public AdcSpdlogGenericMarkDecorator<SessionT>
// {
// protected:
// using base_t = AdcSpdlogGenericMarkDecorator<SessionT>;
// std::string _sessionID;
// public:
// using base_t::logDebug;
// using base_t::logInfo;
// using base_t::logMethodCalling;
// // using SessionT::sessionIdent;
// using typename SessionT::session_ident_t;
// static_assert(traits::formattable<session_ident_t>, "NETWORK SESSION IDENT TYPE MUST BE A FORMATTABLE ONE!!!");
// template <typename... ImplCtorArgTs>
// AdcNetServerSessionSpdlogDecorator(std::shared_ptr<spdlog::logger> logger, ImplCtorArgTs&&... ctor_args)
// : base_t("ADC NETSERVER SESSION", logger, std::forward<ImplCtorArgTs>(ctor_args)...)
// {
// logMethodCalling(__PRETTY_FUNCTION__);
// fmt::format_to(std::back_inserter(_sessionID), "{}", SessionT::sessionIdent());
// this->setPattern(base_t::constructPattern(_sessionID));
// logDebug("Creating network server session with ID = [{}] (ADDR = {})", _sessionID, this->_thisAddress);
// logDebug("Use logger with ", this->loggerInfo());
// }
// virtual ~AdcNetServerSessionSpdlogDecorator()
// {
// logMethodCalling(__PRETTY_FUNCTION__);
// logDebug("Deleting network server session with ID = [{}] (ADDR = {})", _sessionID, this->_thisAddress);
// };
// virtual session_ident_t sessionIdent() const override
// {
// logMethodCalling(__PRETTY_FUNCTION__);
// return SessionT::sessionIdent();
// };
// virtual void start() override
// {
// logMethodCalling(__PRETTY_FUNCTION__);
// logInfo("Starting network server session with ID = [{}]", _sessionID);
// SessionT::start();
// logInfo("Network server session [{}] is started", _sessionID);
// };
// virtual void stop() override
// {
// logMethodCalling(__PRETTY_FUNCTION__);
// logInfo("Stopping network server session with ID = [{}]", _sessionID);
// SessionT::stop();
// logInfo("Network server session [{}] is stopped", _sessionID);
// };
// };
// template <traits::adc_netserver_c ServerT>
// class AdcNetServerSpdlogDecorator : public AdcSpdlogGenericMarkDecorator<ServerT>
// {
// protected:
// using base_t = AdcSpdlogGenericMarkDecorator<ServerT>;
// std::string _serverID;
// public:
// using base_t::logCritical;
// using base_t::logDebug;
// using base_t::logError;
// using base_t::logInfo;
// using base_t::logMsg;
// using base_t::logWarn;
// using typename ServerT::server_ident_t;
// static_assert(traits::formattable<server_ident_t>, "NETWORK SERVER IDENT TYPE MUST BE A FORMATTABLE ONE!!!");
// template <typename... ImplCtorArgTs>
// AdcNetServerSpdlogDecorator(std::shared_ptr<spdlog::logger> logger, ImplCtorArgTs&&... ctor_args)
// : base_t("ADC NETSERVER", logger, std::forward<ImplCtorArgTs>(ctor_args)...)
// {
// this->logMethodCalling(__PRETTY_FUNCTION__);
// fmt::format_to(std::back_inserter(_serverID), "{}", ServerT::serverIdent());
// this->setPattern(base_t::constructPattern(_serverID));
// logDebug("Creating network server with ID = [{}] (ADDR = {})", _serverID, this->_thisAddress);
// logDebug("Use logger with ", this->loggerInfo());
// }
// virtual ~AdcNetServerSpdlogDecorator()
// {
// this->logMethodCalling(__PRETTY_FUNCTION__);
// logDebug("Deleting network server with ID = [{}] (ADDR = {})", _serverID, this->_thisAddress);
// }
// virtual server_ident_t serverIdent() const
// {
// this->logMethodCalling(__PRETTY_FUNCTION__);
// return ServerT::serverIdent();
// }
// virtual void start()
// {
// this->logMethodCalling(__PRETTY_FUNCTION__);
// logInfo("Starting network server");
// ServerT::start();
// logInfo("Network server is started");
// };
// virtual void stop()
// {
// this->logMethodCalling(__PRETTY_FUNCTION__);
// logInfo("Stopping network server ...");
// ServerT::stop();
// logInfo("Network server is stopped");
// };
// };
} // namespace adc::spdlog
#endif

View File

@ -14,12 +14,20 @@
namespace adc::impl
{
class AdcDeviceNetServerASIO : public AdcDeviceNetServer
template <typename IdentT = std::string>
class AdcDeviceNetServerASIO : public AdcDeviceNetServer<IdentT>
{
typedef AdcDeviceNetServer<IdentT> base_t;
public:
template <traits::adc_input_char_range R>
AdcDeviceNetServerASIO(const R& id, asio::io_context& io_context)
: AdcDeviceNetServer(id), _ioContext(io_context), _stopSignal(io_context), _restartSignal(io_context)
using typename base_t::server_ident_t;
typedef std::string session_ident_t;
template <typename ServiceT>
using Session = typename base_t::template Session<ServiceT, session_ident_t>;
AdcDeviceNetServerASIO(const server_ident_t& id, asio::io_context& io_context)
: base_t(id), _ioContext(io_context), _stopSignal(io_context), _restartSignal(io_context)
{
}
@ -39,37 +47,30 @@ public:
// may throw here!
#ifdef USE_OPENSSL_WITH_ASIO
if (endpoint.isTCP() || endpoint.isTLS()) {
asio::ip::tcp::endpoint ept(asio::ip::make_address(endpoint.host()), endpoint.port());
if (endpoint.isTCP()) {
using srv_t = AdcNetServiceASIO<asio::ip::tcp, SessProtoT>;
AdcGenericNetServer::start<Session<srv_t>>("TCP", this, _ioContext, ept);
} else {
using srv_t = AdcNetServiceASIOTLS<asio::ip::tcp, SessProtoT>;
AdcGenericNetServer::start<Session<srv_t>>("TLS", this, _ioContext, ept, std::move(tls_context),
tls_verify_mode);
}
#else
if (endpoint.isTCP()) {
asio::ip::tcp::endpoint ept(asio::ip::make_address(endpoint.host()), endpoint.port());
using srv_t = AdcNetServiceASIO<asio::ip::tcp, AdcStopSeqSessionProto<>>;
AdcDeviceNetServer::start<Session<srv_t>>("TCP", this, _ioContext, ept);
using srv_t = AdcNetServiceASIO<asio::ip::tcp, SessProtoT>;
base_t::template start<Session<srv_t>>("TCP", this, _ioContext, ept);
#ifdef USE_OPENSSL_WITH_ASIO
} else if (endpoint.isTLS()) {
asio::ip::tcp::endpoint ept(asio::ip::make_address(endpoint.host()), endpoint.port());
using srv_t = AdcNetServiceASIOTLS<asio::ip::tcp, SessProtoT>;
base_t::template start<Session<srv_t>>("TLS", this, _ioContext, ept, std::move(tls_context),
tls_verify_mode);
#endif
} else if (endpoint.isLocal()) {
if (endpoint.isLocalStream()) {
asio::local::stream_protocol::endpoint ept(endpoint.template path<std::string>());
using srv_t = AdcNetServiceASIO<asio::local::stream_protocol, SessProtoT>;
AdcGenericNetServer::start<Session<srv_t>>("LOCAL STREAM", this, _ioContext, ept);
base_t::template start<Session<srv_t>>("LOCAL STREAM", this, _ioContext, ept);
// } else if (endpoint.isLocalDatagram()) {
// asio::local::datagram_protocol::endpoint ept(endpoint.template path<std::string>());
// using srv_t = AdcNetServiceASIO<asio::local::datagram_protocol, SessProtoT>;
// AdcDeviceNetServer::start<Session<srv_t>>("LOCAL DGRAM", this, _ioContext, ept);
// base_t::template start<Session<srv_t>>("LOCAL DGRAM", this, _ioContext, ept);
} else if (endpoint.isLocalSeqpacket()) {
asio::local::seq_packet_protocol::endpoint ept(endpoint.template path<std::string>());
using srv_t = AdcNetServiceASIO<asio::local::seq_packet_protocol, SessProtoT>;
AdcGenericNetServer::start<Session<srv_t>>("LOCAL SEQPACK", this, _ioContext, ept);
base_t::template start<Session<srv_t>>("LOCAL SEQPACK", this, _ioContext, ept);
}
} else {
throw std::system_error(std::make_error_code(std::errc::protocol_not_supported));
@ -80,8 +81,7 @@ public:
void start() {}
template <std::ranges::range RST = std::vector<int>, std::ranges::range RRT = std::vector<int>>
void setupSignals(const RST& stop_sig_num = std::vector<int>{SIGINT, SIGTERM},
const RRT& restart_sig_num = std::vector<int>{SIGUSR1})
void setupSignals(const RST& stop_sig_num = {SIGINT, SIGTERM}, const RRT& restart_sig_num = {SIGUSR1})
requires(std::convertible_to<std::ranges::range_value_t<RST>, int> &&
std::convertible_to<std::ranges::range_value_t<RRT>, int>)
{
@ -100,7 +100,7 @@ public:
_restartSignal.async_wait([this](std::error_code ec, int signo) {
signalReceived(ec, signo);
// ?!!!!!!!
restart();
});
}
@ -124,6 +124,16 @@ protected:
{
std::cout << "SIGNAL: " << signo << "\n";
};
virtual void restart()
{
this->stopAllSessions();
_restartSignal.async_wait([this](std::error_code ec, int signo) {
signalReceived(ec, signo);
restart();
});
}
};
} // namespace adc::impl

View File

@ -8,7 +8,7 @@
#include "../net/adc_netproto.h"
#include "../net/asio/adc_device_netserver_asio.h"
typedef adc::impl::AdcDeviceNetServerASIO server_t;
typedef adc::impl::AdcDeviceNetServerASIO<std::string_view> server_t;
typedef adc::AdcDeviceAttribute<std::string, server_t::serialized_t> attr_t;
class Device1 : public adc::AdcGenericDevice<std::string,