List of usage examples for org.apache.commons.net.ntp NTPUDPClient close
public void close()
From source file:com.cscao.apps.ntplib.NTPClient.java
private TimeInfo requestTime(String server, int timeout) { TimeInfo responseInfo = null;/*w w w . j a v a 2 s .co m*/ NTPUDPClient client = new NTPUDPClient(); client.setDefaultTimeout(timeout); try { client.open(); try { InetAddress hostAddr = InetAddress.getByName(server); // System.out.println("> " + hostAddr.getHostName() + "/" // + hostAddr.getHostAddress()); details += "NTP server: " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress(); responseInfo = client.getTime(hostAddr); } catch (IOException ioe) { ioe.printStackTrace(); } } catch (SocketException e) { e.printStackTrace(); } client.close(); return responseInfo; }
From source file:de.tud.kitchen.android.NTPTimeReceiver.java
@Override protected Void doInBackground(InetAddress... params) { NTPUDPClient client = new NTPUDPClient(); client.setDefaultTimeout(10000);/* w w w .j a v a 2 s. c om*/ try { client.open(); } catch (final SocketException se) { se.printStackTrace(); return null; } while (!this.isCancelled()) { try { TimeInfo info = client.getTime(params[0]); info.computeDetails(); Long offsetValue = info.getOffset(); int receiverTimeDelta = (offsetValue == null) ? 0 : offsetValue.intValue(); publishProgress(receiverTimeDelta); } catch (final IOException ioe) { ioe.printStackTrace(); continue; } try { Thread.sleep(1000 * 60); } catch (InterruptedException e) { continue; } } client.close(); return null; }
From source file:com.clustercontrol.port.protocol.ReachAddressNTP.java
/** * NTP????????/*w w w.j av a 2s .co m*/ * * @param addressText * @return NTP */ @Override protected boolean isRunning(String addressText) { m_message = ""; m_messageOrg = ""; m_response = -1; boolean isReachable = false; try { long start = 0; // long end = 0; // boolean retry = true; // ????(true:??false:???) StringBuffer bufferOrg = new StringBuffer(); // String result = ""; InetAddress address = InetAddress.getByName(addressText); bufferOrg.append("Monitoring the NTP Service of " + address.getHostName() + "[" + address.getHostAddress() + "]:" + m_portNo + ".\n\n"); NTPUDPClient client = new NTPUDPClient(); TimeInfo time = null; for (int i = 0; i < m_sentCount && retry; i++) { try { bufferOrg.append(HinemosTime.getDateString() + " Tried to Connect: "); client.setDefaultTimeout(m_timeout); start = HinemosTime.currentTimeMillis(); time = client.getTime(address, m_portNo); end = HinemosTime.currentTimeMillis(); m_response = end - start; if (time != null) { if (m_response > 0) { if (m_response < m_timeout) { result = result + ("\n" + "Response Time = " + m_response + "ms"); } else { m_response = m_timeout; result = result + ("\n" + "Response Time = " + m_response + "ms"); } } else { result = result + ("\n" + "Response Time < 1ms"); } result = new Date(time.getReturnTime()).toString(); retry = false; isReachable = true; } else { result = "failed to get time "; retry = false; isReachable = false; } } catch (SocketException e) { result = (e.getMessage() + "[SocketException]"); retry = true; isReachable = false; } catch (IOException e) { result = (e.getMessage() + "[IOException]"); retry = true; isReachable = false; } finally { bufferOrg.append(result + "\n"); if (client.isOpen()) { client.close(); } } if (i < m_sentCount - 1 && retry) { try { Thread.sleep(m_sentInterval); } catch (InterruptedException e) { break; } } } m_message = result + "(NTP/" + m_portNo + ")"; m_messageOrg = bufferOrg.toString(); return isReachable; } catch (UnknownHostException e) { m_log.debug("isRunning(): " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + e.getMessage()); m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + " (" + e.getMessage() + ")"; return false; } }
From source file:com.blockwithme.time.internal.NTPClockSynchronizer.java
@Override public long getLocalToUTCTimeOffset() throws Exception { final NTPUDPClient client = new NTPUDPClient(); // final Calendar cal = Calendar.getInstance(DEFAULT_LOCAL); // We want to timeout if a response takes longer than 3 seconds client.setDefaultTimeout(TIMEOUT); long offsetSum = 0L; int offsetCount = 0; long bestDelay = Long.MAX_VALUE; long bestOffset = Long.MAX_VALUE; Throwable lastException = null; try {/*from w w w . j av a 2 s . c o m*/ client.open(); for (int i = 0; i < ntpPool.length; i++) { try { final InetAddress hostAddr = InetAddress.getByName(ntpPool[i]); final TimeInfo info = client.getTime(hostAddr); info.computeDetails(); final Long offsetValue = info.getOffset(); final Long delayValue = info.getDelay(); if ((delayValue != null) && (offsetValue != null)) { // cal.setTimeInMillis(offsetValue // + System.currentTimeMillis()); // final long local2UTC = -(cal.get(Calendar.ZONE_OFFSET) + cal // .get(Calendar.DST_OFFSET)); if (delayValue <= 100L) { offsetSum += offsetValue;// + local2UTC; offsetCount++; } if (delayValue < bestDelay) { bestDelay = delayValue; bestOffset = offsetValue;// + local2UTC; } } } catch (final Throwable t) { LOG.error("Error reading tiem through NTP", t); lastException = t; } } } catch (final Throwable t) { LOG.error("Error reading tiem through NTP", t); lastException = t; // NTPUDPClient can't even open at all!?! } finally { client.close(); } if (offsetCount > 0) { return offsetSum / offsetCount; } // OK, not good result. Any result at all? if (bestDelay != Long.MAX_VALUE) { return bestOffset; } // FAIL! throw new Exception("Failed to get NTP time", lastException); }
From source file:ntp.NTPClient.java
public static void main(String[] args) { Properties defaultProps = System.getProperties(); //obtiene las "properties" del sistema defaultProps.put("java.net.preferIPv6Addresses", "true");//mapea el valor true en la variable java.net.preferIPv6Addresses if (args.length == 0) { System.err.println("Usage: NTPClient <hostname-or-address-list>"); System.exit(1);/*from ww w.j av a2s .c o m*/ } Promedio = 0; Cant = 0; 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); } catch (IOException ioe) { System.err.println(ioe.toString()); } } } catch (SocketException e) { System.err.println(e.toString()); } client.close(); System.out.println("\n Pomedio " + (Promedio / Cant)); }
From source file:ntpgraphic.LineChart.java
public static void NTPClient(String[] servers, int i) { Properties defaultProps = System.getProperties(); //obtiene las "properties" del sistema defaultProps.put("java.net.preferIPv6Addresses", "true");//mapea el valor true en la variable java.net.preferIPv6Addresses if (servers.length == 0) { System.err.println("Usage: NTPClient <hostname-or-address-list>"); System.exit(1);/*w w w . j av a2 s . c o m*/ } Promedio = 0; Cant = 0; 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 : servers) { System.out.println(); try { InetAddress hostAddr = InetAddress.getByName(arg); System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress()); TimeInfo info = client.getTime(hostAddr); processResponse(info, i); } catch (IOException ioe) { System.err.println(ioe.toString()); } } } catch (SocketException e) { System.err.println(e.toString()); } client.close(); //System.out.println("\n Pomedio "+(Promedio/Cant)); }
From source file:ntpgraphic.PieChart.java
public static void NTPClient(String[] servers) { Properties defaultProps = System.getProperties(); //obtiene las "properties" del sistema defaultProps.put("java.net.preferIPv6Addresses", "true");//mapea el valor true en la variable java.net.preferIPv6Addresses if (servers.length == 0) { System.err.println("Usage: NTPClient <hostname-or-address-list>"); System.exit(1);/*from www .jav a2 s . co m*/ } Promedio = 0; Cant = 0; int j = 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 : servers) { System.out.println(); try { InetAddress hostAddr = InetAddress.getByName(arg); System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress()); TimeInfo info = client.getTime(hostAddr); processResponse(info, j++); } catch (IOException ioe) { System.err.println(ioe.toString()); } } } catch (SocketException e) { System.err.println(e.toString()); } client.close(); //System.out.println("\n Pomedio "+(Promedio/Cant)); }
From source file:org.apache.common.net.examples.ntp.NTPClient.java
public static final void main(String[] args) { if (args.length == 0) { System.err.println("Usage: NTPClient <hostname-or-address-list>"); System.exit(1);//from w w w . j av a2 s .c o m } NTPUDPClient client = new NTPUDPClient(); // We want to timeout if a response takes longer than 10 seconds client.setDefaultTimeout(10000); try { client.open(); for (int i = 0; i < args.length; i++) { System.out.println(); try { InetAddress hostAddr = InetAddress.getByName(args[i]); System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress()); TimeInfo info = client.getTime(hostAddr); processResponse(info); } catch (IOException ioe) { ioe.printStackTrace(); } } } catch (SocketException e) { e.printStackTrace(); } client.close(); }
From source file:org.blue.star.plugins.check_local_time.java
public boolean execute_check() { /* Declare variables */ NTPUDPClient ntpClient = new NTPUDPClient(); TimeInfo time;//from w w w . j a va 2 s . c om 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; }
From source file:org.eclipse.kura.linux.clock.JavaNtpClockSyncProvider.java
@Override protected boolean syncClock() throws KuraException { boolean ret = false; // connect and get the delta NTPUDPClient ntpClient = new NTPUDPClient(); ntpClient.setDefaultTimeout(this.m_ntpTimeout); try {//from w w w . java 2 s .com ntpClient.open(); try { InetAddress ntpHostAddr = InetAddress.getByName(this.m_ntpHost); TimeInfo info = ntpClient.getTime(ntpHostAddr, this.m_ntpPort); this.m_lastSync = new Date(); info.computeDetails(); this.m_listener.onClockUpdate(info.getOffset()); ret = true; } catch (IOException e) { ntpClient.close(); s_logger.warn( "Error while synchronizing System Clock with NTP host {}. Please verify network connectivity ...", this.m_ntpHost); } } catch (Exception e) { throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e); } return ret; }