Working with data readers.
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
= new MyReaderListener();
subscriber.get_default_datareader_qos(reader_qos);
FooDataReader reader = null;
try {
reader = (FooDataReader) subscriber.create_datareader(topic,
reader_qos,
reader_listener,
StatusKind.STATUS_MASK_ALL);
} catch (RETCODE_ERROR err) {
}
Managing instances
- Given a data reader
FooDataReader reader = ...;
- Getting an instance "key" value of user data type
Foo
Foo data = new Foo();
try {
reader.get_key_value(data, instance_handle);
} catch (RETCODE_ERROR err) {
}
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
Foo
. The samples are removed from the Service. The caller is responsible for deallocating the buffers. FooSeq data_seq = new FooSeq();
SampleInfoSeq info_seq = new SampleInfoSeq();
int max_samples;
int sample_state_mask = SampleStateMask.ANY_SAMPLE_STATE;
int view_state_mask = ViewStateMask.ANY_VIEW_STATE;
int instance_state_mask = InstanceStateMask.ANY_INSTANCE_STATE;
try {
reader.take(data_seq, info_seq,
max_samples,
sample_state_mask,
view_state_mask,
instance_state_mask);
} catch (RETCODE_ERROR) {
}
- Use the received data
for (int i = 0; i < data_seq.size(); ++i) {
}
- 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.
reader.return_loan(data_seq, info_seq);
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. FooSeq data_seq = new FooSeq();
SampleInfoSeq info_seq = new SampleInfoSeq)_;
int max_samples;
int sample_state_mask = SampleStateMask.ANY_SAMPLE_STATE;
int view_state_mask = ViewStateMask.ANY_VIEW_STATE;
int instance_state_mask = InstanceStateMask.ANY_INSTANCE_STATE;
try {
reader.read(data_seq, info_seq,
max_samples,
sample_state_mask,
view_state_mask,
instance_state_mask);
} catch (RETCODE_ERROR) {
}
- Use the received data
for (int i = 0; i < data_seq.size(); ++i) {
}
- Return the data samples and the information buffers back to the middleware
reader.return_loan(data_seq, info_seq);
Tearing down a data reader
- Delete DDSDataReader:
try {
subscriber.delete_datareader(reader);
} catch (RETCODE_ERROR err) {
}