SatCat5
satcat5::ntp::Client Class Reference

Detailed Description

Client and/or server for the Network Time Protocol (NTP).

This class implements a combined client and server for the Network Time Protocol, version 4 (NTPv4), as defined in IETF RFC-5905. The client follows the simplified rules (aka "SNTP") defined in Section 14, with no more than one upstream parent and no peers in the same stratum.

In all modes, the underlying clock is a ptp::TrackingClock object. In server mode, the clock is used as a read-only reference. In client mode, the class will issue ptp::Callback notifications that can be used to discipline the clock (see also: "ptp_tracking.h").

Server mode is enabled using server_start(...) method. If active, the class responds to valid incoming queries immediately, with no attempt to maintain state or rate-limiting. This behavior is not suited for untrusted networks and may be changed in future updates. Server mode and client mode are not mutually exclusive.

Client mode is activated by calling client_connect(...). While client mode is active, the class regularly sends a query to the server; whenever a valid reply is received, it notifies any attached ptp::Callback objects. (See "ptp_tracking.h" for useful examples.) The callbacks should adjust the underlying clock to bring everything into sync; this class does not implement the recommended filter algorithms from Sections 10 and 12.

Broadcast mode and peer-to-peer associations are not currently supported.

Definition at line 43 of file ntp_client.h.

#include <ntp_client.h>

Inheritance diagram for satcat5::ntp::Client:
[legend]
Collaboration diagram for satcat5::ntp::Client:
[legend]

Public Member Functions

 Client (satcat5::ptp::TrackingClock *refclk, satcat5::udp::Dispatch *iface)
 Set the reference clock and network interface for this client.
 
void client_connect (const satcat5::ip::Addr &server, s8 poll_rate=satcat5::ntp::Header::TIME_1MIN)
 Enable client mode by connecting to the specified server. More...
 
void client_close ()
 Close the connection to a remote server.
 
bool client_ok ()
 Is the client currently connected?
 
void client_set_rate (s8 poll_rate)
 Set the client polling rate. More...
 
void server_start (u8 stratum)
 Enable or disable server mode.
 
u64 ntp_now () const
 Get the current time in NTP format.
 
u64 to_ntp (const satcat5::ptp::Time &t) const
 Convert any PTP timestamp to NTP format.
 
satcat5::ptp::Time to_ptp (u64 t) const
 Convert NTP timestamp to PTP format.
 
void timer_once (unsigned msec)
 Configure a one-time notification after X milliseconds.
 
void timer_every (unsigned msec)
 Configure a repeating notification every X milliseconds.
 
void timer_stop ()
 Stop all future notifications.
 
unsigned timer_interval () const
 Accessor for recurring timer interval, if one is set.
 
unsigned timer_remaining () const
 Accessor for time to next event, if one is set.
 
void add_callback (satcat5::ptp::Callback *callback)
 
void remove_callback (satcat5::ptp::Callback *callback)
 

Static Public Member Functions

static unsigned count_timer ()
 Count all objects of this type, including idle timers.
 

Protected Member Functions

void frame_rcvd (satcat5::io::LimitedRead &src) override
 Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value. More...
 
void timer_event () override
 Child class MUST override this method.
 
void rcvd_reply (const satcat5::ntp::Header &msg, u64 rxtime)
 
bool send_reply (const satcat5::ntp::Header &msg, u64 rxtime)
 
bool send_query ()
 
void notify_callbacks (const satcat5::ptp::Measurement &meas)
 Notify all Callback objects of a new Measurement.
 

Protected Attributes

satcat5::ptp::TrackingClock *const m_refclk
 
satcat5::udp::Address m_iface
 
u64 m_reftime
 
u8 m_leap
 
u8 m_stratum
 
s8 m_rate
 
satcat5::net::Type m_filter
 Incoming packet filter.
 
satcat5::util::List< satcat5::ptp::Callbackm_callbacks
 Linked list of registered callback objects.
 

Private Member Functions

void query (unsigned elapsed_msec)
 

Private Attributes

satcat5::net::Protocolm_next
 Linked list of Protocols.
 
unsigned m_trem
 
unsigned m_tnext
 

Member Function Documentation

◆ client_connect()

void Client::client_connect ( const satcat5::ip::Addr server,
s8  poll_rate = satcat5::ntp::Header::TIME_1MIN 
)

Enable client mode by connecting to the specified server.

Polling rate is once every 2^N seconds (see ntp::Header).

Definition at line 57 of file ntp_client.cc.

◆ client_set_rate()

void Client::client_set_rate ( s8  poll_rate)

Set the client polling rate.

Polling rate is once every 2^N seconds (see ntp::Header).

Definition at line 68 of file ntp_client.cc.

◆ frame_rcvd()

void Client::frame_rcvd ( satcat5::io::LimitedRead src)
overrideprotectedvirtual

Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.

The child class SHOULD read the frame contents from the provided "src" object, which is only valid until the function returns.

To send a reply:

  • Call m_iface->open_reply() to obtain a Writable object. (This also writes out any applicable frame headers.)
  • Write frame contents, then call write_finalize().

The child class MUST override this method.

Implements satcat5::net::Protocol.

Definition at line 73 of file ntp_client.cc.


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