Skip to content

Commit

Permalink
Add initial API
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyicker committed Aug 3, 2023
1 parent b9123bc commit 7af6f24
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
16 changes: 16 additions & 0 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,20 @@ kotlin {
androidNativeX64()
mingwX64()
watchosDeviceArm64()

sourceSets {
val commonMain by getting {
dependencies {
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
}
}
}
}

android {
compileSdk = 34
defaultConfig {
minSdk = 1
}
namespace = group.toString()
}
41 changes: 41 additions & 0 deletions library/src/commonMain/kotlin/com/tidal/networktime/NTPServer.kt
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 library/src/commonMain/kotlin/com/tidal/networktime/SNTPClient.kt
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)
}
}

0 comments on commit 7af6f24

Please sign in to comment.