Diag-Client-Lib
tls_client.cpp
Go to the documentation of this file.
1 /* Diagnostic Client library
2  * Copyright (C) 2024 Avijit Dey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
10 
17 
18 namespace boost_support {
19 namespace client {
20 namespace tls {
21 namespace {
25 std::string AppendIpAddressAndPort(std::string_view client_name, std::string_view ip_address,
26  std::uint16_t port_num) {
27  std::string connection_name{client_name};
28  connection_name.append("_");
29  connection_name.append(ip_address);
30  connection_name.append("_");
31  connection_name.append(std::to_string(port_num));
32  return connection_name;
33 }
34 } // namespace
35 
39 template<typename TlsVersion>
41  private:
46 
52 
56  enum class State : std::uint8_t {
57  kConnected = 0U,
58  kDisconnected = 1U
59  };
60 
65 
70 
71  public:
79  TlsClientImpl(std::string_view client_name, std::string_view local_ip_address,
80  std::uint16_t local_port_num, std::string_view ca_certification_path,
81  TlsVersion tls_version) noexcept
82  : io_context_{},
83  tls_context_{tls_version, ca_certification_path},
84  connection_state_{State::kDisconnected},
85  client_name_{AppendIpAddressAndPort(client_name, local_ip_address, local_port_num)},
86  tcp_connection_{client_name,
87  TlsSocket{local_ip_address, local_port_num, tls_context_, io_context_}} {}
88 
92  TlsClientImpl(const TlsClientImpl &other) noexcept = delete;
93  TlsClientImpl &operator=(const TlsClientImpl &other) noexcept = delete;
94 
98  TlsClientImpl(TlsClientImpl &&other) noexcept = delete;
99  TlsClientImpl &operator=(TlsClientImpl &&other) noexcept = delete;
100 
104  ~TlsClientImpl() noexcept = default;
105 
109  void Initialize() noexcept { tcp_connection_.Initialize(); }
110 
114  void DeInitialize() noexcept { tcp_connection_.DeInitialize(); }
115 
122  void SetReadHandler(HandlerRead read_handler) noexcept {
123  tcp_connection_.SetReadHandler(std::move(read_handler));
124  }
125 
134  core_type::Result<void> ConnectToHost(std::string_view host_ip_address,
135  std::uint16_t host_port_num) {
138  if (connection_state_.load(std::memory_order_seq_cst) != State::kConnected) {
139  if (tcp_connection_.ConnectToHost(host_ip_address, host_port_num)) {
140  connection_state_.store(State::kConnected, std::memory_order_seq_cst);
141  result.EmplaceValue();
142  } // else, connect failed
143  } else {
144  // already connected
145  result.EmplaceValue();
146  common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogVerbose(
147  FILE_NAME, __LINE__, __func__,
148  [](std::stringstream &msg) { msg << "Tcp client is already connected"; });
149  }
150  return result;
151  }
152 
160  if (connection_state_.load(std::memory_order_seq_cst) == State::kConnected) {
161  tcp_connection_.DisconnectFromHost();
162  connection_state_.store(State::kDisconnected, std::memory_order_seq_cst);
163  result.EmplaceValue();
164  } else {
165  // Not connected
167  FILE_NAME, __LINE__, __func__,
168  [](std::stringstream &msg) { msg << "Tcp client is in disconnected state"; });
169  }
170  return result;
171  }
172 
177  auto IsConnectedToHost() const noexcept -> bool {
178  return (connection_state_.load(std::memory_order_seq_cst) == State::kConnected);
179  }
180 
190  if (connection_state_.load(std::memory_order_seq_cst) == State::kConnected) {
191  if (tcp_connection_.Transmit(std::move(tcp_message))) { result.EmplaceValue(); }
192  } else {
193  // not connected
195  FILE_NAME, __LINE__, __func__, [](std::stringstream &msg) {
196  msg << "Tcp client is Offline, please connect to server first";
197  });
198  }
199  return result;
200  }
201 
202  private:
207 
212 
216  std::atomic<State> connection_state_;
217 
221  std::string client_name_;
222 
227 };
228 
229 template<typename TlsVersion>
230 TlsClient<TlsVersion>::TlsClient(std::string_view client_name, std::string_view local_ip_address,
231  std::uint16_t local_port_num,
232  std::string_view ca_certification_path,
233  TlsVersion tls_version) noexcept
234  : tls_client_impl_{std::make_unique<TlsClientImpl>(client_name, local_ip_address,
235  local_port_num, ca_certification_path,
236  std::move(tls_version))} {}
237 
238 template<typename TlsVersion>
239 TlsClient<TlsVersion>::TlsClient(TlsClient &&other) noexcept = default;
240 
241 template<typename TlsVersion>
242 TlsClient<TlsVersion> &TlsClient<TlsVersion>::operator=(TlsClient &&other) noexcept = default;
243 
244 template<typename TlsVersion>
245 TlsClient<TlsVersion>::~TlsClient() noexcept = default;
246 
247 template<typename TlsVersion>
248 void TlsClient<TlsVersion>::Initialize() noexcept {
249  tls_client_impl_->Initialize();
250 }
251 
252 template<typename TlsVersion>
254  tls_client_impl_->DeInitialize();
255 }
256 
257 template<typename TlsVersion>
259  tls_client_impl_->SetReadHandler(std::move(read_handler));
260 }
261 
262 template<typename TlsVersion>
264  std::uint16_t host_port_num) {
265  return tls_client_impl_->ConnectToHost(host_ip_address, host_port_num);
266 }
267 
268 template<typename TlsVersion>
270  return tls_client_impl_->DisconnectFromHost();
271 }
272 
273 template<typename TlsVersion>
274 auto TlsClient<TlsVersion>::IsConnectedToHost() const noexcept -> bool {
275  return tls_client_impl_->IsConnectedToHost();
276 }
277 
278 template<typename TlsVersion>
280  return tls_client_impl_->Transmit(std::move(tcp_message));
281 }
282 
283 template class TlsClient<TlsVersion13>;
284 template class TlsClient<TlsVersion12>;
285 
286 } // namespace tls
287 } // namespace client
288 } // namespace boost_support
Class to provide implementation of tls client.
Definition: tls_client.cpp:40
void DeInitialize() noexcept
De-initialize the client.
Definition: tls_client.cpp:114
TlsClientImpl & operator=(TlsClientImpl &&other) noexcept=delete
core_type::Result< void > ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num)
Function to connect to remote ip address and port number.
Definition: tls_client.cpp:134
std::atomic< State > connection_state_
Store the state of tcp connection.
Definition: tls_client.cpp:216
~TlsClientImpl() noexcept=default
Destruct an instance of TcpClientImpl.
core_type::Result< void > Transmit(MessageConstPtr tcp_message)
Function to transmit the provided tcp message.
Definition: tls_client.cpp:187
core_type::Result< void > DisconnectFromHost()
Function to disconnect from remote host if already connected.
Definition: tls_client.cpp:157
void SetReadHandler(HandlerRead read_handler) noexcept
Function to set the read handler that is invoked when message is received.
Definition: tls_client.cpp:122
TlsContext tls_context_
Stores the tls context.
Definition: tls_client.cpp:211
TlsClientImpl(TlsClientImpl &&other) noexcept=delete
Deleted move assignment and move constructor.
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
Constructs an instance of TcpClient.
Definition: tls_client.cpp:79
TlsClientImpl & operator=(const TlsClientImpl &other) noexcept=delete
auto IsConnectedToHost() const noexcept -> bool
Function to get the connection status.
Definition: tls_client.cpp:177
TcpConnectionSecured tcp_connection_
Store the tcp connection.
Definition: tls_client.cpp:226
TlsClientImpl(const TlsClientImpl &other) noexcept=delete
Deleted copy assignment and copy constructor.
Client that manages secured tcp connection.
Definition: tls_client.h:42
void SetReadHandler(HandlerRead read_handler) noexcept
Function to set the read handler that is invoked when message is received.
Definition: tls_client.cpp:258
core_type::Result< void > ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num)
Function to connect to remote ip address and port number.
Definition: tls_client.cpp:263
auto IsConnectedToHost() const noexcept -> bool
Function to get the connection status.
Definition: tls_client.cpp:274
core_type::Result< void > DisconnectFromHost()
Function to disconnect from remote host if already connected.
Definition: tls_client.cpp:269
void DeInitialize() noexcept
De-initialize the client.
Definition: tls_client.cpp:253
boost_support::message::tcp::TcpMessageConstPtr MessageConstPtr
Type alias for Tcp message const pointer.
Definition: tls_client.h:57
core_type::Result< void > Transmit(MessageConstPtr tcp_message)
Function to transmit the provided tcp message.
Definition: tls_client.cpp:279
~TlsClient() noexcept
Destruct an instance of TlsClient.
std::function< void(MessagePtr)> HandlerRead
Tcp function template used for reception.
Definition: tls_client.h:62
TlsClient & operator=(const TlsClient &other) noexcept=delete
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
Constructs an instance of TlsClient.
Definition: tls_client.cpp:230
void Initialize() noexcept
Initialize the client.
Definition: tls_client.cpp:248
static auto GetLibBoostLogger() noexcept -> LibBoostLogger &
Definition: logger.h:20
Wrapper class to hold boost io context required for io object( sockets)
Definition: io_context.h:22
Tls context class responsible for setting cipher suite and loading certificates.
Definition: tls_context.h:24
Class used to create a tcp socket for handling transmission and reception of tcp message from driver.
Definition: tls_socket.h:25
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
Definition: result.h:29
#define FILE_NAME
Definition: file_path.h:14
std::string AppendIpAddressAndPort(std::string_view client_name, std::string_view ip_address, std::uint16_t port_num)
Function to append the ip address and port number to the connection name.
Definition: tls_client.cpp:25
auto MakeErrorCode(BoostSupportErrorDomain::Errc code, BoostSupportErrorDomain::SupportDataType data) noexcept -> core_type::ErrorCode
Create a new ErrorCode within DoipErrorDomain.
State
Definitions of different connection state.
Definition: tls_client.cpp:56