Minimal draft implementation of getGiveBack

It's untested but it should(TM) work.
This commit is contained in:
Gregor Reitzenstein 2021-03-23 15:24:58 +01:00
parent dcdbc42274
commit 8ce5c2f6ff
2 changed files with 41 additions and 1 deletions

View File

@ -131,6 +131,37 @@ impl write::Server for Write {
{ {
unimplemented!() unimplemented!()
} }
fn get_give_back(&mut self,
_params: write::GetGiveBackParams,
mut results: write::GetGiveBackResults)
-> Promise<(), Error>
{
let this = self.0.clone();
let f = async move {
let status = this.machine.get_status().await;
let sess = this.session.clone();
match status {
Status::InUse(Some(uid)) => {
let user = sess.user.lock().await;
if let Some(u) = user.as_ref() {
if u.id == uid {
let token = this.machine.create_token();
let gb = GiveBack(Some(token));
results.get().set_ret(capnp_rpc::new_client(gb));
}
}
},
// Machine not in use
_ => {
}
}
};
Promise::from_future(f.map(|_| Ok(())))
}
} }
struct GiveBack(Option<ReturnToken>); struct GiveBack(Option<ReturnToken>);

View File

@ -23,7 +23,7 @@ use futures_signals::signal::{Mutable, ReadOnlyMutable};
use crate::error::{Result, Error}; use crate::error::{Result, Error};
use crate::db::access; use crate::db::access;
use crate::db::machine::{MachineIdentifier, MachineState}; use crate::db::machine::{MachineIdentifier, MachineState, Status};
use crate::db::user::{User, UserData}; use crate::db::user::{User, UserData};
use crate::network::MachineMap; use crate::network::MachineMap;
@ -113,6 +113,15 @@ impl Machine {
Box::pin(f) Box::pin(f)
} }
pub fn create_token(&self) -> ReturnToken {
ReturnToken::new(self.inner.clone())
}
pub async fn get_status(&self) -> Status {
let guard = self.inner.lock().await;
guard.state.get_cloned().state
}
pub fn signal(&self) -> impl Signal<Item=MachineState> { pub fn signal(&self) -> impl Signal<Item=MachineState> {
let guard = self.inner.try_lock().unwrap(); let guard = self.inner.try_lock().unwrap();
guard.signal() guard.signal()