Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_shield::NtpClientPoolT< ClientT > Class Template Reference

Pool of NTP servers: rate-limited multi-server offset estimation. More...

#include <ntp_client_pool.hpp>

Classes

struct  ServerState
 Runtime state for a configured server. More...
 

Public Member Functions

 NtpClientPoolT (NtpPoolConfig cfg={})
 Construct pool with configuration.
 
 NtpClientPoolT (const NtpClientPoolT &)=delete
 
NtpClientPoolToperator= (const NtpClientPoolT &)=delete
 
 NtpClientPoolT (NtpClientPoolT &&other) noexcept
 Move-construct pool state.
 
NtpClientPoolToperator= (NtpClientPoolT &&other) noexcept
 Move-assign pool state.
 
void set_servers (std::vector< NtpServerConfig > servers)
 Replace server list (keeps pool config).
 
void add_server (NtpServerConfig server_cfg)
 Add one server.
 
void set_default_servers ()
 Replace server list with a conservative default set.
 
void clear_servers ()
 Clear server list.
 
bool measure ()
 Perform measurement using current config (queries up to sample_servers).
 
bool measure_n (std::size_t servers_to_sample)
 Perform measurement using a custom number of servers.
 
int64_t offset_us () const noexcept
 Last estimated pool offset (µs).
 
int64_t utc_time_us () const noexcept
 Current UTC time in microseconds based on pool offset.
 
int64_t utc_time_ms () const noexcept
 Current UTC time in milliseconds based on pool offset.
 
int64_t utc_time_sec () const noexcept
 Current UTC time in seconds based on pool offset.
 
std::vector< NtpSamplelast_samples () const
 Returns last measurement samples (copy).
 
bool apply_samples (const std::vector< NtpSample > &samples)
 Apply pre-collected samples (testing/offline).
 
NtpPoolConfig config () const
 Access config.
 
void set_config (NtpPoolConfig cfg)
 Replace pool configuration.
 

Static Public Member Functions

static std::vector< NtpServerConfigbuild_default_servers ()
 Build a conservative default server list.
 
static int64_t median (std::vector< int64_t > &values)
 Returns median of values.
 
static int64_t median_mad_trim (std::vector< int64_t > &offsets)
 Median with MAD trimming.
 
static int64_t best_delay_offset (const std::vector< NtpSample > &samples)
 Offset from best (lowest) delay sample.
 

Public Attributes

NtpPoolConfig m_cfg
 
std::mutex m_mtx
 
std::vector< ServerStatem_servers
 
std::vector< NtpSamplem_last_samples
 
std::atomic< int64_t > m_offset_us
 
std::mt19937_64 m_rng
 

Private Member Functions

std::vector< std::size_t > pick_servers_locked (std::size_t servers_to_sample)
 
NtpSample query_one (std::size_t server_index)
 
void update_server_state_after_query (std::size_t index, const NtpSample &sample)
 
bool update_from_samples (const std::vector< NtpSample > &samples, const NtpPoolConfig &cfg)
 

Static Private Member Functions

static std::uint64_t init_seed (std::uint64_t seed)
 

Detailed Description

template<class ClientT>
class time_shield::NtpClientPoolT< ClientT >

Pool of NTP servers: rate-limited multi-server offset estimation.

Template Parameters
ClientTNTP client type with interface: ClientT(const std::string& host, int port); bool query(); // may throw int last_error_code() const; int64_t offset_us() const; int64_t delay_us() const; int stratum() const;

Definition at line 78 of file ntp_client_pool.hpp.

Constructor & Destructor Documentation

◆ NtpClientPoolT() [1/3]

template<class ClientT>
time_shield::NtpClientPoolT< ClientT >::NtpClientPoolT ( NtpPoolConfig cfg = {})
inlineexplicit

Construct pool with configuration.

Parameters
cfgPool configuration.

Definition at line 82 of file ntp_client_pool.hpp.

◆ NtpClientPoolT() [2/3]

template<class ClientT>
time_shield::NtpClientPoolT< ClientT >::NtpClientPoolT ( const NtpClientPoolT< ClientT > & )
delete

◆ NtpClientPoolT() [3/3]

template<class ClientT>
time_shield::NtpClientPoolT< ClientT >::NtpClientPoolT ( NtpClientPoolT< ClientT > && other)
inlinenoexcept

Move-construct pool state.

