diff --git a/README.md b/README.md index 213072d..3177fd7 100644 --- a/README.md +++ b/README.md @@ -19,23 +19,27 @@ Rust bindings for the high-performance [eCAL](https://github.com/eclipse-ecal/ec ### Publisher ```rust +use std::time::Duration; use rustecal::{Ecal, EcalComponents, TypedPublisher}; use rustecal_types_string::StringMessage; fn main() { - Ecal::initialize(Some("hello publisher"), EcalComponents::DEFAULT) - .expect("eCAL initialization failed"); + // eCAL init + Ecal::initialize(Some("hello publisher"), EcalComponents::DEFAULT).unwrap(); - let publisher: TypedPublisher = TypedPublisher::::new("chatter") - .expect("Failed to create publisher"); + // create a string publisher on "hello" + let publisher = TypedPublisher::::new("hello").unwrap(); - while Ecal::ok() { - let wrapped = StringMessage("Hello from Rust!".to_string()); - publisher.send(&wrapped); + // prepare the message to send + let message = StringMessage("Hello from Rust!".to_string()); - std::thread::sleep(std::time::Duration::from_millis(500)); + // publish until eCAL shuts down + while Ecal::ok() { + publisher.send(&message); + std::thread::sleep(Duration::from_millis(500)); } + // clean up and finalize eCAL Ecal::finalize(); } ``` @@ -50,20 +54,24 @@ use rustecal_types_string::StringMessage; use rustecal::pubsub::typed_subscriber::Received; fn main() { - Ecal::initialize(Some("hello subscriber"), EcalComponents::DEFAULT) - .expect("eCAL initialization failed"); + // eCAL init + Ecal::initialize(Some("hello subscriber"), EcalComponents::DEFAULT).unwrap(); - let mut subscriber = TypedSubscriber::::new("chatter") - .expect("Failed to create subscriber"); + // create a string subscriber on “hello” + let mut subscriber = TypedSubscriber::::new("hello").unwrap(); + // print each incoming message subscriber.set_callback(|msg: Received| { - println!("Received : {}", msg.msg.0); + let StringMessage(text) = msg.msg; + println!("Received: {}", text); }); + // keep the thread alive so callbacks can run while Ecal::ok() { std::thread::sleep(std::time::Duration::from_millis(100)); } + // clean up and finalize eCAL Ecal::finalize(); } ``` @@ -73,27 +81,36 @@ fn main() { ### Service Server ```rust +use std::time::Duration; use rustecal::{Ecal, EcalComponents}; use rustecal::service::server::ServiceServer; use rustecal::service::types::MethodInfo; fn main() { - Ecal::initialize(Some("mirror server"), EcalComponents::DEFAULT) - .expect("eCAL initialization failed"); - - let mut server = ServiceServer::new("mirror") - .expect("Failed to create server"); - - server.add_method("reverse", Box::new(|_info: MethodInfo, req: &[u8]| { - let mut reversed = req.to_vec(); - reversed.reverse(); - reversed - })).unwrap(); - + // eCAL init + Ecal::initialize(Some("mirror server"), EcalComponents::DEFAULT).unwrap(); + + // create a service server for "mirror" + let mut server = ServiceServer::new("mirror").unwrap(); + + // register the "reverse" method + server + .add_method( + "reverse", + Box::new(|_info: MethodInfo, req: &[u8]| { + let mut reversed = req.to_vec(); + reversed.reverse(); + reversed + }), + ) + .unwrap(); + + // keep the server alive to handle incoming calls while Ecal::ok() { - std::thread::sleep(std::time::Duration::from_millis(100)); + std::thread::sleep(Duration::from_millis(100)); } + // clean up and finalize eCAL Ecal::finalize(); } ``` @@ -103,31 +120,37 @@ fn main() { ### Service Client ```rust +use std::time::Duration; use rustecal::{Ecal, EcalComponents}; use rustecal::service::client::ServiceClient; use rustecal::service::types::ServiceRequest; fn main() { - Ecal::initialize(Some("mirror client"), EcalComponents::DEFAULT) - .expect("eCAL initialization failed"); + // eCAL init + Ecal::initialize(Some("mirror client"), EcalComponents::DEFAULT).unwrap(); - let client = ServiceClient::new("mirror") - .expect("Failed to create client"); + // create a service client for "mirror" + let client = ServiceClient::new("mirror").unwrap(); + // call the "reverse" service until eCAL shuts down while Ecal::ok() { + // prepare the request payload let request = ServiceRequest { payload: b"stressed".to_vec(), }; + // send the request and print the response if any if let Some(response) = client.call("reverse", request, Some(1000)) { println!("Reversed: {}", String::from_utf8_lossy(&response.payload)); } else { println!("No response received."); } - std::thread::sleep(std::time::Duration::from_secs(1)); + // throttle the request rate + std::thread::sleep(Duration::from_secs(1)); } + // clean up and finalize eCAL Ecal::finalize(); } ``` diff --git a/docs/src/examples/protobuf.md b/docs/src/examples/protobuf.md index b363a76..b7d049b 100644 --- a/docs/src/examples/protobuf.md +++ b/docs/src/examples/protobuf.md @@ -5,9 +5,12 @@ ```rust use rustecal::{Ecal, EcalComponents, TypedPublisher}; use rustecal_types_protobuf::{ProtobufMessage, IsProtobufType}; -mod person { include!(concat!(env!("OUT_DIR"), "/pb.people.rs")); } -use person::Person; +mod people { include!(concat!(env!("OUT_DIR"), "/pb.people.rs")); } +mod animal { include!(concat!(env!("OUT_DIR"), "/pb.animal.rs")); } +mod environment { include!(concat!(env!("OUT_DIR"), "/pb.environment.rs")); } + +use people::Person; impl IsProtobufType for Person {} fn main() -> Result<(), Box> { @@ -31,9 +34,14 @@ fn main() -> Result<(), Box> { ```rust use rustecal::{Ecal, EcalComponents, TypedSubscriber}; -use rustecal_types_protobuf::ProtobufMessage; -mod person { include!(concat!(env!("OUT_DIR"), "/pb.people.rs")); } -use person::Person; +use rustecal_types_protobuf::{ProtobufMessage, IsProtobufType}; + +mod people { include!(concat!(env!("OUT_DIR"), "/pb.people.rs")); } +mod animal { include!(concat!(env!("OUT_DIR"), "/pb.animal.rs")); } +mod environment { include!(concat!(env!("OUT_DIR"), "/pb.environment.rs")); } + +use people::Person; +impl IsProtobufType for Person {} fn main() -> Result<(), Box> { Ecal::initialize(Some("protobuf subscriber"), EcalComponents::DEFAULT)?; diff --git a/docs/src/examples/string.md b/docs/src/examples/string.md index c267b5a..596d127 100644 --- a/docs/src/examples/string.md +++ b/docs/src/examples/string.md @@ -19,6 +19,7 @@ fn main() -> Result<(), Box> { } Ecal::finalize(); + Ok(()) } ``` @@ -28,10 +29,10 @@ fn main() -> Result<(), Box> { use rustecal::{Ecal, EcalComponents, TypedSubscriber}; use rustecal_types_string::StringMessage; -fn main() { - Ecal::initialize(Some("string subscriber"), EcalComponents::DEFAULT).unwrap(); +fn main() -> Result<(), Box> { + Ecal::initialize(Some("string subscriber"), EcalComponents::DEFAULT)?; - let mut sub = TypedSubscriber::::new("hello").unwrap(); + let mut sub = TypedSubscriber::::new("hello")?; sub.set_callback(|msg| println!("Received: {}", msg.msg.0)); while Ecal::ok() { diff --git a/rustecal-samples/pubsub/person_receive/src/main.rs b/rustecal-samples/pubsub/person_receive/src/main.rs index 50438a2..105b625 100644 --- a/rustecal-samples/pubsub/person_receive/src/main.rs +++ b/rustecal-samples/pubsub/person_receive/src/main.rs @@ -1,19 +1,12 @@ -mod people { - include!(concat!(env!("OUT_DIR"), "/pb.people.rs")); -} -mod animal { - include!(concat!(env!("OUT_DIR"), "/pb.animal.rs")); -} -mod environment { - include!(concat!(env!("OUT_DIR"), "/pb.environment.rs")); -} - use rustecal::{Ecal, EcalComponents, TypedSubscriber}; use rustecal::pubsub::typed_subscriber::Received; use rustecal_types_protobuf::{ProtobufMessage, IsProtobufType}; -use people::Person; +mod people { include!(concat!(env!("OUT_DIR"), "/pb.people.rs")); } +mod animal { include!(concat!(env!("OUT_DIR"), "/pb.animal.rs")); } +mod environment { include!(concat!(env!("OUT_DIR"), "/pb.environment.rs")); } +use people::Person; impl IsProtobufType for Person {} fn main() { diff --git a/rustecal-samples/pubsub/person_send/src/main.rs b/rustecal-samples/pubsub/person_send/src/main.rs index 957dc84..d9ef7e0 100644 --- a/rustecal-samples/pubsub/person_send/src/main.rs +++ b/rustecal-samples/pubsub/person_send/src/main.rs @@ -1,16 +1,10 @@ -mod people { - include!(concat!(env!("OUT_DIR"), "/pb.people.rs")); -} -mod animal { - include!(concat!(env!("OUT_DIR"), "/pb.animal.rs")); -} -mod environment { - include!(concat!(env!("OUT_DIR"), "/pb.environment.rs")); -} - use rustecal::{Ecal, EcalComponents, TypedPublisher}; use rustecal_types_protobuf::{ProtobufMessage, IsProtobufType}; +mod people { include!(concat!(env!("OUT_DIR"), "/pb.people.rs")); } +mod animal { include!(concat!(env!("OUT_DIR"), "/pb.animal.rs")); } +mod environment { include!(concat!(env!("OUT_DIR"), "/pb.environment.rs")); } + use people::Person; impl IsProtobufType for Person {}