diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2018-02-16 14:39:53 +0100 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2018-02-16 14:46:40 +0100 |
| commit | b84a995a05fecb9ef90c9184959f285f324e7411 (patch) | |
| tree | 88ef342f7b5cc64350a0a76fb2c5e192171ebc02 /opendc-kernel-omega | |
| parent | 2fa0134773a99394aae0efc167af6767e2828c71 (diff) | |
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.
Diffstat (limited to 'opendc-kernel-omega')
| -rw-r--r-- | opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt | 22 |
1 files changed, 14 insertions, 8 deletions
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 @@ -250,11 +250,6 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot get() = this@OmegaSimulation.model /** - * The state of the entity. - */ - override var state: S = process.initialState - - /** * The current point in simulation time. */ override val time: Instant @@ -268,9 +263,9 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, 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. @@ -279,6 +274,16 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot 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. */ lateinit var continuation: Continuation<Envelope<*>> @@ -359,7 +364,8 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot * * @return The envelope containing the message. */ - suspend fun receiveEnvelope(): Envelope<*> = suspendCoroutine { continuation = it } + suspend fun receiveEnvelope() = suspendCoroutine<Envelope<*>> { continuation = it } + .also { sender = it.sender } // Completion continuation implementation /** |
