Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 54 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringMessage> = TypedPublisher::<StringMessage>::new("chatter")
.expect("Failed to create publisher");
// create a string publisher on "hello"
let publisher = TypedPublisher::<StringMessage>::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();
}
```
Expand All @@ -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::<StringMessage>::new("chatter")
.expect("Failed to create subscriber");
// create a string subscriber on “hello”
let mut subscriber = TypedSubscriber::<StringMessage>::new("hello").unwrap();

// print each incoming message
subscriber.set_callback(|msg: Received<StringMessage>| {
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();
}
```
Expand All @@ -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();
}
```
Expand All @@ -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();
}
```
Expand Down
18 changes: 13 additions & 5 deletions docs/src/examples/protobuf.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand All @@ -31,9 +34,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

```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<dyn std::error::Error>> {
Ecal::initialize(Some("protobuf subscriber"), EcalComponents::DEFAULT)?;
Expand Down
7 changes: 4 additions & 3 deletions docs/src/examples/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}

Ecal::finalize();
Ok(())
}
```

Expand All @@ -28,10 +29,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
use rustecal::{Ecal, EcalComponents, TypedSubscriber};
use rustecal_types_string::StringMessage;

fn main() {
Ecal::initialize(Some("string subscriber"), EcalComponents::DEFAULT).unwrap();
fn main() -> Result<(), Box<dyn std::error::Error>> {
Ecal::initialize(Some("string subscriber"), EcalComponents::DEFAULT)?;

let mut sub = TypedSubscriber::<StringMessage>::new("hello").unwrap();
let mut sub = TypedSubscriber::<StringMessage>::new("hello")?;
sub.set_callback(|msg| println!("Received: {}", msg.msg.0));

while Ecal::ok() {
Expand Down
15 changes: 4 additions & 11 deletions rustecal-samples/pubsub/person_receive/src/main.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
14 changes: 4 additions & 10 deletions rustecal-samples/pubsub/person_send/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {}

Expand Down