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

Singleton service for background NTP measurements. More...

#include <ntp_time_service.hpp>

Public Member Functions

 NtpTimeServiceT (const NtpTimeServiceT &)=delete
 
NtpTimeServiceToperator= (const NtpTimeServiceT &)=delete
 
bool init ()
 Start background measurements using stored interval.
 
bool init (std::chrono::milliseconds interval, bool measure_immediately=true)
 Start background measurements with interval and immediate flag.
 
void shutdown ()
 Stop background measurements and release resources.
 
bool running () const noexcept
 Return true when background runner is active.
 
void ensure_started () noexcept
 Ensure background runner is started with current config.
 
int64_t offset_us () noexcept
 Return last estimated offset in microseconds.
 
int64_t utc_time_us () noexcept
 Return current UTC time in microseconds based on offset.
 
int64_t utc_time_ms () noexcept
 Return current UTC time in milliseconds based on offset.
 
int64_t utc_time_sec () noexcept
 Return current UTC time in seconds based on offset.
 
bool last_measure_ok () const noexcept
 Return whether last measurement updated the offset.
 
uint64_t measure_count () const noexcept
 Return total number of measurement attempts.
 
uint64_t fail_count () const noexcept
 Return number of failed measurement attempts.
 
int64_t last_update_realtime_us () const noexcept
 Return realtime timestamp of last measurement attempt.
 
int64_t last_success_realtime_us () const noexcept
 Return realtime timestamp of last successful measurement.
 
bool stale (std::chrono::milliseconds max_age) const noexcept
 Return true when last measurement is older than max_age.
 
bool set_servers (std::vector< NtpServerConfig > servers)
 Replace server list used for new runner instances.
 
bool set_default_servers ()
 Use conservative default servers for new runner instances.
 
bool clear_servers ()
 Clear custom server list and return to default behavior.
 
bool set_pool_config (NtpPoolConfig cfg)
 Override pool configuration for new runner instances.
 
NtpPoolConfig pool_config () const
 Return current pool configuration.
 
std::vector< NtpSamplelast_samples () const
 Return copy of last measurement samples.
 
 NtpTimeServiceT ()=default
 Construct service.
 
 ~NtpTimeServiceT ()
 Stop background runner on destruction.
 
bool apply_config_now ()
 Apply current config by rebuilding the runner.
 

Static Public Member Functions

static NtpTimeServiceTinstance () noexcept
 Return the singleton instance.
 

Private Member Functions

bool is_running_locked () const noexcept
 Check runner status under lock.
 
std::unique_ptr< RunnerT > build_runner_locked ()
 Build a runner with current server list and pool config.
 

Private Attributes

std::mutex m_mtx
 
std::chrono::milliseconds m_interval {std::chrono::seconds(30)}
 
bool m_measure_immediately {true}
 
bool m_has_custom_servers {false}
 
std::vector< NtpServerConfigm_servers
 
bool m_has_custom_pool_cfg {false}
 
NtpPoolConfig m_pool_cfg {}
 
std::unique_ptr< RunnerT > m_runner
 

Static Private Attributes

static NtpTimeServiceT m_instance
 

Detailed Description

template<class RunnerT>
class time_shield::NtpTimeServiceT< RunnerT >

Singleton service for background NTP measurements.

Uses an internal runner to keep offset updated. It exposes UTC time computed as realtime clock plus the latest offset. Configure pool servers and sampling before starting the service.

Definition at line 200 of file ntp_time_service.hpp.

Constructor & Destructor Documentation

◆ NtpTimeServiceT() [1/2]

template<class RunnerT>
time_shield::NtpTimeServiceT< RunnerT >::NtpTimeServiceT ( const NtpTimeServiceT< RunnerT > & )
delete

◆ NtpTimeServiceT() [2/2]

template<class RunnerT>
time_shield::NtpTimeServiceT< RunnerT >::NtpTimeServiceT ( )
default

Construct service.

◆ ~NtpTimeServiceT()

template<class RunnerT>
time_shield::NtpTimeServiceT< RunnerT >::~NtpTimeServiceT ( )
inline

Stop background runner on destruction.

Definition at line 447 of file ntp_time_service.hpp.

Member Function Documentation

◆ apply_config_now()

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::apply_config_now ( )
inline

Apply current config by rebuilding the runner.

Returns
True when runner restarted successfully.

