summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2018-02-19 15:09:19 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2018-02-19 15:09:19 +0100
commit59247a4f7a2dc948b3a63ff185c64922eb4334ea (patch)
tree86781781b4529f23c858dbe004a13706f5b61e6d
parentac0a8505ceb817e33011ff80f8585b199896b9c4 (diff)
refactor(#18): Refactor unused transformation receive methods
This change removes the unused transformation receive methods from the `Context` class as this functionality can now be easily implemented in the standard library using the newly introduced `sender` property.
-rw-r--r--opendc-core/src/main/kotlin/com/atlarge/opendc/simulator/Context.kt50
-rw-r--r--opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt37
-rw-r--r--opendc-kernel-omega/src/test/kotlin/com/atlarge/opendc/omega/SmokeTest.kt5
3 files changed, 20 insertions, 72 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 24f87eff..b1d635fd 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
@@ -119,34 +119,11 @@ interface Context<S, M> : CoroutineContext.Element {
/**
* Retrieve and remove a single message from the instance's mailbox, suspending the function if the mailbox is
- * empty. The execution is resumed after the head of the mailbox is removed after which the message [Envelope] is
- * transformed through `transform` to return the transformed result.
- *
- * @param transform The block to transform the message with in an envelope context, providing access to the sender
- * of the message.
- * @return The transformed message.
- */
- suspend fun <T> receive(transform: suspend Envelope<*>.(Any) -> T): T
-
- /**
- * Retrieve and remove a single message from the instance's mailbox, suspending the function if the mailbox is
- * empty. The execution is either resumed after the head of the mailbox is removed after which the message
- * [Envelope] is transformed through `transform` to return the transformed result or the timeout has been reached.
- *
- * @param timeout The duration to wait before resuming execution.
- * @param transform The block to transform the message with in an envelope context, providing access to the sender
- * of the message.
- * @return The processed message or `null` if the timeout was reached.
- */
- suspend fun <T> receive(timeout: Duration, transform: suspend Envelope<*>.(Any) -> T): T?
-
- /**
- * Retrieve and remove a single message from the instance's mailbox, suspending the function if the mailbox is
* empty. The execution is resumed after the head of the mailbox is removed and returned.
*
* @return The received message.
*/
- suspend fun receive(): Any = receive { it }
+ suspend fun receive(): Any
/**
* Retrieve and remove a single message from the instance's mailbox, suspending the function if the mailbox is
@@ -155,7 +132,7 @@ interface Context<S, M> : CoroutineContext.Element {
*
* @return The received message or `null` if the timeout was reached.
*/
- suspend fun receive(timeout: Duration): Any? = receive(timeout) { it }
+ suspend fun receive(timeout: Duration): Any?
/**
* Send the given message to the specified entity, without providing any guarantees about the actual delivery of
@@ -183,29 +160,6 @@ interface Context<S, M> : CoroutineContext.Element {
}
/**
- * The message envelope that is sent to an [Entity], also containing the metadata of the message.
- *
- * @param T The shape of the message inside the envelope.
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface Envelope<out T : Any> {
- /**
- * The message in this envelope.
- */
- val message: T
-
- /**
- * The sender of the message.
- */
- val sender: Entity<*, *>?
-
- /**
- * The destination of the message.
- */
- val destination: Entity<*, *>
-}
-
-/**
* An [Interrupt] message is sent to an [Entity] process in order to interrupt its suspended state.
*
* @param reason The reason for the interruption of the process.
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 8c1497c8..4d94bf9e 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
@@ -53,9 +53,9 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot
/**
* The message queue.
*/
- private val queue: Queue<MessageContainer> = PriorityQueue(Comparator
- .comparingLong(MessageContainer::time)
- .thenComparingLong(MessageContainer::id))
+ private val queue: Queue<Envelope> = PriorityQueue(Comparator
+ .comparingLong(Envelope::time)
+ .thenComparingLong(Envelope::id))
/**
* The kernel process instance that handles internal operations during the simulation.
@@ -199,13 +199,12 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot
* @property time The point in time to deliver the message.
* @property sender The sender of the message.
* @property destination The destination of the message.
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
- private data class MessageContainer(val id: Long,
- override val message: Any,
- val time: Instant,
- override val sender: Entity<*, *>?,
- override val destination: Entity<*, *>) : Envelope<Any> {
+ private data class Envelope(val id: Long,
+ val message: Any,
+ val time: Instant,
+ val sender: Entity<*, *>?,
+ val destination: Entity<*, *>) {
/**
* A flag to indicate the message has been canceled.
*/
@@ -217,7 +216,7 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot
*
* @param envelope The envelope containing the message to schedule.
*/
- private fun schedule(envelope: MessageContainer) {
+ private fun schedule(envelope: Envelope) {
queue.add(envelope)
}
@@ -230,9 +229,9 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot
* @param delay The time to delay the message.
*/
private fun prepare(message: Any, destination: Entity<*, *>, sender: Entity<*, *>? = null,
- delay: Duration): MessageContainer {
+ delay: Duration): Envelope {
require(delay >= 0) { "The amount of time to delay the message must be a positive number" }
- return MessageContainer(nextId++, message, time + delay, sender, destination)
+ return Envelope(nextId++, message, time + delay, sender, destination)
}
/**
@@ -302,20 +301,16 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot
/**
* The continuation to resume the execution of the process.
*/
- lateinit var continuation: Continuation<Envelope<*>>
+ lateinit var continuation: Continuation<Envelope>
/**
* The last point in time the process has done some work.
*/
var last: Instant = -1
- override suspend fun <T> receive(transform: suspend Envelope<*>.(Any) -> T): T {
- val envelope = receiveEnvelope()
- return transform(envelope, envelope.message)
- }
-
+ override suspend fun receive(): Any = receiveEnvelope().message
- override suspend fun <T> receive(timeout: Duration, transform: suspend Envelope<*>.(Any) -> T): T? {
+ override suspend fun receive(timeout: Duration): Any? {
val send = prepare(Timeout, process, process, timeout).also { schedule(it) }
try {
@@ -323,7 +318,7 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot
if (received.message != Timeout) {
send.canceled = true
- return transform(received, received.message)
+ return received.message
}
return null
@@ -380,7 +375,7 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot
*
* @return The envelope containing the message.
*/
- suspend fun receiveEnvelope() = suspendCoroutine<Envelope<*>> { continuation = it }
+ suspend fun receiveEnvelope() = suspendCoroutine<Envelope> { continuation = it }
.also { sender = it.sender }
// Completion continuation implementation
diff --git a/opendc-kernel-omega/src/test/kotlin/com/atlarge/opendc/omega/SmokeTest.kt b/opendc-kernel-omega/src/test/kotlin/com/atlarge/opendc/omega/SmokeTest.kt
index 74fa686b..c47f9a26 100644
--- a/opendc-kernel-omega/src/test/kotlin/com/atlarge/opendc/omega/SmokeTest.kt
+++ b/opendc-kernel-omega/src/test/kotlin/com/atlarge/opendc/omega/SmokeTest.kt
@@ -41,9 +41,8 @@ internal class SmokeTest {
override val initialState = Unit
override suspend fun Context<Unit, Unit>.run() {
while (true) {
- receive {
- sender?.send(message)
- }
+ val msg = receive()
+ sender?.send(msg)
}
}
}