From 86dc826db4cd91b5a6875d9ecdd64c9238d7b95c Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Mon, 19 Feb 2018 15:12:16 +0100 Subject: refactor(#18): Simplify Context interface This change simplifies the `Context` interface to reduce the amount of methods required to implement by implementors. --- .../kotlin/com/atlarge/opendc/simulator/Context.kt | 22 +++++++++++----------- .../com/atlarge/opendc/omega/OmegaSimulation.kt | 2 -- 2 files changed, 11 insertions(+), 13 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 b1d635fd..23d10e8f 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 @@ -74,14 +74,6 @@ interface Context : CoroutineContext.Element { */ val , S> E.state: S - /** - * Interrupt an [Entity] process in simulation. - * - * @see [Entity.interrupt(Interrupt)] - * @param reason The reason for interrupting the entity. - */ - suspend fun Entity<*, *>.interrupt(reason: String) = interrupt(Interrupt(reason)) - /** * Interrupt an [Entity] process in simulation. * @@ -94,6 +86,14 @@ interface Context : CoroutineContext.Element { */ suspend fun Entity<*, *>.interrupt(interrupt: Interrupt) + /** + * Interrupt an [Entity] process in simulation. + * + * @see [Entity.interrupt(Interrupt)] + * @param reason The reason for interrupting the entity. + */ + suspend fun Entity<*, *>.interrupt(reason: String) = interrupt(Interrupt(reason)) + /** * Suspend the [Context] of the [Entity] in simulation for the given duration of simulation time before resuming * execution and drop all messages that are received during this period. @@ -139,19 +139,19 @@ interface Context : CoroutineContext.Element { * the message. * * @param msg The message to send. + * @param sender The sender of the message. * @param delay The amount of time to wait before the message should be received by the entity. */ - suspend fun Entity<*, *>.send(msg: Any, delay: Duration = 0) + suspend fun Entity<*, *>.send(msg: Any, sender: Entity<*, *>, delay: Duration = 0) /** * Send the given message to the specified entity, without providing any guarantees about the actual delivery of * the message. * * @param msg The message to send. - * @param sender The sender of the message. * @param delay The amount of time to wait before the message should be received by the entity. */ - suspend fun Entity<*, *>.send(msg: Any, sender: Entity<*, *>, delay: Duration = 0) + suspend fun Entity<*, *>.send(msg: Any, delay: Duration = 0) = send(msg, self, delay) /** * This key provides users access to an untyped process context in case the coroutine runs inside a simulation. 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 4d94bf9e..532a033a 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 @@ -327,8 +327,6 @@ internal class OmegaSimulation(bootstrap: Bootstrap) : Simulation, Boot } } - override suspend fun Entity<*, *>.send(msg: Any, delay: Duration) = send(msg, process, delay) - override suspend fun Entity<*, *>.send(msg: Any, sender: Entity<*, *>, delay: Duration) = schedule(prepare(msg, this, sender, delay)) -- cgit v1.2.3