[ntcore] Add method to get server time offset (#4847)

Also exposes this as an event signaled when the offset is updated due to
a ping response from the server.
This commit is contained in:
Peter Johnson
2022-12-30 20:15:57 -08:00
committed by GitHub
parent fe1b62647f
commit f1151d375f
29 changed files with 718 additions and 60 deletions

View File

@@ -10,6 +10,7 @@ import edu.wpi.first.util.datalog.DataLog;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.OptionalLong;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
@@ -594,6 +595,24 @@ public final class NetworkTableInstance implements AutoCloseable {
return m_listeners.add(m_handle, eventKinds, listener);
}
/**
* Add a time synchronization listener. The callback function is called asynchronously on a
* separate thread, so it's important to use synchronization or atomics when accessing any shared
* state from the callback function.
*
* @param immediateNotify Notify listener of current time synchronization value
* @param listener Listener to add
* @return Listener handle
*/
public int addTimeSyncListener(
boolean immediateNotify, Consumer<NetworkTableEvent> listener) {
EnumSet<NetworkTableEvent.Kind> eventKinds = EnumSet.of(NetworkTableEvent.Kind.kTimeSync);
if (immediateNotify) {
eventKinds.add(NetworkTableEvent.Kind.kImmediate);
}
return m_listeners.add(m_handle, eventKinds, listener);
}
/**
* Add a listener for changes on a particular topic. The callback function is called
* asynchronously on a separate thread, so it's important to use synchronization or atomics when
@@ -951,6 +970,19 @@ public final class NetworkTableInstance implements AutoCloseable {
return NetworkTablesJNI.isConnected(m_handle);
}
/**
* Get the time offset between server time and local time. Add this value to local time to get
* the estimated equivalent server time. In server mode, this always returns 0. In client mode,
* this returns the time offset only if the client and server are connected and have exchanged
* synchronization messages. Note the time offset may change over time as it is periodically
* updated; to receive updates as events, add a listener to the "time sync" event.
*
* @return Time offset in microseconds (optional)
*/
public OptionalLong getServerTimeOffset() {
return NetworkTablesJNI.getServerTimeOffset(m_handle);
}
/**
* Starts logging entry changes to a DataLog.
*

View File

@@ -8,6 +8,7 @@ import edu.wpi.first.util.RuntimeLoader;
import edu.wpi.first.util.datalog.DataLog;
import java.io.IOException;
import java.util.EnumSet;
import java.util.OptionalLong;
import java.util.concurrent.atomic.AtomicBoolean;
public final class NetworkTablesJNI {
@@ -262,6 +263,8 @@ public final class NetworkTablesJNI {
public static native boolean isConnected(int inst);
public static native OptionalLong getServerTimeOffset(int inst);
public static native long now();
private static native int startEntryDataLog(int inst, long log, String prefix, String logPrefix);