rustorb-dds 0.3.0

DDS (Data Distribution Service) DCPS API in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! DDS-XTypes in matching: TypeInformation in discovery, type
//! consistency, DATA_REPRESENTATION.

#[path = "../examples/hello_world/greeting.rs"]
mod greeting;
#[path = "../examples/keyed/shape.rs"]
mod shape;
#[allow(dead_code)]
#[path = "../examples/evolve/v1.rs"]
mod v1;
#[allow(dead_code)]
#[path = "../examples/evolve/v2.rs"]
mod v2;

use std::time::{Duration, Instant};

use greeting::Greeting;
use rustorb_dds::{
    DATA_REPRESENTATION_QOS_POLICY_ID, DataReaderQos, DataWriterQos, DomainParticipant,
    DynamicData, DynamicDataFactory, DynamicType, History, ParticipantConfig, Reliability,
    TypeConsistency, data_representation,
};
use shape::Shape;

const WAIT: Duration = Duration::from_secs(10);

fn participant(domain: u32) -> DomainParticipant {
    let mut c = ParticipantConfig::localhost(domain);
    c.announce_period = Duration::from_millis(200);
    DomainParticipant::with_config(c).unwrap()
}

fn reliable_reader() -> DataReaderQos {
    DataReaderQos {
        reliability: Reliability::reliable(),
        history: History::KeepAll,
        ..DataReaderQos::default()
    }
}

fn wait_until(what: &str, mut f: impl FnMut() -> bool) {
    let deadline = Instant::now() + WAIT;
    while !f() {
        assert!(Instant::now() < deadline, "timed out waiting for {what}");
        std::thread::sleep(Duration::from_millis(20));
    }
}

/// Writes greetings until the reader has `n`, and returns them.
fn exchange(
    writer: &rustorb_dds::DataWriter<Greeting>,
    reader: &rustorb_dds::DataReader<Greeting>,
) -> Vec<Greeting> {
    assert!(writer.wait_for_subscriptions(1, WAIT));
    assert!(reader.wait_for_publications(1, WAIT));
    for index in 1..=3 {
        writer
            .write(&Greeting {
                index,
                message: "hi".into(),
            })
            .unwrap();
    }
    let mut got = Vec::new();
    wait_until("samples", || {
        got.extend(reader.take().into_iter().filter_map(|s| s.data));
        got.len() >= 3
    });
    got
}

#[test]
fn registered_types_match_by_type_information() {
    let (a, b) = (participant(50), participant(50));
    a.register_type::<Greeting>().unwrap();
    b.register_type::<Greeting>().unwrap();
    // Registering again does nothing.
    b.register_type::<Greeting>().unwrap();
    let writer = a
        .create_publisher()
        .unwrap()
        .create_datawriter(&a.create_topic::<Greeting>("xt").unwrap())
        .unwrap();
    let reader = b
        .create_subscriber()
        .unwrap()
        .create_datareader_with_qos(
            &b.create_topic::<Greeting>("xt").unwrap(),
            reliable_reader(),
        )
        .unwrap();
    assert_eq!(exchange(&writer, &reader).len(), 3);
}

#[test]
fn one_side_without_type_information_matches_by_name() {
    let (a, b) = (participant(51), participant(51));
    a.register_type::<Greeting>().unwrap();
    let writer = a
        .create_publisher()
        .unwrap()
        .create_datawriter(&a.create_topic::<Greeting>("xt").unwrap())
        .unwrap();
    let reader = b
        .create_subscriber()
        .unwrap()
        .create_datareader_with_qos(
            &b.create_topic::<Greeting>("xt").unwrap(),
            reliable_reader(),
        )
        .unwrap();
    assert_eq!(exchange(&writer, &reader).len(), 3);
}

#[test]
fn force_type_validation_needs_type_information() {
    let (a, b) = (participant(52), participant(52));
    b.register_type::<Greeting>().unwrap();
    let topic_a = a.create_topic::<Greeting>("xt").unwrap();
    let topic_b = b.create_topic::<Greeting>("xt").unwrap();
    let _writer = a
        .create_publisher()
        .unwrap()
        .create_datawriter(&topic_a)
        .unwrap();
    let qos = DataReaderQos {
        type_consistency: TypeConsistency {
            force_type_validation: true,
            ..TypeConsistency::default()
        },
        ..reliable_reader()
    };
    let reader = b
        .create_subscriber()
        .unwrap()
        .create_datareader_with_qos(&topic_b, qos)
        .unwrap();
    wait_until("INCONSISTENT_TOPIC", || {
        topic_b.inconsistent_topic_status().total_count == 1
    });
    // The writer's side sees the reader's policy too.
    wait_until("INCONSISTENT_TOPIC of the writer", || {
        topic_a.inconsistent_topic_status().total_count == 1
    });
    assert!(reader.matched_publications().is_empty());
}

