-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
library/src/commonMain/kotlin/com/tidal/networktime/NTPServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.tidal.networktime | ||
|
||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
sealed interface NTPServer { | ||
val hostName: String | ||
val responseTimeout: Duration | ||
val dnsResolutionStrategy: DNSResolutionStrategy | ||
|
||
sealed interface Unicast : NTPServer { | ||
class Sequential( | ||
override val hostName: String, | ||
override val responseTimeout: Duration = 5.seconds, | ||
override val dnsResolutionStrategy: DNSResolutionStrategy = DNSResolutionStrategy.ALL, | ||
val outgoingRequestGap: Duration = OUTGOING_REQUEST_GAP_DEFAULT_SECONDS.seconds, | ||
) : Unicast | ||
|
||
class Concurrent( | ||
override val hostName: String, | ||
override val responseTimeout: Duration = 5.seconds, | ||
override val dnsResolutionStrategy: DNSResolutionStrategy = DNSResolutionStrategy.ALL, | ||
) : Unicast | ||
|
||
companion object { | ||
private const val OUTGOING_REQUEST_GAP_DEFAULT_SECONDS = 2 | ||
} | ||
} | ||
|
||
class Anycast( | ||
override val hostName: String, | ||
override val responseTimeout: Duration = 68.seconds, | ||
override val dnsResolutionStrategy: DNSResolutionStrategy = DNSResolutionStrategy.ALL, | ||
) : NTPServer | ||
|
||
enum class DNSResolutionStrategy { | ||
IP_V4, | ||
IP_V6, | ||
ALL, | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
library/src/commonMain/kotlin/com/tidal/networktime/SNTPClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.tidal.networktime | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
/** | ||
* Construct a new SNTP client that can be requested to periodically interact with the provided | ||
* [ntpServers] to obtain information about their provided time. | ||
* | ||
* @param ntpServers Representation of supported NTP sources. | ||
* @param coroutineScope The scope where synchronization will run on. | ||
* @param ntpVersion The version number to write in packets. | ||
* @param portSelectionStrategy The strategy for selecting a port to operate on. | ||
* @param minimumSynchronizationInterval The minimum amount of time between performing time queries | ||
* on all unicast sources. The actual value used may be larger than this on occasion based on | ||
* changes to the difference between the time provided by [ntpServers] and [referenceClock] (if it | ||
* ever changes), but will never be lower than this value. | ||
* @param referenceClock A clock used to calculate timing differences with the information obtained | ||
* from [ntpServers]. This clock will never be modified directly. | ||
*/ | ||
class SNTPClient( | ||
vararg val ntpServers: NTPServer, | ||
val coroutineScope: CoroutineScope, | ||
val ntpVersion: NTPVersion = NTPVersion.FOUR, | ||
val portSelectionStrategy: PortSelectionStrategy = PortSelectionStrategy.RFC9109, | ||
val minimumSynchronizationInterval: Duration = 64.seconds, | ||
val referenceClock: () -> Long, | ||
) { | ||
|
||
val time: Long? | ||
get() = TODO("Getting the time") | ||
|
||
fun startSynchronization(): Unit = TODO("Start or return") | ||
|
||
fun stopSynchronization(): Unit = TODO("Stop or return") | ||
|
||
enum class NTPVersion(val descriptor: Short) { | ||
ZERO(0), | ||
ONE(1), | ||
TWO(2), | ||
THREE(3), | ||
FOUR(4), | ||
} | ||
|
||
sealed class PortSelectionStrategy(val pinnedPortNumber: Int?) { | ||
data object RFC5905 : PortSelectionStrategy(123) | ||
|
||
/** | ||
* Make a new selection of a random available port every time a socket is opened. | ||
*/ | ||
data object RFC9109 : PortSelectionStrategy(null) | ||
} | ||
} |