From 4f816318b6672d40f23b22ca44cc06b77cadf961 Mon Sep 17 00:00:00 2001 From: mjkwiatkowski Date: Mon, 23 Feb 2026 12:05:58 +0100 Subject: feat: added a working connection to Redis, and a scaffolding for the RESTful API --- .../kotlin/org/opendc/common/utils/HTTPClient.kt | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt (limited to 'opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt') 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 -- cgit v1.2.3