From 7d82dcbfd859ce1b81e553b9299fdf122bb2e14d Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Wed, 13 Nov 2013 21:52:50 -0800 Subject: [PATCH] Update examples for RPC --- examples/example_client.cpp | 59 +++++++++++++++++++++++++++++++++++++ examples/example_client.py | 3 +- examples/example_server.py | 39 ++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 examples/example_client.cpp create mode 100644 examples/example_server.py diff --git a/examples/example_client.cpp b/examples/example_client.cpp new file mode 100644 index 0000000..0a20e80 --- /dev/null +++ b/examples/example_client.cpp @@ -0,0 +1,59 @@ +#include "capnp/rpc-twoparty.h" +#include +#include +#include "test.capnp.h" +#include +#include + +using namespace capnp; +using namespace capnproto_test::capnp; +using namespace kj; + +Capability::Client getPersistentCap(RpcSystem& client, + rpc::twoparty::Side side, + test::TestSturdyRefObjectId::Tag tag) { + // Create the SturdyRefHostId. + MallocMessageBuilder hostIdMessage(8); + auto hostId = hostIdMessage.initRoot(); + hostId.setSide(side); + + // Create the SturdyRefObjectId. + MallocMessageBuilder objectIdMessage(8); + objectIdMessage.initRoot().setTag(tag); + + // Connect to the remote capability. + return client.restore(hostId, objectIdMessage.getRoot()); +} + +int main() +{ + try + { + kj::UnixEventLoop loop; + auto result = loop.evalLater([&]() { + auto network = Network::newSystemNetwork(); + auto address = loop.wait(network->parseRemoteAddress("127.0.0.1:49999")); + auto stream = loop.wait(address->connect()); + TwoPartyVatNetwork vat(loop, *stream, rpc::twoparty::Side::CLIENT); + auto rpcClient = makeRpcClient(vat, loop); + + // Request the particular capability from the server. + auto client = getPersistentCap(rpcClient, rpc::twoparty::Side::SERVER, + test::TestSturdyRefObjectId::Tag::TEST_INTERFACE).castAs(); + + auto request1 = client.fooRequest(); + request1.setI(5); + auto promise1 = request1.send(); + auto response1 = loop.wait(kj::mv(promise1)); + + assert ("125" == response1.getX()); + }); + + loop.wait(kj::mv(result)); + } + catch (std::exception& e) + { + std::cerr << e.what() << std::endl; + } + return 0; +} \ No newline at end of file diff --git a/examples/example_client.py b/examples/example_client.py index 90c65c3..740b125 100644 --- a/examples/example_client.py +++ b/examples/example_client.py @@ -20,6 +20,7 @@ def example_client(): remote = cap.foo(i=5) response = loop.wait(remote) - assert response.x == 'foo' + assert response.x == '125' + c.close() example_client() \ No newline at end of file diff --git a/examples/example_server.py b/examples/example_server.py new file mode 100644 index 0000000..1f82e1d --- /dev/null +++ b/examples/example_server.py @@ -0,0 +1,39 @@ +import capnp +import test_capnp + +import socket +import traceback + +class Server: + def __init__(self, val=1): + self.val = val + + def foo(self, context): + context.results.x = str(context.params.i * 5 + self.val) + +def restore(ref_id): + return test_capnp.TestInterface.new_server(Server(100)) + +def example_server(host='localhost', port=49999): + backlog = 1 + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind((host,port)) + s.listen(backlog) + + loop = capnp.EventLoop() + while 1: + try: + (clientsocket, address) = s.accept() + stream = capnp.FdAsyncIoStream(clientsocket.fileno()) + restorer = capnp.Restorer(test_capnp.TestSturdyRefObjectId, restore) + server = capnp.RpcServer(loop, stream, restorer) + + waiter = capnp.PromiseFulfillerPair() + loop.wait(waiter) + except KeyboardInterrupt: + break + except: + traceback.print_exc() + +example_server() \ No newline at end of file