christophsturm
05/07/2020, 5:52 PMfun createCaptureServer(port: Int): CompletableFuture<String> {
val future = CompletableFuture<String>()
val undertow = Undertow.builder().addHttpListener(port, "127.0.0.1").setHandler {
future.complete(it.queryString)
}.build()
undertow.start()
return future.thenApplyAsync {
undertow.stop()
it
}
}
possibly using ktor instead of undertow.octylFractal
05/07/2020, 5:55 PMoctylFractal
05/07/2020, 5:56 PMoctylFractal
05/07/2020, 5:56 PMoctylFractal
05/07/2020, 5:57 PMCompletableDeferred
instead of Future
Luis Munoz
05/07/2020, 5:58 PMoctylFractal
05/07/2020, 5:59 PMoctylFractal
05/07/2020, 5:59 PMsuspendCancellableCoroutine
christophsturm
05/07/2020, 6:03 PMchristophsturm
05/07/2020, 6:10 PMthenApplyAsync
part? (stop the http server)octylFractal
05/07/2020, 6:10 PMtry-finally
(assuming you go the suspendCancellableCoroutine
route)christophsturm
05/07/2020, 6:15 PMsuspendCancellableCoroutine
better? when would it cancel?octylFractal
05/07/2020, 6:16 PMoctylFractal
05/07/2020, 6:16 PMoctylFractal
05/07/2020, 6:17 PMsuspendCancellableCoroutine
-- I think that using the future model for actually retrieving the data is betteroctylFractal
05/07/2020, 6:18 PMsuspend fun createCaptureServer(port: Int): String {
val deferred = CompleteableDeferred<String>()
val server = <create server, with handler that completes the deferred>
server.start()
try {
return deferred.await()
} finally {
server.close()
}
}
is my takechristophsturm
05/07/2020, 6:20 PMsuspend fun createCaptureServer(port: Int): String {
val result = suspendCoroutine<String>{cont->
val undertow = Undertow.builder().addHttpListener(port, "127.0.0.1").setHandler {
cont.resume(it.queryString)
}.build()
undertow.start()
}
undertow.close()
return result
}
this is what i came up with, but it does not work that way because undertow is inside the callback, so i would have to make it nullable and a var. which is not so greatchristophsturm
05/07/2020, 6:21 PMchristophsturm
05/07/2020, 6:25 PMval deferred = CompletableDeferred<String>()
val undertow = Undertow.builder().addHttpListener(port, "127.0.0.1").setHandler {
deferred.complete(it.queryString)
}.build()
undertow.start()
val result = deferred.await()
undertow.stop()
return result
}
thanks for your help!octylFractal
05/07/2020, 6:26 PMoctylFractal
05/07/2020, 6:26 PM