From 55dcf6dd803f276af7eed5313fe28efbd4454831 Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Tue, 9 Feb 2021 17:41:05 +0000 Subject: [PATCH] Updates to allow use()ing stuff --- examples/bffh.dhall | 4 ++-- src/api/machine.rs | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/examples/bffh.dhall b/examples/bffh.dhall index 76bc1b7..c25633a 100644 --- a/examples/bffh.dhall +++ b/examples/bffh.dhall @@ -1,5 +1,5 @@ -{ actor_connections = [] : List { _1 : Text, _2 : Text } --- { actor_connections = [{ _1 = "Testmachine", _2 = "Actor" }] +-- { actor_connections = [] : List { _1 : Text, _2 : Text } +{ actor_connections = [{ _1 = "Testmachine", _2 = "Actor" }] , actors = { Actor = { module = "Shelly", params = {=} } } diff --git a/src/api/machine.rs b/src/api/machine.rs index b64ed77..6c13820 100644 --- a/src/api/machine.rs +++ b/src/api/machine.rs @@ -11,7 +11,7 @@ use crate::schema::api_capnp::machine::*; use crate::connection::Session; use crate::db::Databases; use crate::db::machine::{Status, MachineState}; -use crate::machine::Machine as NwMachine; +use crate::machine::{Machine as NwMachine, ReturnToken}; #[derive(Clone)] pub struct Machine { @@ -97,20 +97,29 @@ struct Write(Arc); impl write::Server for Write { fn use_(&mut self, _params: write::UseParams, - _results: write::UseResults) + mut results: write::UseResults) -> Promise<(), Error> { let uid = self.0.session.user.try_lock().unwrap().as_ref().map(|u| u.id.clone()); let new_state = MachineState::used(uid.clone()); let this = self.0.clone(); - let f = this.machine.request_state_change(this.session.user.try_lock().unwrap().as_ref(), new_state) - .map(|res_token| match res_token { + let f = async move { + let res_token = this.machine.request_state_change( + this.session.user.try_lock().unwrap().as_ref(), + new_state + ).await; + + match res_token { // TODO: Do something with the token we get returned - Ok(_tok) => { + Ok(tok) => { + let gb = GiveBack(Some(tok)); + results.get().set_ret(capnp_rpc::new_client(gb)); + return Ok(()); }, Err(e) => Err(capnp::Error::failed(format!("State change request returned {}", e))), - }); + } + }; Promise::from_future(f) } @@ -124,6 +133,23 @@ impl write::Server for Write { } } +struct GiveBack(Option); + +impl write::give_back::Server for GiveBack { + fn ret(&mut self, + _params: write::give_back::RetParams, + _results: write::give_back::RetResults) + -> Promise<(), Error> + { + if let Some(chan) = self.0.take() { + chan.send(()) + .expect("Other end of GiveBack token was dropped?!"); + } + + Promise::ok(()) + } +} + struct Manage(Arc); impl manage::Server for Manage {