Skip to content

Commit

Permalink
feat: only update median TimeData value in Ntp if it's different from…
Browse files Browse the repository at this point in the history
… the last one
  • Loading branch information
medavox committed Nov 2, 2017
1 parent 03406f9 commit 7dfc734
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 6 additions & 2 deletions library/src/main/java/com/medavox/library/mutime/Ntp.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public int compare(TimeData lhsParam, TimeData rhsParam) {

private static SntpClient.SntpResponseListener dynamicCollater = new SntpClient.SntpResponseListener() {
private Set<TimeData> timeDatas = new ConcurrentSkipListSet<>(clockOffsetSorter);
private TimeData oldMedian;

/**Each time we receive new data, recalculate the median offset
* and send the results to persistence*/
Expand All @@ -62,8 +63,11 @@ public void onSntpTimeData(TimeData data) {
TimeData[] sortedResponses = timeDatas.toArray(asArray);
Arrays.sort(sortedResponses, clockOffsetSorter);
TimeData newMedian = sortedResponses[sortedResponses.length / 2];
Log.d(TAG, "new median time:"+newMedian);
MuTime.persistence.onSntpTimeData(newMedian);
if(!newMedian.equals(oldMedian)) {
oldMedian = newMedian;
Log.d(TAG, "new median time:" + newMedian);
MuTime.persistence.onSntpTimeData(newMedian);
}
}
}
};
Expand Down
16 changes: 16 additions & 0 deletions library/src/main/java/com/medavox/library/mutime/TimeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,20 @@ public String toString() {
+"; Device Uptime offset: "+ uptimeOffset
+"]";
}

@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
}
else if(obj instanceof TimeData) {
TimeData td = (TimeData)obj;
return td.getUptimeOffset() == uptimeOffset &&
td.getClockOffset() == clockOffset &&
td.getRoundTripDelay() == roundTripDelay;
}
else {
return false;
}
}
}

0 comments on commit 7dfc734

Please sign in to comment.