Example usage for org.apache.commons.net.ntp NTPUDPClient setVersion

List of usage examples for org.apache.commons.net.ntp NTPUDPClient setVersion

Introduction

In this page you can find the example usage for org.apache.commons.net.ntp NTPUDPClient setVersion.

Prototype

public void setVersion(int version) 

Source Link

Document

Sets the NTP protocol version number that client sets on request packet communicate with remote host.

Usage

From source file:experts.net.ip6.ULUA.java

/**
 * Obtain NTP time stamp value//from   w w w  . j av a  2  s . c  om
 *
 * @param address
 *            NTP server address
 * @return the current time of day in 64-bit NTP format
 * @throws SocketException
 *             If the socket could not be opened which it might be not available any ports.
 * @throws UnknownHostException
 *             If the host could not be found.
 * @throws IOException
 */
public static long obtainNTPTime(String address) throws SocketException, UnknownHostException, IOException {
    NTPUDPClient client = new NTPUDPClient();
    client.setVersion(NtpV3Packet.VERSION_4);

    // throws SocketException
    client.open();
    // throws UnknownHostException from InetAddress.getByName and IOException from client.getTime
    TimeInfo time = client.getTime(InetAddress.getByName(address));
    client.close();

    time.computeDetails();

    return time.getMessage().getTransmitTimeStamp().ntpValue();
}

From source file:org.blue.star.plugins.check_local_time.java

public boolean execute_check() {

    /* Declare variables */
    NTPUDPClient ntpClient = new NTPUDPClient();
    TimeInfo time;/*from  www . j  a  v  a2  s.  co  m*/
    InetAddress server;
    offsets = new long[checkCount];

    /* Check for verbosity */
    if (super.verbose > 0)
        verbose = true;

    /* Configure client to meet our requirements */
    ntpClient.setDefaultTimeout(timeout);
    ntpClient.setVersion(ntpVersion);

    try {
        if (verbose) {
            System.out.println("Using NTP Server: " + hostname);
            System.out.println("Using NTP Port: " + port);
            System.out.println("Using NTP Version: " + ntpVersion);
        }

        server = InetAddress.getByName(hostname);
        ntpClient.open();

        if (verbose)
            System.out.println("Beginning total of " + checkCount + " checks.");

        for (int i = 0; i < checkCount; i++) {
            if (verbose)
                System.out.println("Taking time reading number " + i);

            time = ntpClient.getTime(server, port);

            if (time == null) {
                /* State unknown if we can't connect to NTP server, our local time could be fine */
                check_state = common_h.STATE_UNKNOWN;
                check_message = "State Unknown - Unable to connect to NTP Server!";
                return true;
            }
            time.computeDetails();

            offsets[i] = time.getOffset() - time.getDelay();

            /* Small sleep so that we are at least getting some variety in our readings */
            Thread.sleep(200);
        }

        if (verbose)
            System.out.println("Calculating & storing time averages.");
        /* Calculate and store average offset */
        long total = 0;

        for (int i = 0; i < offsets.length; i++)
            total += offsets[i];

        average = total / offsets.length;
    } catch (Exception e) {
        check_state = common_h.STATE_CRITICAL;
        check_message = e.getMessage();
    } finally {
        try {
            ntpClient.close();
        } catch (Exception e) {
        }
    }
    return true;
}