GitHub - eclipse-ecal/rustecal: Idiomatic Rust bindings for eCAL. (original) (raw)

rustecal

Rust bindings for the high-performance eCAL middleware, providing efficient pub/sub and service-based communication for interprocess and distributed systems.


Features


System Requirements

This crate requires a native installation of the Eclipse eCAL C/C++ runtime, version 6.0 or newer. General instructions for installation can be found here. The crate will fail to compile if the native libraries are not found.


Examples

Publisher

use std::time::Duration; use rustecal::{Ecal, EcalComponents, TypedPublisher}; use rustecal::pubsub::publisher::Timestamp; use rustecal_types_string::StringMessage;

fn main() { // Initialize eCAL Ecal::initialize(Some("hello publisher"), EcalComponents::DEFAULT, None).unwrap();

// create a string publisher on "hello"
let publisher = TypedPublisher::<StringMessage>::new("hello").unwrap();

// prepare the message to send
let message = StringMessage { data: "Hello from Rust".into() };

// publish until eCAL shuts down
while Ecal::ok() {
    publisher.send(&message, Timestamp::Auto);
    std::thread::sleep(Duration::from_millis(500));
}

// clean up and finalize eCAL
Ecal::finalize();

}


Subscriber

use rustecal::{Ecal, EcalComponents, TypedSubscriber}; use rustecal_types_string::StringMessage;

fn main() { // Initialize eCAL Ecal::initialize(Some("hello subscriber"), EcalComponents::DEFAULT, None).unwrap();

// create a string subscriber on β€œhello”
let mut subscriber = TypedSubscriber::<StringMessage>::new("hello").unwrap();

// print each incoming message
subscriber.set_callback(|message| {
    println!("Received: {}", message.payload.data)
});

// 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();

}


Service Server

use std::time::Duration; use rustecal::{Ecal, EcalComponents}; use rustecal:πŸ•β€πŸ¦Ί:server::ServiceServer; use rustecal:πŸ•β€πŸ¦Ί:types::MethodInfo;

fn main() { // Initialize eCAL Ecal::initialize(Some("mirror server"), EcalComponents::DEFAULT, None).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(Duration::from_millis(100));
}

// clean up and finalize eCAL
Ecal::finalize();

}


Service Client

use std::time::Duration; use rustecal::{Ecal, EcalComponents}; use rustecal:πŸ•β€πŸ¦Ί:client::ServiceClient; use rustecal:πŸ•β€πŸ¦Ί:types::ServiceRequest;

fn main() { // Initialize eCAL Ecal::initialize(Some("mirror client"), EcalComponents::DEFAULT, None).unwrap();

// 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.");
    }

    // throttle the request rate
    std::thread::sleep(Duration::from_secs(1));
}

// clean up and finalize eCAL
Ecal::finalize();

}


Documentation

Full user guide: https://eclipse-ecal.github.io/rustecal


License

Licensed under Apache-2.0.


Maintainer

Rex Schilasky