Enough code to allow use()ing stuff

This commit is contained in:
Gregor Reitzenstein 2021-02-09 17:41:32 +00:00
parent a6d4bc06de
commit 67ac6fdb53
4 changed files with 75 additions and 10 deletions

View File

@ -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."),
}

View File

@ -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)))
,
}
}

View File

@ -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<Output=Option<GiveBack>> {
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;
}
}

View File

@ -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 {