Definition at line 91 of file ntp_client_pool.hpp.

Member Function Documentation

◆ add_server()

template<class ClientT>
void time_shield::NtpClientPoolT< ClientT >::add_server ( NtpServerConfig server_cfg)
inline

Add one server.

Parameters
server_cfgServer configuration to add.

Definition at line 135 of file ntp_client_pool.hpp.

◆ apply_samples()

template<class ClientT>
bool time_shield::NtpClientPoolT< ClientT >::apply_samples ( const std::vector< NtpSample > & samples)
inline

Apply pre-collected samples (testing/offline).

Parameters
samplesSample list to apply.
Returns
True when pool offset updated.
Note
Primarily for tests; does not enforce rate limiting or backoff.

Definition at line 452 of file ntp_client_pool.hpp.

◆ best_delay_offset()

template<class ClientT>
static int64_t time_shield::NtpClientPoolT< ClientT >::best_delay_offset ( const std::vector< NtpSample > & samples)
inlinestatic

Offset from best (lowest) delay sample.

Parameters
samplesSample list to scan.
Returns
Offset from the sample with the lowest delay.

Definition at line 511 of file ntp_client_pool.hpp.

◆ build_default_servers()

template<class ClientT>
static std::vector< NtpServerConfig > time_shield::NtpClientPoolT< ClientT >::build_default_servers ( )
inlinestatic

Build a conservative default server list.

Returns
Default server list with conservative timing settings.

Definition at line 144 of file ntp_client_pool.hpp.

◆ clear_servers()

template<class ClientT>
void time_shield::NtpClientPoolT< ClientT >::clear_servers ( )
inline

Clear server list.

Definition at line 384 of file ntp_client_pool.hpp.

◆ config()

template<class ClientT>
NtpPoolConfig time_shield::NtpClientPoolT< ClientT >::config ( ) const
inline

Access config.

Returns
Current pool configuration.

Definition at line 533 of file ntp_client_pool.hpp.

◆ init_seed()

template<class ClientT>
static std::uint64_t time_shield::NtpClientPoolT< ClientT >::init_seed ( std::uint64_t seed)
inlinestaticprivate

Definition at line 570 of file ntp_client_pool.hpp.

◆ last_samples()

template<class ClientT>
std::vector< NtpSample > time_shield::NtpClientPoolT< ClientT >::last_samples ( ) const
inline

Returns last measurement samples (copy).

Returns
Copy of samples from the last measurement.

Definition at line 443 of file ntp_client_pool.hpp.

◆ measure()

template<class ClientT>
bool time_shield::NtpClientPoolT< ClientT >::measure ( )
inline

Perform measurement using current config (queries up to sample_servers).

Returns
True when pool offset updated.

Definition at line 391 of file ntp_client_pool.hpp.

◆ measure_n()

template<class ClientT>
bool time_shield::NtpClientPoolT< ClientT >::measure_n ( std::size_t servers_to_sample)
inline

Perform measurement using a custom number of servers.

Parameters
servers_to_sampleNumber of servers to query in this measurement.
Returns
True when pool offset updated.

Definition at line 399 of file ntp_client_pool.hpp.

◆ median()

template<class ClientT>
static int64_t time_shield::NtpClientPoolT< ClientT >::median ( std::vector< int64_t > & values)
inlinestatic

Returns median of values.

Parameters
valuesValues to process in-place.
Returns
Median of the input values.

Definition at line 463 of file ntp_client_pool.hpp.

◆ median_mad_trim()

template<class ClientT>
static int64_t time_shield::NtpClientPoolT< ClientT >::median_mad_trim ( std::vector< int64_t > & offsets)
inlinestatic

Median with MAD trimming.

Parameters
offsetsOffset list to process in-place.
Returns
Median after MAD-based trimming.

Definition at line 479 of file ntp_client_pool.hpp.

◆ offset_us()

template<class ClientT>
int64_t time_shield::NtpClientPoolT< ClientT >::offset_us ( ) const
inlinenoexcept

Last estimated pool offset (µs).

Returns
Offset in microseconds (UTC - local realtime).

Definition at line 427 of file ntp_client_pool.hpp.

◆ operator=() [1/2]

template<class ClientT>
NtpClientPoolT & time_shield::NtpClientPoolT< ClientT >::operator= ( const NtpClientPoolT< ClientT > & )
delete

◆ operator=() [2/2]

