blob: 3c8ae3c587bb4307f5affab2a98fd1a334f48482 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
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
}
}
public fun checkForInsights() {
val request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:1234/insight"))
.header("Content-type", "text/plain")
.GET()
.build()
val response = client?.send(request, ofString())
if (response?.body() != "null") {
println(response?.body())
}
}
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:1234/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)
}
}
|