Working with data readers.
More...
Working with data readers.
Setting up a data reader
- Set up subscriber
- Set up a topic
- Create a data reader, FooDataReader, of user data type
Foo:
DataReaderQos reader_qos = new DataReaderQos();
DataReaderListener reader_listener = null;
try {
subscriber.get_default_datareader_qos(reader_qos);
Console.WriteLine(
"***Error: failed to get default datareader qos");
}
DataReader reader = subscriber.create_datareader(
topic,
reader_qos,
reader_listener,
if (reader == null) {
Console.WriteLine("***Error: failed to create reader");
}
Managing instances
- Given a data reader
- Getting an instance "key" value of user data type
Foo
Set up reader to access received data
Access received data via a reader
Taking data
- Ensure reader is set up to access received data
- Take samples of user data type
T
. The samples are removed from the Service. The caller is responsible for deallocating the buffers. SampleInfoSeq info_seq = new SampleInfoSeq();
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 {
reader.
take(data_seq, info_seq,
max_samples,
sample_state_mask,
view_state_mask,
instance_state_mask);
} catch (Retcode_NoData) {
return;
Console.WriteLine(
"***Error: failed to access data from the reader");
}
- Use the received data
for (
int i = 0; i < data_seq.
length; ++i) {
SampleInfo info = info_seq.get_at(i);
if (!info.valid_data) {
continue;
}
Console.WriteLine(
" Data = (%d, %d) sample_state = %d",
data.x, data.y, info.sample_state);
}
- Return the data samples and the information buffers back to the middleware. IMPORTANT: Once this call returns, you must not retain any pointers to any part of any sample or sample info object.
try {
Console.WriteLine("***Error: failed to return loan");
}
Reading data
- Ensure reader is set up to access received data
- Read samples of user data type
Foo
. The samples are not removed from the Service. It remains responsible for deallocating the buffers. SampleInfoSeq info_seq = new SampleInfoSeq();
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 {
reader.
read(data_seq, info_seq,
max_samples,
sample_state_mask,
view_state_mask,
instance_state_mask);
} catch (Retcode_NoData) {
return;
Console.WriteLine(
"***Error: failed to access data from the reader");
}
- Use the received data
for (
int i = 0; i < data_seq.
length; ++i) {
SampleInfo info = info_seq.get_at(i);
if (!info.valid_data) {
continue;
}
Console.WriteLine(
" Data = (%d, %d) sample_state = %d",
data.x, data.y, info.sample_state);
}
- Return the data samples and the information buffers back to the middleware
try {
Console.WriteLine("***Error: failed to return loan");
}
Tearing down a data reader
- Delete DataReader:
try {
subscriber.delete_datareader(ref reader);
Console.WriteLine("***Error: failed to delete reader");
}