RTI Connext C# API  6.1.2
XML application examples

How to define DDS applications in XML.

How to define DDS applications in XML.

Sections:

Example XML definition

The following is the configuration (ddsApplication.xml) that we will be using in the examples in this section:

<?xml version="1.0" encoding="UTF-8"?>
<dds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://community.rti.com/schema/current/rti_dds_profiles.xsd">
<types>
<struct name="Shape">
<member name="color" stringMaxLength="128" type="string" key="true"/>
<member name="x" type="int32"/>
<member name="y" type="int32"/>
<member name="shapesize" type="int32"/>
</struct>
</types>
<domain_library name="ExampleDomainLibrary">
<domain name="ExampleDomain" domain_id="0">
<!-- Register the type Shape and use its definition above -->
<register_type name="Shape" type_ref="Shape"/>
<!-- Register the type MyType but do not provide an XML definition. -->
<!-- The application will use a C# type -->
<register_type name="MyType"/>
<topic name="Square" register_type_ref="Shape"/>
<topic name="MyTypeTopic" register_type_ref="MyType"/>
</domain>
</domain_library>
<domain_participant_library name="ExampleParticipantLibrary">
<domain_participant name="ExamplePublicationParticipant"
domain_ref="ExampleDomainLibrary::ExampleDomain">
<publisher name="ExamplePublisher">
<data_writer name="ShapeWriter" topic_ref="Square"/>
</publisher>
</domain_participant>
<domain_participant name="ExampleSubscriptionParticipant"
domain_ref="ExampleDomainLibrary::ExampleDomain">
<data_reader name="ShapeReader" topic_ref="Square">
<content_filter name="ExampleTopic" kind="builtin.sql">
<expression>color='BLUE'</expression>
</content_filter>
</data_reader>
</domain_participant>
<domain_participant name="ExampleIdlParticipant"
domain_ref="ExampleDomainLibrary::ExampleDomain">
<publisher name="ExamplePublisher">
<data_writer name="MyTypeWriter" topic_ref="MyTypeTopic"/>
</publisher>
</domain_participant>
</domain_participant_library>
</dds>

Creating and looking up the DDS entities

Example: Create the participants defined in the XML file

var provider = new Rti.Dds.Core.QosProvider("ddsApplication.xml");
// Create the participants identified by the name we used in their
// XML definition. This operation also creates all the contained
// entities (topics, publishers, subscribers, readers, writers)
using var pubParticipant = provider.CreateParticipantFromConfig(
"ExampleParticipantLibrary::ExamplePublicationParticipant");
using var subParticipant = provider.CreateParticipantFromConfig(
"ExampleParticipantLibrary::ExampleSubscriptionParticipant");
Provides access to XML-defined QoS profiles, data types, and complete DDS systems.
Definition: QosProvider.cs:151
DomainParticipant CreateParticipantFromConfig(string configName)
Creates a DomainParticipant given its configuration name from a description provided in an XML config...
Contains infrastructure types.
Definition: AsyncWaitSetProperty.cs:18
Contains the RTI Connext DDS C# API.
Definition: AsyncWaitSetProperty.cs:18
Contains the RTI Connext C# API.
Definition: Logger.cs:20

Example: lookup the DataWriter and publish data

// Look up the writer using the name we used in its XML definition
//
// The type of ShapeWriter is DynamicData, because ShapeType is
// defined in the XML <types> tag and therefore created at runtime.
var writer = pubParticipant.LookupDataWriter<DynamicData>(
"ExamplePublisher::ShapeWriter");
DynamicData data = writer.CreateData();
data.SetValue("color", "RED");
data.SetValue("shapesize", 70);
data.SetValue("x", 10);
data.SetValue("y", -10);
writer.Write(data);

Example: lookup the DataReader and receive data

var reader = subParticipant.LookupDataReader<DynamicData>("ShapeReader");
reader.DataAvailable += _ =>
{
using var samples = reader.Take();
foreach (var sample in samples.ValidData())
{
Console.WriteLine(sample);
}
};
// ...

Using IDL-based types

The previous examples used DynamicData, but it is also possible to use strongly typed C# classes generated from IDL even if the readers and writers are defined in XML.

Example: Register an IDL type used by a writer or reader defined in XML

// Register specific types (a)
// The 'name' argument must correspond to the name used in
// <register_type name="MyType"/>
DomainParticipantFactory.Instance.RegisterTypeSupport<Example.MyType>(
name: "MyType");
// Or register a namespace within an assembly (b)
DomainParticipantFactory.Instance.RegisterNamespace(
typeof(Example.MyType).Assembly,
"Example");
// Whenever a type is referenced in the XML file, the QosProvider will
// now look for its definition among those that have been explicitly
// registered (a) or belong in a registered namespace (b)
var provider = new QosProvider("ddsApplication.xml");
using var participant = provider.CreateParticipantFromConfig(
"ExampleParticipantLibrary::ExampleIdlParticipant");
// Now we can look up a reader or a writer that uses Example.MyType
// instead of DynamicData
DataWriter<Example.MyType> writer =
participant.LookupDataWriter<Example.MyType>(
"ExamplePublisher::MyTypeWriter");
var sample = new Example.MyType { /* ... */ };
writer.Write(sample);
Console.WriteLine(writer.Topic.TypeName);
Example C# class generated from the IDL struct MyType
Definition: MyType.cs:135
Definition: MyType.cs:17