How to publish and subscribe to topics for dynamically-defined types.
How to publish and subscribe to topics for dynamically-defined types.
In addition to IDL and code generation, data types can be defined dynamically in code and in XML using Rti.Types.Dynamic.DynamicType. Data for those types can be published and subscribed to using Rti.Types.Dynamic.DynamicData.
Example 1: define a type and publish a data sample
.
AddMember(
new StructMember(
"color", typeFactory.CreateString(bounds: 128), isKey:
true))
.
AddMember(
new StructMember(
"x", typeFactory.GetPrimitiveType<
int>()))
.
AddMember(
new StructMember(
"y", typeFactory.GetPrimitiveType<
int>()))
.
AddMember(
new StructMember(
"shapesize", typeFactory.GetPrimitiveType<
int>()))
Topic<DynamicData> topic = participant.CreateTopic(
name: "Square",
type: shapeType);
DataWriter<DynamicData> writer = participant.ImplicitPublisher
.CreateDataWriter(topic);
DynamicData sample = new DynamicData(shapeType);
DynamicData sample2 = writer.CreateData();
sample.SetValue(memberName: "color", value: "GREEN");
sample.SetValue(memberName: "shapesize", value: 40);
sample.SetValue(memberId: 2, value: 100);
sample.SetValue(memberId: 3, value: 100);
sample.SetAnyValue(memberName: "x", value: "17");
A factory for creating DynamicTypes.
Definition: DynamicTypeFactory.cs:21
StructBuilder BuildStruct()
Returns a StructBuilder that allows creating an StructType.
static DynamicTypeFactory Instance
A singleton for the factory.
Definition: DynamicTypeFactory.cs:25
StructType Create()
Creates a new StructType object using the current properties of the StructBuilder instance.
Definition: StructBuilder.cs:130
StructBuilder WithName(string name)
Returns the same StructBuilder instance with a modified Name.
Definition: StructBuilder.cs:65
StructBuilder AddMember(StructMember member)
Returns the same StructBuilder instance with a new StructMember added to Members.
Definition: StructBuilder.cs:98
Contains support for the dynamic definition and manipulation of topic-types.
Definition: Namespaces.cs:118
Support for topic-types.
Definition: Namespaces.cs:112
Contains the RTI Connext C# API.
Definition: Logger.cs:20
Example 2: load a type definition from XML and subscribe to it
static void ProcessData(AnyDataReader anyReader)
{
var reader = (DataReader<DynamicData>)anyReader;
using var samples = reader.Take();
foreach (var sample in samples)
{
if (sample.Info.ValidData)
{
DynamicData data = sample.Data;
string color = data.GetValue<string>(memberName: "color");
int shapesize = data.GetValue<int>(memberName: "shapesize");
var coordinates = (
data.GetValue<int>(memberId: 2),
data.GetValue<int>(memberId: 3)
);
object colorAsObject = data.GetAnyValue("color");
Console.WriteLine(data);
}
}
}
var provider = new QosProvider("Shape.xml");
DynamicType shapeType = provider.GetType("Shape");
Topic<DynamicData> topic = participant.CreateTopic(
name: "Square",
type: shapeType);
DataReader<DynamicData> reader =
participant.ImplicitSubscriber.CreateDataReader(
topic,
preEnableAction: reader => reader.DataAvailable += ProcessData);
The XML definition of the Shape type is:
<?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>
<module name="Example">
<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>
</module>
</types>
</dds>
The rticonnextdds-examples GitHub repository provides additional DynamicData examples