java.lang.Object
net.i2p.time.Timestamper
net.i2p.router.time.RouterTimestamper
- All Implemented Interfaces:
Runnable
Periodically query a series of NTP servers and update any associated listeners. It tries the NTP servers in order, contacting them using SNTP (UDP port 123).
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classFlushes timestamp data when the router shuts down.Nested classes/interfaces inherited from class Timestamper
Timestamper.UpdateListenerModifier and TypeClassDescriptionstatic interfaceInterface to receive update notifications for when we query the time Only used by Clock.Field Summary
Modifier and TypeFieldDescriptionprivate intServers queried per round.private intQuery rounds failed in a row.private final I2PAppContextThe app context.private final booleanTrue to run the thread as a daemon.private booleanTrue while NTP is disabled.private booleanTrue once the first query succeeded.private booleanTrue while the timestamper thread runs.private final List<Timestamper.UpdateListener> Listeners notified of clock updates.private LogLogger, may be null before the context is ready.NTP servers grouped by priority.private intInterval between queries, in ms.The NTP server names configured.private ThreadThe thread running the queries.private booleanTrue when the clock is well synchronized.private final ZonesZone file for the stratum calculations.private static final intNumber of servers to query when changing the clock.private static final booleanDefault setting, NTP off until enabled.private static final intDefault query interval.private static final StringDefault NTP servers, comma separated.private static final intDefault query timeout, in ms.private static final intFailed rounds before slowing down.private static final intIf different SNTP servers differ by more than 10s, someone is b0rked.private static final longTime callers wait for the first query.private static final intShortest query interval allowed.static final StringProperty key for how many SNTP servers are queried at once.static final StringProperty key that turns time synchronization off.static final StringProperty key holding the country of our last public IP address.static final StringProperty key for the milliseconds between time queries.static final StringProperty key naming the SNTP servers to query.private static final intQuery timeout while well synced, in ms.Constructor Summary
ConstructorsConstructorDescriptionDoes not start.Does not start.RouterTimestamper(I2PAppContext ctx, Timestamper.UpdateListener lsnr, boolean daemon) Does not start.Method Summary
Modifier and TypeMethodDescriptionvoidRegister a listener to be notified of time updates.booleanWhether NTP is disabled.getListener(int index) Time update listener at the given index.intNumber of registered time update listeners.intHow often to query the NTP servers, in ms.getServer(int index) NTP server at the given index in the configured list.intNumber of NTP servers in the configured list.private booleanQuery the NTP servers, compare the results, and stamp the time if they agree.voidUnregister a listener from time update notifications.voidrun()Run the main SNTP query loop, updating the time periodically.private voidstampTime(long now, int stratum) Notify the listeners.voidStart the SNTP query thread, unless disabled or already initialized.voidUpdate the time immediately.private voidReload all the config elements from the appContext.voidWait until the initial time synchronization completes, or the timeout is reached.
Field Details
_context
The app context._log
Logger, may be null before the context is ready._servers
_priorityServers
_listeners
Listeners notified of clock updates._queryFrequency
private int _queryFrequencyInterval between queries, in ms._concurringServers
private int _concurringServersServers queried per round._consecutiveFails
private int _consecutiveFailsQuery rounds failed in a row._disabled
private volatile boolean _disabledTrue while NTP is disabled._daemon
private final boolean _daemonTrue to run the thread as a daemon._initialized
private volatile boolean _initializedTrue once the first query succeeded._wellSynced
private boolean _wellSyncedTrue when the clock is well synchronized._isRunning
private volatile boolean _isRunningTrue while the timestamper thread runs._timestamperThread
The thread running the queries._zones
Zone file for the stratum calculations.MIN_QUERY_FREQUENCY
private static final int MIN_QUERY_FREQUENCYShortest query interval allowed.- See Also:
DEFAULT_QUERY_FREQUENCY
private static final int DEFAULT_QUERY_FREQUENCYDefault query interval.- See Also:
DEFAULT_SERVER_LIST
Default NTP servers, comma separated.- See Also:
DEFAULT_DISABLED
private static final boolean DEFAULT_DISABLEDDefault setting, NTP off until enabled.- See Also:
DEFAULT_CONCURRING_SERVERS
private static final int DEFAULT_CONCURRING_SERVERSNumber of servers to query when changing the clock.- See Also:
MAX_CONSECUTIVE_FAILS
private static final int MAX_CONSECUTIVE_FAILSFailed rounds before slowing down.- See Also:
DEFAULT_TIMEOUT
SHORT_TIMEOUT
private static final int SHORT_TIMEOUTQuery timeout while well synced, in ms.- See Also:
MAX_WAIT_INITIALIZATION
private static final long MAX_WAIT_INITIALIZATIONTime callers wait for the first query.- See Also:
PROP_QUERY_FREQUENCY
Property key for the milliseconds between time queries.- See Also:
PROP_SERVER_LIST
Property key naming the SNTP servers to query.- See Also:
PROP_DISABLED
Property key that turns time synchronization off.- See Also:
PROP_CONCURRING_SERVERS
Property key for how many SNTP servers are queried at once.- See Also:
PROP_IP_COUNTRY
Property key holding the country of our last public IP address.- See Also:
MAX_VARIANCE
private static final int MAX_VARIANCEIf different SNTP servers differ by more than 10s, someone is b0rked.- See Also:
Constructor Details
RouterTimestamper
Does not start. Caller MUST callstartTimestamper().- Parameters:
ctx- the application context used to read properties and locate the router
RouterTimestamper
Does not start. Caller MUST call startTimestamper().- Parameters:
ctx- the application context used to read properties and locate the routerlsnr- listener notified of each time update, or null to register none yet
RouterTimestamper
Does not start. Caller MUST call startTimestamper().- Parameters:
ctx- the application context used to read properties and locate the routerlsnr- listener notified of each time update, or null to register none yetdaemon- true to run the query thread as a daemon the JVM may abandon at shutdown
Method Details
getServerCount
public int getServerCount()Number of NTP servers in the configured list.- Returns:
- the server count
getServer
NTP server at the given index in the configured list.- Parameters:
index- zero-based position within the configured server list- Returns:
- the NTP server address at that position in the configured list
getQueryFrequencyMs
public int getQueryFrequencyMs()How often to query the NTP servers, in ms.- Returns:
- the query frequency ms
getIsDisabled
public boolean getIsDisabled()Whether NTP is disabled.- Returns:
- true when NTP is switched off at startup, either by configuration or because the VM comm system is in use
addListener
Register a listener to be notified of time updates.- Parameters:
lsnr- the listener to add; adding the same listener twice registers it once
removeListener
Unregister a listener from time update notifications.- Parameters:
lsnr- the listener to remove; absent listeners are ignored
getListenerCount
public int getListenerCount()Number of registered time update listeners.- Returns:
- the listener count
getListener
Time update listener at the given index.- Parameters:
index- zero-based position within the registered listener list- Returns:
- the registered time update listener at that position
startTimestamper
public void startTimestamper()Start the SNTP query thread, unless disabled or already initialized.waitForInitialization
public void waitForInitialization()Wait until the initial time synchronization completes, or the timeout is reached.- Overrides:
waitForInitializationin classTimestamper
timestampNow
run
public void run()Run the main SNTP query loop, updating the time periodically.- Specified by:
runin interfaceRunnable- Overrides:
runin classTimestamper
queryTime
private boolean queryTime(String[] serverList, int perServerTimeout, boolean preferIPv6) throws IllegalArgumentException Query the NTP servers, compare the results, and stamp the time if they agree.- Parameters:
serverList- the NTP servers to query, tried in random orderperServerTimeout- how long to wait for each server's reply, in millisecondspreferIPv6- true to query the server's IPv6 address first- Returns:
- true if the time was stamped, false if the queries did not agree
- Throws:
IllegalArgumentException- if none of the servers are reachable
stampTime
private void stampTime(long now, int stratum) Notify the listeners.- Parameters:
now- the current internet time in millisecondsstratum- 1-15, 1 being the best
updateConfig
private void updateConfig()Reload all the config elements from the appContext. No logging allowed here.