diff options
| author | mjkwiatkowski <mati.rewa@gmail.com> | 2026-06-15 23:48:44 +0200 |
|---|---|---|
| committer | mjkwiatkowski <mati.rewa@gmail.com> | 2026-06-15 23:48:44 +0200 |
| commit | 0731bd58889df127ef87aba2590d505d79e6646f (patch) | |
| tree | 128aceeaf60ac5c098297f7cfda9fa47f974fc84 /opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt | |
| parent | f1ecbf0ce40d43685d8a6aeba0fe4cdebbd4536f (diff) | |
feat: migrated the past project to the sunfish repo
Diffstat (limited to 'opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt')
| -rw-r--r-- | opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt | 46 |
1 files changed, 46 insertions, 0 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..fc5bc57b --- /dev/null +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt @@ -0,0 +1,46 @@ +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. + * + * @author Mateusz Kwiatkowski + */ + +public class HTTPClient private constructor() { + public companion object { + private var instance: HTTPClient? = null + private var client = HttpClient.newBuilder().build() + + public fun getInstance(): HTTPClient? { + if (instance == null) { + instance = HTTPClient() + } + return instance + } + } + + // TODO: this class must send the experiment JSON file to the digital twin + public fun sendExperiment(experiment: File) { + 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/assets")) + .header("Content-type", "application/json") + // TODO: this is obviously wrong, find an efficient way to send JSON over network + .POST(HttpRequest.BodyPublishers.ofString(String(charArray))) + .build() + val response = client?.send(request, ofString()) + check(response?.statusCode() == 200) + } +}
\ No newline at end of file |
