summaryrefslogtreecommitdiffhomepage
path: root/android
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-03-26 14:09:31 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-03-29 14:22:55 +0000
commit599bc92647b0c9ec6dc32a776afb345cdf7c4fc1 (patch)
treeae7ffc154c2467a5819bc88dcecd481343742050 /android
parent94c32794769d59472829295aab95c3361a948349 (diff)
downloadmullvadvpn-599bc92647b0c9ec6dc32a776afb345cdf7c4fc1.tar.xz
mullvadvpn-599bc92647b0c9ec6dc32a776afb345cdf7c4fc1.zip
Refactor `KeyStatusListener` into an actor
Diffstat (limited to 'android')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/KeyStatusListener.kt35
1 files changed, 30 insertions, 5 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/KeyStatusListener.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/KeyStatusListener.kt
index e5a4e9e0d2..70ac7ef827 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/KeyStatusListener.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/KeyStatusListener.kt
@@ -3,14 +3,26 @@ package net.mullvad.mullvadvpn.service.endpoint
import kotlin.properties.Delegates.observable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
-import kotlinx.coroutines.launch
+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.KeygenEvent
class KeyStatusListener(endpoint: ServiceEndpoint) {
+ companion object {
+ private enum class Command {
+ GenerateKey,
+ VerifyKey,
+ }
+ }
+
private val daemon = endpoint.intermittentDaemon
+ private val commandChannel = spawnActor()
+
var keyStatus by observable<KeygenEvent?>(null) { _, _, status ->
endpoint.sendEvent(Event.WireGuardKeyStatus(status))
}
@@ -29,20 +41,33 @@ class KeyStatusListener(endpoint: ServiceEndpoint) {
endpoint.dispatcher.apply {
registerHandler(Request.WireGuardGenerateKey::class) { _ ->
- generateKey()
+ commandChannel.sendBlocking(Command.GenerateKey)
}
registerHandler(Request.WireGuardVerifyKey::class) { _ ->
- verifyKey()
+ commandChannel.sendBlocking(Command.VerifyKey)
}
}
}
fun onDestroy() {
+ commandChannel.close()
daemon.unregisterListener(this)
}
- private fun generateKey() = GlobalScope.launch(Dispatchers.Default) {
+ private fun spawnActor() = GlobalScope.actor<Command>(Dispatchers.Default, Channel.UNLIMITED) {
+ try {
+ for (command in channel) {
+ when (command) {
+ Command.GenerateKey -> generateKey()
+ Command.VerifyKey -> verifyKey()
+ }
+ }
+ } catch (exception: ClosedReceiveChannelException) {
+ }
+ }
+
+ private suspend fun generateKey() {
val oldStatus = keyStatus
val newStatus = daemon.await().generateWireguardKey()
val newFailure = newStatus?.failure()
@@ -57,7 +82,7 @@ class KeyStatusListener(endpoint: ServiceEndpoint) {
}
}
- private fun verifyKey() = GlobalScope.launch(Dispatchers.Default) {
+ private suspend fun verifyKey() {
// Only update verification status if the key is actually there
(keyStatus as? KeygenEvent.NewKey)?.let { currentStatus ->
keyStatus = KeygenEvent.NewKey(