#[test]
fn different_type_names_are_inconsistent_topics() {
    let (a, b) = (participant(53), participant(53));
    let topic_a = a.create_topic::<Greeting>("xt").unwrap();
    let topic_b = b.create_topic::<Shape>("xt").unwrap();
    let writer = a
        .create_publisher()
        .unwrap()
        .create_datawriter(&topic_a)
        .unwrap();
    let _reader = b
        .create_subscriber()
        .unwrap()
        .create_datareader(&topic_b)
        .unwrap();
    wait_until("INCONSISTENT_TOPIC", || {
        topic_b.inconsistent_topic_status().total_count == 1
    });
    wait_until("INCONSISTENT_TOPIC of the writer", || {
        topic_a.inconsistent_topic_status().total_count_change == 1
    });
    // Read: the change count is reset.
    let s = topic_a.inconsistent_topic_status();
    assert_eq!((s.total_count, s.total_count_change), (1, 0));
    assert!(writer.matched_subscriptions().is_empty());
}

#[test]
fn types_that_are_not_assignable_do_not_match_locally() {
    // In one participant, every registered type is known: a Shape reader
    // is not assignable from a Greeting writer (FINAL types with other
    // members), whatever their names.
    let p = participant(54);
    p.register_type::<Greeting>().unwrap();
    p.register_type::<Shape>().unwrap();
    let writer = p
        .create_publisher()
        .unwrap()
        .create_datawriter(&p.create_topic::<Greeting>("xt").unwrap())
        .unwrap();
    let reader = p
        .create_subscriber()
        .unwrap()
        .create_datareader(&p.create_topic::<Shape>("xt").unwrap())
        .unwrap();
    assert!(writer.matched_subscriptions().is_empty());
    assert!(reader.matched_publications().is_empty());
}

#[test]
fn xcdr2_writers() {
    let (a, b) = (participant(55), participant(55));
    let writer = a
        .create_publisher()
        .unwrap()
        .create_datawriter_with_qos(
            &a.create_topic::<Greeting>("xt").unwrap(),
            DataWriterQos {
                data_representation: vec![data_representation::XCDR2],
                ..DataWriterQos::default()
            },
        )
        .unwrap();
    let reader = b
        .create_subscriber()
        .unwrap()
        .create_datareader_with_qos(
            &b.create_topic::<Greeting>("xt").unwrap(),
            reliable_reader(),
        )
        .unwrap();
    assert_eq!(exchange(&writer, &reader).len(), 3);
    // A reader that takes XCDR only does not match it.
    let xcdr_only = b
        .create_subscriber()
        .unwrap()
        .create_datareader_with_qos(
            &b.create_topic::<Greeting>("xt").unwrap(),
            DataReaderQos {
                data_representation: vec![data_representation::XCDR],
                ..DataReaderQos::default()
            },
        )
        .unwrap();
    wait_until("REQUESTED_INCOMPATIBLE_QOS", || {
        xcdr_only.requested_incompatible_qos_status().last_policy_id
            == Some(DATA_REPRESENTATION_QOS_POLICY_ID)
    });
    // An XML writer cannot be made.
    assert!(
        a.create_publisher()
            .unwrap()
            .create_datawriter_with_qos(
                &a.create_topic::<Greeting>("xt").unwrap(),
                DataWriterQos {
                    data_representation: vec![data_representation::XML],
                    ..DataWriterQos::default()
                },
            )
            .is_err()
    );
}

