bffh/src/api.rs

64 lines
1.8 KiB
Rust
Raw Normal View History

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-09-08 09:56:40 +02:00
pub mod api_capnp {
2020-02-14 12:20:17 +01:00
include!(concat!(env!("OUT_DIR"), "/schema/api_capnp.rs"));
}
2020-09-07 17:23:42 +02:00
use smol::net::TcpStream;
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
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<()> {
let client = DifAPI {};
2020-09-08 09:56:40 +02:00
let api: api_capnp::diflouroborane::Client = capnp_rpc::new_client(client);
let mut message = capnp::message::Builder::new_default();
2020-09-08 09:56:40 +02:00
let mut outer = message.init_root::<crate::connection::connection_capnp::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;
2020-09-08 09:56:40 +02:00
impl api_capnp::diflouroborane::Server for DifAPI {
fn machines(&mut self,
2020-09-08 09:56:40 +02:00
_params: api_capnp::diflouroborane::MachinesParams,
mut results: api_capnp::diflouroborane::MachinesResults)
-> Promise<(), Error>
{
let mut b = results.get();
2020-09-08 09:56:40 +02:00
let mach = capnp_rpc::new_client(MachinesAPI);
b.set_mach(mach);
Promise::ok(())
}
}
pub struct MachinesAPI;
2020-09-08 09:56:40 +02:00
impl api_capnp::machines::Server for MachinesAPI {
fn list(&mut self,
2020-09-08 09:56:40 +02:00
_params: api_capnp::machines::ListParams,
mut results: api_capnp::machines::ListResults)
-> Promise<(), Error>
{
let mut l = results.get();
l.init_machines(0);
Promise::ok(())
}
2020-02-14 12:20:17 +01:00
}