Skip to content

Commit

Permalink
Implement sync start and stop
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyicker committed Aug 10, 2023
1 parent 44fa5a9 commit a5751f4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tidal.networktime.internal

import kotlinx.coroutines.Job

internal class MutableState(var job: Job? = null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.tidal.networktime.internal

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

internal class OperationCoordinator
@OptIn(ExperimentalCoroutinesApi::class)
constructor(
private val mutableState: MutableState,
private val coroutineScope: CoroutineScope,
globalDispatcher: CoroutineDispatcher,
private val privateDispatcher: CoroutineDispatcher = globalDispatcher.limitedParallelism(1),
) {

fun dispatchStartSync() = dispatch(StartSyncRunnable(mutableState))

fun dispatchStopSync() = dispatch(StopSyncRunnable(mutableState))

private fun dispatch(block: () -> Unit) = coroutineScope.launch {
withContext(privateDispatcher) { block() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package com.tidal.networktime.internal

import com.tidal.networktime.NTPServer
import com.tidal.networktime.ReadableClock
import io.ktor.client.HttpClient
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

Expand All @@ -11,13 +14,14 @@ internal class PlatformAgnosticSNTPClient(
val referenceClock: ReadableClock,
val coroutineScope: CoroutineScope,
val synchronizationInterval: Duration = 64.seconds,
httpClientFactory: HttpClientFactory = HttpClientFactory(),
private val httpClient: HttpClient = HttpClientFactory()(),
private val operationCoordinator: OperationCoordinator =
OperationCoordinator(MutableState(), coroutineScope, Dispatchers.IO),
) {
val synchronizedEpochTime: Duration?
get() = TODO("Get the time")
private val httpClient = httpClientFactory()

fun startSynchronization(): Unit = TODO("Start or return")
fun enableSynchronization() = operationCoordinator.dispatchStartSync()

fun stopSynchronization(): Unit = TODO("Stop or return")
fun disableSynchronization() = operationCoordinator.dispatchStopSync()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tidal.networktime.internal

internal class StartSyncRunnable(private val mutableState: MutableState) : () -> Unit {
override operator fun invoke() = mutableState.run {
if (job != null) {
return
}
job = TODO()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tidal.networktime.internal

internal class StopSyncRunnable(private val mutableState: MutableState) : () -> Unit {
override operator fun invoke() = mutableState.run {
job?.cancel() ?: return
job = null
}
}

0 comments on commit a5751f4

Please sign in to comment.