Start rewrite

This commit is contained in:
Gregor Reitzenstein 2020-11-02 14:56:45 +01:00
parent 2044abfea8
commit f2cdb78b90
4 changed files with 80 additions and 44 deletions

View File

@ -29,7 +29,7 @@ signal-hook = "0.1"
slog = "2.5"
libc = "0.2"
rsasl = "0.1"
rsasl = "0.2.3"
[build-dependencies]
capnpc = "0.13"

View File

@ -26,13 +26,8 @@ pub enum Window {
Help,
}
enum ConnState<F> {
Connecting(F),
Connected(Api)
}
/// Application state struct
pub struct Sute<'a, S, F> {
pub struct Sute<'a, S> {
// TODO: BE SMART. Inputs change the state, resize signals change the state, futures completing
// change the state.
@ -41,12 +36,12 @@ pub struct Sute<'a, S, F> {
statesig: MutableSignalCloned<SuteState<'a>>,
signal: S,
inputs: Inputs,
api: Option<ConnState<F>>,
api: Option<Api>,
new: bool
}
impl<'a, S: Unpin, F: Unpin> Sute<'a, S, F> {
pub fn new(s: S, log: Arc<LogDrain<'a>>, api: F) -> Self {
impl<'a, S: Unpin> Sute<'a, S> {
pub fn new(s: S, log: Arc<LogDrain<'a>>, api: Api) -> Self {
let inputs = Inputs::new();
let state = Mutable::new(SuteState::new(log));
@ -55,7 +50,7 @@ impl<'a, S: Unpin, F: Unpin> Sute<'a, S, F> {
state: state,
signal: s,
inputs: inputs,
api: Some(ConnState::Connecting(api)),
api: Some(api),
new: true,
}
}
@ -85,7 +80,7 @@ impl<'a, S: Unpin, F: Unpin> Sute<'a, S, F> {
}
}
impl<'a, S: Signal<Item=(u16,u16)> + Unpin, F: Future<Output=Api> + Unpin> Signal for Sute<'a, S, F> {
impl<'a, S: Signal<Item=(u16,u16)> + Unpin> Signal for Sute<'a, S> {
type Item = SuteState<'a>;
fn poll_change(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
if let Poll::Ready(Some(key)) = Pin::new(&mut self.inputs).poll_next(cx) {
@ -94,13 +89,6 @@ impl<'a, S: Signal<Item=(u16,u16)> + Unpin, F: Future<Output=Api> + Unpin> Signa
if let Poll::Ready(Some(size)) = Pin::new(&mut self.signal).poll_change(cx) {
self.handle_resize(size);
}
if let Some(ConnState::Connecting(mut apif)) = self.api.take() {
if let Poll::Ready(api) = Pin::new(&mut apif).poll(cx) {
self.api = Some(ConnState::Connected(api));
} else {
self.api = Some(ConnState::Connecting(apif));
}
}
// TODO chunk this?
Pin::new(&mut self.statesig).poll_change(cx)

View File

@ -4,6 +4,8 @@ extern crate slog;
use std::io;
use std::sync::{Arc, Mutex};
use smol::net::TcpStream;
use tui::backend::{Backend, TermionBackend};
use tui::Terminal;
use termion::raw::IntoRawMode;
@ -45,7 +47,7 @@ fn main() -> Result<(), io::Error> {
.takes_value(true))
.get_matches();
let server = matches.value_of("server").unwrap_or("localhost");
let server = matches.value_of("server").unwrap_or("localhost:59661");
let stdout = io::stdout().into_raw_mode()?;
let backend = TermionBackend::new(stdout);
@ -61,8 +63,30 @@ fn main() -> Result<(), io::Error> {
let log = slog::Logger::root(slog::Fuse::new(drain.clone()), o!());
let resize = util::Resize::new()?;
let api = schema::Api::connect(log.clone(), server);
let app = app::Sute::new(resize, drain, Box::pin(api));
let lex = smol::LocalExecutor::new();
let stream_f = async move {
TcpStream::connect(server).await.unwrap()
};
let stream = smol::block_on(lex.run(stream_f));
let (f1, mut api) = schema::Api::from_stream(stream);
lex.spawn(f1).detach();
let i = log.clone();
let f = async {
println!("API ready");
let mut auth = api.authentication().await;
println!("AUTH ready: {:?}", &auth);
let mechs = auth.mechanisms().await;
println!("MECHS ready: {:?}", &mechs);
for mech in mechs {
println!("{}", mech);
}
};
let app = app::Sute::new(resize, drain, api);
crit!(log, "This is a test: {}", 42);
error!(log, "This is a test: {}", 42);

View File

@ -1,3 +1,5 @@
use std::fmt;
use std::any::Any;
use std::ffi::CStr;
use slog::Logger;
@ -25,20 +27,16 @@ mod api_capnp {
const PLAIN: *const libc::c_char = b"PLAIN" as *const u8 as *const libc::c_char;
pub struct Api {
stream: TcpStream,
bffh: connection_capnp::bootstrap::Client,
pub struct API {
inner: connection_capnp::bootstrap::Client,
log: Logger,
}
impl Api {
pub fn new(stream: TcpStream, bffh: connection_capnp::bootstrap::Client) -> Self {
Self { stream, bffh }
fn new(log: Logger, inner: connection_capnp::bootstrap::Client) -> Self {
Self { inner}
}
pub fn connect<A: AsyncToSocketAddrs>(log: Logger, addr: A) -> impl Future<Output=Api> {
let f = async {
let mut stream = TcpStream::connect(addr).await.unwrap();
handshake(log.clone(), &mut stream).await.unwrap();
pub fn from_stream(stream: TcpStream) -> (impl Future, Self) {
let network = Box::new(twoparty::VatNetwork::new(stream.clone(), stream.clone(),
rpc_twoparty_capnp::Side::Client, Default::default()));
@ -46,14 +44,16 @@ impl Api {
let bffh: connection_capnp::bootstrap::Client
= rpc_system.bootstrap(rpc_twoparty_capnp::Side::Server);
let mut api = Api::new(stream, bffh);
(rpc_system, Api::new(bffh))
}
api.authenticate(log).await;
api
};
f
pub async fn authentication(&mut self) -> Authentication {
let req = self.bffh.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())
}
async fn authenticate(&mut self, log: Logger) {
@ -114,6 +114,30 @@ impl Api {
//}
}
pub struct Authentication {
inner: auth_capnp::authentication::Client,
}
impl fmt::Debug for Authentication {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Authentication")
.field("inner", &self.inner.type_id())
.finish()
}
}
impl Authentication {
pub fn new(inner: auth_capnp::authentication::Client) -> Self {
Self { inner }
}
pub async fn mechanisms(&mut self) -> Vec<String> {
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()
}
}
async fn handshake(log: Logger, mut stream: &mut TcpStream) -> Result<(), io::Error> {
let host = "localhost";
let program = format!("{}-{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));