Definition at line 453 of file ntp_time_service.hpp.

◆ build_runner_locked()

template<class RunnerT>
std::unique_ptr< RunnerT > time_shield::NtpTimeServiceT< RunnerT >::build_runner_locked ( )
inlineprivate

Build a runner with current server list and pool config.

Definition at line 501 of file ntp_time_service.hpp.

◆ clear_servers()

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::clear_servers ( )
inline

Clear custom server list and return to default behavior.

Returns
False when service is already running.

Definition at line 403 of file ntp_time_service.hpp.

◆ ensure_started()

template<class RunnerT>
void time_shield::NtpTimeServiceT< RunnerT >::ensure_started ( )
inlinenoexcept

Ensure background runner is started with current config.

Definition at line 287 of file ntp_time_service.hpp.

◆ fail_count()

template<class RunnerT>
uint64_t time_shield::NtpTimeServiceT< RunnerT >::fail_count ( ) const
inlinenoexcept

Return number of failed measurement attempts.

Returns
Number of failed measurement attempts.

Definition at line 342 of file ntp_time_service.hpp.

◆ init() [1/2]

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::init ( )
inline

Start background measurements using stored interval.

Returns
True when background runner started.

Definition at line 217 of file ntp_time_service.hpp.

◆ init() [2/2]

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::init ( std::chrono::milliseconds interval,
bool measure_immediately = true )
inline

Start background measurements with interval and immediate flag.

Parameters
intervalMeasurement interval.
measure_immediatelyMeasure before first sleep if true.
Returns
True when background runner started.

Definition at line 225 of file ntp_time_service.hpp.

◆ instance()

template<class RunnerT>
static NtpTimeServiceT & time_shield::NtpTimeServiceT< RunnerT >::instance ( )
inlinestaticnoexcept

Return the singleton instance.

Returns
Singleton instance.

Definition at line 204 of file ntp_time_service.hpp.

◆ is_running_locked()

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::is_running_locked ( ) const
inlineprivatenoexcept

Check runner status under lock.

Definition at line 496 of file ntp_time_service.hpp.

◆ last_measure_ok()

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::last_measure_ok ( ) const
inlinenoexcept

Return whether last measurement updated the offset.

Returns
True when last measurement updated the offset.

Definition at line 326 of file ntp_time_service.hpp.

◆ last_samples()

template<class RunnerT>
std::vector< NtpSample > time_shield::NtpTimeServiceT< RunnerT >::last_samples ( ) const
inline

Return copy of last measurement samples.

Returns
Copy of samples from the last measurement.

Definition at line 438 of file ntp_time_service.hpp.

◆ last_success_realtime_us()

template<class RunnerT>
int64_t time_shield::NtpTimeServiceT< RunnerT >::last_success_realtime_us ( ) const
inlinenoexcept

Return realtime timestamp of last successful measurement.

Returns
Realtime microseconds timestamp for last successful measurement.

Definition at line 358 of file ntp_time_service.hpp.

◆ last_update_realtime_us()

template<class RunnerT>
int64_t time_shield::NtpTimeServiceT< RunnerT >::last_update_realtime_us ( ) const
inlinenoexcept

Return realtime timestamp of last measurement attempt.

Returns
Realtime microseconds timestamp for last measurement attempt.

Definition at line 350 of file ntp_time_service.hpp.

◆ measure_count()

template<class RunnerT>
uint64_t time_shield::NtpTimeServiceT< RunnerT >::measure_count ( ) const
inlinenoexcept

Return total number of measurement attempts.

Returns
Number of measurement attempts.

Definition at line 334 of file ntp_time_service.hpp.

◆ offset_us()

template<class RunnerT>
int64_t time_shield::NtpTimeServiceT< RunnerT >::offset_us ( )
inlinenoexcept

Return last estimated offset in microseconds.

Returns
Offset in microseconds (UTC - local realtime).

Definition at line 296 of file ntp_time_service.hpp.

◆ operator=()

template<class RunnerT>
NtpTimeServiceT & time_shield::NtpTimeServiceT< RunnerT >::operator= ( const NtpTimeServiceT< RunnerT > & )
delete

◆ pool_config()

template<class RunnerT>
NtpPoolConfig time_shield::NtpTimeServiceT< RunnerT >::pool_config ( ) const
inline

Return current pool configuration.

