diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-01-15 21:17:34 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-04-22 19:16:13 +0000 |
| commit | 140393401156ccddb3d0e4ab37c9cf47bc43255b (patch) | |
| tree | c49648188cab108b44e4885210448d333ff2167f /android | |
| parent | 42f65f2f3678d15378301c426a2f029323f78357 (diff) | |
| download | mullvadvpn-140393401156ccddb3d0e4ab37c9cf47bc43255b.tar.xz mullvadvpn-140393401156ccddb3d0e4ab37c9cf47bc43255b.zip | |
Create service side `VoucherRedeemer`
Diffstat (limited to 'android')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/VoucherRedeemer.kt | 40 |
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 + } + } +} |
