java.lang.Object
net.i2p.util.Clock
- All Implemented Interfaces:
Timestamper.UpdateListener
- Direct Known Subclasses:
RouterClock
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 ClassesModifier and TypeClassDescriptionstatic interfaceListener for clock offset changes.Field Summary
Modifier and TypeFieldDescriptionprotected booleanWhether the offset has ever been changed.protected final I2PAppContextThe I2P application context.private AtomicIntegerHow often the clock is reconfigured, in ms.protected final booleanWhether the system clock was determined to be invalid at startup.private AtomicIntegerHow many times the clock has been reconfigured.private final Set<Clock.ClockUpdateListener> Notified when the clock is reconfigured.protected longThe current offset from system time.private AtomicLongLast time read from the saved clock file.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
ConstructorsConstructorDescriptionClock(I2PAppContext context) Create a new Clock with the given context, detecting system clock validity.Method Summary
Modifier 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.This is a dummy, see RouterClock and RouterTimestamper for the real thing.booleanCheck whether the offset has been updated at least once.longnow()Retrieve the current time synchronized with whatever reference clock is in use.voidUnregister a listener for clock offset changes.voidsetNow(long realTime) Adjust the clock to the given real time, computing the offset.voidsetNow(long realTime, int stratum) Warning - overridden in RouterClock.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.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.
Field Details
_context
The I2P application context._isSystemClockBad
protected final boolean _isSystemClockBadWhether the system clock was determined to be invalid at startup._startedOn
protected long _startedOnThe time when this clock was started._statCreated
protected boolean _statCreatedWhether the clock skew stat has been created._offset
protected volatile long _offsetThe current offset from system time._alreadyChanged
protected boolean _alreadyChangedWhether the offset has ever been changed._listeners
Notified when the clock is reconfigured._iter
How many times the clock has been reconfigured._frequency
How often the clock is reconfigured, in ms._savedTime
Last time read from the saved clock file.MAX_OFFSET
public static final long MAX_OFFSETIf the clock is skewed by 3+ days, forget it.- See Also:
MAX_LIVE_OFFSET
public static final long MAX_LIVE_OFFSETAfter 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_CHANGEIf 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
Create a new Clock with the given context, detecting system clock validity.- Parameters:
context- the I2P app context
Method Details
getInstance
Shared clock for code without a router context.- Returns:
- the clock of the global context, never null
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
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 fromSystem.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 fromSystem.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:
setNowin interfaceTimestamper.UpdateListener- Parameters:
realTime- the current timestratum- the reference clock's stratum, discarded here and used only byRouterClock
now
public long now()Retrieve the current time synchronized with whatever reference clock is in use.- Returns:
- the current synchronized time in milliseconds
addUpdateListener
Register a listener for clock offset changes.- Parameters:
lsnr- the listener to add
removeUpdateListener
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