summaryrefslogtreecommitdiffhomepage
path: root/android/src
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-02-02 01:01:34 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-04-21 12:24:47 +0000
commit43a054d7dd19a5d1e5d2b7351d779229d6ac6c4d (patch)
treee2ee45d4cdbd9f0feb9489fc0242bed900fc0b16 /android/src
parent3930e72e7e64335f6538bb10756b6d1cd865d714 (diff)
downloadmullvadvpn-43a054d7dd19a5d1e5d2b7351d779229d6ac6c4d.tar.xz
mullvadvpn-43a054d7dd19a5d1e5d2b7351d779229d6ac6c4d.zip
Set relay location using `RelayListListener`
Diffstat (limited to 'android/src')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt51
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)
+ }
}