Xclox
C++11 header-only cross-platform date and time library with an asynchronous NTP client
client.hpp
1 /*
2  * Copyright (c) 2024 Abdullatif Kalla.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE.txt file in the root directory of this source tree.
6  */
7 
8 #ifndef XCLOX_CLIENT_HPP
9 #define XCLOX_CLIENT_HPP
10 
11 #include "query.hpp"
12 
13 #include <list>
14 
15 namespace xclox {
16 
17 namespace ntp {
18 
47  class Client {
48  public:
57 
59 
64  explicit Client(Callback callable)
65  : m_callable(callable)
66  {
67  }
68 
71  {
72  m_pool.join();
73  }
74 
80  void query(const std::string& server, const std::chrono::milliseconds& timeout = std::chrono::milliseconds(DefaultTimeout::ms))
81  {
82  std::lock_guard<std::mutex> lock(m_mutex);
83  purgeQueryList();
84  m_queryList.push_back(Query::start(m_pool, server, m_callable, timeout));
85  }
86 
88  void setCallback(Callback callable)
89  {
90  m_callable = std::move(callable);
91  }
92 
94  void cancel()
95  {
96  std::lock_guard<std::mutex> lock(m_mutex);
97  for (auto query : m_queryList) {
98  if (auto shared = query.lock()) {
99  shared->cancel();
100  }
101  }
102  purgeQueryList();
103  }
104 
105  private:
106  void purgeQueryList()
107  {
108  m_queryList.remove_if(std::mem_fn(&std::weak_ptr<Query>::expired));
109  }
110 
111  Callback m_callable;
112  asio::thread_pool m_pool;
113  std::mutex m_mutex;
114  std::list<std::weak_ptr<Query>> m_queryList;
115  };
116 
117 } // namespace ntp
118 
119 } // namespace xclox
120 
121 #endif // XCLOX_CLIENT_HPP
Client is an asynchronous multi-query NTP client.
Definition: client.hpp:47
void query(const std::string &server, const std::chrono::milliseconds &timeout=std::chrono::milliseconds(DefaultTimeout::ms))
Place a NTP query [thread-safe].
Definition: client.hpp:80
void setCallback(Callback callable)
Register a callable for reporting the result of the query back to the caller.
Definition: client.hpp:88
void cancel()
Cancel all current queries [thread-safe].
Definition: client.hpp:94
Query::Callback Callback
Type of query callback.
Definition: client.hpp:54
~Client()
Default destructor.
Definition: client.hpp:70
Client(Callback callable)
Default constructor.
Definition: client.hpp:64
Query::DefaultTimeout DefaultTimeout
Type of query timeout holder.
Definition: client.hpp:56
internal::DefaultTimeout< QuerySingle, 5000 > DefaultTimeout
Type of query timeout milliseconds holder.
Definition: query.hpp:65
std::function< void(const std::string &, const std::string &, Status, const Packet &, const std::chrono::steady_clock::duration &)> Callback
Type of query callback.
Definition: query.hpp:64
static std::weak_ptr< Query > start(asio::thread_pool &pool, const std::string &server, Callback callback, const std::chrono::milliseconds &timeout=std::chrono::milliseconds(DefaultTimeout::ms))
Starts querying all resolved addresses of server one at a time until success.
Definition: query.hpp:87
Status
Type of query status.
Definition: query.hpp:55