summaryrefslogtreecommitdiff
path: root/opendc-simulator/opendc-simulator-compute
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-09-30 15:37:35 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-10-03 17:17:40 +0200
commita2ce07026bf3ef17326e72f395dfa2dd9d9b17be (patch)
tree347cd882148b43808bdd146fb7870b00103e5e6b /opendc-simulator/opendc-simulator-compute
parent4f5a1f88d0c6aa19ce4cab0ec7b9b13a24c92fbe (diff)
refactor(simulator): Create separate callbacks for remaining events
This change creates separate callbacks for the remaining events: onStart, onStop and onConverge.
Diffstat (limited to 'opendc-simulator/opendc-simulator-compute')
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/device/SimPsu.kt18
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/workload/SimWorkloadLifecycle.kt24
2 files changed, 14 insertions, 28 deletions
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/device/SimPsu.kt b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/device/SimPsu.kt
index b05d8ad9..8400c225 100644
--- a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/device/SimPsu.kt
+++ b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/device/SimPsu.kt
@@ -24,7 +24,6 @@ package org.opendc.simulator.compute.device
import org.opendc.simulator.compute.power.PowerDriver
import org.opendc.simulator.flow.FlowConnection
-import org.opendc.simulator.flow.FlowEvent
import org.opendc.simulator.flow.FlowSource
import org.opendc.simulator.power.SimPowerInlet
import java.util.*
@@ -82,19 +81,22 @@ public class SimPsu(
}
override fun createConsumer(): FlowSource = object : FlowSource {
+ override fun onStart(conn: FlowConnection, now: Long) {
+ _ctx = conn
+ }
+
+ override fun onStop(conn: FlowConnection, now: Long, delta: Long) {
+ _ctx = null
+ }
+
override fun onPull(conn: FlowConnection, now: Long, delta: Long): Long {
val powerDraw = computePowerDraw(_driver?.computePower() ?: 0.0)
conn.push(powerDraw)
return Long.MAX_VALUE
}
- override fun onEvent(conn: FlowConnection, now: Long, event: FlowEvent) {
- when (event) {
- FlowEvent.Start -> _ctx = conn
- FlowEvent.Converge -> _powerDraw = conn.rate
- FlowEvent.Exit -> _ctx = null
- else -> {}
- }
+ override fun onConverge(conn: FlowConnection, now: Long, delta: Long) {
+ _powerDraw = conn.rate
}
}
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/workload/SimWorkloadLifecycle.kt b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/workload/SimWorkloadLifecycle.kt
index b85be39d..cc4f1f6a 100644
--- a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/workload/SimWorkloadLifecycle.kt
+++ b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/workload/SimWorkloadLifecycle.kt
@@ -24,7 +24,6 @@ package org.opendc.simulator.compute.workload
import org.opendc.simulator.compute.SimMachineContext
import org.opendc.simulator.flow.FlowConnection
-import org.opendc.simulator.flow.FlowEvent
import org.opendc.simulator.flow.FlowSource
/**
@@ -41,29 +40,14 @@ public class SimWorkloadLifecycle(private val ctx: SimMachineContext) {
*/
public fun waitFor(consumer: FlowSource): FlowSource {
waiting.add(consumer)
- return object : FlowSource {
- override fun onPull(conn: FlowConnection, now: Long, delta: Long): Long {
- return try {
- consumer.onPull(conn, now, delta)
- } catch (cause: Throwable) {
- complete(consumer)
- throw cause
- }
- }
-
- override fun onEvent(conn: FlowConnection, now: Long, event: FlowEvent) {
+ return object : FlowSource by consumer {
+ override fun onStop(conn: FlowConnection, now: Long, delta: Long) {
try {
- consumer.onEvent(conn, now, event)
-
- if (event == FlowEvent.Exit) {
- complete(consumer)
- }
- } catch (cause: Throwable) {
+ consumer.onStop(conn, now, delta)
+ } finally {
complete(consumer)
- throw cause
}
}
-
override fun toString(): String = "SimWorkloadLifecycle.Consumer[delegate=$consumer]"
}
}