2020-09-27 22:09:22 +00:00
|
|
|
package org.capnproto;
|
|
|
|
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
|
|
public class Request<Params, Results> {
|
|
|
|
|
2020-10-23 18:25:50 +00:00
|
|
|
protected Params params;
|
|
|
|
private PipelineFactory<Results> pipelineFactory;
|
2020-09-29 11:24:49 +00:00
|
|
|
RequestHook hook;
|
2020-09-27 22:09:22 +00:00
|
|
|
|
2020-10-13 16:19:24 +00:00
|
|
|
public Request(Params params,
|
|
|
|
PipelineFactory<Results> pipelineFactory,
|
|
|
|
RequestHook hook) {
|
2020-09-27 22:09:22 +00:00
|
|
|
this.params = params;
|
2020-10-13 16:19:24 +00:00
|
|
|
this.pipelineFactory = pipelineFactory;
|
2020-09-27 22:09:22 +00:00
|
|
|
this.hook = hook;
|
|
|
|
}
|
|
|
|
|
2020-10-13 16:19:24 +00:00
|
|
|
public Params getParams() {
|
|
|
|
return params;
|
2020-09-27 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 16:19:24 +00:00
|
|
|
public Results send() {
|
|
|
|
var typelessPromise = this.hook.send();
|
|
|
|
this.hook = null; // prevent reuse
|
|
|
|
return pipelineFactory.newPipeline(typelessPromise);
|
2020-09-27 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 16:19:24 +00:00
|
|
|
static <P, R> Request<P, R> newBrokenRequest(Throwable exc) {
|
2020-10-23 18:25:50 +00:00
|
|
|
|
2020-09-27 22:09:22 +00:00
|
|
|
final MessageBuilder message = new MessageBuilder();
|
|
|
|
|
|
|
|
var hook = new RequestHook() {
|
|
|
|
@Override
|
|
|
|
public RemotePromise<AnyPointer.Reader> send() {
|
|
|
|
return new RemotePromise<>(CompletableFuture.failedFuture(exc), null);
|
|
|
|
}
|
|
|
|
|
2020-10-08 13:17:02 +00:00
|
|
|
@Override
|
|
|
|
public CompletableFuture<?> sendStreaming() {
|
|
|
|
return CompletableFuture.failedFuture(exc);
|
|
|
|
}
|
|
|
|
|
2020-09-27 22:09:22 +00:00
|
|
|
@Override
|
|
|
|
public Object getBrand() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var root = message.getRoot(AnyPointer.factory);
|
2020-10-13 16:19:24 +00:00
|
|
|
return new Request<P, R>(null, null, hook);
|
2020-09-29 13:08:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 16:19:24 +00:00
|
|
|
static <P, R> Request<P, R> fromTypeless(
|
|
|
|
FromPointerBuilder<P> paramsFactory,
|
|
|
|
PipelineFactory<R> pipelineFactory,
|
|
|
|
Request<AnyPointer.Builder, AnyPointer.Pipeline> typeless) {
|
|
|
|
return new Request<>(typeless.params.getAs(paramsFactory), pipelineFactory, typeless.hook);
|
2020-09-27 22:09:22 +00:00
|
|
|
}
|
|
|
|
}
|