-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package bridge | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.engine.* | ||
import io.ktor.server.netty.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.channels.ClosedReceiveChannelException | ||
import kotlinx.coroutines.runBlocking | ||
import technology.idlab.logging.Log | ||
|
||
class HttpReader: Reader { | ||
private val buffer = Channel<ByteArray>(Channel.Factory.UNLIMITED); | ||
|
||
private val embeddedServer = embeddedServer(Netty, port = 8080) { | ||
routing { | ||
post("/") { | ||
Log.shared.debug("Incoming request") | ||
val body = call.receive<ByteArray>() | ||
Log.shared.debug("Received ${body.size} bytes") | ||
buffer.send(body) | ||
|
||
Log.shared.debug("Responding") | ||
call.response.status(HttpStatusCode.OK) | ||
call.respondText("OK") | ||
Log.shared.debug("Response sent") | ||
} | ||
} | ||
}.start(wait = false) | ||
|
||
override suspend fun read(): Reader.Result { | ||
try { | ||
val result = buffer.receive() | ||
return Reader.Result.success(result) | ||
} catch (e: ClosedReceiveChannelException) { | ||
return Reader.Result.closed() | ||
} catch (e: Exception) { | ||
Log.shared.fatal(e) | ||
} | ||
} | ||
|
||
override fun readSync(): Reader.Result { | ||
return runBlocking { read() } | ||
} | ||
|
||
override fun isClosed(): Boolean { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package technology.idlab.bridge | ||
|
||
import bridge.Writer | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.engine.cio.CIO | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import kotlinx.coroutines.runBlocking | ||
import technology.idlab.logging.Log | ||
|
||
class HttpWriter(private val endpoint: String): Writer { | ||
private val client = HttpClient(CIO) | ||
|
||
override suspend fun push(value: ByteArray) { | ||
// Create request. | ||
Log.shared.debug("POST $endpoint (${value.size} bytes)") | ||
val res = client.post(endpoint) { | ||
setBody(value) | ||
} | ||
Log.shared.debug("Received response: ${res.status.value} - ${res.bodyAsText()}") | ||
|
||
// Check status code. | ||
if (res.status.value != 200) { | ||
Log.shared.fatal("ERROR: Status code ${res.status.value} received from $endpoint") | ||
} | ||
} | ||
|
||
override fun pushSync(value: ByteArray) { | ||
runBlocking { push(value) } | ||
} | ||
|
||
override fun close() { | ||
client.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package channels | ||
|
||
import bridge.HttpReader | ||
import technology.idlab.bridge.HttpWriter | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class HttpTest { | ||
@Test | ||
fun exec() { | ||
// Create a writer and a reader. | ||
val writer = HttpWriter("http://localhost:8080") | ||
val reader = HttpReader() | ||
|
||
// Push a value to the writer and read it from the reader. | ||
writer.pushSync("Hello, World!".toByteArray()) | ||
val res = reader.readSync() | ||
|
||
// Check if result is the same. | ||
assertEquals("Hello, World!", String(res.value)) | ||
} | ||
} |