summaryrefslogtreecommitdiff
path: root/opendc-core
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2017-09-20 00:59:54 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2017-09-20 00:59:54 +0200
commit39cfa9724c71796f2c16aa1ed90fbd4425540eef (patch)
tree920ef35be300be4681ffd63f016f4db51ddd543e /opendc-core
parent90e0d1d8c6ab94e020dd4cb4831a369b270a69b7 (diff)
Add support for timeouts on receive calls
This change allows processes to set a timeout when waiting for a message to arrive.
Diffstat (limited to 'opendc-core')
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Readable.kt25
1 files changed, 25 insertions, 0 deletions
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Readable.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Readable.kt
index 772d9013..8faf17ab 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Readable.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Readable.kt
@@ -24,6 +24,9 @@
package nl.atlarge.opendc.kernel.messaging
+import nl.atlarge.opendc.kernel.time.Duration
+
+
/**
* A [Readable] instance has a mailbox associated with the instance to which objects can send messages, which can be
* received by the class.
@@ -43,8 +46,30 @@ interface Readable {
/**
* Retrieve and removes a single message from the entity's mailbox, suspending the function if the mailbox is empty.
+ * The execution is resumed after the message has landed in the entity's mailbox or the timeout was reached,
+ *
+ * If the message has been received, the message [Envelope] is mapped through `block` to generate a processed
+ * message. If the timeout was reached, `block` is not called and `null` is returned.
+ *
+ * @param timeout The duration to wait before resuming execution.
+ * @param block The block to process the message with.
+ * @return The processed message or `null` if the timeout was reached.
+ */
+ suspend fun <T> receive(timeout: Duration, block: Envelope<*>.(Any) -> T): T?
+
+ /**
+ * Retrieve and removes a single message from the entity's mailbox, suspending the function until a message has
+ * landed in the entity's mailbox.
*
* @return The message that was received from the entity's mailbox.
*/
suspend fun receive(): Any = receive { it }
+
+ /**
+ * Retrieve and removes a single message from the entity's mailbox, suspending the function until a message has
+ * landed in the entity's mailbox or the timeout has been reached.
+ *
+ * @return The message that was received from the entity's mailbox or `null` if the timeout was reached.
+ */
+ suspend fun receive(timeout: Duration): Any? = receive(timeout) { it }
}