add ez-rpc

This commit is contained in:
Vaci Koblizek 2020-12-03 12:13:50 +00:00
parent e3eabe6476
commit 998b569d4c
4 changed files with 117 additions and 0 deletions

View file

@ -0,0 +1,46 @@
package org.capnproto;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class EzRpcClient {
private final AsynchronousSocketChannel socket;
private final TwoPartyClient twoPartyRpc;
private final Capability.Client client;
public EzRpcClient(SocketAddress address) throws Exception {
this.socket = AsynchronousSocketChannel.open();
var connected = new CompletableFuture<Void>();
this.socket.connect(address, null, new CompletionHandler<>() {
@Override
public void completed(java.lang.Void result, Object attachment) {
connected.complete(null);
}
@Override
public void failed(Throwable exc, Object attachment) {
connected.completeExceptionally(exc);
}
});
this.twoPartyRpc = new TwoPartyClient(socket);
this.client = new Capability.Client(connected.thenApply(void_ -> this.twoPartyRpc.bootstrap()));
}
public Capability.Client getMain() {
return this.client;
}
public <T> CompletableFuture<T> runUntil(CompletableFuture<T> done) {
return this.twoPartyRpc.runUntil(done);
}
}

View file

@ -0,0 +1,38 @@
package org.capnproto;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
public class EzRpcServer {
private final AsynchronousChannelGroup channelgroup;
private final AsynchronousServerSocketChannel serverAcceptSocket;
private final TwoPartyServer twoPartyRpc;
private final int port;
public EzRpcServer(Capability.Server bootstrapInterface, SocketAddress address) throws IOException {
this(new Capability.Client(bootstrapInterface), address);
}
public EzRpcServer(Capability.Client bootstrapInterface, SocketAddress address) throws IOException {
this.channelgroup = AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(1));
this.serverAcceptSocket = AsynchronousServerSocketChannel.open(this.channelgroup);
this.serverAcceptSocket.bind(address);
var localAddress = (InetSocketAddress) this.serverAcceptSocket.getLocalAddress();
this.port = localAddress.getPort();
this.twoPartyRpc = new TwoPartyServer(bootstrapInterface);
}
public int getPort() {
return this.port;
}
public CompletableFuture<java.lang.Void> start() {
return this.twoPartyRpc.listen(this.serverAcceptSocket);
}
}

View file

@ -0,0 +1,31 @@
package org.capnproto;
import org.capnproto.rpctest.Test;
import org.junit.Assert;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
public class EzRpcTest {
@org.junit.Test
public void testBasic() throws Exception {
var callCount = new Counter();
var address = new InetSocketAddress("localhost", 0);
var server = new EzRpcServer(new RpcTestUtil.TestInterfaceImpl(callCount), address);
server.start();
var client = new EzRpcClient(new InetSocketAddress("localhost", server.getPort()));
var cap = new Test.TestInterface.Client(client.getMain());
var request = cap.fooRequest();
request.getParams().setI(123);
request.getParams().setJ(true);
var response = client.runUntil(request.send()).join();
Assert.assertEquals("foo", response.getX().toString());
Assert.assertEquals(1, callCount.value());
}
}

View file

@ -0,0 +1,2 @@
handlers = java.util.logging.FileHandler
java.util.logging.ConsoleHandler.level = ALL