java.lang.Object
net.i2p.util.Clock
net.i2p.router.RouterClock
- All Implemented Interfaces:
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).
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 ClassesModifier and TypeClassDescriptionstatic interfaceListener interface for receiving notifications when the router clock shifts.Nested classes/interfaces inherited from class Clock
Clock.ClockUpdateListenerModifier and TypeClassDescriptionstatic interfaceListener for clock offset changes.Field Summary
Modifier and TypeFieldDescriptionprivate longThe clock offset being sought.private longUse system time for this.private longThe offset of the last proposal.private longWhen the clock was last shifted.private longWhen the clock was last slewed.private intThe stratum of the last proposal.private final Set<RouterClock.ClockShiftListener> The listeners for clock shifts.private final RouterTimestamperThe timestamper making the proposals.static final intNTP stratum assumed before any external time source is known.private static final longBackward shift that counts as massive.private static final longIf the system clock shifts by this much, call the callback, we probably need a soft restart.private static final longHow often we will slew the clock i.e.private static final longThe max NTP Timestamper delay is 30m right now, make this longer than that.private static final StringTesting only - disables clock adjustment when set.private static final intWorst NTP stratum accepted.Fields inherited from class Clock
_alreadyChanged, _context, _isSystemClockBad, _offset, _startedOn, _statCreated, MAX_LIVE_OFFSET, MAX_OFFSET, MIN_OFFSET_CHANGEModifier and TypeFieldDescriptionprotected booleanWhether the offset has ever been changed.protected final I2PAppContextThe I2P application context.protected final booleanWhether the system clock was determined to be invalid at startup.protected longThe current offset from system time.protected longThe time when this clock was started.protected booleanWhether the clock skew stat has been created.static final longAfter we've started up and shifted the clock, don't allow shifts of more than 10 minutes.static final longIf the clock is skewed by 3+ days, forget it.static final longIf the clock skew changes by less than this, ignore the update (so we don't slide all over the place).Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionvoidAdd a listener to be notified of massive system clock shifts.The timestamper used for time synchronization.private voidnotifyMassive(long shift) Notifies the listeners of a massive shift.longnow()Retrieve the current time synchronized with whatever reference clock is in use.voidRemove a clock shift listener.voidsetNow(long realTime, int stratum) The current time, used by the timestamper when synchronizing.voidsetOffset(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.private voidsetOffset(long offsetMs, boolean force, int stratum) The offset, optionally forcing the change.private voidsetOffset(long offsetMs, int stratum) The offset with the given stratum.voidstart()Start the router clock and its associated timestamper.Methods inherited from class Clock
addUpdateListener, fireOffsetChanged, getInstance, getLog, getOffset, getUpdatedSuccessfully, removeUpdateListener, setNow, setOffsetModifier and TypeMethodDescriptionvoidRegister a listener for clock offset changes.protected voidfireOffsetChanged(long delta) Notify registered listeners of a clock offset change.static ClockShared clock for code without a router context.protected LoggetLog()Fetch the log on demand to avoid circular dependencies (logging uses the clock).longCurrent offset from system time.booleanCheck whether the offset has been updated at least once.voidUnregister a listener for clock offset changes.voidsetNow(long realTime) Adjust the clock to the given real time, computing the offset.voidsetOffset(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.
Field Details
MAX_SLEW
private static final long MAX_SLEWHow often we will slew the clock i.e. ppm = 1000000/MAX_SLEW We should be able to slew really fast, this is probably a lot faster than what NTP does 1/50 is 12s in a 10m tunnel lifetime, that should be fine. All of this is @since 0.7.12.- See Also:
DEFAULT_STRATUM
public static final int DEFAULT_STRATUMNTP stratum assumed before any external time source is known.- See Also:
WORST_STRATUM
MIN_DELAY_FOR_WORSE_STRATUM
private static final long MIN_DELAY_FOR_WORSE_STRATUMThe max NTP Timestamper delay is 30m right now, make this longer than that.- See Also:
_desiredOffset
private volatile long _desiredOffsetThe clock offset being sought._lastSlewed
private volatile long _lastSlewedWhen the clock was last slewed._lastChanged
private long _lastChangedUse system time for this._lastStratum
private int _lastStratumThe stratum of the last proposal._lastProposedOffset
private long _lastProposedOffsetThe offset of the last proposal._timeStamper
The timestamper making the proposals.MASSIVE_SHIFT_FORWARD
private static final long MASSIVE_SHIFT_FORWARDIf the system clock shifts by this much, call the callback, we probably need a soft restart.- See Also:
MASSIVE_SHIFT_BACKWARD
private static final long MASSIVE_SHIFT_BACKWARDBackward shift that counts as massive.- See Also:
PROP_DISABLE_ADJUSTMENT
Testing only - disables clock adjustment when set.- See Also:
_shiftListeners
The listeners for clock shifts._lastShiftNanos
private volatile long _lastShiftNanosWhen the clock was last shifted.
Constructor Details
RouterClock
Does not start. Caller MUST callstart().- Parameters:
context- the router context used to create the timestamper and to resolve clock settings
Method Details
start
public void start()Start the router clock and its associated timestamper. The timestamper will begin synchronizing with NTP or peer time sources.getTimestamper
The timestamper used for time synchronization.- Overrides:
getTimestamperin classClock- Returns:
- the RouterTimestamper instance
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:
setOffsetin classClock- Parameters:
offsetMs- the delta fromSystem.currentTimeMillis()(NOT the delta from now())force- if true, bypass all sanity checks
setOffset
private void setOffset(long offsetMs, int stratum) The offset with the given stratum.- Parameters:
offsetMs- the delta fromSystem.currentTimeMillis()(NOT the delta from now())stratum- the stratum of the time source
setOffset
private void setOffset(long offsetMs, boolean force, int stratum) The offset, optionally forcing the change.- Parameters:
offsetMs- the delta fromSystem.currentTimeMillis()(NOT the delta from now())force- whether to bypass sanity checksstratum- the stratum of the time source
setNow
public void setNow(long realTime, int stratum) The current time, used by the timestamper when synchronizing.- Specified by:
setNowin interfaceTimestamper.UpdateListener- Overrides:
setNowin classClock- Parameters:
realTime- the current time from the time sourcestratum- the stratum level
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.
notifyMassive
private void notifyMassive(long shift) Notifies the listeners of a massive shift.- Parameters:
shift- the size of the shift, in milliseconds
addShiftListener
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
removeShiftListener
Remove a clock shift listener.- Parameters:
lsnr- the listener to remove