Working with subscribers.
More...
Working with subscribers.
Setting up a subscriber
- Create a Subscriber
SubscriberQos subscriber_qos = new SubscriberQos();
SubscriberListener subscriber_listener = null;
try {
participant.get_default_subscriber_qos(subscriber_qos);
} catch (DDS.Exception) {
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
- Set up to handle the DATA_ON_READERS_STATUS status, in one or both of the following two ways.
Access received data via a subscriber
- 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);
} catch (DDS.Exception) {
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();
} catch (DDS.Exception) {
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:
- Indicate that data will be accessed via the subscriber:
try {
subscriber.begin_access();
} catch (DDS.Exception) {
}
- Indicate that the data access via the subscriber is done:
try {
subscriber.end_access();
} catch (DDS.Exception) {
}
Tearing down a subscriber
- Delete Subscriber:
try {
participant.delete_subscriber(ref subscriber);
} catch (DDS.Exception) {
Console.WriteLine(
"***Error: failed to delete subscriber");
}