public class Clock extends Object implements Timestamper.UpdateListener
| Modifier and Type | Class and Description |
|---|---|
static interface | Clock.ClockUpdateListenerListener for clock offset changes. |
| Modifier and Type | Field and Description |
|---|---|
protected boolean | _alreadyChangedwhether the offset has ever been changed |
protected I2PAppContext | _contextthe I2P application context |
protected boolean | _isSystemClockBadwhether the system clock was determined to be invalid at startup |
protected long | _offsetthe current offset from system time |
protected long | _startedOnthe time when this clock was started |
protected boolean | _statCreatedwhether the clock skew stat has been created |
static long | MAX_LIVE_OFFSETafter we've started up and shifted the clock, don't allow shifts of more than 10 minutes |
static long | MAX_OFFSETif the clock is skewed by 3+ days, forget it |
static long | MIN_OFFSET_CHANGEif the clock skewed changes by less than this, ignore the update (so we don't slide all over the place) |
| Constructor and Description |
|---|
Clock(I2PAppContext context)Create a new Clock with the given context, detecting system clock validity. |
| Modifier and Type | Method and Description |
|---|---|
void | addUpdateListener(Clock.ClockUpdateListener lsnr)Register a listener for clock offset changes. |
protected void | fireOffsetChanged(long delta)Notify registered listeners of a clock offset change. |
static Clock | getInstance()Get the global clock instance. |
protected Log | getLog()Fetch the log on demand to avoid circular dependencies (logging uses the clock). |
long | getOffset()Get the current offset from system time. |
Timestamper | getTimestamper()This is a dummy, see RouterClock and RouterTimestamper for the real thing |
boolean | getUpdatedSuccessfully()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 | removeUpdateListener(Clock.ClockUpdateListener lsnr)Unregister a listener for clock offset changes. |
void | setNow(long realTime)Set 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. |
protected boolean _alreadyChanged
protected final I2PAppContext _context
protected final boolean _isSystemClockBad
protected volatile long _offset
protected long _startedOn
protected boolean _statCreated
public static final long MAX_LIVE_OFFSET
public static final long MAX_OFFSET
public static final long MIN_OFFSET_CHANGE
public Clock(I2PAppContext context)
context - the I2P app contextpublic void addUpdateListener(Clock.ClockUpdateListener lsnr)
lsnr - the listener to addprotected void fireOffsetChanged(long delta)
delta - the offset change in millisecondspublic static Clock getInstance()
protected Log getLog()
public long getOffset()
public Timestamper getTimestamper()
public boolean getUpdatedSuccessfully()
public long now()
public void removeUpdateListener(Clock.ClockUpdateListener lsnr)
lsnr - the listener to removepublic void setNow(long realTime)
realTime - the real time in millisecondspublic void setNow(long realTime,
int stratum)setNow in interface Timestamper.UpdateListenerstratum - ignoredrealTime - the current timepublic void setOffset(long offsetMs)
offsetMs - the delta from System.currentTimeMillis() (NOT the delta from now())public void setOffset(long offsetMs,
boolean force)offsetMs - the delta from System.currentTimeMillis() (NOT the delta from now())force - if true, bypass all sanity checks