2020-09-27 22:09:11 +00:00
|
|
|
package org.capnproto;
|
|
|
|
|
2020-09-27 22:09:22 +00:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
2020-09-27 22:09:11 +00:00
|
|
|
public interface ClientHook {
|
2020-09-27 22:09:16 +00:00
|
|
|
|
|
|
|
Object NULL_CAPABILITY_BRAND = new Object();
|
|
|
|
Object BROKEN_CAPABILITY_BRAND = new Object();
|
|
|
|
|
2020-09-27 22:09:22 +00:00
|
|
|
Request<AnyPointer.Builder, AnyPointer.Reader> newCall(long interfaceId, short methodId);
|
|
|
|
|
|
|
|
VoidPromiseAndPipeline call(long interfaceId, short methodId, CallContextHook context);
|
|
|
|
|
2020-09-27 22:09:16 +00:00
|
|
|
default ClientHook getResolved() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-27 22:09:22 +00:00
|
|
|
default CompletableFuture<ClientHook> whenMoreResolved() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-27 22:09:16 +00:00
|
|
|
default Object getBrand() {
|
|
|
|
return NULL_CAPABILITY_BRAND;
|
|
|
|
}
|
|
|
|
|
2020-09-27 22:09:22 +00:00
|
|
|
default CompletableFuture<java.lang.Void> whenResolved() {
|
|
|
|
var promise = whenMoreResolved();
|
|
|
|
return promise != null
|
|
|
|
? promise.thenCompose(ClientHook::whenResolved)
|
|
|
|
: CompletableFuture.completedFuture(null);
|
|
|
|
}
|
|
|
|
|
2020-09-27 22:09:16 +00:00
|
|
|
default boolean isNull() {
|
|
|
|
return getBrand() == NULL_CAPABILITY_BRAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
default boolean isError() {
|
|
|
|
return getBrand() == BROKEN_CAPABILITY_BRAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
default Integer getFd() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-27 22:09:22 +00:00
|
|
|
final class VoidPromiseAndPipeline {
|
|
|
|
public final CompletableFuture<java.lang.Void> promise;
|
|
|
|
public final PipelineHook pipeline;
|
|
|
|
|
|
|
|
VoidPromiseAndPipeline(CompletableFuture<java.lang.Void> promise, PipelineHook pipeline) {
|
|
|
|
this.promise = promise;
|
|
|
|
this.pipeline = pipeline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 22:09:16 +00:00
|
|
|
static ClientHook newBrokenCap(String reason) {
|
|
|
|
return newBrokenClient(reason, false, BROKEN_CAPABILITY_BRAND);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ClientHook newBrokenCap(Throwable exc) {
|
|
|
|
return newBrokenClient(exc, false, BROKEN_CAPABILITY_BRAND);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ClientHook newNullCap() {
|
|
|
|
return newBrokenClient(new RuntimeException("Called null capability"), true, NULL_CAPABILITY_BRAND);
|
|
|
|
}
|
|
|
|
|
|
|
|
static private ClientHook newBrokenClient(String reason, boolean resolved, Object brand) {
|
|
|
|
return newBrokenClient(new RuntimeException(reason), resolved, brand);
|
|
|
|
}
|
|
|
|
|
|
|
|
static private ClientHook newBrokenClient(Throwable exc, boolean resolved, Object brand) {
|
|
|
|
return new ClientHook() {
|
2020-09-27 22:09:22 +00:00
|
|
|
@Override
|
|
|
|
public Request<AnyPointer.Builder, AnyPointer.Reader> newCall(long interfaceId, short methodId) {
|
|
|
|
return Request.newBrokenRequest(exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public VoidPromiseAndPipeline call(long interfaceId, short methodId, CallContextHook context) {
|
|
|
|
return new VoidPromiseAndPipeline(CompletableFuture.failedFuture(exc), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CompletableFuture<ClientHook> whenMoreResolved() {
|
|
|
|
if (resolved) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return CompletableFuture.failedFuture(exc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 22:09:16 +00:00
|
|
|
@Override
|
|
|
|
public Object getBrand() {
|
|
|
|
return brand;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-09-27 22:09:11 +00:00
|
|
|
}
|