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" slog = "2.5"
libc = "0.2" libc = "0.2"
rsasl = "0.1" rsasl = "0.2.3"
[build-dependencies] [build-dependencies]
capnpc = "0.13" capnpc = "0.13"

View File

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

View File

@ -4,6 +4,8 @@ extern crate slog;
use std::io; use std::io;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use smol::net::TcpStream;
use tui::backend::{Backend, TermionBackend}; use tui::backend::{Backend, TermionBackend};
use tui::Terminal; use tui::Terminal;
use termion::raw::IntoRawMode; use termion::raw::IntoRawMode;
@ -45,7 +47,7 @@ fn main() -> Result<(), io::Error> {
.takes_value(true)) .takes_value(true))
.get_matches(); .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 stdout = io::stdout().into_raw_mode()?;
let backend = TermionBackend::new(stdout); 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 log = slog::Logger::root(slog::Fuse::new(drain.clone()), o!());
let resize = util::Resize::new()?; let resize = util::Resize::new()?;
let api = schema::Api::connect(log.clone(), server); let lex = smol::LocalExecutor::new();
let app = app::Sute::new(resize, drain, Box::pin(api)); 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); crit!(log, "This is a test: {}", 42);
error!(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 std::ffi::CStr;
use slog::Logger; use slog::Logger;
@ -25,35 +27,33 @@ mod api_capnp {
const PLAIN: *const libc::c_char = b"PLAIN" as *const u8 as *const libc::c_char; const PLAIN: *const libc::c_char = b"PLAIN" as *const u8 as *const libc::c_char;
pub struct Api { pub struct API {
stream: TcpStream, inner: connection_capnp::bootstrap::Client,
bffh: connection_capnp::bootstrap::Client, log: Logger,
} }
impl Api { impl Api {
pub fn new(stream: TcpStream, bffh: connection_capnp::bootstrap::Client) -> Self { fn new(log: Logger, inner: connection_capnp::bootstrap::Client) -> Self {
Self { stream, bffh } Self { inner}
} }
pub fn connect<A: AsyncToSocketAddrs>(log: Logger, addr: A) -> impl Future<Output=Api> { pub fn from_stream(stream: TcpStream) -> (impl Future, Self) {
let f = async { let network = Box::new(twoparty::VatNetwork::new(stream.clone(), stream.clone(),
let mut stream = TcpStream::connect(addr).await.unwrap(); rpc_twoparty_capnp::Side::Client, Default::default()));
handshake(log.clone(), &mut stream).await.unwrap(); let mut rpc_system = RpcSystem::new(network, None);
let network = Box::new(twoparty::VatNetwork::new(stream.clone(), stream.clone(), let bffh: connection_capnp::bootstrap::Client
rpc_twoparty_capnp::Side::Client, Default::default())); = rpc_system.bootstrap(rpc_twoparty_capnp::Side::Server);
let mut rpc_system = RpcSystem::new(network, None); (rpc_system, Api::new(bffh))
let bffh: connection_capnp::bootstrap::Client }
= rpc_system.bootstrap(rpc_twoparty_capnp::Side::Server);
let mut api = Api::new(stream, bffh); pub async fn authentication(&mut self) -> Authentication {
let req = self.bffh.auth_request().send().promise;
api.authenticate(log).await; // TODO: When's that an Err?
let res = req.await.unwrap();
api // TODO: When's that an Err?
}; let tmp = res.get().unwrap();
Authentication::new(tmp.get_auth().unwrap())
f
} }
async fn authenticate(&mut self, log: Logger) { 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> { async fn handshake(log: Logger, mut stream: &mut TcpStream) -> Result<(), io::Error> {
let host = "localhost"; let host = "localhost";
let program = format!("{}-{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); let program = format!("{}-{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));