orchestra-toolkit 0.6.3

Client to interract with Orchestra system, uses HGTP protocol
Documentation
/* Copyright 2024-2025 LEDR Technologies Inc.
* This file is part of the Orchestra library, which helps developer use our Orchestra technology which is based on AvesTerra, owned and developped by Georgetown University, under license agreement with LEDR Technologies Inc.
*
* The Orchestra library is a free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
*
* The Orchestra library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with the Orchestra library. If not, see <https://www.gnu.org/licenses/>.
*
* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
*/

use std::{str::FromStr, thread};

use anyhow::Context;
use futures::StreamExt;
use orchestra_toolkit::*;

const RETRIEVE_COUNT: i32 = 512;
// This is the number of concurrent requests that will be sent in the asynchronous
// example. This is basically the factor by which it will be faster than the
// synchronous example.
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(())
}