jhelovuo / RustDDS

Rust implementation of Data Distribution Service

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Help: Example of IPC

Machine-Jonte opened this issue · comments

Hi!

I've been trying to use this for IPC, but have not really managed to do it. I scanned through the examples but could only find examples of sending messages in the same process.

Any simple example of IPC that does not use ROS?

Below is my failed example:

use rustdds::policy::{Durability, History, Liveliness, Reliability};
use rustdds::*;
use serde::{Deserialize, Serialize};
use std::{env, process};

macro_rules! unwrap_option_or_continue {
    ($data: expr) => {
        if !$data.is_none() {
            $data.unwrap()
        } else {
            continue;
        }
    };
}

macro_rules! unwrap_err_or_continue {
    ($data: expr) => {
        if !$data.is_err() {
            $data.unwrap()
        } else {
            dbg!(&$data);
            continue;
        }
    };
}

#[derive(Serialize, Deserialize, Debug)]
struct SomeType {
    a: i32,
}

fn main() {
    let args: Vec<_> = env::args().take(3).collect();

    match &args[..] {
        [_, t, topic] if t == "pub" => publisher(topic),
        [_, t, topic] if t == "sub" => subscriber(topic),
        [_, t, topic] if t == "both" => both(topic),
        _ => {
            println!("Usage: dds pub|sub|both <topic>");
            process::exit(1);
        }
    }
}

fn create_qos_policy() -> QosPolicies {
    let qos: QosPolicies = {
        QosPolicyBuilder::new()
            .durability(Durability::Volatile)
            .liveliness(Liveliness::Automatic {
                lease_duration: rustdds::Duration::DURATION_INFINITE,
            })
            .reliability(Reliability::Reliable {
                max_blocking_time: rustdds::Duration::from_millis(100),
            })
            .history(History::KeepLast { depth: 10 })
            .build()
    };
    qos
}

fn both(topic: &str) {
    let topic_server = topic.to_owned();
    let topic_subscriber = topic.to_owned();
    std::thread::spawn(move || publisher(&topic_server));
    std::thread::spawn(move || subscriber(&topic_subscriber));
    std::thread::sleep(std::time::Duration::from_secs(10));
}

fn publisher(topic: &str) {
    let domain_participant = DomainParticipant::new(0).unwrap();
    let qos = create_qos_policy();
    let publisher = domain_participant.create_publisher(&qos).unwrap();
    let some_topic = domain_participant
        .create_topic(
            topic.to_string(),
            "SomeType".to_string(),
            &qos,
            TopicKind::NoKey,
        )
        .unwrap();
    let writer = publisher
        .create_datawriter_no_key::<SomeType, CDRSerializerAdapter<SomeType>>(&some_topic, None)
        .unwrap();
    let mut i = 0;
    loop {
        i += 1;
        let some_data = SomeType { a: i };
        println!("Sending: {:?}", some_data);
        writer.write(some_data, None).unwrap();
        std::thread::sleep(std::time::Duration::from_millis(500));
    }
}

fn subscriber(topic: &str) {
    // DomainParticipant is always necessary
    let domain_participant = DomainParticipant::new(0).unwrap();
    let qos = create_qos_policy();
    dbg!(&domain_participant.discovered_topics());
    let some_topic = domain_participant
        .find_topic(topic, std::time::Duration::from_secs(3))
        .unwrap()
        .unwrap();
    let subscriber = domain_participant.create_subscriber(&qos).unwrap();
    // let some_topic = domain_participant
    //     .create_topic(
    //         topic.to_string(),
    //         "SomeType".to_string(),
    //         &qos,
    //         TopicKind::NoKey,
    //     )
    //     .unwrap();
    let mut reader = subscriber
        .create_datareader_no_key::<SomeType, CDRDeserializerAdapter<SomeType>>(&some_topic, None)
        .unwrap();
    loop {
        let sample = unwrap_option_or_continue!(unwrap_err_or_continue!(reader.read_next_sample()));
        dbg!(&sample);
    }
}

Have you tried the Shapes Demo? Just start two instances of the Shapes Demo, one publishing and the other subscribing. Or you can even try to download a Shapes Demo program from some other DDS vendor, such as RTI or eProsima, and test against those.

Hi, I did give the Shapes Demo a try. It appears I'm having a similar issue as the issue: #249. I'm also running on Windows, so that is likely why.