diff --git a/CMakeLists.txt b/CMakeLists.txt index 80f3910..478ce59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,9 +13,9 @@ project(diag-client) # Cmake options option(BUILD_DIAG_CLIENT "Option to use Dlt for logging" ON) option(BUILD_SHARED_LIBS "Option to build as shared library" OFF) -option(BUILD_WITH_DLT "Option to use Dlt for logging" OFF) +option(BUILD_WITH_DLT "Option to use Dlt for logging" ON) option(BUILD_DOXYGEN "Option to generate doxygen file" OFF) -option(BUILD_WITH_TEST "Option to build test target" OFF) +option(BUILD_WITH_TEST "Option to build test target" ON) option(BUILD_EXAMPLES "Option to build example targets" OFF) # add compiler preprocessor flag when dlt enabled diff --git a/diag-client-lib/lib/boost-support/include/boost-support/client/tcp/tcp_client.h b/diag-client-lib/lib/boost-support/include/boost-support/client/tcp/tcp_client.h index c7873ac..8901a21 100644 --- a/diag-client-lib/lib/boost-support/include/boost-support/client/tcp/tcp_client.h +++ b/diag-client-lib/lib/boost-support/include/boost-support/client/tcp/tcp_client.h @@ -46,12 +46,15 @@ class TcpClient final { public: /** * @brief Constructs an instance of TcpClient + * @param[in] client_name + * The name of the client * @param[in] local_ip_address * The local ip address * @param[in] local_port_num * The local port number */ - TcpClient(std::string_view local_ip_address, std::uint16_t local_port_num) noexcept; + TcpClient(std::string_view client_name, std::string_view local_ip_address, + std::uint16_t local_port_num) noexcept; /** * @brief Deleted copy assignment and copy constructor diff --git a/diag-client-lib/lib/boost-support/include/boost-support/client/tls/tls_client.h b/diag-client-lib/lib/boost-support/include/boost-support/client/tls/tls_client.h index d7d2dcd..d323e33 100644 --- a/diag-client-lib/lib/boost-support/include/boost-support/client/tls/tls_client.h +++ b/diag-client-lib/lib/boost-support/include/boost-support/client/tls/tls_client.h @@ -71,8 +71,9 @@ class TlsClient final { * @param[in] ca_certification_path * The path to root ca certificate */ - TlsClient(std::string_view local_ip_address, std::uint16_t local_port_num, - std::string_view ca_certification_path, TlsVersion tls_version) noexcept; + TlsClient(std::string_view client_name, std::string_view local_ip_address, + std::uint16_t local_port_num, std::string_view ca_certification_path, + TlsVersion tls_version) noexcept; /** * @brief Deleted copy assignment and copy constructor diff --git a/diag-client-lib/lib/boost-support/include/boost-support/server/tcp/tcp_acceptor.h b/diag-client-lib/lib/boost-support/include/boost-support/server/tcp/tcp_acceptor.h index d21906c..ef2e005 100644 --- a/diag-client-lib/lib/boost-support/include/boost-support/server/tcp/tcp_acceptor.h +++ b/diag-client-lib/lib/boost-support/include/boost-support/server/tcp/tcp_acceptor.h @@ -29,8 +29,8 @@ class TcpAcceptor final { * @param[in] maximum_connection * The maximum number of accepted connection allowed */ - TcpAcceptor(std::string_view local_ip_address, std::uint16_t local_port_num, - std::uint8_t maximum_connection) noexcept; + TcpAcceptor(std::string_view acceptor_name, std::string_view local_ip_address, + std::uint16_t local_port_num, std::uint8_t maximum_connection) noexcept; /** * @brief Destruct an instance of TcpAcceptor diff --git a/diag-client-lib/lib/boost-support/include/boost-support/server/tcp/tcp_server.h b/diag-client-lib/lib/boost-support/include/boost-support/server/tcp/tcp_server.h index eaaf122..d1e940a 100644 --- a/diag-client-lib/lib/boost-support/include/boost-support/server/tcp/tcp_server.h +++ b/diag-client-lib/lib/boost-support/include/boost-support/server/tcp/tcp_server.h @@ -57,10 +57,12 @@ class TcpServer final { public: /** * @brief Constructs an instance of TcpServer + * @param[in] server_name + * The name of the server * @param[in] tcp_socket * The underlying tcp socket required for communication */ - explicit TcpServer(TcpSocket tcp_socket) noexcept; + explicit TcpServer(std::string_view server_name, TcpSocket tcp_socket) noexcept; /** * @brief Deleted copy assignment and copy constructor diff --git a/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_acceptor.h b/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_acceptor.h index 5dbbfe3..5e12c3b 100644 --- a/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_acceptor.h +++ b/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_acceptor.h @@ -47,12 +47,12 @@ class TlsAcceptor final { * @param[in] maximum_connection * The maximum number of accepted connection allowed */ - TlsAcceptor(std::string_view local_ip_address, std::uint16_t local_port_num, - std::uint8_t maximum_connection, TlsVersion tls_version, + TlsAcceptor(std::string_view acceptor_name, std::string_view local_ip_address, + std::uint16_t local_port_num, std::uint8_t maximum_connection, TlsVersion tls_version, std::string_view certificate_path, std::string_view private_key_path) noexcept; /** - * @brief Destruct an instance of TcpAcceptor + * @brief Destruct an instance of TlsAcceptor */ ~TlsAcceptor() noexcept; diff --git a/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_server.h b/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_server.h index 45f5883..62a7847 100644 --- a/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_server.h +++ b/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_server.h @@ -60,7 +60,7 @@ class TlsServer final { * @param[in] tls_socket * The underlying tls socket required for communication */ - explicit TlsServer(TlsSocket tls_socket) noexcept; + explicit TlsServer(std::string_view server_name, TlsSocket tls_socket) noexcept; /** * @brief Deleted copy assignment and copy constructor diff --git a/diag-client-lib/lib/boost-support/src/boost-support/client/tcp/tcp_client.cpp b/diag-client-lib/lib/boost-support/src/boost-support/client/tcp/tcp_client.cpp index a6243ed..c78cba0 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/client/tcp/tcp_client.cpp +++ b/diag-client-lib/lib/boost-support/src/boost-support/client/tcp/tcp_client.cpp @@ -21,6 +21,20 @@ namespace boost_support { namespace client { namespace tcp { +namespace { +/** + * @brief Function to append the ip address and port number to the connection name + */ +std::string AppendIpAddressAndPort(std::string_view client_name, std::string_view ip_address, + std::uint16_t port_num) { + std::string connection_name{client_name}; + connection_name.append("_"); + connection_name.append(ip_address); + connection_name.append("_"); + connection_name.append(std::to_string(port_num)); + return connection_name; +} +} // namespace /** * @brief Class to provide implementation of tcp client @@ -54,10 +68,13 @@ class TcpClient::TcpClientImpl final { * @param[in] local_port_num * The local port number of client */ - TcpClientImpl(std::string_view local_ip_address, std::uint16_t local_port_num) noexcept + TcpClientImpl(std::string_view client_name, std::string_view local_ip_address, + std::uint16_t local_port_num) noexcept : io_context_{}, connection_state_{State::kDisconnected}, - tcp_connection_{socket::tcp::TcpSocket{local_ip_address, local_port_num, io_context_}} {} + client_name_{AppendIpAddressAndPort(client_name, local_ip_address, local_port_num)}, + tcp_connection_{client_name, + socket::tcp::TcpSocket{local_ip_address, local_port_num, io_context_}} {} /** * @brief Deleted copy assignment and copy constructor @@ -183,14 +200,18 @@ class TcpClient::TcpClientImpl final { */ std::atomic connection_state_; + std::string client_name_; + /** * @brief Store the tcp connection */ TcpConnection tcp_connection_; }; -TcpClient::TcpClient(std::string_view local_ip_address, std::uint16_t local_port_num) noexcept - : tcp_client_impl_{std::make_unique(local_ip_address, local_port_num)} {} +TcpClient::TcpClient(std::string_view client_name, std::string_view local_ip_address, + std::uint16_t local_port_num) noexcept + : tcp_client_impl_{ + std::make_unique(client_name, local_ip_address, local_port_num)} {} TcpClient::TcpClient(TcpClient &&other) noexcept = default; diff --git a/diag-client-lib/lib/boost-support/src/boost-support/client/tls/tls_client.cpp b/diag-client-lib/lib/boost-support/src/boost-support/client/tls/tls_client.cpp index fee8fe7..a970a62 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/client/tls/tls_client.cpp +++ b/diag-client-lib/lib/boost-support/src/boost-support/client/tls/tls_client.cpp @@ -18,6 +18,20 @@ namespace boost_support { namespace client { namespace tls { +namespace { +/** + * @brief Function to append the ip address and port number to the connection name + */ +std::string AppendIpAddressAndPort(std::string_view client_name, std::string_view ip_address, + std::uint16_t port_num) { + std::string connection_name{client_name}; + connection_name.append("_"); + connection_name.append(ip_address); + connection_name.append("_"); + connection_name.append(std::to_string(port_num)); + return connection_name; +} +} // namespace /** * @brief Class to provide implementation of tls client @@ -62,12 +76,14 @@ class TlsClient::TlsClientImpl final { * @param[in] local_port_num * The local port number of client */ - TlsClientImpl(std::string_view local_ip_address, std::uint16_t local_port_num, - std::string_view ca_certification_path, TlsVersion tls_version) noexcept + TlsClientImpl(std::string_view client_name, std::string_view local_ip_address, + std::uint16_t local_port_num, std::string_view ca_certification_path, + TlsVersion tls_version) noexcept : io_context_{}, tls_context_{tls_version, ca_certification_path}, connection_state_{State::kDisconnected}, - tcp_connection_{TlsSocket{local_ip_address, local_port_num, tls_context_, io_context_}} {} + tcp_connection_{AppendIpAddressAndPort(client_name, local_ip_address, local_port_num), + TlsSocket{local_ip_address, local_port_num, tls_context_, io_context_}} {} /** * @brief Deleted copy assignment and copy constructor @@ -205,11 +221,13 @@ class TlsClient::TlsClientImpl final { }; template -TlsClient::TlsClient(std::string_view local_ip_address, std::uint16_t local_port_num, +TlsClient::TlsClient(std::string_view client_name, std::string_view local_ip_address, + std::uint16_t local_port_num, std::string_view ca_certification_path, TlsVersion tls_version) noexcept - : tls_client_impl_{std::make_unique( - local_ip_address, local_port_num, ca_certification_path, std::move(tls_version))} {} + : tls_client_impl_{std::make_unique(client_name, local_ip_address, + local_port_num, ca_certification_path, + std::move(tls_version))} {} template TlsClient::TlsClient(TlsClient &&other) noexcept = default; diff --git a/diag-client-lib/lib/boost-support/src/boost-support/client/udp/udp_client.cpp b/diag-client-lib/lib/boost-support/src/boost-support/client/udp/udp_client.cpp index 11c7e27..278040b 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/client/udp/udp_client.cpp +++ b/diag-client-lib/lib/boost-support/src/boost-support/client/udp/udp_client.cpp @@ -29,7 +29,7 @@ class UdpClient::UdpClientImpl final { * The local port number of client */ UdpClientImpl(std::string_view local_ip_address, std::uint16_t local_port_num) noexcept - : io_context_{}, + : io_context_{"UdpClient"}, udp_connection_{ socket::udp::UdpSocket{local_ip_address, local_port_num, io_context_.GetContext()}} {} diff --git a/diag-client-lib/lib/boost-support/src/boost-support/connection/tcp/tcp_connection.h b/diag-client-lib/lib/boost-support/src/boost-support/connection/tcp/tcp_connection.h index 1b1bea7..3204e59 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/connection/tcp/tcp_connection.h +++ b/diag-client-lib/lib/boost-support/src/boost-support/connection/tcp/tcp_connection.h @@ -13,7 +13,8 @@ #include #include #include -#include +#include +#include #include #include "boost-support/error_domain/boost_support_error_domain.h" @@ -73,12 +74,13 @@ class TcpConnection final { * @param[in] socket * The socket used for read and writing messages */ - explicit TcpConnection(Socket socket) noexcept + explicit TcpConnection(std::string_view connection_name, Socket socket) noexcept : socket_{std::move(socket)}, handler_read_{}, exit_request_{false}, running_{false}, cond_var_{}, + connection_name_{connection_name}, thread_{}, mutex_{} {} @@ -114,19 +116,20 @@ class TcpConnection final { // Open socket socket_.Open(); // Start thread to receive messages - thread_ = std::thread([this]() { - std::unique_lock lck(mutex_); - while (!exit_request_) { - if (!running_) { - cond_var_.wait(lck, [this]() { return exit_request_ || running_; }); - } - if (!exit_request_.load() && running_) { - lck.unlock(); - running_ = ReadMessage(); - lck.lock(); - } - } - }); + thread_ = utility::thread::Thread{ + connection_name_, [this]() { + std::unique_lock lck(mutex_); + while (!exit_request_) { + if (!running_) { + cond_var_.wait(lck, [this]() { return exit_request_ || running_; }); + } + if (!exit_request_.load() && running_) { + lck.unlock(); + running_ = ReadMessage(); + lck.lock(); + } + } + }}; } /** @@ -140,7 +143,7 @@ class TcpConnection final { running_ = false; } cond_var_.notify_all(); - if (thread_.joinable()) { thread_.join(); } + thread_.Join(); } /** @@ -216,10 +219,15 @@ class TcpConnection final { */ std::condition_variable cond_var_; + /** + * @brief Store the connection name + */ + std::string connection_name_; + /** * @brief Store the thread */ - std::thread thread_; + utility::thread::Thread thread_; /** * @brief mutex to lock critical section @@ -276,12 +284,13 @@ class TcpConnection final { * @param[in] socket * The socket used for read and writing messages */ - explicit TcpConnection(Socket socket) noexcept + explicit TcpConnection(std::string_view connection_name, Socket socket) noexcept : socket_{std::move(socket)}, handler_read_{}, exit_request_{false}, running_{false}, cond_var_{}, + connection_name_{connection_name}, thread_{}, mutex_{} {} @@ -331,7 +340,7 @@ class TcpConnection final { */ void Initialize() noexcept { // Start thread to receive messages - thread_ = std::thread([this]() { + thread_ = utility::thread::Thread(connection_name_, [this]() { std::unique_lock lck(mutex_); while (!exit_request_) { if (!running_) { @@ -362,7 +371,7 @@ class TcpConnection final { running_ = false; } cond_var_.notify_all(); - if (thread_.joinable()) { thread_.join(); } + thread_.Join(); } /** @@ -405,10 +414,15 @@ class TcpConnection final { */ std::condition_variable cond_var_; + /** + * @brief Store the connection name + */ + std::string connection_name_; + /** * @brief Store the thread */ - std::thread thread_; + utility::thread::Thread thread_; /** * @brief mutex to lock critical section diff --git a/diag-client-lib/lib/boost-support/src/boost-support/server/tcp/tcp_acceptor.cpp b/diag-client-lib/lib/boost-support/src/boost-support/server/tcp/tcp_acceptor.cpp index 99bde86..928ed52 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/server/tcp/tcp_acceptor.cpp +++ b/diag-client-lib/lib/boost-support/src/boost-support/server/tcp/tcp_acceptor.cpp @@ -28,6 +28,14 @@ using Tcp = boost::asio::ip::tcp; */ using TcpIpAddress = boost::asio::ip::address; +/** + * @brief Function to create server name + */ +std::string CreateServerName(std::string_view server_name, std::uint16_t server_count) { + std::string final_server_name{server_name}; + final_server_name.append(std::to_string(server_count)); + return final_server_name; +} } // namespace class TcpAcceptor::TcpAcceptorImpl final { @@ -48,9 +56,11 @@ class TcpAcceptor::TcpAcceptorImpl final { * @param[in] maximum_connection * The maximum number of accepted connection */ - TcpAcceptorImpl(std::string_view local_ip_address, std::uint16_t local_port_num, - std::uint8_t maximum_connection) noexcept + TcpAcceptorImpl(std::string_view acceptor_name, std::string_view local_ip_address, + std::uint16_t local_port_num, std::uint8_t maximum_connection) noexcept : io_context_{}, + server_count_{0u}, + acceptor_name_{acceptor_name}, acceptor_{io_context_, Tcp::endpoint(TcpIpAddress::from_string(std::string{local_ip_address}.c_str()), local_port_num)} { @@ -71,12 +81,15 @@ class TcpAcceptor::TcpAcceptorImpl final { // blocking accept TcpSocket::Socket accepted_socket{acceptor_.accept(endpoint, ec)}; if (ec.value() == boost::system::errc::success) { - tcp_server.emplace(TcpSocket{std::move(accepted_socket)}); + tcp_server.emplace(CreateServerName(acceptor_name_, server_count_), + TcpSocket{std::move(accepted_socket)}); common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug( FILE_NAME, __LINE__, __func__, [&endpoint](std::stringstream &msg) { - msg << "Tcp socket connection received from client " - << "<" << endpoint.address().to_string() << "," << endpoint.port() << ">"; + msg << "Tcp socket connection received from client " << "<" + << endpoint.address().to_string() << "," << endpoint.port() << ">"; }); + // increment the server count + server_count_++; } else { common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogError( FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) { @@ -97,16 +110,26 @@ class TcpAcceptor::TcpAcceptorImpl final { */ boost::asio::io_context io_context_; + /** + * @brief Keeps the count of server created + */ + std::uint16_t server_count_; + + /** + * @brief Store the name of the acceptor + */ + std::string acceptor_name_; + /** * @brief Store the tcp acceptor */ Acceptor acceptor_; }; -TcpAcceptor::TcpAcceptor(std::string_view local_ip_address, std::uint16_t local_port_num, - std::uint8_t maximum_connection) noexcept - : tcp_acceptor_impl_{std::make_unique(local_ip_address, local_port_num, - maximum_connection)} {} +TcpAcceptor::TcpAcceptor(std::string_view acceptor_name, std::string_view local_ip_address, + std::uint16_t local_port_num, std::uint8_t maximum_connection) noexcept + : tcp_acceptor_impl_{std::make_unique(acceptor_name, local_ip_address, + local_port_num, maximum_connection)} {} TcpAcceptor::~TcpAcceptor() noexcept = default; diff --git a/diag-client-lib/lib/boost-support/src/boost-support/server/tcp/tcp_server.cpp b/diag-client-lib/lib/boost-support/src/boost-support/server/tcp/tcp_server.cpp index 37a0463..b2256d5 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/server/tcp/tcp_server.cpp +++ b/diag-client-lib/lib/boost-support/src/boost-support/server/tcp/tcp_server.cpp @@ -28,7 +28,8 @@ class TcpServer::TcpServerImpl final { * @param[in] tcp_socket * The underlying tcp socket required for communication */ - explicit TcpServerImpl(TcpSocket tcp_socket) noexcept : tcp_connection_{std::move(tcp_socket)} {} + explicit TcpServerImpl(std::string_view server_name, TcpSocket tcp_socket) noexcept + : tcp_connection_{server_name, std::move(tcp_socket)} {} /** * @brief Deleted copy assignment and copy constructor @@ -84,8 +85,8 @@ class TcpServer::TcpServerImpl final { TcpConnection tcp_connection_; }; -TcpServer::TcpServer(TcpServer::TcpSocket tcp_socket) noexcept - : tcp_server_impl_{std::make_unique(std::move(tcp_socket))} {} +TcpServer::TcpServer(std::string_view server_name, TcpServer::TcpSocket tcp_socket) noexcept + : tcp_server_impl_{std::make_unique(server_name, std::move(tcp_socket))} {} TcpServer::TcpServer(TcpServer &&other) noexcept = default; diff --git a/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_acceptor.cpp b/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_acceptor.cpp index 6a1f867..b0dd1d1 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_acceptor.cpp +++ b/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_acceptor.cpp @@ -30,6 +30,14 @@ using Tcp = boost::asio::ip::tcp; */ using TcpIpAddress = boost::asio::ip::address; +/** + * @brief Function to create server name + */ +std::string CreateServerName(std::string_view server_name, std::uint16_t server_count) { + std::string final_server_name{server_name}; + final_server_name.append(std::to_string(server_count)); + return final_server_name; +} } // namespace template @@ -56,11 +64,13 @@ class TlsAcceptor::TlsAcceptorImpl final { * @param[in] maximum_connection * The maximum number of accepted connection */ - TlsAcceptorImpl(std::string_view local_ip_address, std::uint16_t local_port_num, - std::uint8_t maximum_connection, TlsVersion tls_version, - std::string_view certificate_path, std::string_view private_key_path) noexcept + TlsAcceptorImpl(std::string_view acceptor_name, std::string_view local_ip_address, + std::uint16_t local_port_num, std::uint8_t maximum_connection, + TlsVersion tls_version, std::string_view certificate_path, + std::string_view private_key_path) noexcept : io_context_{}, tls_context_{std::forward(tls_version), certificate_path, private_key_path}, + acceptor_name_{acceptor_name}, acceptor_{io_context_, Tcp::endpoint(TcpIpAddress::from_string(std::string{local_ip_address}.c_str()), local_port_num)} { @@ -81,12 +91,15 @@ class TlsAcceptor::TlsAcceptorImpl final { // blocking accept TlsSocket::TcpSocket accepted_socket{acceptor_.accept(endpoint, ec)}; if (ec.value() == boost::system::errc::success) { - tls_server.emplace(TlsSocket{std::move(accepted_socket), tls_context_}); + tls_server.emplace(CreateServerName(acceptor_name_, server_count_), + TlsSocket{std::move(accepted_socket), tls_context_}); common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug( FILE_NAME, __LINE__, __func__, [&endpoint](std::stringstream &msg) { - msg << "Tls socket connection received from client " - << "<" << endpoint.address().to_string() << "," << endpoint.port() << ">"; + msg << "Tls socket connection received from client " << "<" + << endpoint.address().to_string() << "," << endpoint.port() << ">"; }); + // increment the server count + server_count_++; } else { common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogError( FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) { @@ -112,6 +125,16 @@ class TlsAcceptor::TlsAcceptorImpl final { */ TlsContext tls_context_; + /** + * @brief Keeps the count of server created + */ + std::uint16_t server_count_; + + /** + * @brief Store the name of the acceptor + */ + std::string acceptor_name_; + /** * @brief Store the tcp acceptor */ @@ -119,13 +142,14 @@ class TlsAcceptor::TlsAcceptorImpl final { }; template -TlsAcceptor::TlsAcceptor(std::string_view local_ip_address, +TlsAcceptor::TlsAcceptor(std::string_view acceptor_name, + std::string_view local_ip_address, std::uint16_t local_port_num, std::uint8_t maximum_connection, TlsVersion tls_version, std::string_view certificate_path, std::string_view private_key_path) noexcept : tls_acceptor_impl_{std::make_unique( - local_ip_address, local_port_num, maximum_connection, std::move(tls_version), - certificate_path, private_key_path)} {} + acceptor_name, local_ip_address, local_port_num, maximum_connection, + std::move(tls_version), certificate_path, private_key_path)} {} template TlsAcceptor::~TlsAcceptor() noexcept = default; diff --git a/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_server.cpp b/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_server.cpp index afd1f5e..18764ce 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_server.cpp +++ b/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_server.cpp @@ -33,7 +33,8 @@ class TlsServer::TlsServerImpl final { * @param[in] tcp_socket * The underlying tcp socket required for communication */ - explicit TlsServerImpl(TlsSocket tcp_socket) noexcept : tcp_connection_{std::move(tcp_socket)} {} + explicit TlsServerImpl(std::string_view server_name, TlsSocket tcp_socket) noexcept + : tcp_connection_{server_name, std::move(tcp_socket)} {} /** * @brief Deleted copy assignment and copy constructor @@ -89,8 +90,8 @@ class TlsServer::TlsServerImpl final { TcpConnectionSecured tcp_connection_; }; -TlsServer::TlsServer(TlsServer::TlsSocket tls_socket) noexcept - : tls_server_impl_{std::make_unique(std::move(tls_socket))} {} +TlsServer::TlsServer(std::string_view server_name, TlsServer::TlsSocket tls_socket) noexcept + : tls_server_impl_{std::make_unique(server_name, std::move(tls_socket))} {} TlsServer::TlsServer(TlsServer &&other) noexcept = default; diff --git a/diag-client-lib/lib/boost-support/src/boost-support/socket/io_context.cpp b/diag-client-lib/lib/boost-support/src/boost-support/socket/io_context.cpp index 84223d1..c7bbc9f 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/socket/io_context.cpp +++ b/diag-client-lib/lib/boost-support/src/boost-support/socket/io_context.cpp @@ -16,9 +16,18 @@ IoContext::IoContext() noexcept exit_request_{false}, running_{false}, cond_var_{}, + mutex_{} {} + +IoContext::IoContext(std::string_view context_name) noexcept + : io_context_{}, + exit_request_{false}, + running_{false}, + cond_var_{}, + context_name_{context_name}, + thread_{}, mutex_{} { // start thread to execute async tasks - thread_ = std::thread([this]() { + thread_ = utility::thread::Thread(context_name_, [this]() { std::unique_lock lck(mutex_); while (!exit_request_) { if (!running_) { @@ -40,7 +49,7 @@ IoContext::~IoContext() noexcept { exit_request_ = true; running_ = false; cond_var_.notify_all(); - thread_.join(); + thread_.Join(); } void IoContext::Initialize() noexcept { diff --git a/diag-client-lib/lib/boost-support/src/boost-support/socket/io_context.h b/diag-client-lib/lib/boost-support/src/boost-support/socket/io_context.h index cdf817a..04e553e 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/socket/io_context.h +++ b/diag-client-lib/lib/boost-support/src/boost-support/socket/io_context.h @@ -9,7 +9,9 @@ #define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_IO_CONTEXT_H_ #include -#include +#include + +#include "utility/thread.h" namespace boost_support { namespace socket { @@ -26,10 +28,15 @@ class IoContext final { public: /** - * @brief Constructs an instance of IoContext + * @brief Default constructs an instance of IoContext */ IoContext() noexcept; + /** + * @brief Constructs an instance of IoContext + */ + IoContext(std::string_view context_name) noexcept; + /** * @brief Deleted copy assignment and copy constructor */ @@ -84,10 +91,15 @@ class IoContext final { */ std::condition_variable cond_var_; + /** + * @brief The name of context + */ + std::string context_name_; + /** * @brief The thread itself */ - std::thread thread_; + utility::thread::Thread thread_; /** * @brief mutex to lock critical section diff --git a/diag-client-lib/lib/boost-support/src/boost-support/socket/tcp/tcp_socket.cpp b/diag-client-lib/lib/boost-support/src/boost-support/socket/tcp/tcp_socket.cpp index 4c5b825..3a29781 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/socket/tcp/tcp_socket.cpp +++ b/diag-client-lib/lib/boost-support/src/boost-support/socket/tcp/tcp_socket.cpp @@ -47,8 +47,8 @@ core_type::Result TcpSocket::Open() noexcept { common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug( FILE_NAME, __LINE__, __func__, [this](std::stringstream &msg) { Tcp::endpoint const endpoint_{tcp_socket_.local_endpoint()}; - msg << "Tcp Socket opened and bound to " - << "<" << endpoint_.address().to_string() << "," << endpoint_.port() << ">"; + msg << "Tcp Socket opened and bound to " << "<" << endpoint_.address().to_string() + << "," << endpoint_.port() << ">"; }); result.EmplaceValue(); } else { @@ -81,8 +81,8 @@ core_type::Result TcpSocket::Connect( common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug( FILE_NAME, __LINE__, __func__, [this](std::stringstream &msg) { Tcp::endpoint const endpoint_{tcp_socket_.remote_endpoint()}; - msg << "Tcp Socket connected to host " - << "<" << endpoint_.address().to_string() << "," << endpoint_.port() << ">"; + msg << "Tcp Socket connected to host " << "<" << endpoint_.address().to_string() << "," + << endpoint_.port() << ">"; }); result.EmplaceValue(); } else { @@ -125,8 +125,8 @@ core_type::Result TcpSocket::Transmit( common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug( FILE_NAME, __LINE__, __func__, [this](std::stringstream &msg) { Tcp::endpoint const endpoint_{tcp_socket_.remote_endpoint()}; - msg << "Tcp message sent to " - << "<" << endpoint_.address().to_string() << "," << endpoint_.port() << ">"; + msg << "Tcp message sent to " << "<" << endpoint_.address().to_string() << "," + << endpoint_.port() << ">"; }); result.EmplaceValue(); } else { @@ -159,13 +159,13 @@ core_type::Result TcpSocket::R // Check for error if (ec.value() == boost::system::errc::success) { // read the next bytes to read - std::uint32_t const read_next_bytes = [&rx_buffer]() noexcept -> std::uint32_t { + std::uint32_t const read_next_bytes{[&rx_buffer]() noexcept -> std::uint32_t { return static_cast( (static_cast(rx_buffer[4u] << 24u) & 0xFF000000) | (static_cast(rx_buffer[5u] << 16u) & 0x00FF0000) | (static_cast(rx_buffer[6u] << 8u) & 0x0000FF00) | (static_cast(rx_buffer[7u] & 0x000000FF))); - }(); + }()}; if (read_next_bytes != 0u) { // reserve the buffer @@ -179,9 +179,8 @@ core_type::Result TcpSocket::R remote_endpoint.address().to_string(), remote_endpoint.port(), std::move(rx_buffer))}; common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug( FILE_NAME, __LINE__, __func__, [&remote_endpoint](std::stringstream &msg) { - msg << "Tcp Message received from " - << "<" << remote_endpoint.address().to_string() << "," << remote_endpoint.port() - << ">"; + msg << "Tcp Message received from " << "<" << remote_endpoint.address().to_string() + << "," << remote_endpoint.port() << ">"; }); result.EmplaceValue(std::move(tcp_rx_message)); } else { diff --git a/diag-client-lib/lib/doip-client/connection/connection_manager.cpp b/diag-client-lib/lib/doip-client/connection/connection_manager.cpp index 4aa2ee3..1ab57b2 100644 --- a/diag-client-lib/lib/doip-client/connection/connection_manager.cpp +++ b/diag-client-lib/lib/doip-client/connection/connection_manager.cpp @@ -8,6 +8,8 @@ #include "connection/connection_manager.h" +#include + #include "channel/tcp_channel/doip_tcp_channel.h" #include "channel/udp_channel/doip_udp_channel.h" #include "sockets/socket_handler.h" @@ -15,6 +17,20 @@ namespace doip_client { namespace connection { +namespace { +using namespace std::literals; + +/** + * @brief Tcp connection name + */ +constexpr std::string_view kDoipTcpConnectionName{"DTcpCntn_"sv}; + +/** + * @brief Udp connection name + */ +constexpr std::string_view kDoipUdpConnectionName{"DUdpCntn_"sv}; + +} // namespace /** * @brief Doip Tcp Connection handle connection between two layers @@ -44,8 +60,10 @@ class DoipTcpConnection final : public uds_transport::Connection { */ DoipTcpConnection(uds_transport::ConversionHandler const &conversation_handler, std::string_view tcp_ip_address, std::uint16_t port_num) - : uds_transport::Connection{1u, conversation_handler}, - doip_tcp_channel_{sockets::TcpSocketHandler{TcpClient{tcp_ip_address, port_num}}, *this} {} + : uds_transport::Connection{kDoipTcpConnectionName, 1u, conversation_handler}, + doip_tcp_channel_{ + sockets::TcpSocketHandler{TcpClient{GetConnectionName(), tcp_ip_address, port_num}}, + *this} {} /** * @brief Destruct an instance of DoipTcpConnection @@ -182,7 +200,7 @@ class DoipUdpConnection final : public uds_transport::Connection { */ DoipUdpConnection(uds_transport::ConversionHandler const &conversation_handler, std::string_view udp_ip_address, std::uint16_t port_num) - : uds_transport::Connection(1, conversation_handler), + : uds_transport::Connection{kDoipUdpConnectionName, 1, conversation_handler}, doip_udp_channel_{sockets::UdpSocketHandler{UdpClient{udp_ip_address, port_num}}, sockets::UdpSocketHandler{UdpClient{udp_ip_address, port_num}}, *this} {} diff --git a/diag-client-lib/lib/uds-transport-layer-api/uds_transport/connection.h b/diag-client-lib/lib/uds-transport-layer-api/uds_transport/connection.h index ed4bcf0..71566ab 100644 --- a/diag-client-lib/lib/uds-transport-layer-api/uds_transport/connection.h +++ b/diag-client-lib/lib/uds-transport-layer-api/uds_transport/connection.h @@ -7,8 +7,11 @@ */ #ifndef DIAGNOSTIC_CLIENT_LIB_LIB_UDS_TRANSPORT_LAYER_API_UDS_TRANSPORT_CONNECTION_H #define DIAGNOSTIC_CLIENT_LIB_LIB_UDS_TRANSPORT_LAYER_API_UDS_TRANSPORT_CONNECTION_H + /* includes */ #include +#include +#include #include "core/include/span.h" #include "uds_transport/protocol_handler.h" @@ -16,6 +19,25 @@ #include "uds_transport/protocol_types.h" namespace uds_transport { +namespace { + +template +std::string Append(T... args) { + std::string appended_name{}; + (appended_name.append(args), ...); + return appended_name; +} + +/** + * @brief Function to append the connection id to the connection name + */ +inline std::string CreateConnectionName(std::string_view connection_name, + std::uint8_t connection_id) { + std::string final_connection_name{connection_name}; + final_connection_name.append(std::to_string(connection_id)); + return final_connection_name; +} +} // namespace /** * @brief Interface class to handle connection between two layers @@ -34,15 +56,18 @@ class Connection { /** * @brief Constructor to create a new connection + * @param[in] connection_name + * The name of the connection * @param[in] connection_id * The connection identification * @param[in] conversation_handler * The reference to conversation handler */ - Connection(ConnectionId connection_id, + Connection(std::string_view connection_name, ConnectionId connection_id, uds_transport::ConversionHandler const &conversation_handler) noexcept : conversation_handler_{conversation_handler}, - connection_id_{connection_id} {} + connection_id_{connection_id}, + connection_name_{CreateConnectionName(connection_name, connection_id_)} {} /** * @brief Destruct an instance of Connection @@ -59,7 +84,13 @@ class Connection { * @brief Function to get the connection id * @return The connection id */ - ConnectionId GetConnectionId() const noexcept { return connection_id_; } + [[nodiscard]] ConnectionId GetConnectionId() const noexcept { return connection_id_; } + + /** + * @brief Function to get the connection name + * @return The connection name + */ + [[nodiscard]] std::string_view GetConnectionName() const noexcept { return connection_name_; } /** * @brief Function to start the connection @@ -147,6 +178,11 @@ class Connection { * @brief Store the connection id */ ConnectionId connection_id_; + + /** + * @brief Store the connection name + */ + std::string connection_name_; }; } // namespace uds_transport diff --git a/diag-client-lib/lib/utility-support/utility/file_path.h b/diag-client-lib/lib/utility-support/utility/file_path.h new file mode 100644 index 0000000..a390da4 --- /dev/null +++ b/diag-client-lib/lib/utility-support/utility/file_path.h @@ -0,0 +1,14 @@ +#include + +namespace utility { +namespace details { +// constexpr function to strip the file path +constexpr std::string_view StripFilePath(std::string_view path) { + size_t pos{path.find_last_of("/\\")}; + return (pos == std::string_view::npos) ? path : path.substr(pos + 1); +} +} // namespace details +} // namespace utility + +// Macro to get the file name during compile time +#define FILE_NAME utility::details::StripFilePath(__FILE__) diff --git a/diag-client-lib/lib/utility-support/utility/thread.h b/diag-client-lib/lib/utility-support/utility/thread.h index 56efef4..1a830be 100644 --- a/diag-client-lib/lib/utility-support/utility/thread.h +++ b/diag-client-lib/lib/utility-support/utility/thread.h @@ -51,14 +51,20 @@ class Thread final { Thread(Thread &&other) noexcept = default; Thread &operator=(Thread &&other) noexcept = default; + /** + * @brief Destructor + */ ~Thread() noexcept { Join(); } + /** + * @brief Function to join the running thread + */ void Join() noexcept { if (thread_.joinable()) { thread_.join(); } } private: - std::thread thread_; + std::thread thread_{}; }; } // namespace thread diff --git a/test/component/test_cases/diagnostic_message_test.cpp b/test/component/test_cases/diagnostic_message_test.cpp index 8ae16b4..a99de05 100644 --- a/test/component/test_cases/diagnostic_message_test.cpp +++ b/test/component/test_cases/diagnostic_message_test.cpp @@ -22,6 +22,8 @@ namespace test { namespace component { namespace test_cases { +// Diag Server name +constexpr std::string_view kDiagServerName{"DiagServer"}; // Diag Test Server Tcp Ip Address constexpr std::string_view kDiagTcpIpAddress{"172.16.25.128"}; // Diag Test Server port number @@ -112,7 +114,7 @@ class DiagMessageFixture : public component::ComponentTest { protected: DiagMessageFixture() - : tcp_acceptor_{kDiagTcpIpAddress, kDiagTcpPortNum, 1u}, + : tcp_acceptor_{kDiagServerName, kDiagTcpIpAddress, kDiagTcpPortNum, 1u}, doip_tcp_handler_{}, diag_client_{diag::client::CreateDiagnosticClient(kDiagClientConfigPath)} {} diff --git a/test/component/test_cases/routing_activation_test.cpp b/test/component/test_cases/routing_activation_test.cpp index 449f03a..60043f0 100644 --- a/test/component/test_cases/routing_activation_test.cpp +++ b/test/component/test_cases/routing_activation_test.cpp @@ -22,6 +22,8 @@ namespace test { namespace component { namespace test_cases { +// Tls Server name +constexpr std::string_view kDiagServerName{"DiagServer"}; // Diag Test Server Tcp Ip Address constexpr std::string_view kDiagTcpIpAddress{"172.16.25.128"}; // Diag Test Server port number @@ -54,7 +56,7 @@ class RoutingActivationFixture : public component::ComponentTest { protected: RoutingActivationFixture() - : tcp_acceptor_{kDiagTcpIpAddress, kDiagTcpPortNum, 1u}, + : tcp_acceptor_{kDiagServerName, kDiagTcpIpAddress, kDiagTcpPortNum, 1u}, doip_tcp_handler_{}, diag_client_{diag::client::CreateDiagnosticClient(kDiagClientConfigPath)} {} diff --git a/test/component/test_cases/tls_test.cpp b/test/component/test_cases/tls_test.cpp index c6d12b9..abe97a8 100644 --- a/test/component/test_cases/tls_test.cpp +++ b/test/component/test_cases/tls_test.cpp @@ -26,10 +26,14 @@ namespace test { namespace component { namespace test_cases { +// Tls Server name +constexpr std::string_view kTlsServerName{"TlsServer"}; // Tls Server Tcp Ip Address constexpr std::string_view kTlsServerIpAddress{"172.16.25.128"}; // Tls Server port number constexpr std::uint16_t kTlsServerTcpPortNum{3496U}; +// Tls client name +constexpr std::string_view kTlsClientName{"TlsClient"}; // Tls client Tcp Ip Address constexpr std::string_view kTlsClientIpAddress{"172.16.25.127"}; // Tls client port number @@ -63,7 +67,8 @@ class Tls12Fixture : public component::ComponentTest { protected: Tls12Fixture() - : tls_acceptor_{kTlsServerIpAddress, + : tls_acceptor_{kTlsServerName, + kTlsServerIpAddress, kTlsServerTcpPortNum, 1u, TlsServerVersion{ @@ -73,7 +78,7 @@ class Tls12Fixture : public component::ComponentTest { kServerPrivateKeyPath}, tls_server_{}, tls_client_{ - kTlsClientIpAddress, kTlsClientTcpPortNum, kCACertificatePath, + kTlsClientName, kTlsClientIpAddress, kTlsClientTcpPortNum, kCACertificatePath, TlsClientVersion{{TlsClientCipherSuite::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TlsClientCipherSuite::TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256}}} {} @@ -131,7 +136,8 @@ class Tls13Fixture : public component::ComponentTest { protected: Tls13Fixture() - : tls_acceptor_{kTlsServerIpAddress, + : tls_acceptor_{kTlsServerName, + kTlsServerIpAddress, kTlsServerTcpPortNum, 1u, TlsServerVersion{{TlsServerCipherSuite::TLS_AES_128_GCM_SHA256, @@ -139,7 +145,7 @@ class Tls13Fixture : public component::ComponentTest { kServerCertificatePath, kServerPrivateKeyPath}, tls_server_{}, - tls_client_{kTlsClientIpAddress, kTlsClientTcpPortNum, kCACertificatePath, + tls_client_{kTlsClientName, kTlsClientIpAddress, kTlsClientTcpPortNum, kCACertificatePath, TlsClientVersion{{TlsClientCipherSuite::TLS_AES_128_GCM_SHA256, TlsClientCipherSuite::TLS_AES_256_GCM_SHA384}}} {} @@ -225,7 +231,7 @@ TEST_F(Tls13Fixture, SendDataFromServerToClient) { .HasValue()); std::this_thread::sleep_for(std::chrono::milliseconds{2000}); - tls_client_.DisconnectFromHost(); + // tls_client_.DisconnectFromHost(); } } // namespace test_cases