mirror of
https://github.com/corda/corda.git
synced 2025-06-23 01:19:00 +00:00
[CORDA-3628] - Implement sendAll API (#5990)
* [CORDA-3628] - Implement sendAll API * detekt * Some minor refactorings and docs * Eliminate warnings * Address Rick's comments * Switch sendAll to use a set
This commit is contained in:
@ -25,6 +25,7 @@ import net.corda.core.node.NodeInfo
|
||||
import net.corda.core.node.ServiceHub
|
||||
import net.corda.core.serialization.CordaSerializable
|
||||
import net.corda.core.serialization.SerializationDefaults
|
||||
import net.corda.core.serialization.SerializedBytes
|
||||
import net.corda.core.serialization.serialize
|
||||
import net.corda.core.transactions.SignedTransaction
|
||||
import net.corda.core.utilities.ProgressTracker
|
||||
@ -317,6 +318,53 @@ abstract class FlowLogic<out T> {
|
||||
return castMapValuesToKnownType(receiveAllMap(associateSessionsToReceiveType(receiveType, sessions)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Queues the given [payload] for sending to the provided [sessions] and continues without suspending.
|
||||
*
|
||||
* Note that the other parties may receive the message at some arbitrary later point or not at all: if one of the provided [sessions]
|
||||
* is offline then message delivery will be retried until the corresponding node comes back or until the message is older than the
|
||||
* network's event horizon time.
|
||||
*
|
||||
* @param payload the payload to send.
|
||||
* @param sessions the sessions to send the provided payload to.
|
||||
* @param maySkipCheckpoint whether checkpointing should be skipped.
|
||||
*/
|
||||
@Suspendable
|
||||
@JvmOverloads
|
||||
fun sendAll(payload: Any, sessions: Set<FlowSession>, maySkipCheckpoint: Boolean = false) {
|
||||
val sessionToPayload = sessions.map { it to payload }.toMap()
|
||||
return sendAll(sessionToPayload, maySkipCheckpoint)
|
||||
}
|
||||
|
||||
/**
|
||||
* Queues the given payloads for sending to the provided sessions and continues without suspending.
|
||||
*
|
||||
* Note that the other parties may receive the message at some arbitrary later point or not at all: if one of the provided [sessions]
|
||||
* is offline then message delivery will be retried until the corresponding node comes back or until the message is older than the
|
||||
* network's event horizon time.
|
||||
*
|
||||
* @param payloadsPerSession a mapping that contains the payload to be sent to each session.
|
||||
* @param maySkipCheckpoint whether checkpointing should be skipped.
|
||||
*/
|
||||
@Suspendable
|
||||
@JvmOverloads
|
||||
fun sendAll(payloadsPerSession: Map<FlowSession, Any>, maySkipCheckpoint: Boolean = false) {
|
||||
val request = FlowIORequest.Send(
|
||||
sessionToMessage = serializePayloads(payloadsPerSession)
|
||||
)
|
||||
stateMachine.suspend(request, maySkipCheckpoint)
|
||||
}
|
||||
|
||||
@Suspendable
|
||||
private fun serializePayloads(payloadsPerSession: Map<FlowSession, Any>): Map<FlowSession, SerializedBytes<Any>> {
|
||||
val cachedSerializedPayloads = mutableMapOf<Any, SerializedBytes<Any>>()
|
||||
|
||||
return payloadsPerSession.mapValues { (_, payload) ->
|
||||
cachedSerializedPayloads[payload] ?: payload.serialize(context = SerializationDefaults.P2P_CONTEXT).also { cachedSerializedPayloads[payload] = it }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Invokes the given subflow. This function returns once the subflow completes successfully with the result
|
||||
* returned by that subflow's [call] method. If the subflow has a progress tracker, it is attached to the
|
||||
|
Reference in New Issue
Block a user