fabaccess-bffh/bffhd/capnp/machinesystem.rs

134 lines
3.7 KiB
Rust
Raw Normal View History

2022-05-05 15:50:44 +02:00
use crate::capnp::machine::Machine;
use crate::resources::search::ResourcesHandle;
use crate::resources::Resource;
2022-03-12 17:31:53 +01:00
use crate::session::SessionHandle;
2022-05-05 15:50:44 +02:00
use crate::RESOURCES;
use api::machinesystem_capnp::machine_system::info;
2022-03-12 17:31:53 +01:00
use capnp::capability::Promise;
2022-03-13 17:29:21 +01:00
use capnp_rpc::pry;
2022-06-24 13:57:47 +02:00
use tracing::Span;
const TARGET: &str = "bffh::api::machinesystem";
2021-12-06 21:53:42 +01:00
2022-03-13 17:29:21 +01:00
#[derive(Clone)]
pub struct Machines {
2022-06-24 13:57:47 +02:00
span: Span,
2022-03-12 17:31:53 +01:00
session: SessionHandle,
2022-03-13 17:29:21 +01:00
resources: ResourcesHandle,
2021-12-06 21:53:42 +01:00
}
impl Machines {
2022-03-12 17:31:53 +01:00
pub fn new(session: SessionHandle) -> Self {
2022-06-24 13:57:47 +02:00
let span = tracing::info_span!(
target: TARGET,
parent: &session.span,
"MachineSystem",
);
2022-03-13 20:33:26 +01:00
// FIXME no unwrap bad
2022-05-05 15:50:44 +02:00
Self {
2022-06-24 13:57:47 +02:00
span,
2022-05-05 15:50:44 +02:00
session,
resources: RESOURCES.get().unwrap().clone(),
}
2021-12-06 21:53:42 +01:00
}
}
2022-03-12 17:31:53 +01:00
impl info::Server for Machines {
fn get_machine_list(
&mut self,
_: info::GetMachineListParams,
2022-03-13 17:29:21 +01:00
mut result: info::GetMachineListResults,
2022-03-12 17:31:53 +01:00
) -> Promise<(), ::capnp::Error> {
2022-06-24 13:57:47 +02:00
let _guard = self.span.enter();
let _span = tracing::trace_span!(
target: TARGET,
parent: &self.span,
"getMachineList",
)
.entered();
tracing::trace!("method call");
2022-05-05 15:50:44 +02:00
let machine_list: Vec<(usize, &Resource)> = self
.resources
.list_all()
2022-03-13 17:29:21 +01:00
.into_iter()
.filter(|resource| resource.visible(&self.session))
.enumerate()
.collect();
let mut builder = result.get().init_machine_list(machine_list.len() as u32);
for (i, m) in machine_list {
let resource = m.clone();
2022-03-15 20:00:43 +01:00
let mbuilder = builder.reborrow().get(i as u32);
2022-03-13 17:29:21 +01:00
Machine::build(self.session.clone(), resource, mbuilder);
}
2022-06-24 13:57:47 +02:00
// TODO: indicate result?
tracing::trace!("method return");
2022-03-13 17:29:21 +01:00
Promise::ok(())
2022-03-12 17:31:53 +01:00
}
2022-03-13 17:29:21 +01:00
2022-03-12 17:31:53 +01:00
fn get_machine(
&mut self,
2022-03-13 17:29:21 +01:00
params: info::GetMachineParams,
mut result: info::GetMachineResults,
2022-03-12 17:31:53 +01:00
) -> Promise<(), ::capnp::Error> {
2022-06-24 13:57:47 +02:00
let _guard = self.span.enter();
let _span = tracing::trace_span!(
target: TARGET,
parent: &self.span,
"getMachine",
)
.entered();
2022-03-13 17:29:21 +01:00
let params = pry!(params.get());
let id = pry!(params.get_id());
2022-06-24 13:57:47 +02:00
tracing::trace!(params.id = id, "method call");
2022-03-13 17:29:21 +01:00
if let Some(resource) = self.resources.get_by_id(id) {
2022-06-24 13:57:47 +02:00
tracing::trace!(results = "Just", results.inner = id, "method return");
2022-03-15 20:00:43 +01:00
let builder = result.get();
Machine::optional_build(self.session.clone(), resource.clone(), builder);
2022-06-24 13:57:47 +02:00
} else {
tracing::trace!(results = "Nothing", "method return");
2022-03-13 17:29:21 +01:00
}
Promise::ok(())
2022-03-12 17:31:53 +01:00
}
2022-03-13 17:29:21 +01:00
2022-03-12 17:31:53 +01:00
fn get_machine_u_r_n(
&mut self,
2022-03-13 17:29:21 +01:00
params: info::GetMachineURNParams,
mut result: info::GetMachineURNResults,
2022-03-12 17:31:53 +01:00
) -> Promise<(), ::capnp::Error> {
2022-06-24 13:57:47 +02:00
let _guard = self.span.enter();
let _span = tracing::trace_span!(
target: TARGET,
parent: &self.span,
"getMachineURN",
)
.entered();
2022-03-13 17:29:21 +01:00
let params = pry!(params.get());
let urn = pry!(params.get_urn());
2022-06-24 13:57:47 +02:00
tracing::trace!(params.urn = urn, "method call");
2022-03-13 17:29:21 +01:00
if let Some(resource) = self.resources.get_by_urn(urn) {
2022-06-24 13:57:47 +02:00
tracing::trace!(
results = "Just",
results.inner = resource.get_id(),
"method return"
);
2022-03-15 20:00:43 +01:00
let builder = result.get();
Machine::optional_build(self.session.clone(), resource.clone(), builder);
2022-06-24 13:57:47 +02:00
} else {
tracing::trace!(results = "Nothing", "method return");
2022-03-13 17:29:21 +01:00
}
Promise::ok(())
2022-03-12 17:31:53 +01:00
}
}