Update examples for RPC
This commit is contained in:
59
examples/example_client.cpp
Normal file
59
examples/example_client.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "capnp/rpc-twoparty.h"
|
||||
#include <kj/async-unix.h>
|
||||
#include <kj/thread.h>
|
||||
#include "test.capnp.h"
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
using namespace capnp;
|
||||
using namespace capnproto_test::capnp;
|
||||
using namespace kj;
|
||||
|
||||
Capability::Client getPersistentCap(RpcSystem<rpc::twoparty::SturdyRefHostId>& client,
|
||||
rpc::twoparty::Side side,
|
||||
test::TestSturdyRefObjectId::Tag tag) {
|
||||
// Create the SturdyRefHostId.
|
||||
MallocMessageBuilder hostIdMessage(8);
|
||||
auto hostId = hostIdMessage.initRoot<rpc::twoparty::SturdyRefHostId>();
|
||||
hostId.setSide(side);
|
||||
|
||||
// Create the SturdyRefObjectId.
|
||||
MallocMessageBuilder objectIdMessage(8);
|
||||
objectIdMessage.initRoot<test::TestSturdyRefObjectId>().setTag(tag);
|
||||
|
||||
// Connect to the remote capability.
|
||||
return client.restore(hostId, objectIdMessage.getRoot<ObjectPointer>());
|
||||
}
|
||||
|
||||
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<test::TestInterface>();
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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()
|
||||
39
examples/example_server.py
Normal file
39
examples/example_server.py
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user