summaryrefslogtreecommitdiff
path: root/opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt
diff options
context:
space:
mode:
authormjkwiatkowski <mati.rewa@gmail.com>2026-02-26 19:51:57 +0100
committermjkwiatkowski <mati.rewa@gmail.com>2026-02-26 19:51:57 +0100
commit82f8418f49b0564c5093c28be1ca522a628d0b4f (patch)
tree140e6e4579229c8eefac398e58f386f270dff328 /opendc-common/src/main/kotlin/org/opendc/common/utils/HTTPClient.kt
parent4f816318b6672d40f23b22ca44cc06b77cadf961 (diff)
feat: added notes from last meeting with my supervisorHEADmaster
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.kt30
1 files changed, 13 insertions, 17 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