From 8ce5c2f6ffe57e9c35a60d1bdfa2f0161260e96e Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Tue, 23 Mar 2021 15:24:58 +0100 Subject: [PATCH] Minimal draft implementation of `getGiveBack` It's untested but it should(TM) work. --- src/api/machine.rs | 31 +++++++++++++++++++++++++++++++ src/machine.rs | 11 ++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/api/machine.rs b/src/api/machine.rs index ab4f013..c9c3642 100644 --- a/src/api/machine.rs +++ b/src/api/machine.rs @@ -131,6 +131,37 @@ impl write::Server for Write { { 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); diff --git a/src/machine.rs b/src/machine.rs index 291a097..3ed3436 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -23,7 +23,7 @@ use futures_signals::signal::{Mutable, ReadOnlyMutable}; use crate::error::{Result, Error}; 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::network::MachineMap; @@ -113,6 +113,15 @@ impl Machine { 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 { let guard = self.inner.try_lock().unwrap(); guard.signal()