diff --git a/Cargo.toml b/Cargo.toml index aadd331..3a2948d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,42 @@ chrono = { workspace = true, optional = true } static_init = { workspace = true } unshell-macros = { path = "./unshell-macros" } +[[example]] +name = "leaf_derive" +path = "examples/protocol/leaf_derive.rs" + +[[example]] +name = "remote_shell_endpoint" +path = "examples/protocol/remote_shell_endpoint.rs" + +[[example]] +name = "remote_shell_receive" +path = "examples/protocol/remote_shell_receive.rs" + +[[example]] +name = "bench" +path = "examples/protocol/bench/bench.rs" + +[[example]] +name = "op_encode_call" +path = "examples/protocol/bench/op_encode_call.rs" + +[[example]] +name = "op_decode_call" +path = "examples/protocol/bench/op_decode_call.rs" + +[[example]] +name = "op_forward_call_receive" +path = "examples/protocol/bench/op_forward_call_receive.rs" + +[[example]] +name = "op_local_call_receive" +path = "examples/protocol/bench/op_local_call_receive.rs" + +[[example]] +name = "op_hook_data_receive" +path = "examples/protocol/bench/op_hook_data_receive.rs" + [profile.minimize] inherits = "release" strip = true # Strip symbols from the binary diff --git a/examples/protocol_bench.rs b/examples/protocol/bench/bench.rs similarity index 96% rename from examples/protocol_bench.rs rename to examples/protocol/bench/bench.rs index 77c78af..c072e00 100644 --- a/examples/protocol_bench.rs +++ b/examples/protocol/bench/bench.rs @@ -43,7 +43,7 @@ fn main() { } println!(); - println!("Run `cargo run --example protocol_bench -- tools` to build and execute"); + println!("Run `cargo run --example bench -- tools` to build and execute"); println!("the standalone operation binaries under strace, perf, and heaptrack."); } @@ -302,11 +302,11 @@ fn run_external_tools() { build_examples(root); let ops = [ - ("encode_call", "protocol_op_encode_call"), - ("decode_call", "protocol_op_decode_call"), - ("forward_call_receive", "protocol_op_forward_call_receive"), - ("local_call_receive", "protocol_op_local_call_receive"), - ("hook_data_receive", "protocol_op_hook_data_receive"), + ("encode_call", "op_encode_call"), + ("decode_call", "op_decode_call"), + ("forward_call_receive", "op_forward_call_receive"), + ("local_call_receive", "op_local_call_receive"), + ("hook_data_receive", "op_hook_data_receive"), ]; let heap_dir = root.join("heaptrack-cli"); diff --git a/examples/protocol_op_decode_call.rs b/examples/protocol/bench/op_decode_call.rs similarity index 82% rename from examples/protocol_op_decode_call.rs rename to examples/protocol/bench/op_decode_call.rs index d0f0654..3eed735 100644 --- a/examples/protocol_op_decode_call.rs +++ b/examples/protocol/bench/op_decode_call.rs @@ -1,4 +1,4 @@ -#[path = "support/protocol_bench_common.rs"] +#[path = "support/bench_common.rs"] mod common; fn main() { diff --git a/examples/protocol_op_encode_call.rs b/examples/protocol/bench/op_encode_call.rs similarity index 82% rename from examples/protocol_op_encode_call.rs rename to examples/protocol/bench/op_encode_call.rs index b0e1603..3706e35 100644 --- a/examples/protocol_op_encode_call.rs +++ b/examples/protocol/bench/op_encode_call.rs @@ -1,4 +1,4 @@ -#[path = "support/protocol_bench_common.rs"] +#[path = "support/bench_common.rs"] mod common; fn main() { diff --git a/examples/protocol_op_forward_call_receive.rs b/examples/protocol/bench/op_forward_call_receive.rs similarity index 83% rename from examples/protocol_op_forward_call_receive.rs rename to examples/protocol/bench/op_forward_call_receive.rs index a85b783..4882774 100644 --- a/examples/protocol_op_forward_call_receive.rs +++ b/examples/protocol/bench/op_forward_call_receive.rs @@ -1,4 +1,4 @@ -#[path = "support/protocol_bench_common.rs"] +#[path = "support/bench_common.rs"] mod common; fn main() { diff --git a/examples/protocol_op_hook_data_receive.rs b/examples/protocol/bench/op_hook_data_receive.rs similarity index 83% rename from examples/protocol_op_hook_data_receive.rs rename to examples/protocol/bench/op_hook_data_receive.rs index 256f3e8..b298a97 100644 --- a/examples/protocol_op_hook_data_receive.rs +++ b/examples/protocol/bench/op_hook_data_receive.rs @@ -1,4 +1,4 @@ -#[path = "support/protocol_bench_common.rs"] +#[path = "support/bench_common.rs"] mod common; fn main() { diff --git a/examples/protocol_op_local_call_receive.rs b/examples/protocol/bench/op_local_call_receive.rs similarity index 83% rename from examples/protocol_op_local_call_receive.rs rename to examples/protocol/bench/op_local_call_receive.rs index 4fd0070..06de76a 100644 --- a/examples/protocol_op_local_call_receive.rs +++ b/examples/protocol/bench/op_local_call_receive.rs @@ -1,4 +1,4 @@ -#[path = "support/protocol_bench_common.rs"] +#[path = "support/bench_common.rs"] mod common; fn main() { diff --git a/examples/support/protocol_bench_common.rs b/examples/protocol/bench/support/bench_common.rs similarity index 100% rename from examples/support/protocol_bench_common.rs rename to examples/protocol/bench/support/bench_common.rs diff --git a/examples/protocol/leaf_derive.rs b/examples/protocol/leaf_derive.rs new file mode 100644 index 0000000..c63b1cc --- /dev/null +++ b/examples/protocol/leaf_derive.rs @@ -0,0 +1,46 @@ +use std::error::Error; + +use unshell::Leaf; +use unshell::protocol::tree::{Endpoint, Ingress, LocalEvent, ProtocolEndpoint}; + +#[derive(Leaf)] +#[leaf(org = "org", product = "example", version = "v1", leaf_name = "echo")] +#[leaf(procedures(call, stream))] +struct EchoLeaf; + +fn path(parts: &[&str]) -> Vec { + parts.iter().map(|part| (*part).to_owned()).collect() +} + +fn main() -> Result<(), Box> { + let mut endpoint = ProtocolEndpoint::new( + path(&["agent"]), + Some(Vec::new()), + Vec::new(), + vec![EchoLeaf::protocol_leaf_spec()], + ); + + let hook_id = endpoint.allocate_hook_id(); + let frame = endpoint.make_call( + path(&["agent"]), + Some(EchoLeaf::protocol_leaf_name()), + EchoLeaf::protocol_procedure_id("call").expect("known procedure suffix"), + Some(hook_id), + b"hello leaf".to_vec(), + )?; + + let outcome = endpoint.receive(&Ingress::Parent, frame)?; + let Some(LocalEvent::Call { header, message }) = outcome.event else { + return Err("expected local leaf call".into()); + }; + + assert_eq!(header.dst_leaf.as_deref(), Some("org.example.v1.echo")); + assert_eq!(message.procedure_id, "org.example.v1.echo.call"); + + println!( + "leaf={} procedure={}", + EchoLeaf::protocol_leaf_name(), + message.procedure_id + ); + Ok(()) +} diff --git a/examples/protocol_remote_shell_endpoint.rs b/examples/protocol/remote_shell_endpoint.rs similarity index 99% rename from examples/protocol_remote_shell_endpoint.rs rename to examples/protocol/remote_shell_endpoint.rs index 1b8f8de..37a2210 100644 --- a/examples/protocol_remote_shell_endpoint.rs +++ b/examples/protocol/remote_shell_endpoint.rs @@ -1,4 +1,4 @@ -#[path = "support/protocol_remote_shell_common.rs"] +#[path = "support/remote_shell_common.rs"] mod common; use std::error::Error; diff --git a/examples/protocol_remote_shell_receive.rs b/examples/protocol/remote_shell_receive.rs similarity index 97% rename from examples/protocol_remote_shell_receive.rs rename to examples/protocol/remote_shell_receive.rs index 2fbedc1..9be345f 100644 --- a/examples/protocol_remote_shell_receive.rs +++ b/examples/protocol/remote_shell_receive.rs @@ -1,4 +1,4 @@ -#[path = "support/protocol_remote_shell_common.rs"] +#[path = "support/remote_shell_common.rs"] mod common; use std::error::Error; diff --git a/examples/support/protocol_remote_shell_common.rs b/examples/protocol/support/remote_shell_common.rs similarity index 100% rename from examples/support/protocol_remote_shell_common.rs rename to examples/protocol/support/remote_shell_common.rs