template<class ClientT>
NtpClientPoolT & time_shield::NtpClientPoolT< ClientT >::operator= ( NtpClientPoolT< ClientT > && other)
inlinenoexcept

Move-assign pool state.

Definition at line 104 of file ntp_client_pool.hpp.

◆ pick_servers_locked()

template<class ClientT>
std::vector< std::size_t > time_shield::NtpClientPoolT< ClientT >::pick_servers_locked ( std::size_t servers_to_sample)
inlineprivate

Definition at line 577 of file ntp_client_pool.hpp.

◆ query_one()

template<class ClientT>
NtpSample time_shield::NtpClientPoolT< ClientT >::query_one ( std::size_t server_index)
inlineprivate

Definition at line 599 of file ntp_client_pool.hpp.

◆ set_config()

template<class ClientT>
void time_shield::NtpClientPoolT< ClientT >::set_config ( NtpPoolConfig cfg)
inline

Replace pool configuration.

Parameters
cfgNew pool configuration.

Definition at line 539 of file ntp_client_pool.hpp.

◆ set_default_servers()

template<class ClientT>
void time_shield::NtpClientPoolT< ClientT >::set_default_servers ( )
inline

Replace server list with a conservative default set.

Definition at line 379 of file ntp_client_pool.hpp.

◆ set_servers()

template<class ClientT>
void time_shield::NtpClientPoolT< ClientT >::set_servers ( std::vector< NtpServerConfig > servers)
inline

Replace server list (keeps pool config).

Parameters
serversServer configurations to use.

Definition at line 122 of file ntp_client_pool.hpp.

◆ update_from_samples()

template<class ClientT>
bool time_shield::NtpClientPoolT< ClientT >::update_from_samples ( const std::vector< NtpSample > & samples,
const NtpPoolConfig & cfg )
inlineprivate

Definition at line 666 of file ntp_client_pool.hpp.

◆ update_server_state_after_query()

template<class ClientT>
void time_shield::NtpClientPoolT< ClientT >::update_server_state_after_query ( std::size_t index,
const NtpSample & sample )
inlineprivate

Definition at line 638 of file ntp_client_pool.hpp.

◆ utc_time_ms()

template<class ClientT>
int64_t time_shield::NtpClientPoolT< ClientT >::utc_time_ms ( ) const
inlinenoexcept

Current UTC time in milliseconds based on pool offset.

Returns
UTC time in milliseconds using pool offset.

Definition at line 435 of file ntp_client_pool.hpp.

◆ utc_time_sec()

template<class ClientT>
int64_t time_shield::NtpClientPoolT< ClientT >::utc_time_sec ( ) const
inlinenoexcept

Current UTC time in seconds based on pool offset.

Returns
UTC time in seconds using pool offset.

Definition at line 439 of file ntp_client_pool.hpp.

◆ utc_time_us()

template<class ClientT>
int64_t time_shield::NtpClientPoolT< ClientT >::utc_time_us ( ) const
inlinenoexcept

Current UTC time in microseconds based on pool offset.

Returns
UTC time in microseconds using pool offset.

Definition at line 431 of file ntp_client_pool.hpp.

Member Data Documentation

◆ m_cfg

template<class ClientT>
NtpPoolConfig time_shield::NtpClientPoolT< ClientT >::m_cfg

Definition at line 559 of file ntp_client_pool.hpp.

◆ m_last_samples

template<class ClientT>
std::vector<NtpSample> time_shield::NtpClientPoolT< ClientT >::m_last_samples

Definition at line 563 of file ntp_client_pool.hpp.

◆ m_mtx

template<class ClientT>
std::mutex time_shield::NtpClientPoolT< ClientT >::m_mtx
mutable

Definition at line 561 of file ntp_client_pool.hpp.

◆ m_offset_us

template<class ClientT>
std::atomic<int64_t> time_shield::NtpClientPoolT< ClientT >::m_offset_us

Definition at line 565 of file ntp_client_pool.hpp.

◆ m_rng

template<class ClientT>
std::mt19937_64 time_shield::NtpClientPoolT< ClientT >::m_rng

Definition at line 567 of file ntp_client_pool.hpp.

◆ m_servers

template<class ClientT>
std::vector<ServerState> time_shield::NtpClientPoolT< ClientT >::m_servers

Definition at line 562 of file ntp_client_pool.hpp.


The documentation for this class was generated from the following file: