java.lang.Object
net.i2p.util.Clock
All Implemented Interfaces:
Timestamper.UpdateListener
Direct Known Subclasses:
RouterClock

public class Clockextends Object implements Timestamper.UpdateListener
Alternate location for determining the time which takes into account an offset. This offset will ideally be periodically updated so as to serve as the difference between the local computer's current time and the time as known by some reference (such as an NTP synchronized clock).

Protected members are used in the subclass RouterClock, which has access to a router's transports (particularly peer clock skews) to second-guess the sanity of clock adjustments.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Listener for clock offset changes.
  • Field Summary

    Modifier and Type
    Field
    Description
    protected boolean
    Whether the offset has ever been changed.
    protected final I2PAppContext
    The I2P application context.
    How often the clock is reconfigured, in ms.
    protected final boolean
    Whether the system clock was determined to be invalid at startup.
    How many times the clock has been reconfigured.
    Notified when the clock is reconfigured.
    protected long
    The current offset from system time.
    private AtomicLong
    Last time read from the saved clock file.
    protected long
    The time when this clock was started.
    protected boolean
    Whether the clock skew stat has been created.
    static final long
    After we've started up and shifted the clock, don't allow shifts of more than 10 minutes.
    static final long
    If the clock is skewed by 3+ days, forget it.
    static final long
    If the clock skew changes by less than this, ignore the update (so we don't slide all over the place).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new Clock with the given context, detecting system clock validity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Register a listener for clock offset changes.
    protected void
    fireOffsetChanged(long delta)
    Notify registered listeners of a clock offset change.
    static Clock
    Shared clock for code without a router context.
    protected Log
    Fetch the log on demand to avoid circular dependencies (logging uses the clock).
    long
    Current offset from system time.
    This is a dummy, see RouterClock and RouterTimestamper for the real thing.
    boolean
    Check whether the offset has been updated at least once.
    long
    now()
    Retrieve the current time synchronized with whatever reference clock is in use.
    void
    Unregister a listener for clock offset changes.
    void
    setNow(long realTime)
    Adjust the clock to the given real time, computing the offset.
    void
    setNow(long realTime, int stratum)
    Warning - overridden in RouterClock.
    void
    setOffset(long offsetMs)
    Specify how far away from the "correct" time the computer is - a positive value means that the system time is slow, while a negative value means the system time is fast.
    void
    setOffset(long offsetMs, boolean force)
    Specify how far away from the "correct" time the computer is - a positive value means that the system time is slow, while a negative value means the system time is fast.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • _context

      protected final I2PAppContext _context
      The I2P application context.
    • _isSystemClockBad

      protected final boolean _isSystemClockBad
      Whether the system clock was determined to be invalid at startup.
    • _startedOn

      protected long _startedOn
      The time when this clock was started.
    • _statCreated

      protected boolean _statCreated
      Whether the clock skew stat has been created.
    • _offset

      protected volatile long _offset
      The current offset from system time.
    • _alreadyChanged

      protected boolean _alreadyChanged
      Whether the offset has ever been changed.
    • _listeners

      private final Set<Clock.ClockUpdateListener> _listeners
      Notified when the clock is reconfigured.
    • _iter

      private AtomicInteger _iter
      How many times the clock has been reconfigured.
    • _frequency

      private AtomicInteger _frequency
      How often the clock is reconfigured, in ms.
    • _savedTime

      private AtomicLong _savedTime
      Last time read from the saved clock file.
    • MAX_OFFSET

      public static final long MAX_OFFSET
      If the clock is skewed by 3+ days, forget it.
      See Also:
    • MAX_LIVE_OFFSET

      public static final long MAX_LIVE_OFFSET
      After we've started up and shifted the clock, don't allow shifts of more than 10 minutes.
      See Also:
    • MIN_OFFSET_CHANGE

      public static final long MIN_OFFSET_CHANGE
      If the clock skew changes by less than this, ignore the update (so we don't slide all over the place).
      See Also:
  • Constructor Details

    • Clock

      public Clock(I2PAppContext context)
      Create a new Clock with the given context, detecting system clock validity.
      Parameters:
      context - the I2P app context
  • Method Details

    • getInstance

      public static Clock getInstance()
      Shared clock for code without a router context.
      Returns:
      the clock of the global context, never null
    • getTimestamper

      public Timestamper getTimestamper()
      This is a dummy, see RouterClock and RouterTimestamper for the real thing.
      Returns:
      a newly built dummy timestamper, as the base Clock keeps none
    • getLog

      protected Log getLog()
      Fetch the log on demand to avoid circular dependencies (logging uses the clock).
      Returns:
      the log for this class, fetched from the context's LogManager on each call
    • setOffset

      public void setOffset(long offsetMs)
      Specify how far away from the "correct" time the computer is - a positive value means that the system time is slow, while a negative value means the system time is fast.
      Parameters:
      offsetMs - the delta from System.currentTimeMillis() (NOT the delta from now())
    • setOffset

      public void setOffset(long offsetMs, boolean force)
      Specify how far away from the "correct" time the computer is - a positive value means that the system time is slow, while a negative value means the system time is fast.

      Warning - overridden in RouterClock.

      Parameters:
      offsetMs - the delta from System.currentTimeMillis() (NOT the delta from now())
      force - if true, bypass all sanity checks
    • getOffset

      public long getOffset()
      Current offset from system time.
      Returns:
      the current delta from System.currentTimeMillis() in milliseconds
    • getUpdatedSuccessfully

      public boolean getUpdatedSuccessfully()
      Check whether the offset has been updated at least once.
      Returns:
      whether the offset has been updated at least once
    • setNow

      public void setNow(long realTime)
      Adjust the clock to the given real time, computing the offset.
      Parameters:
      realTime - the real time in milliseconds
    • setNow

      public void setNow(long realTime, int stratum)
      Warning - overridden in RouterClock.
      Specified by:
      setNow in interface Timestamper.UpdateListener
      Parameters:
      realTime - the current time
      stratum - the reference clock's stratum, discarded here and used only by RouterClock
    • now

      public long now()
      Retrieve the current time synchronized with whatever reference clock is in use.
      Returns:
      the current synchronized time in milliseconds
    • addUpdateListener

      public void addUpdateListener(Clock.ClockUpdateListener lsnr)
      Register a listener for clock offset changes.
      Parameters:
      lsnr - the listener to add
    • removeUpdateListener

      public void removeUpdateListener(Clock.ClockUpdateListener lsnr)
      Unregister a listener for clock offset changes.
      Parameters:
      lsnr - the listener to remove
    • fireOffsetChanged

      protected void fireOffsetChanged(long delta)
      Notify registered listeners of a clock offset change.
      Parameters:
      delta - the offset change in milliseconds