java.lang.Object
net.i2p.time.Timestamper
net.i2p.router.time.RouterTimestamper
All Implemented Interfaces:
Runnable

public class RouterTimestamperextends Timestamper
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).
  • Field Details

    • _context

      private final I2PAppContext _context
      The app context.
    • _log

      private Log _log
      Logger, may be null before the context is ready.
    • _servers

      private final List<String> _servers
      The NTP server names configured.
    • _priorityServers

      private List<List<String>> _priorityServers
      NTP servers grouped by priority.
    • _listeners

      private final List<Timestamper.UpdateListener> _listeners
      Listeners notified of clock updates.
    • _queryFrequency

      private int _queryFrequency
      Interval between queries, in ms.
    • _concurringServers

      private int _concurringServers
      Servers queried per round.
    • _consecutiveFails

      private int _consecutiveFails
      Query rounds failed in a row.
    • _disabled

      private volatile boolean _disabled
      True while NTP is disabled.
    • _daemon

      private final boolean _daemon
      True to run the thread as a daemon.
    • _initialized

      private volatile boolean _initialized
      True once the first query succeeded.
    • _wellSynced

      private boolean _wellSynced
      True when the clock is well synchronized.
    • _isRunning

      private volatile boolean _isRunning
      True while the timestamper thread runs.
    • _timestamperThread

      private Thread _timestamperThread
      The thread running the queries.
    • _zones

      private final Zones _zones
      Zone file for the stratum calculations.
    • MIN_QUERY_FREQUENCY

      private static final int MIN_QUERY_FREQUENCY
      Shortest query interval allowed.
      See Also:
    • DEFAULT_QUERY_FREQUENCY

      private static final int DEFAULT_QUERY_FREQUENCY
      Default query interval.
      See Also:
    • DEFAULT_SERVER_LIST

      private static final String DEFAULT_SERVER_LIST
      Default NTP servers, comma separated.
      See Also:
    • DEFAULT_DISABLED

      private static final boolean DEFAULT_DISABLED
      Default setting, NTP off until enabled.
      See Also:
    • DEFAULT_CONCURRING_SERVERS

      private static final int DEFAULT_CONCURRING_SERVERS
      Number of servers to query when changing the clock.
      See Also:
    • MAX_CONSECUTIVE_FAILS

      private static final int MAX_CONSECUTIVE_FAILS
      Failed rounds before slowing down.
      See Also:
    • DEFAULT_TIMEOUT

      private static final int DEFAULT_TIMEOUT
      Default query timeout, in ms.
      See Also:
    • SHORT_TIMEOUT

      private static final int SHORT_TIMEOUT
      Query timeout while well synced, in ms.
      See Also:
    • MAX_WAIT_INITIALIZATION

      private static final long MAX_WAIT_INITIALIZATION
      Time callers wait for the first query.
      See Also:
    • PROP_QUERY_FREQUENCY

      public static final String PROP_QUERY_FREQUENCY
      Property key for the milliseconds between time queries.
      See Also:
    • PROP_SERVER_LIST

      public static final String PROP_SERVER_LIST
      Property key naming the SNTP servers to query.
      See Also:
    • PROP_DISABLED

      public static final String PROP_DISABLED
      Property key that turns time synchronization off.
      See Also:
    • PROP_CONCURRING_SERVERS

      public static final String PROP_CONCURRING_SERVERS
      Property key for how many SNTP servers are queried at once.
      See Also:
    • PROP_IP_COUNTRY

      public static final String PROP_IP_COUNTRY
      Property key holding the country of our last public IP address.
      See Also:
    • MAX_VARIANCE

      private static final int MAX_VARIANCE
      If different SNTP servers differ by more than 10s, someone is b0rked.
      See Also:
  • Constructor Details

    • RouterTimestamper

      public RouterTimestamper(I2PAppContext ctx)
      Does not start. Caller MUST call startTimestamper().
      Parameters:
      ctx - the application context used to read properties and locate the router
    • RouterTimestamper

      public RouterTimestamper(I2PAppContext ctx, Timestamper.UpdateListener lsnr)
      Does not start. Caller MUST call startTimestamper().
      Parameters:
      ctx - the application context used to read properties and locate the router
      lsnr - listener notified of each time update, or null to register none yet
    • RouterTimestamper

      public RouterTimestamper(I2PAppContext ctx, Timestamper.UpdateListener lsnr, boolean daemon)
      Does not start. Caller MUST call startTimestamper().
      Parameters:
      ctx - the application context used to read properties and locate the router
      lsnr - listener notified of each time update, or null to register none yet
      daemon - 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

      public String getServer(int index)
      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

      public void addListener(Timestamper.UpdateListener lsnr)
      Register a listener to be notified of time updates.
      Parameters:
      lsnr - the listener to add; adding the same listener twice registers it once
    • removeListener

      public void removeListener(Timestamper.UpdateListener lsnr)
      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

      public Timestamper.UpdateListener getListener(int index)
      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:
      waitForInitialization in class Timestamper
    • timestampNow

      public void timestampNow()
      Update the time immediately.
      Overrides:
      timestampNow in class Timestamper
    • run

      public void run()
      Run the main SNTP query loop, updating the time periodically.
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Timestamper
    • 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 order
      perServerTimeout - how long to wait for each server's reply, in milliseconds
      preferIPv6 - 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 milliseconds
      stratum - 1-15, 1 being the best
    • updateConfig

      private void updateConfig()
      Reload all the config elements from the appContext. No logging allowed here.