receiveMessageOrTimeout

suspend fun <M : AgentMessage> KSLProcessBuilder.receiveMessageOrTimeout(mailbox: AgentModel.AgentMailbox<M>, timeout: Double, timeoutPriority: Int = TIMEOUT_PRIORITY, suspensionName: String? = null, predicate: (M) -> Boolean = { true }): WaitResult<M>(source)

Wait, for at most timeout time units, for a message matching predicate in mailbox.

The result is satisfied with the message if one arrives in time, or is already waiting in the mailbox (then the call returns at once). Otherwise, when the time runs out, the result is timed out, nothing is taken from the mailbox, and a matching message delivered later is kept in the mailbox as if nobody had waited. While it waits, the receiver takes its place among the mailbox's waiting receivers in the order it started waiting, as with receiveMessage, and a receiver terminated before it takes a message handed to it hands the message back in the same way.

A message delivered at the very instant the time runs out wins at the default priorities: the timer runs late in the instant. Give an earlier timeoutPriority to let the time limit win instead.

val reply = receiveMessageOrTimeout(mailbox, timeout = 5.0) { it.conversationId == convId }
if (reply.timedOut) giveUp() else handle(reply.value)

Return

the message, or that the wait timed out, with the time waited

Parameters

mailbox

the mailbox to receive from

timeout

the time limit; zero or more, or infinite for no limit

timeoutPriority

the priority of the event that ends the wait when the time runs out

suspensionName

a name for the suspension, for tracing

predicate

selects the messages to accept; by default, any message