How to publish a topic and work with Publishers and DataWriters.
How to publish a topic and work with Publishers and DataWriters.
Sections:
See also Entity examples for examples that affect all DDS Entities, including Publishers and DataWriters.
Writing data and instances
Example: create a DataWriter using default QoS and publish a data sample
factory.CreateParticipant(domainId: 0);
participant.CreateTopic<Shape>("Square");
participant.ImplicitPublisher.CreateDataWriter(topic);
var shape = new Shape
{
color = "GREEN",
shapesize = 50,
x = 0,
y = 20
};
Singleton that manages the creation of DomainParticipant objects.
Definition: DomainParticipantFactory.cs:25
static DomainParticipantFactory Instance
Gets the singleton instance of this class.
Definition: DomainParticipantFactory.cs:39
Container for all other Entity objects.
Definition: DomainParticipant.cs:39
Allows an application to set the value of the data to be published under a given Topic<T>.
Definition: DataWriter.cs:29
void Write(T sample)
Modifies the value of a data instance.
The most basic description of the data to be published and subscribed.
Definition: Topic.cs:33
Contains DomainParticipant and related classes.
Definition: DomainParticipant.cs:33
Contains the classes to support publishing topics.
Definition: Namespaces.cs:74
Contains classes for defining topics.
Definition: Namespaces.cs:67
Contains the RTI Connext DDS C# API.
Definition: AsyncWaitSetProperty.cs:18
Contains the RTI Connext C# API.
Definition: Logger.cs:20
The IDL definition of the Shape
type used in the example is the following:
struct Shape {
@key string<128> color;
int32 x;
int32 y;
int32 shapesize;
};
};
For more information about IDL types, see IDL examples.
Example: registering and disposing an instance
var greenHandle = writer.RegisterInstance(new Shape { color = "GREEN" });
var shape = new Shape
{
color = "GREEN",
shapesize = 50,
x = 0,
y = 20
};
for (int i = 0; i < 100; i++)
{
shape.x = i;
writer.Write(shape, greenHandle);
}
writer.DisposeInstance(greenHandle);
Example: writing data with additional parameters
var greenHandle = writer.RegisterInstance(new Shape { color = "GREEN" });
var shape = new Shape
{
color = "GREEN",
shapesize = 50,
x = 0,
y = 20
};
writer.Write(shape, new WriteParams
{
InstanceHandle = greenHandle,
SourceTimestamp = Time.FromSeconds(1),
Flag = 1234
});
These examples from the rticonnextdds-examples GitHub repository show other advanced ways to write data:
Instance management is discussed in the Keys and Instances section of the Getting Started Guide, which explains how to use the following methods:
Configuring a Publisher
Example: create a publisher with a Partition QoS
var publisherQos = participant.DefaultPublisherQos
.WithPartition(p => p.Add("A"));
Publisher publisher = participant.CreatePublisher(publisherQos);
var writer1 = publisher.CreateDataWriter(topic1);
var writer2 = publisher.CreateDataWriter(topic2);
These examples from the rticonnextdds-examples GitHub repository demonstrate uses cases that involve a Publisher:
Looking up a previously created Entity
Example: lookup a previously-created DataWriter
var publisher = participant.CreatePublisher(
participant.DefaultPublisherQos.WithPublisherName("MyPublisher"));
DataWriter<Shape> writer1 = publisher.CreateDataWriter(topic1);
DataWriter<Shape> writer2 = publisher.CreateDataWriter(
topic2,
publisher.DefaultDataWriterQos.WithPublicationName("MyWriter2"));
AnyDataWriter lookupWriter1 = publisher.LookupDataWriter(topic1.Name);
Debug.Assert(writer1 == (DataWriter<Shape>) lookupWriter1);
AnyDataWriter lookupWriter2 = publisher.LookupDataWriter(topic2.Name);
Debug.Assert(writer2 == (DataWriter<Shape>) lookupWriter2);
lookupWriter2 = publisher.LookupDataWriterByName("MyWriter2");
Debug.Assert(writer2 == (DataWriter<Shape>) lookupWriter2);
lookupWriter2 = participant.LookupDataWriter("MyPublisher::MyWriter2");
Debug.Assert(writer2 == (DataWriter<Shape>) lookupWriter2);
foreach (AnyDataWriter writer in publisher.DataWriters)
{
IAnyTopic relatedTopic = writer.TopicUntyped;
Console.WriteLine($"DataWriter for topic {relatedTopic.Name} and type {relatedTopic.TypeName}");
}
foreach (Publisher pub in participant.Publishers)
{
string pubName = pub.Qos.PublisherName.Name ?? "(unnamed)";
Console.WriteLine($"Publisher: {pubName}");
foreach (AnyDataWriter writer in pub.DataWriters)
{
IAnyTopic relatedTopic = writer.TopicUntyped;
Console.WriteLine($"- DataWriter for topic {relatedTopic.Name} and type {relatedTopic.TypeName}");
}
}
@ Debug
The message contains debug information that might be relevant to your application.
Looking up a matched subscription
Example: lookup matched subscriptions
foreach (var subscription in writer.GetMatchedSubscriptionData())
{
string name = subscription.SubscriptionName.Name;
string typeName = subscription.TypeName;
}
See also Looking up matched publications and Accessing Information on Entity Discovery.