Skip to main content

hello_dds_publisher/
hello_dds_publisher.rs

1//! hello_dds_publisher โ€” minimal-DDS-Publisher, der regelmaessig
2//! Samples auf Topic "Chatter" schickt.
3//!
4//! # Usage
5//!
6//! ```text
7//! # Terminal 1:
8//! cargo run -p zerodds-dcps --example hello_dds_publisher
9//! # Terminal 2 (gleicher Host, anderer Prozess):
10//! cargo run -p zerodds-dcps --example hello_dds_subscriber
11//! ```
12//!
13//! Der Publisher laeuft bis Ctrl-C. Jede Sekunde wird ein Sample
14//! "hello #<counter>" an alle matched Subscriber auf Domain 0 gesendet.
15//!
16//! # Was hier passiert
17//!
18//! 1. `DomainParticipantFactory::create_participant(0, ...)` startet
19//!    die `DcpsRuntime` โ€” das sind 4 UDP-Sockets + SPDP/SEDP-Threads.
20//! 2. SPDP-Beacons werden alle 5 s auf die Multicast-Gruppe 239.255.0.1
21//!    gesendet; damit findet uns der Subscriber.
22//! 3. `create_topic("Chatter", ...)` legt die Topic-Registry-Eintrag an.
23//! 4. `create_datawriter` registriert einen User-Writer + EntityId bei
24//!    der Runtime und announced ihn via SEDP.
25//! 5. `writer.write(&sample)` encodiert den Sample und uebergibt ihn
26//!    an den internen `ReliableWriter`, der ihn an alle matched Reader
27//!    via UDP schickt.
28
29#![allow(clippy::print_stdout, clippy::print_stderr)]
30
31use std::thread;
32use std::time::Duration;
33
34use zerodds_dcps::{
35    DataWriterQos, DomainParticipantFactory, DomainParticipantQos, PublisherQos, RawBytes, TopicQos,
36};
37
38fn main() -> Result<(), Box<dyn std::error::Error>> {
39    let factory = DomainParticipantFactory::instance();
40    let participant = factory.create_participant(0, DomainParticipantQos::default())?;
41
42    let topic = participant.create_topic::<RawBytes>("Chatter", TopicQos::default())?;
43    let publisher = participant.create_publisher(PublisherQos::default());
44    let writer = publisher.create_datawriter::<RawBytes>(&topic, DataWriterQos::default())?;
45
46    println!("hello_dds_publisher: sending on Domain 0 Topic 'Chatter' โ€” Ctrl-C to stop");
47
48    // Ein paar Sekunden warten, damit SPDP+SEDP Subscriber entdecken kann.
49    // Ohne Warte-Phase gehen die ersten Samples ins Leere (noch kein Reader
50    // gematched). v1.3 bringt `wait_for_matched_subscription`.
51    thread::sleep(Duration::from_secs(3));
52
53    let mut counter: u32 = 0;
54    loop {
55        let payload = format!("hello #{counter}");
56        writer.write(&RawBytes::new(payload.clone().into_bytes()))?;
57        println!("  -> {payload}");
58        counter = counter.wrapping_add(1);
59        thread::sleep(Duration::from_secs(1));
60    }
61}