Class NetTimeConnector<C extends NetTimeConfiguration>

java.lang.Object
net.time4j.clock.AbstractClock
net.time4j.clock.NetTimeConnector<C>
Type Parameters:
C - generic configuration type
All Implemented Interfaces:
TimeSource<Moment>
Direct Known Subclasses:
DaytimeClock, HttpClock, SntpConnector

public 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 using SystemClock.MONOTONIC 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.

Author:
Meno Hochschild
See Also:
SystemClock.MONOTONIC
  • Method Details

    • currentTime

      public 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();
       
      Returns:
      Moment
      See Also:
      isRunning()
    • 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.

      Returns:
      boolean
      See Also:
      connect()
    • 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().

      Throws:
      IOException - if connection fails or in case of any inconsistent server answers
      See Also:
      currentTime()
    • reconfigure

      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().

      Returns:
      true if successful else false
      See Also:
      loadNetTimeConfiguration()
    • getNetTimeConfiguration

      public C getNetTimeConfiguration()

      Yields the current configuration parameters.

      Returns:
      configuration object
      See Also:
      reconfigure()
    • setLogWriter

      public void setLogWriter(PrintWriter out)

      Installs a logging stream for any messages during connection.

      Parameters:
      out - output stream (null disables logging)
    • isLogEnabled

      public boolean isLogEnabled()

      Determines if the internal logging is enabled.

      Returns:
      true if there is any installed log writer else false
      See Also:
      setLogWriter(PrintWriter)
    • getLastConnectionTime

      public Moment getLastConnectionTime()

      Yields the time of last connection.

      Returns:
      moment of last connection or null if there was not any connection yet
      See Also:
      connect()
    • getLastOffsetInMicros

      public long getLastOffsetInMicros()

      Yields the last offset between net time and local time in microseconds.

      Returns:
      offset in microseconds (0 if there was not any connection yet)
      See Also:
      connect()