Skip to content

Commit

Permalink
Fix ReceiveMessags premature client close
Browse files Browse the repository at this point in the history
  • Loading branch information
sdsantos committed Jul 26, 2021
1 parent 91846d2 commit 589cdf4
Showing 1 changed file with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package tech.relaycorp.awaladroid.messaging

import java.util.logging.Level
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.onCompletion
import tech.relaycorp.awaladroid.Awala
import tech.relaycorp.awaladroid.GatewayException
import tech.relaycorp.awaladroid.GatewayProtocolException
Expand All @@ -23,6 +23,7 @@ import tech.relaycorp.relaynet.bindings.pdc.StreamingMode
import tech.relaycorp.relaynet.messages.InvalidMessageException
import tech.relaycorp.relaynet.ramf.RAMFException
import tech.relaycorp.relaynet.wrappers.cms.EnvelopedDataException
import java.util.logging.Level

internal class ReceiveMessages(
private val pdcClientBuilder: () -> PDCClient = { PoWebClient.initLocal(Awala.POWEB_PORT) }
Expand All @@ -36,16 +37,19 @@ internal class ReceiveMessages(
fun receive(): Flow<IncomingMessage> =
getNonceSigners()
.flatMapLatest { nonceSigners ->
pdcClientBuilder().use {
try {
collectParcels(it, nonceSigners)
} catch (exp: ServerException) {
throw ReceiveMessageException("Server error", exp)
} catch (exp: ClientBindingException) {
throw GatewayProtocolException("Client error", exp)
} catch (exp: NonceSignerException) {
throw GatewayProtocolException("Client signing error", exp)
}
val pdcClient = pdcClientBuilder()
try {
collectParcels(pdcClient, nonceSigners)
.onCompletion {
@Suppress("BlockingMethodInNonBlockingContext")
pdcClient.close()
}
} catch (exp: ServerException) {
throw ReceiveMessageException("Server error", exp)
} catch (exp: ClientBindingException) {
throw GatewayProtocolException("Client error", exp)
} catch (exp: NonceSignerException) {
throw GatewayProtocolException("Client signing error", exp)
}
}

Expand Down

0 comments on commit 589cdf4

Please sign in to comment.