2020-02-14 12:20:17 +01:00
|
|
|
// module needs to be top level for generated functions to be in scope:
|
|
|
|
// https://github.com/capnproto/capnproto-rust/issues/16
|
2020-04-28 18:40:50 +02:00
|
|
|
pub mod gen {
|
2020-02-14 12:20:17 +01:00
|
|
|
include!(concat!(env!("OUT_DIR"), "/schema/api_capnp.rs"));
|
|
|
|
}
|
|
|
|
|
|
|
|
use async_std::net::TcpStream;
|
2020-09-07 09:45:55 +02:00
|
|
|
use futures_util::FutureExt;
|
2020-02-14 12:20:17 +01:00
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
use slog::Logger;
|
|
|
|
|
2020-05-04 13:22:14 +02:00
|
|
|
use crate::error::Result;
|
2020-02-18 16:55:19 +01:00
|
|
|
|
2020-09-07 09:45:55 +02:00
|
|
|
use capnp::capability::Promise;
|
|
|
|
use capnp::Error;
|
|
|
|
use capnp_rpc::RpcSystem;
|
|
|
|
use capnp_rpc::twoparty::VatNetwork;
|
|
|
|
use capnp_rpc::rpc_twoparty_capnp::Side;
|
|
|
|
|
2020-05-04 13:22:14 +02:00
|
|
|
pub async fn handle_connection(log: Logger, socket: TcpStream) -> Result<()> {
|
2020-09-07 09:45:55 +02:00
|
|
|
let client = DifAPI {};
|
|
|
|
let api = gen::diflouroborane::ToClient::new(client).into_client::<capnp_rpc::Server>();
|
|
|
|
|
|
|
|
let mut message = capnp::message::Builder::new_default();
|
|
|
|
let mut outer = message.init_root::<crate::connection::gen::message::Builder>();
|
|
|
|
outer.set_api(api.clone());
|
|
|
|
|
|
|
|
let network = VatNetwork::new(socket.clone(), socket, Side::Server, Default::default());
|
|
|
|
let rpc = RpcSystem::new(Box::new(network), Some(api.client)).map(|_| ());
|
|
|
|
|
|
|
|
rpc.await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DifAPI;
|
|
|
|
|
|
|
|
impl gen::diflouroborane::Server for DifAPI {
|
|
|
|
fn machines(&mut self,
|
|
|
|
_params: gen::diflouroborane::MachinesParams,
|
|
|
|
mut results: gen::diflouroborane::MachinesResults)
|
|
|
|
-> Promise<(), Error>
|
|
|
|
{
|
|
|
|
let mut b = results.get();
|
|
|
|
let mach = gen::machines::ToClient::new(MachinesAPI).into_client::<capnp_rpc::Server>();
|
|
|
|
b.set_mach(mach);
|
|
|
|
Promise::ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MachinesAPI;
|
|
|
|
|
|
|
|
impl gen::machines::Server for MachinesAPI {
|
|
|
|
fn list(&mut self,
|
|
|
|
_params: gen::machines::ListParams,
|
|
|
|
mut results: gen::machines::ListResults)
|
|
|
|
-> Promise<(), Error>
|
|
|
|
{
|
|
|
|
let mut l = results.get();
|
|
|
|
l.init_machines(0);
|
|
|
|
Promise::ok(())
|
|
|
|
}
|
2020-02-14 12:20:17 +01:00
|
|
|
}
|