diff options
Diffstat (limited to 'android/src/main')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt index bb5434e3e7..a65c313f54 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt @@ -1,12 +1,34 @@ package net.mullvad.mullvadvpn.service.endpoint import kotlin.properties.Delegates.observable +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.channels.ClosedReceiveChannelException +import kotlinx.coroutines.channels.actor +import kotlinx.coroutines.channels.sendBlocking import net.mullvad.mullvadvpn.ipc.Event +import net.mullvad.mullvadvpn.ipc.Request +import net.mullvad.mullvadvpn.model.Constraint +import net.mullvad.mullvadvpn.model.LocationConstraint +import net.mullvad.mullvadvpn.model.RelayConstraintsUpdate import net.mullvad.mullvadvpn.model.RelayList +import net.mullvad.mullvadvpn.model.RelaySettingsUpdate import net.mullvad.mullvadvpn.service.MullvadDaemon class RelayListListener(endpoint: ServiceEndpoint) { - val daemon = endpoint.intermittentDaemon + companion object { + private enum class Command { + SetRelayLocation, + } + } + + private val commandChannel = spawnActor() + private val daemon = endpoint.intermittentDaemon + + private var selectedRelayLocation by observable<LocationConstraint?>(null) { _, _, _ -> + commandChannel.sendBlocking(Command.SetRelayLocation) + } var relayList by observable<RelayList?>(null) { _, _, relays -> endpoint.sendEvent(Event.NewRelayList(relays)) @@ -20,9 +42,14 @@ class RelayListListener(endpoint: ServiceEndpoint) { fetchInitialRelayList(daemon) } } + + endpoint.dispatcher.registerHandler(Request.SetRelayLocation::class) { request -> + selectedRelayLocation = request.relayLocation + } } fun onDestroy() { + commandChannel.close() daemon.unregisterListener(this) } @@ -39,4 +66,26 @@ class RelayListListener(endpoint: ServiceEndpoint) { } } } + + private fun spawnActor() = GlobalScope.actor<Command>(Dispatchers.Default, Channel.CONFLATED) { + try { + for (command in channel) { + when (command) { + Command.SetRelayLocation -> updateRelayConstraints() + } + } + } catch (exception: ClosedReceiveChannelException) { + // Closed sender, so stop the actor + } + } + + private suspend fun updateRelayConstraints() { + val constraint: Constraint<LocationConstraint> = selectedRelayLocation?.let { location -> + Constraint.Only(location) + } ?: Constraint.Any() + + val update = RelaySettingsUpdate.Normal(RelayConstraintsUpdate(constraint)) + + daemon.await().updateRelaySettings(update) + } } |
