hook interfaces and broken implementations
This commit is contained in:
parent
57bacc9dd8
commit
cf5c4f1119
9 changed files with 190 additions and 0 deletions
19
runtime/src/main/java/org/capnproto/CallContextHook.java
Normal file
19
runtime/src/main/java/org/capnproto/CallContextHook.java
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public interface CallContextHook {
|
||||||
|
AnyPointer.Reader getParams();
|
||||||
|
|
||||||
|
void releaseParams();
|
||||||
|
|
||||||
|
AnyPointer.Builder getResults();
|
||||||
|
|
||||||
|
CompletableFuture<java.lang.Void> tailCall(RequestHook request);
|
||||||
|
|
||||||
|
void allowCancellation();
|
||||||
|
|
||||||
|
CompletableFuture<PipelineHook> onTailCall();
|
||||||
|
|
||||||
|
ClientHook.VoidPromiseAndPipeline directTailCall(RequestHook request);
|
||||||
|
}
|
|
@ -1,18 +1,35 @@
|
||||||
package org.capnproto;
|
package org.capnproto;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public interface ClientHook {
|
public interface ClientHook {
|
||||||
|
|
||||||
Object NULL_CAPABILITY_BRAND = new Object();
|
Object NULL_CAPABILITY_BRAND = new Object();
|
||||||
Object BROKEN_CAPABILITY_BRAND = new Object();
|
Object BROKEN_CAPABILITY_BRAND = new Object();
|
||||||
|
|
||||||
|
Request<AnyPointer.Builder, AnyPointer.Reader> newCall(long interfaceId, short methodId);
|
||||||
|
|
||||||
|
VoidPromiseAndPipeline call(long interfaceId, short methodId, CallContextHook context);
|
||||||
|
|
||||||
default ClientHook getResolved() {
|
default ClientHook getResolved() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default CompletableFuture<ClientHook> whenMoreResolved() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
default Object getBrand() {
|
default Object getBrand() {
|
||||||
return NULL_CAPABILITY_BRAND;
|
return NULL_CAPABILITY_BRAND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default CompletableFuture<java.lang.Void> whenResolved() {
|
||||||
|
var promise = whenMoreResolved();
|
||||||
|
return promise != null
|
||||||
|
? promise.thenCompose(ClientHook::whenResolved)
|
||||||
|
: CompletableFuture.completedFuture(null);
|
||||||
|
}
|
||||||
|
|
||||||
default boolean isNull() {
|
default boolean isNull() {
|
||||||
return getBrand() == NULL_CAPABILITY_BRAND;
|
return getBrand() == NULL_CAPABILITY_BRAND;
|
||||||
}
|
}
|
||||||
|
@ -25,6 +42,16 @@ public interface ClientHook {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static ClientHook newBrokenCap(String reason) {
|
static ClientHook newBrokenCap(String reason) {
|
||||||
return newBrokenClient(reason, false, BROKEN_CAPABILITY_BRAND);
|
return newBrokenClient(reason, false, BROKEN_CAPABILITY_BRAND);
|
||||||
}
|
}
|
||||||
|
@ -43,6 +70,25 @@ public interface ClientHook {
|
||||||
|
|
||||||
static private ClientHook newBrokenClient(Throwable exc, boolean resolved, Object brand) {
|
static private ClientHook newBrokenClient(Throwable exc, boolean resolved, Object brand) {
|
||||||
return new ClientHook() {
|
return new ClientHook() {
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getBrand() {
|
public Object getBrand() {
|
||||||
return brand;
|
return brand;
|
||||||
|
|
10
runtime/src/main/java/org/capnproto/PipelineHook.java
Normal file
10
runtime/src/main/java/org/capnproto/PipelineHook.java
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
interface PipelineHook {
|
||||||
|
|
||||||
|
ClientHook getPipelinedCap(PipelineOp[] ops);
|
||||||
|
|
||||||
|
static PipelineHook newBrokenPipeline(Throwable exc) {
|
||||||
|
return ops -> ClientHook.newBrokenCap(exc);
|
||||||
|
}
|
||||||
|
}
|
25
runtime/src/main/java/org/capnproto/PipelineOp.java
Normal file
25
runtime/src/main/java/org/capnproto/PipelineOp.java
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
final class PipelineOp {
|
||||||
|
|
||||||
|
enum Type {
|
||||||
|
NOOP,
|
||||||
|
GET_POINTER_FIELD
|
||||||
|
}
|
||||||
|
|
||||||
|
final PipelineOp.Type type;
|
||||||
|
final short pointerIndex;
|
||||||
|
|
||||||
|
private PipelineOp(PipelineOp.Type type, short pointerIndex) {
|
||||||
|
this.type = type;
|
||||||
|
this.pointerIndex = pointerIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PipelineOp Noop() {
|
||||||
|
return new PipelineOp(Type.NOOP, (short) 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static PipelineOp PointerField(short pointerIndex) {
|
||||||
|
return new PipelineOp(Type.GET_POINTER_FIELD, pointerIndex);
|
||||||
|
}
|
||||||
|
}
|
22
runtime/src/main/java/org/capnproto/RemotePromise.java
Normal file
22
runtime/src/main/java/org/capnproto/RemotePromise.java
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
class RemotePromise<Results> {
|
||||||
|
|
||||||
|
final CompletableFuture<Response> response;
|
||||||
|
final PipelineHook pipeline;
|
||||||
|
|
||||||
|
RemotePromise(CompletableFuture<Response> response, PipelineHook pipeline) {
|
||||||
|
this.response = response;
|
||||||
|
this.pipeline = pipeline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CompletableFuture<Response> getResponse() {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PipelineHook getHook() {
|
||||||
|
return pipeline;
|
||||||
|
}
|
||||||
|
}
|
42
runtime/src/main/java/org/capnproto/Request.java
Normal file
42
runtime/src/main/java/org/capnproto/Request.java
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public class Request<Params, Results> {
|
||||||
|
|
||||||
|
private final AnyPointer.Builder params;
|
||||||
|
private final RequestHook hook;
|
||||||
|
|
||||||
|
Request(AnyPointer.Builder params, RequestHook hook) {
|
||||||
|
this.params = params;
|
||||||
|
this.hook = hook;
|
||||||
|
}
|
||||||
|
|
||||||
|
AnyPointer.Builder params() {
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
CompletableFuture<Results> send() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static <T, U> Request<T, U> newBrokenRequest(Throwable exc) {
|
||||||
|
final MessageBuilder message = new MessageBuilder();
|
||||||
|
|
||||||
|
var hook = new RequestHook() {
|
||||||
|
@Override
|
||||||
|
public RemotePromise<AnyPointer.Reader> send() {
|
||||||
|
return new RemotePromise<>(CompletableFuture.failedFuture(exc), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getBrand() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var root = message.getRoot(AnyPointer.factory);
|
||||||
|
return new Request<T, U>(root, hook);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
6
runtime/src/main/java/org/capnproto/RequestHook.java
Normal file
6
runtime/src/main/java/org/capnproto/RequestHook.java
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
interface RequestHook {
|
||||||
|
RemotePromise<AnyPointer.Reader> send();
|
||||||
|
Object getBrand();
|
||||||
|
}
|
16
runtime/src/main/java/org/capnproto/Response.java
Normal file
16
runtime/src/main/java/org/capnproto/Response.java
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
class Response {
|
||||||
|
|
||||||
|
final ResponseHook hook;
|
||||||
|
final AnyPointer.Reader results;
|
||||||
|
|
||||||
|
public Response(AnyPointer.Reader reader, ResponseHook hook) {
|
||||||
|
this.hook = hook;
|
||||||
|
this.results = reader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final <T> T getAs(FromPointerReader<T> factory) {
|
||||||
|
return this.results.getAs(factory);
|
||||||
|
}
|
||||||
|
}
|
4
runtime/src/main/java/org/capnproto/ResponseHook.java
Normal file
4
runtime/src/main/java/org/capnproto/ResponseHook.java
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
package org.capnproto;
|
||||||
|
|
||||||
|
interface ResponseHook {
|
||||||
|
}
|
Loading…
Reference in a new issue