#[test]
fn other_versions_of_a_type_come_by_type_lookup() {
    let (a, b) = (participant(56), participant(56));
    a.register_type::<v1::evolve::Mutable>().unwrap();
    b.register_type::<v2::evolve::Mutable>().unwrap();
    let writer = a
        .create_publisher()
        .unwrap()
        .create_datawriter_with_qos(
            &a.create_topic::<v1::evolve::Mutable>("xt").unwrap(),
            DataWriterQos {
                data_representation: vec![data_representation::XCDR2],
                ..DataWriterQos::default()
            },
        )
        .unwrap();
    let reader = b
        .create_subscriber()
        .unwrap()
        .create_datareader_with_qos(
            &b.create_topic::<v2::evolve::Mutable>("xt").unwrap(),
            reliable_reader(),
        )
        .unwrap();
    assert!(writer.wait_for_subscriptions(1, WAIT));
    assert!(reader.wait_for_publications(1, WAIT));
    // Each side asked the other for its TypeObject to check it.
    assert!(a.learnt_types() >= 1);
    assert!(b.learnt_types() >= 1);
    writer
        .write(&v1::evolve::Mutable {
            number: 7,
            text: "seven".into(),
        })
        .unwrap();
    let mut got = Vec::new();
    wait_until("the sample", || {
        got.extend(reader.take().into_iter().filter_map(|s| s.data));
        !got.is_empty()
    });
    // The member version 1 does not have takes its default.
    assert_eq!(
        got[0],
        v2::evolve::Mutable {
            extra: 0,
            number: 7,
            text: "seven".into(),
        }
    );
}

#[test]
fn types_that_come_by_type_lookup_can_be_inconsistent() {
    // An APPENDABLE writer and a MUTABLE reader: not assignable, which
    // each side finds once it has the other's TypeObject.
    let (a, b) = (participant(57), participant(57));
    a.register_type::<v1::evolve::Appendable>().unwrap();
    b.register_type::<v1::evolve::Mutable>().unwrap();
    let topic_a = a.create_topic::<v1::evolve::Appendable>("xt").unwrap();
    let topic_b = b.create_topic::<v1::evolve::Mutable>("xt").unwrap();
    let writer = a
        .create_publisher()
        .unwrap()
        .create_datawriter(&topic_a)
        .unwrap();
    let _reader = b
        .create_subscriber()
        .unwrap()
        .create_datareader(&topic_b)
        .unwrap();
    wait_until("INCONSISTENT_TOPIC", || {
        topic_b.inconsistent_topic_status().total_count == 1
    });
    wait_until("INCONSISTENT_TOPIC of the writer", || {
        topic_a.inconsistent_topic_status().total_count == 1
    });
    assert!(writer.matched_subscriptions().is_empty());
    assert!(a.learnt_types() >= 1 && b.learnt_types() >= 1);
}

/// A reader in a program without the type: it gets the type of the
/// writer's topic by TypeLookup and reads DynamicData, instances
/// included.
#[test]
fn dynamic_data_readers_read_types_they_discover() {
    let (a, b) = (participant(58), participant(58));
    a.register_type::<Shape>().unwrap();
    let writer = a
        .create_publisher()
        .unwrap()
        .create_datawriter_with_qos(
            &a.create_topic::<Shape>("dyn").unwrap(),
            DataWriterQos {
                data_representation: vec![data_representation::XCDR2],
                ..DataWriterQos::default()
            },
        )
        .unwrap();
    let ty = b
        .discover_topic_type("dyn", WAIT)
        .unwrap()
        .expect("the type of the topic");
    assert_eq!(ty.get_name(), "Keyed::Shape");
    assert!(ty.is_keyed());
    let topic = b.create_dynamic_topic("dyn", &ty).unwrap();
    assert_eq!(topic.type_name(), "Keyed::Shape");
    let reader = b
        .create_subscriber()
        .unwrap()
        .create_datareader_with_qos(&topic, reliable_reader())
        .unwrap();
    assert!(writer.wait_for_subscriptions(1, WAIT));
    assert!(reader.wait_for_publications(1, WAIT));
    let shape = |id| Shape {
        id,
        index: 10 + id,
        message: format!("shape {id}"),
    };
    writer.write(&shape(1)).unwrap();
    writer.write(&shape(2)).unwrap();
    writer.dispose(&shape(1)).unwrap();
    let mut got = Vec::new();
    wait_until("the samples", || {
        got.extend(reader.take());
        got.len() == 3
    });
    let data: Vec<&DynamicData> = got.iter().filter_map(|s| s.data.as_ref()).collect();
    assert_eq!(data.len(), 2);
    let (id, index, message) = (
        data[0].get_member_id_by_name("id"),
        data[0].get_member_id_by_name("index"),
        data[0].get_member_id_by_name("message"),
    );
    assert_eq!(data[1].get_uint32_value(id).unwrap(), 2);
    assert_eq!(data[1].get_uint32_value(index).unwrap(), 12);
    assert_eq!(data[1].get_string_value(message).unwrap(), "shape 2");
    // The dispose is of the instance of the first sample.
    assert!(got[2].data.is_none());
    assert_eq!(got[2].info.instance_handle, got[0].info.instance_handle);
    assert_ne!(got[1].info.instance_handle, got[0].info.instance_handle);
    assert_eq!(
        reader.lookup_instance(data[0]),
        Some(got[0].info.instance_handle)
    );
}

