Skip to content

Commit

Permalink
fix: prevent no valid NTP responses from an IP causing a NullPointerE…
Browse files Browse the repository at this point in the history
…xception
  • Loading branch information
Adam Howard committed Dec 11, 2017
1 parent 1a7c1e8 commit 952dad5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions library/src/main/java/com/medavox/library/mutime/Ntp.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ private static class StringToTimeDataThread extends Thread {
public void run() {
TimeData bestResponse = bestResponseAgainstSingleIp(_repeatCount, ntpHost);
Log.v(TAG, "got time data \""+bestResponse+"\" from "+ntpHost);
listener.onSntpTimeData(bestResponse);
if(bestResponse != null) {
listener.onSntpTimeData(bestResponse);
}
}

}
Expand Down Expand Up @@ -146,7 +148,10 @@ public static InetAddress[] resolveNtpPoolToIpAddresses(String ntpPoolAddress) {
/**
* Takes a single NTP host (as a String),
* performs an SNTP request on it repeatCount number of times,
* and returns the single result with the lowest round-trip delay
* and returns the single result with the lowest round-trip delay.
*
* Returns null if none of the requests to the IP 1) return a successful response,
* or 2) meet the minimum NTP requirements (root delay, root dispersion, round-trip delay).
*/
private static TimeData bestResponseAgainstSingleIp(final int repeatCount, String ntpHost) {
TimeData[] responses = new TimeData[repeatCount];
Expand All @@ -169,7 +174,8 @@ public TimeData performWork(String ntpHost) {
}

/**
* Takes a List of NTP responses, and returns the one with the smallest round-trip delay
* Takes a List of NTP responses, and returns the one with the smallest round-trip delay.
* Returns null if all the passed TimeData objects are null.
*/
private static TimeData filterLeastRoundTripDelay(TimeData... responseTimeList) {
long bestRoundTrip = Long.MAX_VALUE;
Expand All @@ -182,6 +188,9 @@ private static TimeData filterLeastRoundTripDelay(TimeData... responseTimeList)
bestIndex = i;
}
}
if(bestIndex == -1) {
return null;
}
return responseTimeList[bestIndex];
}

Expand Down

0 comments on commit 952dad5

Please sign in to comment.