summaryrefslogtreecommitdiff
path: root/opendc-simulator/opendc-simulator-flow/src/main
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2022-09-22 22:28:44 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2022-09-22 22:37:50 +0200
commit92787292269783701cb7f1082f0262e7e2851df9 (patch)
treee1df12207a5229530544328773dcf6a002f81210 /opendc-simulator/opendc-simulator-flow/src/main
parent17fa7619f1d7e96680e018d3f12f333fb75cdac1 (diff)
refactor(sim/compute): Simplify SimHypervisor class
This change simplifies the SimHypervisor class into a single implementation. Previously, it was implemented as an abstract class with multiple implementations for each multiplexer type. We now pass the multiplexer type as parameter to the SimHypervisor constructor.
Diffstat (limited to 'opendc-simulator/opendc-simulator-flow/src/main')
-rw-r--r--opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/FlowMultiplexer.kt10
-rw-r--r--opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/FlowMultiplexerFactory.kt60
-rw-r--r--opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/ForwardingFlowMultiplexer.kt6
-rw-r--r--opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/MaxMinFlowMultiplexer.kt5
4 files changed, 81 insertions, 0 deletions
diff --git a/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/FlowMultiplexer.kt b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/FlowMultiplexer.kt
index 3b4583e2..8752c559 100644
--- a/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/FlowMultiplexer.kt
+++ b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/FlowMultiplexer.kt
@@ -31,6 +31,16 @@ import org.opendc.simulator.flow.FlowSource
*/
public interface FlowMultiplexer {
/**
+ * The maximum number of inputs supported by the multiplexer.
+ */
+ public val maxInputs: Int
+
+ /**
+ * The maximum number of outputs supported by the multiplexer.
+ */
+ public val maxOutputs: Int
+
+ /**
* The inputs of the multiplexer that can be used to consume sources.
*/
public val inputs: Set<FlowConsumer>
diff --git a/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/FlowMultiplexerFactory.kt b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/FlowMultiplexerFactory.kt
new file mode 100644
index 00000000..a863e3ad
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/FlowMultiplexerFactory.kt
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2022 AtLarge Research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package org.opendc.simulator.flow.mux
+
+import org.opendc.simulator.flow.FlowConvergenceListener
+import org.opendc.simulator.flow.FlowEngine
+
+/**
+ * Factory interface for a [FlowMultiplexer] implementation.
+ */
+public fun interface FlowMultiplexerFactory {
+ /**
+ * Construct a new [FlowMultiplexer] using the specified [engine] and [listener].
+ */
+ public fun newMultiplexer(engine: FlowEngine, listener: FlowConvergenceListener?): FlowMultiplexer
+
+ public companion object {
+ /**
+ * A [FlowMultiplexerFactory] constructing a [MaxMinFlowMultiplexer].
+ */
+ private val MAX_MIN_FACTORY = FlowMultiplexerFactory { engine, listener -> MaxMinFlowMultiplexer(engine, listener) }
+
+ /**
+ * A [FlowMultiplexerFactory] constructing a [ForwardingFlowMultiplexer].
+ */
+ private val FORWARDING_FACTORY = FlowMultiplexerFactory { engine, listener -> ForwardingFlowMultiplexer(engine, listener) }
+
+ /**
+ * Return a [FlowMultiplexerFactory] that returns [MaxMinFlowMultiplexer] instances.
+ */
+ @JvmStatic
+ public fun maxMinMultiplexer(): FlowMultiplexerFactory = MAX_MIN_FACTORY
+
+ /**
+ * Return a [ForwardingFlowMultiplexer] that returns [ForwardingFlowMultiplexer] instances.
+ */
+ @JvmStatic
+ public fun forwardingMultiplexer(): FlowMultiplexerFactory = FORWARDING_FACTORY
+ }
+}
diff --git a/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/ForwardingFlowMultiplexer.kt b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/ForwardingFlowMultiplexer.kt
index c18b2701..c50e9bbc 100644
--- a/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/ForwardingFlowMultiplexer.kt
+++ b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/ForwardingFlowMultiplexer.kt
@@ -37,6 +37,12 @@ public class ForwardingFlowMultiplexer(
private val engine: FlowEngine,
private val listener: FlowConvergenceListener? = null
) : FlowMultiplexer, FlowConvergenceListener {
+
+ override val maxInputs: Int
+ get() = _outputs.size
+
+ override val maxOutputs: Int = Int.MAX_VALUE
+
override val inputs: Set<FlowConsumer>
get() = _inputs
private val _inputs = mutableSetOf<Input>()
diff --git a/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/MaxMinFlowMultiplexer.kt b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/MaxMinFlowMultiplexer.kt
index 7605f67d..f2a4c1a4 100644
--- a/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/MaxMinFlowMultiplexer.kt
+++ b/opendc-simulator/opendc-simulator-flow/src/main/kotlin/org/opendc/simulator/flow/mux/MaxMinFlowMultiplexer.kt
@@ -37,6 +37,11 @@ public class MaxMinFlowMultiplexer(
private val engine: FlowEngine,
parent: FlowConvergenceListener? = null
) : FlowMultiplexer {
+
+ override val maxInputs: Int = Int.MAX_VALUE
+
+ override val maxOutputs: Int = Int.MAX_VALUE
+
/**
* The inputs of the multiplexer.
*/