diff options
| author | mjkwiatkowski <mati.rewa@gmail.com> | 2026-02-26 19:51:57 +0100 |
|---|---|---|
| committer | mjkwiatkowski <mati.rewa@gmail.com> | 2026-02-26 19:51:57 +0100 |
| commit | 82f8418f49b0564c5093c28be1ca522a628d0b4f (patch) | |
| tree | 140e6e4579229c8eefac398e58f386f270dff328 /opendc-common/src/main/kotlin/org/opendc/common/utils | |
| parent | 4f816318b6672d40f23b22ca44cc06b77cadf961 (diff) | |
Diffstat (limited to 'opendc-common/src/main/kotlin/org/opendc/common/utils')
4 files changed, 26 insertions, 51 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 index cc89d48f..fc5bc57b 100644 --- a/opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt @@ -1,35 +1,25 @@ package org.opendc.common.utils import java.io.File +import java.io.InputStreamReader 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() + private var client = HttpClient.newBuilder().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 @@ -38,13 +28,19 @@ public class HTTPClient private constructor() { // TODO: this class must send the experiment JSON file to the digital twin public fun sendExperiment(experiment: File) { - val body : HttpRequest.BodyPublisher + val input = experiment.inputStream() + val charArray = CharArray(experiment.length().toInt()) + val isr = InputStreamReader(input) + + isr.read(charArray) + val request = HttpRequest.newBuilder() - .uri(URI.create("http://localhost:8080/")) + .uri(URI.create("http://localhost:8080/assets")) .header("Content-type", "application/json") // TODO: this is obviously wrong, find an efficient way to send JSON over network - .POST(HttpRequest.BodyPublishers.ofString(experiment)) + .POST(HttpRequest.BodyPublishers.ofString(String(charArray))) .build() - println("Haha") + val response = client?.send(request, ofString()) + check(response?.statusCode() == 200) } }
\ 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 a43e23a8..23baac27 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,50 +1,33 @@ package org.opendc.common.utils import io.javalin.Javalin -import io.javalin.http.Handler +import org.opendc.common.annotations.Endpoint +import org.opendc.common.api.AssetsController /** * Represents the digital twin monitoring server. + * For endpoint documentation see `AssetsController`. * @author Mateusz Kwiatkowski * @see <a href=https://javalin.io/documentation>https://javalin.io/documentation</a> + * @see org.opendc.common.api.AssetsController */ +@OptIn(Endpoint::class) public class JavalinRunner { - private val handshake: Handler = Handler { ctx -> ctx.status(200) } - - private val scenario: Handler = Handler { ctx -> - } + private val assetsController : AssetsController = AssetsController() init { - // Make a CRUD RESTful API - // Specify server config val app = Javalin.create().start() + app.get("/assets", assetsController.getAssets()) - // 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) + app.get("/assets/{id}", assetsController.getAssetsId()) - // sends a specific experiment - app.post("/experiment", scenario) + app.post("/assets", assetsController.postAsset()) - // changes a specific experiment - app.put("/experiment/:id", scenario) - // this should delete metrics associated with the experiment + app.put("/assets/{id}", assetsController.putAssetId()) - // deletes an experiment with id - app.delete("/experiment/:id", scenario) + app.delete("/assets/{id}", assetsController.deleteAssetId()) - // deletes all experiments - app.delete("/experiment", scenario) + app.delete("/assets", assetsController.deleteAsset()) } }
\ 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 1430898e..81eb6752 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 @@ -1,13 +1,10 @@ package org.opendc.common.utils import com.fasterxml.jackson.dataformat.toml.TomlMapper -import org.apache.kafka.clients.consumer.KafkaConsumer import org.apache.kafka.clients.producer.KafkaProducer import org.apache.kafka.clients.producer.ProducerRecord import org.opendc.common.ProtobufMetrics import java.util.* -import kotlin.time.Duration.Companion.microseconds -import kotlin.time.toJavaDuration /** * Represents the Kafka interface. * @constructor `topic` the Kafka topic @@ -34,7 +31,7 @@ public class Kafka(private val topic: String) { try { producer.send(ProducerRecord(this.topic, value)) } catch (e: Exception) { - println("${e.message}") + println("${e.message}") } } } 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 index 67547778..b659d40a 100644 --- a/opendc-common/src/main/kotlin/org/opendc/common/utils/Redis.kt +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/Redis.kt @@ -40,5 +40,4 @@ public class Redis { println(res5) jedis.close() } - }
\ No newline at end of file |
