From 16ea5a0270eb5f9432264f76f99ceac6d1ee2f8f Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 25 Jul 2021 13:30:45 +0100 Subject: [PATCH] wip client etc. Signed-off-by: Ian Jackson --- src/bin/client.rs | 41 +++++++++++++++++++++++++++++++++++------ src/config.rs | 9 ++++++++- src/prelude.rs | 10 ++++++++-- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/bin/client.rs b/src/bin/client.rs index d7c8301..cf16b37 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -11,16 +11,45 @@ struct Client { rx_write_stream: something, }*/ -#[throws(AE)] +#[allow(unused_variables)] // xxx +async fn run_client(ic: InstanceConfig, hclient: Arc>) + -> Result +{ + throw!(anyhow!("arrgh")); +} + #[tokio::main] -async fn main() { +async fn main() -> Result<(), AE> { let ics = config::read(LinkEnd::Client)?; + if ics.is_empty() { throw!(anyhow!("no associations with server(s)")); } + + env_logger::init(); let https = HttpsConnector::new(); - let hclient = Client::builder().build::<_, hyper::Body>(https); + let hclient = hyper::Client::builder().build::<_, hyper::Body>(https); + let hclient = Arc::new(hclient); - let clients = ics.into_iter().map(|| run_client(ic)); - + info!("starting"); + let () = future::select_all( + ics.into_iter().map(|ic| Box::pin(async { + let assocname = ic.to_string(); + info!("{} starting", &assocname); + let hclient = hclient.clone(); + let join = task::spawn(async { + run_client(ic, hclient).await.void_unwrap_err() + }); + match join.await { + Ok(e) => { + error!("{} failed: {:?}", &assocname, e); + }, + Err(je) => { + error!("{} panicked!", &assocname); + panic::resume_unwind(je.into_panic()); + }, + } + })) + ).await.0; - eprintln!("{:#?}", &instances); + error!("quitting because one of your client connections crashed"); + process::exit(16); } diff --git a/src/config.rs b/src/config.rs index dea5bab..30e2c12 100644 --- a/src/config.rs +++ b/src/config.rs @@ -191,6 +191,13 @@ impl FromStr for SectionName { SN::Link(LinkName { server, client }) } } +impl Display for InstanceConfig { + #[throws(fmt::Error)] + fn fmt(&self, f: &mut fmt::Formatter) { + write!(f, "[{} {}]", &self.server, &self.vaddr)?; + } +} + impl Display for SectionName { #[throws(fmt::Error)] fn fmt(&self, f: &mut fmt::Formatter) { @@ -700,7 +707,7 @@ pub fn read(end: LinkEnd) -> Vec { agg.read_extra(extra).context("extra config")?; } - eprintln!("GOT {:#?}", agg); + //eprintln!("GOT {:#?}", agg); Ok::<_,AE>(agg) })().context("read configuration")?; diff --git a/src/prelude.rs b/src/prelude.rs index 7b02ea0..7c7d099 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -10,21 +10,27 @@ pub use std::io::{self, ErrorKind, Read as _}; pub use std::mem; pub use std::net::{IpAddr, Ipv4Addr}; pub use std::path::{Path, PathBuf}; +pub use std::panic; +pub use std::process; pub use std::str::FromStr; pub use std::sync::Arc; pub use anyhow::{anyhow, Context}; pub use extend::ext; pub use fehler::{throw, throws}; +pub use futures::future; pub use hyper::Uri; +pub use hyper_tls::HttpsConnector; pub use ipnet::IpNet; pub use itertools::{iproduct, Itertools}; pub use lazy_regex::regex_is_match; +pub use log::{info, error}; pub use structopt::StructOpt; +pub use tokio::task; pub use tokio::time::Duration; -pub use void::{self, Void}; +pub use void::{self, Void, ResultVoidExt, ResultVoidErrExt}; -pub use crate::config; +pub use crate::config::{self, InstanceConfig}; pub use crate::utils::*; pub use crate::types::*; -- 2.30.2