use std::{str::FromStr, thread};
use anyhow::Context;
use futures::StreamExt;
use orchestra_toolkit::*;
const RETRIEVE_COUNT: i32 = 512;
const CONCURRENT_COUNT: usize = 4;
fn synchronous_world(session: Session, auth: Token) {
for i in 0..RETRIEVE_COUNT {
let res = session
.invoke_entity(Entity::new(0, 0, 9), Method::Retrieve, auth)
.call();
match res {
Ok(v) => println!(" sync {i:>3}: {v}"),
Err(e) => println!(" sync {i:>3}: {e}"),
}
}
}
fn asynchronous_world(session: Session, auth: Token) {
session.run_async(|s| _asynchronous_world(s, auth))
}
async fn _asynchronous_world(session: &SessionAsync, auth: Token) {
let futures = (0..RETRIEVE_COUNT).map(|i| {
let s = session.clone();
async move {
let res = s
.invoke_entity(Entity::new(0, 0, 9), Method::Retrieve, auth)
.await;
match res {
Ok(v) => println!("async {i:>3}: {v}"),
Err(e) => println!("async {i:>3}: {e}"),
}
}
});
let stream = futures::stream::iter(futures).buffer_unordered(CONCURRENT_COUNT);
let _ = stream.collect::<Vec<_>>().await;
}
fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
let mut config = SessionConfig::default();
if let Ok(host) = std::env::var("AVESTERRA_HOST") {
config.address = host;
}
if let Ok(port_str) = std::env::var("AVESTERRA_PORT") {
config.port = u16::from_str(&port_str).context("Parsing `AVESTERRA_PORT`")?;
}
if let Ok(cert_dir_path) = std::env::var("AVESTERRA_CERTIFICATE_DIR_PATH") {
config.pem_filepath = (cert_dir_path + "/avesterra.pem").try_into()?;
}
let auth = if let Ok(auth_str) = std::env::var("AVESTERRA_AUTH") {
Token::from_str(&auth_str).context("Parsing authorization token given from env")?
} else {
Token::NULL
};
let session = Session::initialize(config)?;
let s1 = session.clone();
let s2 = session.clone();
let t1 = thread::spawn(move || synchronous_world(s1, auth));
let t2 = thread::spawn(move || asynchronous_world(s2, auth));
t1.join().unwrap();
t2.join().unwrap();
session.finalize();
Ok(())
}