net.i2p.router

## Class RouterClock

- java.lang.Object
- - net.i2p.util.Clock
  - - net.i2p.router.RouterClock

- All Implemented Interfaces:
    Timestamper.UpdateListener

      public class RouterClock
      extends Clock

  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). RouterClock is a subclass of Clock with access to router transports. Configuration permitting, it will block clock offset changes which would increase peer clock skew.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `RouterClock.ClockShiftListener` Listener interface for receiving notifications when the router clock shifts.

    - ### Nested classes/interfaces inherited from class net.i2p.util.Clock

      `Clock.ClockUpdateListener`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_STRATUM`

    - ### Fields inherited from class net.i2p.util.Clock

      `_alreadyChanged, _context, _isSystemClockBad, _offset, _startedOn, _statCreated, MAX_LIVE_OFFSET, MAX_OFFSET, MIN_OFFSET_CHANGE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `RouterClock(RouterContext context)` Does not start.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addShiftListener(RouterClock.ClockShiftListener lsnr)` Add a listener to be notified of massive system clock shifts.
    `long` | `getDeltaOffset()` Deprecated.
    `Timestamper` | `getTimestamper()` Get the timestamper used for time synchronization.
    `long` | `now()` Retrieve the current time synchronized with whatever reference clock is in use.
    `void` | `removeShiftListener(RouterClock.ClockShiftListener lsnr)` Remove a clock shift listener.
    `void` | `setNow(long realTime, int stratum)` Set the current time, used by the timestamper when synchronizing.
    `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.
    `void` | `start()` Start the router clock and its associated timestamper.

    - ### Methods inherited from class net.i2p.util.Clock

      `addUpdateListener, fireOffsetChanged, getInstance, getLog, getOffset, getUpdatedSuccessfully, removeUpdateListener, setNow, setOffset`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_STRATUM

          public static final int DEFAULT_STRATUM

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### RouterClock

          public RouterClock(RouterContext context)

      Does not start. Caller MUST call start()

  - ### Method Detail

    - #### addShiftListener

          public void addShiftListener(RouterClock.ClockShiftListener lsnr)

      Add a listener to be notified of massive system clock shifts. These are shifts of one minute or more.
      Parameters:
        `lsnr` - the listener to add

    - #### getDeltaOffset

          @Deprecated
          public long getDeltaOffset()

      Deprecated.

    - #### getTimestamper

          public Timestamper getTimestamper()

      Get the timestamper used for time synchronization.
      Overrides:
        `getTimestamper` in class `Clock`
      Returns:
        the RouterTimestamper instance

    - #### now

          public long now()

      Retrieve the current time synchronized with whatever reference clock is in use. Do really simple clock slewing, like NTP but without jitter prevention. Slew the clock toward the desired offset, but only up to a maximum slew rate, and never let the clock go backwards because of slewing. Take care to only access the volatile variables once for speed and to avoid having another thread change them This is called about a zillion times a second, so we can do the slewing right here rather than in some separate thread to keep it simple. Avoiding backwards clocks when updating in a thread would be hard too.
      Overrides:
        `now` in class `Clock`

    - #### removeShiftListener

          public void removeShiftListener(RouterClock.ClockShiftListener lsnr)

      Remove a clock shift listener.
      Parameters:
        `lsnr` - the listener to remove

    - #### setNow

          public void setNow(long realTime,
                             int stratum)

      Set the current time, used by the timestamper when synchronizing.
      Specified by:
        `setNow` in interface `Timestamper.UpdateListener`
      Overrides:
        `setNow` in class `Clock`
      Parameters:
        `realTime` - the current time from the time source
        `stratum` - the stratum level of the time source (lower is better)

    - #### 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.
      Overrides:
        `setOffset` in class `Clock`
      Parameters:
        `offsetMs` - the delta from System.currentTimeMillis() (NOT the delta from now())

    - #### start

          public void start()

      Start the router clock and its associated timestamper. The timestamper will begin synchronizing with NTP or peer time sources.