Skip to main content

ping_pong/
ping_pong.rs

1//! Two flows, one Atomic Hop round-trip (`Value::Message`).
2//!
3//! ```text
4//! cargo run -p byteflow-actors --example ping_pong
5//! ```
6
7use byteflow::{samples, std_native_table, FlowOutcome, Runtime, RuntimeConfig, Value};
8
9fn main() {
10    let chunk = samples::ping_pong();
11    let rt = match Runtime::with_natives_and_config(
12        chunk,
13        std_native_table(),
14        RuntimeConfig {
15            workers: 1,
16            quantum: 10_000,
17        },
18    ) {
19        Ok(rt) => rt,
20        Err(e) => {
21            eprintln!("runtime: {e}");
22            std::process::exit(1);
23        }
24    };
25    let Some(main) = rt.function_index("main") else {
26        eprintln!("missing main");
27        std::process::exit(1);
28    };
29    let handle = match rt.spawn(main, &[]) {
30        Ok(h) => h,
31        Err(e) => {
32            eprintln!("spawn: {e}");
33            std::process::exit(1);
34        }
35    };
36    let outcome = handle.join();
37    let metrics = rt.metrics();
38    rt.shutdown();
39
40    match outcome {
41        FlowOutcome::Completed(Value::Int(2)) => {
42            println!("pong replied 2 (Atomic Hop)");
43            println!("{metrics}");
44        }
45        other => {
46            eprintln!("unexpected {other:?}");
47            std::process::exit(1);
48        }
49    }
50}