Working with subscribers.
More...
Working with subscribers.
Setting up a subscriber
- Set up participant
- Create a Subscriber
SubscriberQos subscriber_qos = new SubscriberQos();
SubscriberListener subscriber_listener = null;
try {
participant.get_default_subscriber_qos(subscriber_qos);
Console.WriteLine(
"***Error: failed to get default subscriber qos");
}
Subscriber subscriber = participant.create_subscriber(
subscriber_qos,
subscriber_listener,
if (subscriber == null) {
Console.WriteLine(
"***Error: failed to create subscriber");
}
Set up subscriber to access received data
Access received data via a subscriber
- Ensure subscriber is set up to access received data
- Get the list of readers that have data samples available:
DataReaderSeq reader_seq = new DataReaderSeq();
int max_samples = ResourceLimitsQosPolicy.LENGTH_UNLIMITED;
SampleStateKind sample_state_mask =
SampleStateKind.ANY_SAMPLE_STATE;
ViewStateKind view_state_mask = ViewStateKind.ANY_VIEW_STATE;
InstanceStateKind instance_state_mask =
InstanceStateKind.ANY_INSTANCE_STATE;
try {
subscriber.get_datareaders(
reader_seq,
sample_state_mask,
view_state_mask,
instance_state_mask);
Console.WriteLine(
"***Error: failed to access received data via subscriber");
return;
}
- Upon successfully getting the list of readers with data, process the data readers to either:
If the intent is to access the data coherently or in order, the list of data readers must be processed in the order returned:
for (int i = 0; i < reader_seq.length; ++i) {
DataReader reader = reader_seq.get_at(i);
}
- Alternatively, call Subscriber.notify_datareaders() to invoke the DataReaderListener for each of the data readers.
try {
subscriber.notify_datareaders();
Console.WriteLine(
"***Error: failed to notify datareaders");
}
Access received data coherently and/or in order
To access the received data coherently and/or in an ordered manner, according to the settings of the PresentationQosPolicy attached to a Subscriber:
Tearing down a subscriber
- Delete Subscriber:
try {
participant.delete_subscriber(ref subscriber);
Console.WriteLine(
"***Error: failed to delete subscriber");
}