r12f / rnp

A simple layer 4 ping tool for cloud.
Apache License 2.0
72 stars 7 forks source link

Request to release as a lib crate #166

Closed harshasrisri closed 1 year ago

harshasrisri commented 1 year ago

Hi @r12f, May I request you to release a lib crate so that it can be used in other projects as well?

r12f commented 1 year ago

Hi @harshasrisri , yes, you could directly add rnp as dependency and use it to build your own program.

All we need are adding the following things in your Cargo.toml dependency list, if they are not yet:

futures-intrusive = "0.4"
rnp = "0.1.146"
tokio = "1.26.0"

Then, we can start using them directly as below:

use anyhow::Result;
use futures_intrusive::sync::ManualResetEvent;
use rnp::*;
use std::{sync::Arc, time::Duration};

#[tokio::main]
async fn main() -> Result<(), String> {
    let config = RnpPingRunnerConfig {
        // ...
    };

    let stop_event = Arc::new(ManualResetEvent::new(false));
    let rnp_exit_failure_reason = config.result_processor_config.exit_failure_reason.clone();
    let mut runner = PingRunnerCore::new(config, stop_event.clone());

    runner.run_warmup_pings().await;
    runner.start_running_normal_pings();
    runner.join().await;

    if let Some(rnp_exit_failure_reason) = rnp_exit_failure_reason {
        if rnp_exit_failure_reason.lock().unwrap().is_some() {
            return Err("Ping failed!".to_string());
        }
    }

    return Ok(());
}

Here is the result of the sample code above: image