summaryrefslogtreecommitdiff
path: root/simulator/opendc-compute/opendc-compute-core/src
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-03-05 16:26:06 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-03-07 16:13:16 +0100
commit10f71541cd2c72e12f1b2325ee4f25e38a10e0ef (patch)
tree5cd19515be73755911cbfdff0d477532e0dee02d /simulator/opendc-compute/opendc-compute-core/src
parent249a272702bb79a901848ed4957d0992e82b3f92 (diff)
compute: Convert Server to stateful interface
This change converts the Server data class which can be used as a stateful object to control an instance running in the cloud.
Diffstat (limited to 'simulator/opendc-compute/opendc-compute-core/src')
-rw-r--r--simulator/opendc-compute/opendc-compute-core/src/main/kotlin/org/opendc/compute/core/Server.kt47
-rw-r--r--simulator/opendc-compute/opendc-compute-core/src/main/kotlin/org/opendc/compute/core/ServerWatcher.kt39
2 files changed, 64 insertions, 22 deletions
diff --git a/simulator/opendc-compute/opendc-compute-core/src/main/kotlin/org/opendc/compute/core/Server.kt b/simulator/opendc-compute/opendc-compute-core/src/main/kotlin/org/opendc/compute/core/Server.kt
index 1fb5679a..ff212613 100644
--- a/simulator/opendc-compute/opendc-compute-core/src/main/kotlin/org/opendc/compute/core/Server.kt
+++ b/simulator/opendc-compute/opendc-compute-core/src/main/kotlin/org/opendc/compute/core/Server.kt
@@ -22,51 +22,54 @@
package org.opendc.compute.core
-import kotlinx.coroutines.flow.Flow
import org.opendc.compute.core.image.Image
import org.opendc.core.resource.Resource
-import org.opendc.core.resource.TagContainer
-import java.util.UUID
/**
- * A server instance that is running on some physical or virtual machine.
+ * A stateful object representing a server instance that is running on some physical or virtual machine.
*/
-public data class Server(
+public interface Server : Resource {
/**
- * The unique identifier of the server.
+ * The name of the server.
*/
- public override val uid: UUID,
+ public override val name: String
/**
- * The optional name of the server.
+ * The flavor of the server.
*/
- public override val name: String,
+ public val flavor: Flavor
/**
- * The tags of this server.
+ * The image of the server.
*/
- public override val tags: TagContainer,
+ public val image: Image
/**
- * The hardware configuration of the server.
+ * The tags assigned to the server.
*/
- public val flavor: Flavor,
+ public override val tags: Map<String, String>
/**
- * The image running on the server.
+ * The last known state of the server.
*/
- public val image: Image,
+ public val state: ServerState
/**
- * The last known state of the server.
+ * Register the specified [ServerWatcher] to watch the state of the server.
+ *
+ * @param watcher The watcher to register for the server.
+ */
+ public fun watch(watcher: ServerWatcher)
+
+ /**
+ * De-register the specified [ServerWatcher] from the server to stop it from receiving events.
+ *
+ * @param watcher The watcher to de-register from the server.
*/
- public val state: ServerState,
+ public fun unwatch(watcher: ServerWatcher)
/**
- * The events that are emitted by the server.
+ * Refresh the local state of the resource.
*/
- public val events: Flow<ServerEvent>
-) : Resource {
- override fun hashCode(): Int = uid.hashCode()
- override fun equals(other: Any?): Boolean = other is Server && uid == other.uid
+ public suspend fun refresh()
}
diff --git a/simulator/opendc-compute/opendc-compute-core/src/main/kotlin/org/opendc/compute/core/ServerWatcher.kt b/simulator/opendc-compute/opendc-compute-core/src/main/kotlin/org/opendc/compute/core/ServerWatcher.kt
new file mode 100644
index 00000000..a93a8382
--- /dev/null
+++ b/simulator/opendc-compute/opendc-compute-core/src/main/kotlin/org/opendc/compute/core/ServerWatcher.kt
@@ -0,0 +1,39 @@
+/*
+ * 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.compute.core
+
+/**
+ * An interface used to watch the state of [Server] instances.
+ */
+public interface ServerWatcher {
+ /**
+ * This method is invoked when the state of a [Server] changes.
+ *
+ * Note that the state of [server] might not reflect the state as reported by the invocation, as a call to
+ * [Server.refresh] is required to update its state.
+ *
+ * @param server The server whose state has changed.
+ * @param newState The new state of the server.
+ */
+ public fun onStateChanged(server: Server, newState: ServerState) {}
+}