fabaccess-bffh/src/api/machines.rs

70 lines
2.0 KiB
Rust
Raw Normal View History

2020-11-17 14:28:04 +01:00
use std::sync::Arc;
2020-11-17 14:15:29 +01:00
use capnp::capability::Promise;
use capnp::Error;
2020-11-17 14:28:04 +01:00
use crate::schema::api_capnp::machines;
use crate::connection::Session;
2020-11-20 13:06:55 +01:00
use crate::db::Databases;
2020-12-15 13:12:22 +01:00
use crate::network::Network;
2020-11-20 13:06:55 +01:00
use super::machine::Machine;
2020-11-17 14:28:04 +01:00
/// An implementation of the `Machines` API
2020-11-17 14:35:16 +01:00
pub struct Machines {
2020-11-17 14:28:04 +01:00
/// A reference to the connection — as long as at least one API endpoint is
/// still alive the session has to be as well.
session: Arc<Session>,
2020-11-20 13:06:55 +01:00
db: Databases,
2020-12-15 13:12:22 +01:00
network: Arc<Network>,
2020-11-17 14:28:04 +01:00
}
impl Machines {
2020-12-15 13:12:22 +01:00
pub fn new(session: Arc<Session>, db: Databases, network: Arc<Network>) -> Self {
info!(session.log, "Machines created");
2020-12-15 13:12:22 +01:00
Self { session, db, network }
2020-11-17 14:28:04 +01:00
}
}
2020-11-17 14:15:29 +01:00
impl machines::Server for Machines {
fn list_machines(&mut self,
_params: machines::ListMachinesParams,
mut results: machines::ListMachinesResults)
-> Promise<(), Error>
{
2020-12-16 11:32:31 +01:00
let v: Vec<(String, crate::machine::Machine)> = self.network.machines.iter()
.map(|(n, m)| (n.clone(), m.clone()))
.collect();
let mut res = results.get();
let mut machines = res.init_machines(v.len() as u32);
for (i, (name, machine)) in v.into_iter().enumerate() {
let machine = Arc::new(Machine::new(self.session.clone(), machine, self.db.clone()));
let mut builder = machines.reborrow().get(i as u32);
Machine::fill(machine, &mut builder);
}
2020-11-17 14:15:29 +01:00
Promise::ok(())
}
fn get_machine(&mut self,
2020-11-20 13:06:55 +01:00
params: machines::GetMachineParams,
2020-11-17 14:15:29 +01:00
mut results: machines::GetMachineResults)
-> Promise<(), Error>
{
2020-12-16 11:32:31 +01:00
if let Ok(uid) = params.get().and_then(|x| x.get_uid()) {
if let Some(machine_inner) = self.network.machines.get(uid) {
let machine = Arc::new(Machine::new(self.session.clone(), machine_inner.clone(), self.db.clone()));
let mut builder = results.get().init_machine();
Machine::fill(machine, &mut builder);
}
}
Promise::ok(())
2020-11-17 14:15:29 +01:00
}
}