diff --git a/src/app.rs b/src/app.rs index 47512f6..ee8194c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -140,6 +140,32 @@ impl<'a, S: Unpin> Sute<'a, S> { } } }, + Some(("use", m)) => { + if let Some(uid) = m.value_of("uid") { + let uid = uid.to_string(); + if let Some(mut api) = self.session.as_ref().map(|s| s.bootstrap.clone()) { + let log = self.log.clone(); + + let machines = api.machines(); + let machine = machines.then(|mut machines| { + machines.get_machine(uid) + }); + let f = async move { + let m = machine.await; + if let Some(gb) = m.as_ref().unwrap().use_().await { + info!(log, "Was allowed to use the machine, giving it back meow"); + gb.give_back().await; + } else { + info!(log, "Wasn't allowed to use the machine :("); + } + }; + let old_f = self.future.replace(Box::pin(f.map(|_| ()))); + if !old_f.is_none() { + warn!(self.log, "Dropping a future"); + } + } + } + } Some((s, m)) => info!(self.log, "Got Command {} with params {:?}", s, m), None => error!(self.log, "No command provided."), } diff --git a/src/commands.rs b/src/commands.rs index 53d90c9..1a2f449 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -33,6 +33,9 @@ impl<'help> CommandParser<'help> { .arg(Arg::new("uid") .takes_value(true))) .subcommand(App::new("list")) + .subcommand(App::new("use") + .arg(Arg::new("uid") + .takes_value(true))) , } } diff --git a/src/schema/machine.rs b/src/schema/machine.rs index 2b06885..5b755b6 100644 --- a/src/schema/machine.rs +++ b/src/schema/machine.rs @@ -3,10 +3,15 @@ use std::any::Any; use std::future::Future; use futures::FutureExt; -use super::api_capnp::machine::read::Client; +use super::api_capnp::machine::{ + read::{Client as ReadClient}, + write::{Client as WriteClient}, + write::give_back::{Client as GiveBackClient}, +}; pub struct Machine { - read: Client, + read: ReadClient, + write: WriteClient } impl fmt::Debug for Machine { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -17,9 +22,10 @@ impl fmt::Debug for Machine { } impl Machine { - pub fn new(read: Client) -> Self { + pub fn new(read: ReadClient, write: WriteClient) -> Self { Machine { read, + write, } } @@ -33,4 +39,28 @@ impl Machine { }) }) } + + pub fn use_(&self) -> impl Future> { + let req = self.write.use_request().send().promise; + + req.map(|res| { + res.unwrap().get().unwrap().get_ret().ok().map(|gb| { + GiveBack::new(gb) + }) + }) + } +} + +pub struct GiveBack { + client: GiveBackClient +} + +impl GiveBack { + pub fn new(client: GiveBackClient) -> Self { + Self { client } + } + + pub async fn give_back(self) { + self.client.ret_request().send().promise.await; + } } diff --git a/src/schema/machines.rs b/src/schema/machines.rs index db7f53a..da86182 100644 --- a/src/schema/machines.rs +++ b/src/schema/machines.rs @@ -39,10 +39,15 @@ impl Machines { let tmp = res.unwrap(); let moretmp = tmp.get().unwrap(); if let Ok(m) = moretmp.get_machine() { - match m.get_read() { - Ok(m) => return Some(Machine::new(m)), - Err(e) => println!("{}", e), - } + let read = m.get_read().map_err(|e| { + println!("get_machine->get_read Err: {}", e); + }).unwrap(); + + let write = m.get_write().map_err(|e| { + println!("get_machine->get_write Err: {}", e); + }).unwrap(); + + return Some(Machine::new(read, write)); } None }) @@ -58,9 +63,10 @@ impl Machines { let mut out = Vec::new(); if let Ok(m) = moretmp.get_machines() { for machine in m.iter() { - match machine.get_read() { - Ok(read) => out.push(Machine::new(read)), - Err(e) => println!("{}", e), + if let Ok(read) = machine.get_read() { + if let Ok(write) = machine.get_write() { + out.push(Machine::new(read, write)); + } } } } else {