From b84a995a05fecb9ef90c9184959f285f324e7411 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Fri, 16 Feb 2018 14:39:53 +0100 Subject: refactor(#18): Provide access to latest sender This change adds a `sender` property to the `Context` interface to provide processes access to the sender of the latest received message. Please note that methods like `hold()` and `interrupt()` may change the value of this property. --- .../kotlin/com/atlarge/opendc/simulator/Context.kt | 8 ++++++++ .../com/atlarge/opendc/omega/OmegaSimulation.kt | 22 ++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/opendc-core/src/main/kotlin/com/atlarge/opendc/simulator/Context.kt b/opendc-core/src/main/kotlin/com/atlarge/opendc/simulator/Context.kt index c89bdb59..13170256 100644 --- a/opendc-core/src/main/kotlin/com/atlarge/opendc/simulator/Context.kt +++ b/opendc-core/src/main/kotlin/com/atlarge/opendc/simulator/Context.kt @@ -50,6 +50,14 @@ interface Context { */ val delta: Duration + /** + * The sender of the last received message or `null` in case the process has not received any messages yet. + * + * Note that this property is only guaranteed to be correct when accessing after a single suspending call. Methods + * like `hold()` and `interrupt()` may change the value of this property. + */ + val sender: Entity<*, *>? + /** * The observable state of the entity bound to this scope. */ diff --git a/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt b/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt index 9c6d4ab3..bd3f4529 100644 --- a/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt +++ b/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt @@ -249,11 +249,6 @@ internal class OmegaSimulation(bootstrap: Bootstrap) : Simulation, Boot override val model: M get() = this@OmegaSimulation.model - /** - * The state of the entity. - */ - override var state: S = process.initialState - /** * The current point in simulation time. */ @@ -268,9 +263,9 @@ internal class OmegaSimulation(bootstrap: Bootstrap) : Simulation, Boot get() = maxOf(time - last, 0) /** - * The [CoroutineContext] for a [Context]. + * The state of the entity. */ - override val context: CoroutineContext = EmptyCoroutineContext + override var state: S = process.initialState /** * The observable state of an [Entity] within the simulation is provided by the context of the simulation. @@ -278,6 +273,16 @@ internal class OmegaSimulation(bootstrap: Bootstrap) : Simulation, Boot override val , S> T.state: S get() = context?.state ?: initialState + /** + * The sender of the last received message or `null` in case the process has not received any messages yet. + */ + override var sender: Entity<*, *>? = null + + /** + * The [CoroutineContext] for a [Context]. + */ + override val context: CoroutineContext = EmptyCoroutineContext + /** * The continuation to resume the execution of the process. */ @@ -359,7 +364,8 @@ internal class OmegaSimulation(bootstrap: Bootstrap) : Simulation, Boot * * @return The envelope containing the message. */ - suspend fun receiveEnvelope(): Envelope<*> = suspendCoroutine { continuation = it } + suspend fun receiveEnvelope() = suspendCoroutine> { continuation = it } + .also { sender = it.sender } // Completion continuation implementation /** -- cgit v1.2.3