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

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

Introduction

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

Prototype

public TimeInfo getTime(InetAddress host) throws IOException 

Source Link

Document

Retrieves the time information from the specified server on the default NTP port and returns it.

Usage

From source file:org.urbancortex.presenter.NTPClient.java

public static TimeInfo main(String[] args) {
    System.out.println(args);/* w  w w .j  av a  2 s.co m*/

    if (args.length == 0) {
        System.err.println("Usage: NTPClient <hostname-or-address-list>");
        System.exit(1);
    }

    NTPUDPClient client = new NTPUDPClient();
    // We want to timeout if a response takes longer than 10 seconds
    client.setDefaultTimeout(5000);
    try {
        client.open();
        for (String arg : args) {
            System.out.println();
            try {
                InetAddress hostAddr = InetAddress.getByName(arg);
                System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress());
                TimeInfo info = client.getTime(hostAddr);
                processResponse(info);

                return info;

            } catch (IOException ioe) {
                ioe.printStackTrace();
                socketTimeout = true;
                //                    csv_logger.stopNTPTask();
                System.out.println("couldn't establish a connection" + ioe);
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
        socketTimeout = true;
        //            csv_logger.stopNTPTask();
        System.out.println("couldn't establish a connection" + e);
    }

    client.close();
    return null;
}

From source file:org.urbancortex.routes.NTPClient.java

public static TimeInfo main(String[] args) {
    System.out.println(args);/*  www . ja  va 2  s  . c  o  m*/

    if (args.length == 0) {
        System.err.println("Usage: NTPClient <hostname-or-address-list>");
        System.exit(1);
    }

    NTPUDPClient client = new NTPUDPClient();
    // We want to timeout if a response takes longer than 10 seconds
    client.setDefaultTimeout(10000);
    try {
        client.open();
        for (String arg : args) {
            System.out.println();
            try {
                InetAddress hostAddr = InetAddress.getByName(arg);
                System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress());
                TimeInfo info = client.getTime(hostAddr);
                processResponse(info);

                return info;

            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }

    client.close();
    return null;
}

From source file:org.xenmaster.monitoring.MonitoringAgent.java

private MonitoringAgent() {
    vmData = ArrayListMultimap.create();
    hostData = ArrayListMultimap.create();
    data = new ConcurrentSkipListMap<>();
    NTPUDPClient nuc = new NTPUDPClient();
    ringBuffer = new RingBuffer<>(Record.EVENT_FACTORY, new SingleThreadedClaimStrategy(RING_SIZE),
            new BlockingWaitStrategy());
    eventHandler = new EventHandler();
    comptroller = new Comptroller();
    emitter = new Emitter();
    collector = new Collector();
    correl = new Correlator();

    setUpEngine();/* w ww . jav  a 2  s .  c o  m*/

    try {
        timeInfo = nuc.getTime(InetAddress.getByName(NTP_SERVER));
        timeInfo.computeDetails();
        Logger.getLogger(getClass())
                .info("Current time "
                        + new DateTime(System.currentTimeMillis() + timeInfo.getOffset())
                                .toString("dd/MM/yyyy HH:mm:ss.S")
                        + ". Clock drift " + timeInfo.getOffset() + " milliseconds");
    } catch (IOException ex) {
        Logger.getLogger(getClass()).warn("NTP time retrieval failed", ex);
    }
}

From source file:util.android.util.DateUtils.java

public static Date getNTPTime() {
    NTPUDPClient timeClient = new NTPUDPClient();
    timeClient.setDefaultTimeout(2000);//from  w ww . j  a  v a 2  s .co m

    TimeInfo timeInfo;
    try {
        timeClient.open();
        InetAddress inetAddress = InetAddress.getByName(getNTPServer());
        timeInfo = timeClient.getTime(inetAddress);

        long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
        Date time = new Date(returnTime);
        timeClient.close();
        return time;
    } catch (Exception e) {
        e.printStackTrace();
        return new Date();
    }
}