DataReaders¶
DataReaders enable the user access to the data received by a subscriber on a topic, and takes as a template parameter the data type being exchanged. The settings for the reader are either inherited from the subscriber, or explicitly set in its own QoS policies and listener:
Inherited from the subscriber:
dds_entity_t reader = dds_create_reader (subscriber, topic, NULL, NULL);
dds::sub::DataReader<DataType> reader(sub, topic);
reader = DataReader(subscriber, topic)
Explicitly set in its own QoS policies and listener:
dds_qos_t *qos = dds_create_qos ();
dds_listener_t *listener = dds_create_listener(NULL);
dds_lset_data_available(listener, data_available);
dds_entity_t reader = dds_create_reader (participant, topic, qos, listener);
dds::sub::qos::DataReaderQos rqos;
dds::sub::NoOpAnyDataReaderListener listener;
dds::sub::DataReader<DataType> reader(sub, topic, rqos, &listener, dds::core::status::StatusMask::data_available());
qos = Qos()
listener = MyListener()
reader = DataReader(participant, topic, qos=qos, listener=listener)
The data is accessed by either reading or taking the samples from the reader. Both return a container of samples , which have:
The received sample of the exchanged datatype accessed through data().
The metadata for the received sample accessed through info().
The metadata contains such information as:
Sample timestamp (time of writing).
Data validity (whether the call to data() will return anything that should be processed).
Sample state (READ/NOT_READ/…).
The difference between these two different access methods is the state of the reader after the access is finished.
The take operation only returns samples that have not yet been returned in a take operation,
int MAXSAMPLES 10;
void *samples[MAX_SAMPLES];
dds_sample_info_t infos[MAX_SAMPLES];
int samples_received = dds_take (reader, samples, info, MAX_SAMPLES, MAX_SAMPLES);
for (int i = 0; i < samples_received; i++) {
if (info[i].valid_data) {
/*print the data*/
}
}
auto samples = reader.take();
for (const auto & sample:samples) {
if (!sample.valid())
continue;
const auto &data = sample.data();
/*print the data*/
}
for sample in reader.take_iter(timeout=duration(milliseconds=10)):
print(sample)
The read operation returns all samples currently stored by the reader.
int MAXSAMPLES 10;
void *samples[MAX_SAMPLES];
dds_sample_info_t infos[MAX_SAMPLES];
int samples_received = dds_read (reader, samples, info, MAX_SAMPLES, MAX_SAMPLES);
for (int i = 0; i < samples_received; i++) {
if (info[i].valid_data) {
/*print the data*/
}
}
auto samples = reader.read();
for (const auto & sample:samples) {
if (!sample.valid() ||
sample.state() != dds::sub::status::SampleState::not_read())
continue;
const auto &data = sample.data();
/*print the data?*/
}
for sample in reader.read_iter(timeout=duration(milliseconds=10)):
print(sample)