From f8160f5b16e0ec65a8d547690356440c17b17914 Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Wed, 4 Nov 2020 15:41:49 +0100 Subject: [PATCH] Future based scheduling --- src/app.rs | 28 ++++++++++++++++++++++++++-- src/commands.rs | 7 ++++++- src/main.rs | 3 +++ src/schema/api.rs | 15 ++++++++++----- src/schema/authentication.rs | 13 +++++++++---- 5 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/app.rs b/src/app.rs index 349ae3b..e87ae17 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,9 +2,9 @@ use std::mem; use std::pin::Pin; use std::task::{Context, Poll, Waker}; use std::sync::{Arc, Mutex, MutexGuard}; +use std::borrow::BorrowMut; use futures::prelude::*; -use futures::ready; use futures_signals::signal::{Mutable, Signal, MutableSignalCloned, MutableLockMut}; use termion::event::Key; @@ -45,6 +45,8 @@ pub struct Sute<'a, S> { cmds: CommandParser<'a>, drain: Arc>, + + future: Option>>>, } impl<'a, S: Unpin> Sute<'a, S> { @@ -52,8 +54,9 @@ impl<'a, S: Unpin> Sute<'a, S> { let inputs = Inputs::new(); let state = Mutable::new(SuteState::new()); let cmds = CommandParser::new(); + let future = None; - Self { state, signal, inputs, api, log, cmds, drain, } + Self { state, signal, inputs, api, log, cmds, drain, future } } fn run_cmd(&mut self, cmdline: String) { @@ -62,6 +65,22 @@ impl<'a, S: Unpin> Sute<'a, S> { Ok(matches) => { match matches.subcommand() { Some(("quit", _)) => self.state.lock_mut().running = false, + Some(("authenticate", m)) => { + if let Some(mut api) = self.api.clone() { + let log2 = self.log.clone(); + let f = api.authentication().then(move |mut auth| { + auth.mechanisms().map(move |mechs| { + info!(log2, "Oh yeah about `authenticate` of yours:"); + for mech in mechs { + info!(log2, "Mech: {}", mech); + } + }) + }); + self.future.replace(Box::pin(f)); + } else { + error!(self.log, "No connection, can't authenticate!"); + } + } Some((s, m)) => info!(self.log, "Got Command {} with params {:?}", s, m), None => error!(self.log, "No command provided."), } @@ -124,6 +143,11 @@ impl <'a, S: Unpin + Signal> Future for Sute<'a, S> { return Poll::Ready(()); } + if let Some(f) = self.future.take() { + pin_mut!(f); + f.poll(cx); + } + if let Poll::Ready(Some(size)) = Pin::new(&mut self.signal).poll_change(cx) { self.handle_resize(size); } diff --git a/src/commands.rs b/src/commands.rs index eaf5fe8..2f033ba 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,6 +1,7 @@ use clap::{ App, AppSettings, + Arg, ArgMatches, Error, }; @@ -24,13 +25,17 @@ impl<'help> CommandParser<'help> { .subcommand(App::new("quit")) .subcommand(App::new("connect")) .subcommand(App::new("authenticate") + .arg(Arg::new("user") + .takes_value(true)) + .arg(Arg::new("password") + .takes_value(true)) ) , } } pub fn parse>(&mut self, iter: I) -> Result { - self.app.try_get_matches_from_mut(iter) + self.app.clone().try_get_matches_from(iter) } } diff --git a/src/main.rs b/src/main.rs index cd9f12d..e6ecd0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ #[macro_use] extern crate slog; +#[macro_use] +extern crate futures; + use std::io; use std::sync::{Arc, Mutex}; use std::thread; diff --git a/src/schema/api.rs b/src/schema/api.rs index 9536c0f..3e64200 100644 --- a/src/schema/api.rs +++ b/src/schema/api.rs @@ -1,10 +1,13 @@ use std::fmt; +use std::future::Future; +use futures::FutureExt; use slog::Logger; use super::connection_capnp::bootstrap::Client; use super::Authentication; +#[derive(Clone)] pub struct API { inner: Client, log: Logger, @@ -15,13 +18,15 @@ impl API { Self { log, inner} } - pub async fn authentication(&mut self) -> Authentication { + pub fn authentication(&mut self) -> impl Future { let req = self.inner.auth_request().send().promise; // TODO: When's that an Err? - let res = req.await.unwrap(); - // TODO: When's that an Err? - let tmp = res.get().unwrap(); - Authentication::new(tmp.get_auth().unwrap()) + req.map(|res| { + // TODO: When's that an Err? + let tmp = res.unwrap(); + let moretmp = tmp.get().unwrap(); + Authentication::new(moretmp.get_auth().unwrap()) + }) } ///// Authenticate to the server. Returns true on success, false on error diff --git a/src/schema/authentication.rs b/src/schema/authentication.rs index c1202bd..59b4f07 100644 --- a/src/schema/authentication.rs +++ b/src/schema/authentication.rs @@ -1,5 +1,8 @@ use std::fmt; use std::any::Any; +use std::future::Future; + +use futures::FutureExt; use slog::Logger; use super::auth_capnp::authentication::Client; @@ -20,10 +23,12 @@ impl Authentication { Self { inner } } - pub async fn mechanisms(&mut self) -> Vec { + pub fn mechanisms(&mut self) -> impl Future> { let req = self.inner.mechanisms_request().send().promise; - let res = req.await.unwrap(); - let tmp = res.get().unwrap(); - tmp.get_mechs().unwrap().iter().map(|x| x.unwrap().to_string()).collect() + req.map(|res| { + let res = res.unwrap(); + let tmp = res.get().unwrap(); + tmp.get_mechs().unwrap().iter().map(|x| x.unwrap().to_string()).collect() + }) } }