use of org.apache.commons.net.ntp.NTPUDPClient in project openhab1-addons by openhab.
the class NtpBinding method getTime.
/**
* Queries the given timeserver <code>hostname</code> and returns the time
* in milliseconds.
*
* @param hostname the timeserver to query
* @return the time in milliseconds or the current time of the system if an
* error occurs.
*/
protected static long getTime(String hostname) {
try {
NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_TIMEOUT);
InetAddress inetAddress = InetAddress.getByName(hostname);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
return timeInfo.getReturnTime();
} catch (UnknownHostException uhe) {
logger.warn("the given hostname '{}' of the timeserver is unknown -> returning current sytem time instead", hostname);
} catch (IOException ioe) {
logger.warn("couldn't establish network connection [host '{}'] -> returning current sytem time instead", hostname);
}
return System.currentTimeMillis();
}
use of org.apache.commons.net.ntp.NTPUDPClient in project incubator-rya by apache.
the class TimeUtils method getNtpServerDate.
/**
* Queries the specified NTP Server for the time.
* Do not query time server more than once every 4 seconds.
* @param timeServerHost the time server host name.
* @return the NTP server {@link Date} or {@code null}.
* @throws IOException
*/
public static Date getNtpServerDate(final String timeServerHost) throws IOException {
try {
TimeInfo timeInfo = null;
final NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_SERVER_TIMEOUT_MS);
final InetAddress inetAddress = InetAddress.getByName(timeServerHost);
if (inetAddress != null) {
timeInfo = timeClient.getTime(inetAddress);
if (timeInfo != null) {
// TODO: which time to use?
final long serverTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
// long serverTime = timeInfo.getReturnTime();
final Date ntpDate = new Date(serverTime);
return ntpDate;
}
}
} catch (final IOException e) {
throw new IOException("Unable to get NTP server time.", e);
}
return null;
}
use of org.apache.commons.net.ntp.NTPUDPClient in project deeplearning4j by deeplearning4j.
the class NTPTimeSource method queryServerNow.
private void queryServerNow() {
Long offsetResult = null;
for (int i = 0; i < MAX_QUERY_RETRIES; i++) {
try {
NTPUDPClient client = new NTPUDPClient();
// Timeout if a response takes longer than 10 seconds
client.setDefaultTimeout(DEFAULT_NTP_TIMEOUT_MS);
client.open();
InetAddress address = InetAddress.getByName(ntpServer);
TimeInfo info = client.getTime(address);
info.computeDetails();
Long offset = info.getOffset();
if (offset == null) {
throw new Exception("Could not calculate time offset (offset is null)");
} else {
offsetResult = offset;
break;
}
} catch (Exception e) {
log.error("Error querying NTP server, attempt {} of {}", (i + 1), MAX_QUERY_RETRIES, e);
}
}
if (offsetResult == null) {
log.error("Could not successfully query NTP server after " + MAX_QUERY_RETRIES + " tries");
throw new RuntimeException("Could not successfully query NTP server after " + MAX_QUERY_RETRIES + " tries");
}
lastOffsetGetTimeSystemMS = System.currentTimeMillis();
lastOffsetMilliseconds = offsetResult;
log.debug("Updated local time offset based on NTP server result. Offset = {}", lastOffsetMilliseconds);
}
use of org.apache.commons.net.ntp.NTPUDPClient in project incubator-rya by apache.
the class TimeUtils method getNtpServerDate.
/**
* Queries the specified NTP Server for the time.
* Do not query time server more than once every 4 seconds.
* @param timeServerHost the time server host name.
* @return the NTP server {@link Date} or {@code null}.
* @throws IOException
*/
public static Date getNtpServerDate(final String timeServerHost) throws IOException {
try {
TimeInfo timeInfo = null;
final NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_SERVER_TIMEOUT_MS);
final InetAddress inetAddress = InetAddress.getByName(timeServerHost);
if (inetAddress != null) {
timeInfo = timeClient.getTime(inetAddress);
if (timeInfo != null) {
// TODO: which time to use?
final long serverTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
// long serverTime = timeInfo.getReturnTime();
final Date ntpDate = new Date(serverTime);
return ntpDate;
}
}
} catch (final IOException e) {
throw new IOException("Unable to get NTP server time.", e);
}
return null;
}
use of org.apache.commons.net.ntp.NTPUDPClient in project smarthome by eclipse.
the class NtpHandler method getTime.
/**
* Queries the given timeserver <code>hostname</code> and returns the time
* in milliseconds.
*
* @param hostname the timeserver to query
* @return the time in milliseconds or the current time of the system if an
* error occurs.
*/
public long getTime(String hostname) {
try {
NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_TIMEOUT);
InetAddress inetAddress = InetAddress.getByName(hostname);
TimeInfo timeInfo = timeClient.getTime(inetAddress, port.intValue());
timeInfo.computeDetails();
long serverMillis = timeInfo.getReturnTime() + timeInfo.getOffset();
logger.debug("{} Got time update from host '{}': {}.", getThing().getUID(), hostname, SDF.format(new Date(serverMillis)));
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
return serverMillis;
} catch (UnknownHostException uhe) {
logger.debug("{} The given hostname '{}' of the timeserver is unknown -> returning current sytem time instead. ({})", getThing().getUID(), hostname, uhe.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/offline.comm-error-unknown-host [\"" + (hostname == null ? "null" : hostname) + "\"]");
} catch (IOException ioe) {
logger.debug("{} Couldn't establish network connection to host '{}' -> returning current sytem time instead. ({})", getThing().getUID(), hostname, ioe.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/offline.comm-error-connection [\"" + (hostname == null ? "null" : hostname) + "\"]");
}
return System.currentTimeMillis();
}
Aggregations