C - generic configuration typepublic abstract class NetTimeConnector<C extends NetTimeConfiguration> extends AbstractClock
Represents an abstract connection object to an internet time server.
A socket connection can be established by connect()
and then the difference between the local computer clock and the net time
can be queried. Next time queries of this clock are based on this difference
and the local clock - until the next CONNECT.
| Modifier | Constructor and Description |
|---|---|
protected |
NetTimeConnector(C ntc)
Creates a configured instance.
|
| Modifier and Type | Method and Description |
|---|---|
void |
connect()
Queries a time server for the current time.
|
Moment |
currentTime()
Yields the current time after a connection has been established
at least once.
|
protected abstract Moment |
doConnect()
Will be called by
connect(). |
protected abstract Class<C> |
getConfigurationType()
Yields the configuration type.
|
Moment |
getLastConnectionTime()
Yields the time of last connection.
|
long |
getLastOffsetInMicros()
Yields the last offset between net time and local time in
microseconds.
|
C |
getNetTimeConfiguration()
Yields the current configuration parameters.
|
boolean |
isLogEnabled()
Determines if the internal logging is enabled.
|
boolean |
isRunning()
The clock is running as soon as there was established a connection
at least once.
|
protected C |
loadNetTimeConfiguration()
Loads the configuration parameters via a
ServiceLoader. |
protected void |
log(String prefix,
String message)
Logs given message if a
PrintWriter has been installed. |
boolean |
reconfigure()
Queries the configuration parameters to be used for the next
connection.
|
void |
setLogWriter(PrintWriter out)
Installs a logging stream for any messages during connection.
|
inLocalView, inZonalView, inZonalViewprotected NetTimeConnector(C ntc)
Creates a configured instance.
ntc - Verbindungskonfigurationpublic Moment currentTime()
Yields the current time after a connection has been established at least once.
If there was no connection yet then this method just displays the time of clock construction. Note that this method is still sensible for any local clock change triggered by the underlying operating system. If an application needs a fresh internet time then following code can be used instead (causing network traffic):
NetTimeConnector<?> clock = ...; clock.connect(); Moment currentTime = clock.getLastConnectionTime();
isRunning()public boolean isRunning()
The clock is running as soon as there was established a connection at least once.
If there is not yet any connection then this clock will only display the time of its construction.
connect()public final void connect()
throws IOException
Queries a time server for the current time.
The result can then be achieved by the method currentTime()
which is based on the network offset calculated in last connection.
A connection to the server only happens in this method and not in
the method currentTime().
IOException - if connection fails or in case of any
inconsistent server answerscurrentTime()public final boolean reconfigure()
Queries the configuration parameters to be used for the next connection.
The configuration will be determined by a ServiceLoader.
If not available then this connector will just choose the configuration
which was given during construction of this instance. Any possible
IllegalStateException will be logged to the error console.
This method delegates to loadNetTimeConfiguration().
true if successful else falseloadNetTimeConfiguration()public C getNetTimeConfiguration()
Yields the current configuration parameters.
reconfigure()public void setLogWriter(PrintWriter out)
Installs a logging stream for any messages during connection.
out - output stream (null disables logging)public boolean isLogEnabled()
Determines if the internal logging is enabled.
true if there is any installed log writer
else falsesetLogWriter(PrintWriter)public Moment getLastConnectionTime()
Yields the time of last connection.
null if there was not
any connection yetconnect()public long getLastOffsetInMicros()
Yields the last offset between net time and local time in microseconds.
0 if there was not any
connection yet)connect()protected abstract Moment doConnect() throws IOException, ParseException
Will be called by connect().
IOException - in case of any connection failureParseException - if the server reply is not readableconnect()protected C loadNetTimeConfiguration()
Loads the configuration parameters via a ServiceLoader.
Subclasses should always start by calling
super.loadNetTimeConfiguration() if this
method is overridden.
IllegalStateException - in case of any configuration errorreconfigure()protected void log(String prefix, String message)
Logs given message if a PrintWriter has been installed.
prefix - optional prefixmessage - message to be loggedsetLogWriter(PrintWriter)Copyright © 2014–2015. All rights reserved.