Module rustdds::dds[][src]

DDS interface

DDS usage summary

  • Create a DomainParticipant. You have to choose a domain id. The default value is zero.
  • Create or find a Topic from the DomainParticipant. Topics have a name and a type.
  • Create a Publisher and/or Subscriber from the DomainParticipant.
  • To receive data, create a DataReader from Subscriber and Topic.
  • To send data, create a DataWriterfrom Publisher and Topic.
  • Data from DataReader can be read or taken. Taking removes the data samples from the DataReader, whereas reading only marks them as read.
  • Topics are either WithKey or NoKey. WithKey topics are like map data structures, containing multiple instances (map items), identified by key. The key must be something that can be extracted from the data samples. Instances can be created (published) and deleted (disposed). NoKey topics have always only one instance of the data.
  • Data is sent and received in consecutive samples. When read, a smaple is accompanied with metadata (SampleInfo).

Interfacing Rust data types to DDS

  • DDS takes care of serialization and deserialization. In order to do this, the payload data must be Serde serializable/deserializable.
  • If your data is to be communicated over a WithKey topic, the payload data type must implement Keyed trait from this crate.
  • If you are using CDR serialization (DDS default), then use CDRSerializerAdapter and CDRDeserializerAdapter when such adapters are required. If you need to use another serialization format, then you should find or write a Serde data format implementation and wrap it as a (De)SerializerAdaper.

Examples

use rustdds::dds::DomainParticipant;
use rustdds::dds::{No_Key_DataReader as DataReader, No_Key_DataWriter as DataWriter, no_key::DataSample};
use rustdds::dds::qos::QosPolicyBuilder;
use rustdds::dds::qos::policy::Reliability;
use rustdds::dds::data_types::DDSDuration;
use rustdds::dds::data_types::TopicKind;
use rustdds::serialization::{CDRSerializerAdapter, CDRDeserializerAdapter};
use serde::{Serialize, Deserialize};

// DomainParticipant is always necessary
let domain_participant = DomainParticipant::new(0);

let qos = QosPolicyBuilder::new()
  .reliability(Reliability::Reliable { max_blocking_time: DDSDuration::DURATION_ZERO })
  .build();

// DDS Subscriber, only one is necessary for each thread (slight difference to
// DDS specification)
let subscriber = domain_participant.create_subscriber(&qos).unwrap();

// DDS Publisher, only one is necessary for each thread (slight difference to
// DDS specification)
let publisher = domain_participant.create_publisher(&qos).unwrap();

// Some DDS Topic that we can write and read from (basically only binds readers
// and writers together)
let some_topic = domain_participant.create_topic("some_topic", "SomeType", &qos, TopicKind::NoKey).unwrap();

// Used type needs Serialize for writers and Deserialize for readers
#[derive(Serialize, Deserialize)]
struct SomeType {
  a: i32
}

// Creating DataReader requires type and deserializer adapter (which is recommended to be CDR).
// Reader needs to be mutable if any operations are used.
let mut reader = subscriber
  .create_datareader_no_key::<SomeType, CDRDeserializerAdapter<SomeType>>(
    some_topic.clone(),
    None,
    None)
  .unwrap();

// Creating DataWriter required type and serializer adapter (which is recommended to be CDR).
let writer = publisher
  .create_datawriter_no_key::<SomeType, CDRSerializerAdapter<SomeType>>(
    None,
    some_topic,
    None)
  .unwrap();

// Readers implement mio Evented trait and thus function the same way as
// std::sync::mpcs and can be handled the same way for reading the data

let some_data = SomeType { a: 1 };

// This should send the data to all who listen "some_topic" topic.
writer.write(some_data, None).unwrap();

// ... Some data has arrived at some point for the reader
let data_sample = if let Ok(Some(value)) = reader.read_next_sample() {
  value
} else {
  // no data has arrived
  return;
};

// Getting reference to actual data from the data sample
let actual_data = data_sample.value();

Modules

data_types

Datatypes needed for overall operability with this crate

error

DDS Error

no_key

Participating in NoKey topics.

qos

DDS Quality of Service

statusevents
traits

All DDS related traits for functionality such as getting DDS EntityId

with_key

Participating in WithKey topics.

Structs

DomainParticipant

DDS DomainParticipant generally only one per domain per machine should be active

No_Key_DataReader

DDS DataReader for no key topics.

No_Key_DataWriter

DDS DataWriter for no key topics

Publisher

DDS Publisher

Subscriber

DDS Subscriber

Topic

DDS Topic

With_Key_DataReader

DDS DataReader for with_key topics.

With_Key_DataWriter

DDS DataWriter for keyed topics