Skip to content

Commit

Permalink
Use thread utility to name threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Iandiehard committed Nov 10, 2024
1 parent a4c9398 commit 8a7e4cf
Show file tree
Hide file tree
Showing 25 changed files with 310 additions and 98 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -183,14 +200,18 @@ class TcpClient::TcpClientImpl final {
*/
std::atomic<State> 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<TcpClientImpl>(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<TcpClientImpl>(client_name, local_ip_address, local_port_num)} {}

TcpClient::TcpClient(TcpClient &&other) noexcept = default;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,12 +76,14 @@ class TlsClient<TlsVersion>::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
Expand Down Expand Up @@ -205,11 +221,13 @@ class TlsClient<TlsVersion>::TlsClientImpl final {
};

template<typename TlsVersion>
TlsClient<TlsVersion>::TlsClient(std::string_view local_ip_address, std::uint16_t local_port_num,
TlsClient<TlsVersion>::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<TlsClientImpl>(
local_ip_address, local_port_num, ca_certification_path, std::move(tls_version))} {}
: tls_client_impl_{std::make_unique<TlsClientImpl>(client_name, local_ip_address,
local_port_num, ca_certification_path,
std::move(tls_version))} {}

template<typename TlsVersion>
TlsClient<TlsVersion>::TlsClient(TlsClient &&other) noexcept = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()}} {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <string>
#include <string_view>
#include <utility>

#include "boost-support/error_domain/boost_support_error_domain.h"
Expand Down Expand Up @@ -73,12 +74,13 @@ class TcpConnection<ConnectionType::kClient, Socket> 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_{} {}

Expand Down Expand Up @@ -114,19 +116,20 @@ class TcpConnection<ConnectionType::kClient, Socket> final {
// Open socket
socket_.Open();
// Start thread to receive messages
thread_ = std::thread([this]() {
std::unique_lock<std::mutex> 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<std::mutex> 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();
}
}
}};
}

/**
Expand All @@ -140,7 +143,7 @@ class TcpConnection<ConnectionType::kClient, Socket> final {
running_ = false;
}
cond_var_.notify_all();
if (thread_.joinable()) { thread_.join(); }
thread_.Join();
}

/**
Expand Down Expand Up @@ -216,10 +219,15 @@ class TcpConnection<ConnectionType::kClient, Socket> 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
Expand Down Expand Up @@ -276,12 +284,13 @@ class TcpConnection<ConnectionType::kServer, Socket> 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_{} {}

Expand Down Expand Up @@ -331,7 +340,7 @@ class TcpConnection<ConnectionType::kServer, Socket> final {
*/
void Initialize() noexcept {
// Start thread to receive messages
thread_ = std::thread([this]() {
thread_ = utility::thread::Thread(connection_name_, [this]() {
std::unique_lock<std::mutex> lck(mutex_);
while (!exit_request_) {
if (!running_) {
Expand Down Expand Up @@ -362,7 +371,7 @@ class TcpConnection<ConnectionType::kServer, Socket> final {
running_ = false;
}
cond_var_.notify_all();
if (thread_.joinable()) { thread_.join(); }
thread_.Join();
}

/**
Expand Down Expand Up @@ -405,10 +414,15 @@ class TcpConnection<ConnectionType::kServer, Socket> 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
Expand Down
Loading

0 comments on commit 8a7e4cf

Please sign in to comment.