From 9ae85f342e7ca831297a0851617d3e0bb07e49f1 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Mon, 7 Aug 2023 10:44:56 +0200 Subject: [PATCH] Allow the reference clock to be adjusted --- .../kotlin/com/tidal/networktime/ReadableClock.kt | 7 +++++++ .../commonMain/kotlin/com/tidal/networktime/SNTPClient.kt | 7 ++++--- .../kotlin/com/tidal/networktime/WriteableClock.kt | 7 +++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 library/src/commonMain/kotlin/com/tidal/networktime/ReadableClock.kt create mode 100644 library/src/commonMain/kotlin/com/tidal/networktime/WriteableClock.kt diff --git a/library/src/commonMain/kotlin/com/tidal/networktime/ReadableClock.kt b/library/src/commonMain/kotlin/com/tidal/networktime/ReadableClock.kt new file mode 100644 index 00000000..7b37d6c8 --- /dev/null +++ b/library/src/commonMain/kotlin/com/tidal/networktime/ReadableClock.kt @@ -0,0 +1,7 @@ +package com.tidal.networktime + +import kotlin.time.Duration + +interface ReadableClock { + val time: Duration +} diff --git a/library/src/commonMain/kotlin/com/tidal/networktime/SNTPClient.kt b/library/src/commonMain/kotlin/com/tidal/networktime/SNTPClient.kt index 9a362e31..cfea16ff 100644 --- a/library/src/commonMain/kotlin/com/tidal/networktime/SNTPClient.kt +++ b/library/src/commonMain/kotlin/com/tidal/networktime/SNTPClient.kt @@ -10,15 +10,16 @@ import kotlin.time.Duration.Companion.seconds * [ntpServers] to obtain information about their provided time. * * @param ntpServers Representation of supported unicast NTP sources. - * @param referenceClock A provider of UNIX time, used to calculate timing differences with the - * information obtained from [ntpServers]. + * @param referenceClock A provider of UNIX time used to calculate timing differences with the + * information obtained from [ntpServers]. May optionally implement [WriteableClock] to be adjusted + * on every synchronization with the calculated time difference. * @param coroutineScope The scope where synchronization will run on. * @param syncInterval The amount of time to wait between a sync finishing and the next one being * started. */ class SNTPClient( vararg val ntpServers: NTPServer, - val referenceClock: () -> Duration, + val referenceClock: ReadableClock, val coroutineScope: CoroutineScope = GlobalScope, val syncInterval: Duration = 64.seconds, ) { diff --git a/library/src/commonMain/kotlin/com/tidal/networktime/WriteableClock.kt b/library/src/commonMain/kotlin/com/tidal/networktime/WriteableClock.kt new file mode 100644 index 00000000..6aaa5f09 --- /dev/null +++ b/library/src/commonMain/kotlin/com/tidal/networktime/WriteableClock.kt @@ -0,0 +1,7 @@ +package com.tidal.networktime + +import kotlin.time.Duration + +interface WriteableClock : ReadableClock { + override var time: Duration +}