Returns
Current pool configuration.

Definition at line 428 of file ntp_time_service.hpp.

◆ running()

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::running ( ) const
inlinenoexcept

Return true when background runner is active.

Returns
True when background runner is active.

Definition at line 281 of file ntp_time_service.hpp.

◆ set_default_servers()

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::set_default_servers ( )
inline

Use conservative default servers for new runner instances.

Returns
False when service is already running.

Definition at line 391 of file ntp_time_service.hpp.

◆ set_pool_config()

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::set_pool_config ( NtpPoolConfig cfg)
inline

Override pool configuration for new runner instances.

Parameters
cfgPool configuration to apply.
Returns
False when service is already running.

Definition at line 416 of file ntp_time_service.hpp.

◆ set_servers()

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::set_servers ( std::vector< NtpServerConfig > servers)
inline

Replace server list used for new runner instances.

Parameters
serversServer configurations to use.
Returns
False when service is already running.

Definition at line 379 of file ntp_time_service.hpp.

◆ shutdown()

template<class RunnerT>
void time_shield::NtpTimeServiceT< RunnerT >::shutdown ( )
inline

Stop background measurements and release resources.

Definition at line 263 of file ntp_time_service.hpp.

◆ stale()

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::stale ( std::chrono::milliseconds max_age) const
inlinenoexcept

Return true when last measurement is older than max_age.

Parameters
max_ageMaximum allowed age.
Returns
True when last measurement age exceeds max_age.

Definition at line 367 of file ntp_time_service.hpp.

◆ utc_time_ms()

template<class RunnerT>
int64_t time_shield::NtpTimeServiceT< RunnerT >::utc_time_ms ( )
inlinenoexcept

Return current UTC time in milliseconds based on offset.

Returns
UTC time in milliseconds using last offset.

Definition at line 314 of file ntp_time_service.hpp.

◆ utc_time_sec()

template<class RunnerT>
int64_t time_shield::NtpTimeServiceT< RunnerT >::utc_time_sec ( )
inlinenoexcept

Return current UTC time in seconds based on offset.

Returns
UTC time in seconds using last offset.

Definition at line 320 of file ntp_time_service.hpp.

◆ utc_time_us()

template<class RunnerT>
int64_t time_shield::NtpTimeServiceT< RunnerT >::utc_time_us ( )
inlinenoexcept

Return current UTC time in microseconds based on offset.

Returns
UTC time in microseconds using last offset.

Definition at line 305 of file ntp_time_service.hpp.

Member Data Documentation

◆ m_has_custom_pool_cfg

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::m_has_custom_pool_cfg {false}
private

Definition at line 530 of file ntp_time_service.hpp.

◆ m_has_custom_servers

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::m_has_custom_servers {false}
private

Definition at line 527 of file ntp_time_service.hpp.

◆ m_instance

template<class RunnerT>
NtpTimeServiceT time_shield::NtpTimeServiceT< RunnerT >::m_instance
staticprivate

Definition at line 536 of file ntp_time_service.hpp.

◆ m_interval

template<class RunnerT>
std::chrono::milliseconds time_shield::NtpTimeServiceT< RunnerT >::m_interval {std::chrono::seconds(30)}
private

Definition at line 524 of file ntp_time_service.hpp.

◆ m_measure_immediately

template<class RunnerT>
bool time_shield::NtpTimeServiceT< RunnerT >::m_measure_immediately {true}
private

Definition at line 525 of file ntp_time_service.hpp.

◆ m_mtx

template<class RunnerT>
std::mutex time_shield::NtpTimeServiceT< RunnerT >::m_mtx
mutableprivate

Definition at line 523 of file ntp_time_service.hpp.

◆ m_pool_cfg

template<class RunnerT>
NtpPoolConfig time_shield::NtpTimeServiceT< RunnerT >::m_pool_cfg {}
private

Definition at line 531 of file ntp_time_service.hpp.

◆ m_runner

template<class RunnerT>
std::unique_ptr<RunnerT> time_shield::NtpTimeServiceT< RunnerT >::m_runner
private

Definition at line 533 of file ntp_time_service.hpp.

◆ m_servers

template<class RunnerT>
std::vector<NtpServerConfig> time_shield::NtpTimeServiceT< RunnerT >::m_servers
private

Definition at line 528 of file ntp_time_service.hpp.


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