/// A DynamicData writer of a type made at run time, read by a program
/// built with an assignable type.
#[test]
fn dynamic_data_writers_write_to_typed_readers() {
    use rustorb_dds::DescribeType;
    let (a, b) = (participant(59), participant(59));
    // Version 1 written dynamically, version 2 read.
    let ty = DynamicType::from(<v1::evolve::Appendable as DescribeType>::describe_type());
    let topic = a.create_dynamic_topic("dyn", &ty).unwrap();
    let writer = a
        .create_publisher()
        .unwrap()
        .create_datawriter_with_qos(
            &topic,
            DataWriterQos {
                data_representation: vec![data_representation::XCDR2],
                ..DataWriterQos::default()
            },
        )
        .unwrap();
    b.register_type::<v2::evolve::Appendable>().unwrap();
    let reader = b
        .create_subscriber()
        .unwrap()
        .create_datareader_with_qos(
            &b.create_topic::<v2::evolve::Appendable>("dyn").unwrap(),
            reliable_reader(),
        )
        .unwrap();
    assert!(writer.wait_for_subscriptions(1, WAIT));
    assert!(reader.wait_for_publications(1, WAIT));
    let mut sample = DynamicDataFactory::create_data(&ty);
    sample
        .set_int32_value(sample.get_member_id_by_name("number"), 5)
        .unwrap();
    sample
        .set_string_value(sample.get_member_id_by_name("text"), "five")
        .unwrap();
    writer.write(&sample).unwrap();
    // A sample of another type is refused.
    let other = DynamicType::from(<v2::evolve::Appendable as DescribeType>::describe_type());
    assert!(
        writer
            .write(&DynamicDataFactory::create_data(&other))
            .is_err()
    );
    let mut got = Vec::new();
    wait_until("the sample", || {
        got.extend(reader.take().into_iter().filter_map(|s| s.data));
        !got.is_empty()
    });
    assert_eq!(
        got[0],
        v2::evolve::Appendable {
            number: 5,
            text: "five".into(),
            extra: 0,
        }
    );
}

/// A recursive type (a tree), written with DynamicData and read by a
/// participant that learns the type by TypeLookup: the types of a
/// strongly connected component (DDS-XTypes 1.3, 7.3.4.9) come together.
#[test]
fn recursive_types_come_by_type_lookup() {
    use rustorb_xtypes::types::*;
    let (a, b) = (participant(60), participant(60));
    let fwd = Type::forward("Tree::Node");
    let node = Type::from(StructType {
        name: "Tree::Node".into(),
        members: vec![
            StructMember::new("value", 0, Type::Primitive(Primitive::Int32)),
            StructMember::new("children", 1, Type::sequence(fwd.clone())),
        ],
        ..StructType::default()
    });
    fwd.define(node.clone()).unwrap();
    let ty = DynamicType::from(node);
    let writer = a
        .create_publisher()
        .unwrap()
        .create_datawriter_with_qos(
            &a.create_dynamic_topic("tree", &ty).unwrap(),
            DataWriterQos {
                data_representation: vec![data_representation::XCDR2],
                ..DataWriterQos::default()
            },
        )
        .unwrap();
    let found = b
        .discover_topic_type("tree", WAIT)
        .unwrap()
        .expect("the type of the topic");
    assert!(found.equals(&ty));
    let reader = b
        .create_subscriber()
        .unwrap()
        .create_datareader_with_qos(
            &b.create_dynamic_topic("tree", &found).unwrap(),
            reliable_reader(),
        )
        .unwrap();
    assert!(writer.wait_for_subscriptions(1, WAIT));
    assert!(reader.wait_for_publications(1, WAIT));
    let mut sample = DynamicDataFactory::create_data(&ty);
    sample.set_int32_value(0, 1).unwrap();
    let child = sample.loan_value(1).unwrap().loan_value(0).unwrap();
    child.set_int32_value(0, 2).unwrap();
    child
        .loan_value(1)
        .unwrap()
        .loan_value(0)
        .unwrap()
        .set_int32_value(0, 3)
        .unwrap();
    writer.write(&sample).unwrap();
    let mut got = Vec::new();
    wait_until("the sample", || {
        got.extend(reader.take().into_iter().filter_map(|s| s.data));
        !got.is_empty()
    });
    assert_eq!(got[0], sample);
}