|
SatCat5
|
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>
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::Callback > | m_callbacks |
| Linked list of registered callback objects. | |
Private Member Functions | |
| void | query (unsigned elapsed_msec) |
Private Attributes | |
| satcat5::net::Protocol * | m_next |
| Linked list of Protocols. | |
| unsigned | m_trem |
| unsigned | m_tnext |
| 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.
| 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.
|
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:
The child class MUST override this method.
Implements satcat5::net::Protocol.
Definition at line 73 of file ntp_client.cc.