diff --git a/runtime-rpc/src/main/java/org/capnproto/EzRpcClient.java b/runtime-rpc/src/main/java/org/capnproto/EzRpcClient.java new file mode 100644 index 0000000..b28da33 --- /dev/null +++ b/runtime-rpc/src/main/java/org/capnproto/EzRpcClient.java @@ -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(); + + 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 CompletableFuture runUntil(CompletableFuture done) { + return this.twoPartyRpc.runUntil(done); + } +} diff --git a/runtime-rpc/src/main/java/org/capnproto/EzRpcServer.java b/runtime-rpc/src/main/java/org/capnproto/EzRpcServer.java new file mode 100644 index 0000000..2764c5a --- /dev/null +++ b/runtime-rpc/src/main/java/org/capnproto/EzRpcServer.java @@ -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 start() { + return this.twoPartyRpc.listen(this.serverAcceptSocket); + } +} diff --git a/runtime-rpc/src/test/java/org/capnproto/EzRpcTest.java b/runtime-rpc/src/test/java/org/capnproto/EzRpcTest.java new file mode 100644 index 0000000..a089aa9 --- /dev/null +++ b/runtime-rpc/src/test/java/org/capnproto/EzRpcTest.java @@ -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()); + } +} diff --git a/runtime-rpc/src/test/logging.properties b/runtime-rpc/src/test/logging.properties new file mode 100644 index 0000000..73307ed --- /dev/null +++ b/runtime-rpc/src/test/logging.properties @@ -0,0 +1,2 @@ +handlers = java.util.logging.FileHandler +java.util.logging.ConsoleHandler.level = ALL \ No newline at end of file