summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkPort.kt9
-rw-r--r--opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkSwitch.kt33
-rw-r--r--opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtual.kt77
-rw-r--r--opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtualTest.kt77
4 files changed, 194 insertions, 2 deletions
diff --git a/opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkPort.kt b/opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkPort.kt
index e5b85dd1..102e5625 100644
--- a/opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkPort.kt
+++ b/opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkPort.kt
@@ -55,8 +55,13 @@ public abstract class SimNetworkPort {
port._link = link
// Start bi-directional flow channel between the two ports
- provider.startConsumer(port.createConsumer())
- port.provider.startConsumer(createConsumer())
+ try {
+ provider.startConsumer(port.createConsumer())
+ port.provider.startConsumer(createConsumer())
+ } catch (e: Throwable) {
+ disconnect()
+ throw e
+ }
}
/**
diff --git a/opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkSwitch.kt b/opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkSwitch.kt
new file mode 100644
index 00000000..7dc249ab
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkSwitch.kt
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2021 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.network
+
+/**
+ * A network device connects devices on a network by switching the traffic over its ports.
+ */
+public interface SimNetworkSwitch {
+ /**
+ * The ports of the switch.
+ */
+ public val ports: List<SimNetworkPort>
+}
diff --git a/opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtual.kt b/opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtual.kt
new file mode 100644
index 00000000..05daaa5c
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-network/src/main/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtual.kt
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2021 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.network
+
+import org.opendc.simulator.resources.*
+
+/**
+ * A [SimNetworkSwitch] that can support new networking ports on demand.
+ */
+public class SimNetworkSwitchVirtual(interpreter: SimResourceInterpreter) : SimNetworkSwitch {
+ /**
+ * The ports of this switch.
+ */
+ override val ports: List<Port>
+ get() = _ports
+ private val _ports = mutableListOf<Port>()
+
+ /**
+ * The [SimResourceSwitchMaxMin] to actually perform the switching.
+ */
+ private val switch = SimResourceSwitchMaxMin(interpreter)
+
+ /**
+ * Open a new port on the switch.
+ */
+ public fun newPort(): Port {
+ val port = Port()
+ _ports.add(port)
+ return port
+ }
+
+ /**
+ * A port on the network switch.
+ */
+ public inner class Port : SimNetworkPort(), AutoCloseable {
+ /**
+ * A flag to indicate that this virtual port was removed from the switch.
+ */
+ private var isClosed: Boolean = false
+
+ override val provider: SimResourceProvider
+ get() = _provider
+ private val _provider = switch.newOutput()
+
+ override fun createConsumer(): SimResourceConsumer {
+ val forwarder = SimResourceForwarder(isCoupled = true)
+ switch.addInput(forwarder)
+ return forwarder
+ }
+
+ override fun close() {
+ isClosed = true
+ _provider.close()
+ _ports.remove(this)
+ }
+ }
+}
diff --git a/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtualTest.kt b/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtualTest.kt
new file mode 100644
index 00000000..3a749bfe
--- /dev/null
+++ b/opendc-simulator/opendc-simulator-network/src/test/kotlin/org/opendc/simulator/network/SimNetworkSwitchVirtualTest.kt
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2021 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.network
+
+import io.mockk.spyk
+import io.mockk.verify
+import org.junit.jupiter.api.Assertions.assertTrue
+import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.assertThrows
+import org.opendc.simulator.core.runBlockingSimulation
+import org.opendc.simulator.resources.*
+import org.opendc.simulator.resources.consumer.SimWorkConsumer
+
+/**
+ * Test suite for the [SimNetworkSwitchVirtual] class.
+ */
+class SimNetworkSwitchVirtualTest {
+ @Test
+ fun testConnect() = runBlockingSimulation {
+ val interpreter = SimResourceInterpreter(coroutineContext, clock)
+ val sink = SimNetworkSink(interpreter, capacity = 100.0)
+ val source = spyk(Source(interpreter))
+ val switch = SimNetworkSwitchVirtual(interpreter)
+ val consumer = source.consumer
+
+ switch.newPort().connect(sink)
+ switch.newPort().connect(source)
+
+ assertTrue(sink.isConnected)
+ assertTrue(source.isConnected)
+
+ verify { source.createConsumer() }
+ verify { consumer.onEvent(any(), SimResourceEvent.Start) }
+ }
+
+ @Test
+ fun testConnectClosedPort() = runBlockingSimulation {
+ val interpreter = SimResourceInterpreter(coroutineContext, clock)
+ val sink = SimNetworkSink(interpreter, capacity = 100.0)
+ val switch = SimNetworkSwitchVirtual(interpreter)
+
+ val port = switch.newPort()
+ port.close()
+
+ assertThrows<IllegalStateException> {
+ port.connect(sink)
+ }
+ }
+
+ private class Source(interpreter: SimResourceInterpreter) : SimNetworkPort() {
+ val consumer = spyk(SimWorkConsumer(Double.POSITIVE_INFINITY, utilization = 0.8))
+
+ public override fun createConsumer(): SimResourceConsumer = consumer
+
+ override val provider: SimResourceProvider = SimResourceSource(0.0, interpreter)
+ }
+}