summaryrefslogtreecommitdiffhomepage
path: root/android/src
diff options
context:
space:
mode:
Diffstat (limited to 'android/src')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/VoucherRedeemer.kt40
1 files changed, 40 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/VoucherRedeemer.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/VoucherRedeemer.kt
new file mode 100644
index 0000000000..6b83b0d333
--- /dev/null
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/VoucherRedeemer.kt
@@ -0,0 +1,40 @@
+package net.mullvad.mullvadvpn.service.endpoint
+
+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.VoucherSubmissionResult
+
+class VoucherRedeemer(private val endpoint: ServiceEndpoint) {
+ private val daemon
+ get() = endpoint.intermittentDaemon
+
+ private val voucherChannel = spawnActor()
+
+ init {
+ endpoint.dispatcher.registerHandler(Request.SubmitVoucher::class) { request ->
+ voucherChannel.sendBlocking(request.voucher)
+ }
+ }
+
+ fun onDestroy() {
+ voucherChannel.close()
+ }
+
+ private fun spawnActor() = GlobalScope.actor<String>(Dispatchers.Default, Channel.UNLIMITED) {
+ try {
+ for (voucher in channel) {
+ val result = daemon.await().submitVoucher(voucher)
+
+ endpoint.sendEvent(Event.VoucherSubmissionResult(voucher, result))
+ }
+ } catch (exception: ClosedReceiveChannelException) {
+ // Voucher channel was closed, stop the actor
+ }
+ }
+}