use crate::session::SessionHandle; use api::machinesystem_capnp::machine_system::{ info, InfoParams, InfoResults, Server as MachineSystem, }; use capnp::capability::Promise; use capnp_rpc::pry; use crate::capnp::machine::Machine; use crate::RESOURCES; use crate::resources::Resource; use crate::resources::search::ResourcesHandle; #[derive(Clone)] pub struct Machines { session: SessionHandle, resources: ResourcesHandle, } impl Machines { pub fn new(session: SessionHandle) -> Self { // FIXME no unwrap bad Self { session, resources: RESOURCES.get().unwrap().clone() } } } impl MachineSystem for Machines { fn info(&mut self, _: InfoParams, mut result: InfoResults) -> Promise<(), ::capnp::Error> { // TODO permission checking result.get().set_info(capnp_rpc::new_client(self.clone())); Promise::ok(()) } } impl info::Server for Machines { fn get_machine_list( &mut self, _: info::GetMachineListParams, mut result: info::GetMachineListResults, ) -> Promise<(), ::capnp::Error> { let machine_list: Vec<(usize, &Resource)> = self.resources.list_all() .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(); let mbuilder = builder.reborrow().get(i as u32); Machine::build(self.session.clone(), resource, mbuilder); } Promise::ok(()) } fn get_machine( &mut self, params: info::GetMachineParams, mut result: info::GetMachineResults, ) -> Promise<(), ::capnp::Error> { let params = pry!(params.get()); let id = pry!(params.get_id()); if let Some(resource) = self.resources.get_by_id(id) { let builder = result.get(); Machine::build(self.session.clone(), resource.clone(), builder); } Promise::ok(()) } fn get_machine_u_r_n( &mut self, params: info::GetMachineURNParams, mut result: info::GetMachineURNResults, ) -> Promise<(), ::capnp::Error> { let params = pry!(params.get()); let urn = pry!(params.get_urn()); if let Some(resource) = self.resources.get_by_urn(urn) { let builder = result.get(); Machine::build(self.session.clone(), resource.clone(), builder); } Promise::ok(()) } }