summaryrefslogtreecommitdiff
path: root/opendc-common/src/main/kotlin/org/opendc/common/utils
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-common/src/main/kotlin/org/opendc/common/utils')
-rw-r--r--opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt50
-rw-r--r--opendc-common/src/main/kotlin/org/opendc/common/utils/JavalinRunner.kt45
-rw-r--r--opendc-common/src/main/kotlin/org/opendc/common/utils/Kafka.kt15
-rw-r--r--opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt1
-rw-r--r--opendc-common/src/main/kotlin/org/opendc/common/utils/Redis.kt44
5 files changed, 129 insertions, 26 deletions
diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt
new file mode 100644
index 00000000..cc89d48f
--- /dev/null
+++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt
@@ -0,0 +1,50 @@
+package org.opendc.common.utils
+
+import java.io.File
+import java.net.URI
+import java.net.http.*
+import java.net.http.HttpResponse.BodyHandlers.ofString
+/**
+ * Singleton class representing the real datacenter client.
+ * The client is asynchronous and initiates the connection first.
+ *
+ * @constructor Initiates the connection.
+ *
+ * @author Mateusz Kwiatkowski
+ */
+
+public class HTTPClient private constructor() {
+ public companion object {
+ private var instance: HTTPClient? = null
+ private var client: HttpClient? = null
+ private var handshake = HttpRequest.newBuilder()
+ .uri(URI.create("http://localhost:8080/"))
+ .build()
+
+ public fun getInstance(): HTTPClient? {
+ if (instance == null) {
+ try {
+ client = HttpClient.newBuilder().build()
+ val response = client?.send(handshake, ofString())
+ check(response?.statusCode() == 200)
+ } catch (e: IllegalStateException) {
+ println("${e.message}")
+ }
+ instance = HTTPClient()
+ }
+ return instance
+ }
+ }
+
+ // TODO: this class must send the experiment JSON file to the digital twin
+ public fun sendExperiment(experiment: File) {
+ val body : HttpRequest.BodyPublisher
+ val request = HttpRequest.newBuilder()
+ .uri(URI.create("http://localhost:8080/"))
+ .header("Content-type", "application/json")
+ // TODO: this is obviously wrong, find an efficient way to send JSON over network
+ .POST(HttpRequest.BodyPublishers.ofString(experiment))
+ .build()
+ println("Haha")
+ }
+} \ No newline at end of file
diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/JavalinRunner.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/JavalinRunner.kt
index 9db7bfaf..a43e23a8 100644
--- a/opendc-common/src/main/kotlin/org/opendc/common/utils/JavalinRunner.kt
+++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/JavalinRunner.kt
@@ -1,25 +1,50 @@
package org.opendc.common.utils
import io.javalin.Javalin
-import io.javalin.http.Context
import io.javalin.http.Handler
+/**
+ * Represents the digital twin monitoring server.
+ * @author Mateusz Kwiatkowski
+ * @see <a href=https://javalin.io/documentation>https://javalin.io/documentation</a>
+ */
+
public class JavalinRunner {
+ private val handshake: Handler = Handler { ctx -> ctx.status(200) }
- private val handleHello: Handler = Handler { ctx ->
- ctx.status(200)
- ctx.contentType("application/x-protobuf")
- ctx.result("Hello world")
+ private val scenario: Handler = Handler { ctx ->
}
init {
+ // Make a CRUD RESTful API
+ // Specify server config
val app = Javalin.create().start()
- app.get("/hello", handleHello)
+ // returns a list of all experiments
+ app.get("/experiment", handshake)
+
+ // returns a specific experiment
+ app.get("/experiment/:id", handshake)
+
+ // you need another endpoint for the metrics
+
+ // get the results for the metrics evaluation
+ app.get("/results/:id", handshake)
+
+ // returns all results
+ app.get("/results", handshake)
+
+ // sends a specific experiment
+ app.post("/experiment", scenario)
+
+ // changes a specific experiment
+ app.put("/experiment/:id", scenario)
+ // this should delete metrics associated with the experiment
+
+ // deletes an experiment with id
+ app.delete("/experiment/:id", scenario)
- app.exception<Exception?>(Exception::class.java, { e: Exception?, ctx: Context? ->
- e!!.printStackTrace()
- ctx!!.status(500)
- })
+ // deletes all experiments
+ app.delete("/experiment", scenario)
}
} \ No newline at end of file
diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/Kafka.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/Kafka.kt
index d7ccd385..1430898e 100644
--- a/opendc-common/src/main/kotlin/org/opendc/common/utils/Kafka.kt
+++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/Kafka.kt
@@ -38,19 +38,4 @@ public class Kafka(private val topic: String) {
}
}
}
-
- // TODO: fix
- public fun getReceive() : () -> Unit {
- val consumer = KafkaConsumer<String, ProtobufMetrics.ProtoExport>(properties)
- return fun() : Unit {
- try {
- consumer.subscribe(listOf(topic))
- while (true) {
- consumer.poll(1.microseconds.toJavaDuration())
- }
- } catch (e: Exception) {
- println("${e.message}")
- }
- }
- }
} \ No newline at end of file
diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt
index 03fd902c..35d03feb 100644
--- a/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt
+++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt
@@ -47,7 +47,6 @@ public class PostgresqlDB {
private fun String.asJdbc(database : String) : String {
return "jdbc:postgresql://$this/$database"
-
}
} \ No newline at end of file
diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/Redis.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/Redis.kt
new file mode 100644
index 00000000..67547778
--- /dev/null
+++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/Redis.kt
@@ -0,0 +1,44 @@
+package org.opendc.common.utils
+
+import com.fasterxml.jackson.dataformat.toml.TomlMapper
+import redis.clients.jedis.RedisClient
+import redis.clients.jedis.StreamEntryID
+import redis.clients.jedis.params.XReadParams
+import java.util.Properties
+
+/**
+ * This class represents the Redis server instance.
+ * @author Mateusz Kwiatkowski
+ * @see <a href=https://redis.io/docs/latest/>https://redis.io/docs/latest/</a>
+ *
+ * @see <a href=https://redis.io/docs/latest/develop/data-types/streams/>https://redis.io/docs/latest/develop/data-types/streams/</a>
+ */
+
+@Suppress("DEPRECATION")
+public class Redis {
+ private var properties : Properties
+
+ init {
+ properties = TomlMapper().readerFor(Properties().javaClass)
+ .readValue(Kafka::class.java.getResource("/producer.toml"))
+ }
+
+ public fun run() {
+ val jedis : RedisClient = RedisClient.create("redis://localhost:6379")
+
+ val res5 = jedis.xread(
+ XReadParams.xReadParams().block(300).count(100),
+ object : HashMap<String?, StreamEntryID?>() {
+ init {
+ put("${properties.getProperty("table")}", StreamEntryID())
+ }
+ })
+
+ // in Redis you can subscribe to updates to a stream.
+ // you should base your application off this.
+ // you can listen for new items with XREAD
+ println(res5)
+ jedis.close()
+ }
+
+} \ No newline at end of file