Is it possible to read a data before write to queue?

2 posts / 0 new
Last post
Offline
Last seen: 5 years 5 months ago
Joined: 03/11/2015
Posts: 1
Is it possible to read a data before write to queue?

Hi, I'm just trying to read or take a data  before write to queue.

 

static void on_data_available_callback(void *listener_data /* unused */, 
 DDS_DataReader* data_reader) {
 DDS_StringDataReader *string_reader = NULL;
 char sample[MAX_STRING_SIZE]; 
 struct DDS_SampleInfo info;
 DDS_ReturnCode_t retcode;
 int i=0;
string_reader = DDS_StringDataReader_narrow(data_reader);
 if (string_reader == NULL) {
 /* In this specific case, this will never fail */
 puts("DDS_StringDataReader_narrow failed.");
 return;
 }
 
 Sleep(1000);
 /* Loop until there are messages available in the queue */
 for(;;) {
 retcode = DDS_StringDataReader_read_next_sample(
 string_reader,
 sample,
 &info);
 
 if (retcode == DDS_RETCODE_NO_DATA) {
 /* No more samples */
 printf("Read a data : \n");
 printf("Empty Queue\n");
 Sleep(1000);
 //break;
 } else if (retcode != DDS_RETCODE_OK) {
 printf("Unable to take data from data reader, error %d\n", retcode);
 return;
 }
if(retcode != DDS_RETCODE_NO_DATA) {
 if (info.valid_data) {
 /* Data is valid (this isn't just a lifecycle sample): print it */
 printf("Read a data : \n");
 puts(sample);
 /* If empty string is received, clean shutdown*/
 if(strlen(sample) == 0){
 shutdown_flag = DDS_BOOLEAN_TRUE; 
 }
 }
 Sleep(2000);
 }
 }

 

I tried to start my publisher and my subscriber simultaneously. But, everytime, subscriber read a data when publisher write a data to queue only.

Is it possible to read a data before write? If then, what can I do?

Thanks

 Hyunji

Gerardo Pardo's picture
Offline
Last seen: 3 weeks 6 days ago
Joined: 06/02/2010
Posts: 592

Hello Hyunji,

I am a bit confused by your question. You asked "But, everytime, subscriber read a data when publisher write a data to queue only. Is it possible to read a data before write?"

For an application to read some data somebody had to write it first... It does not have to be the same application of course, it can be (and often is) a different application. But if nobody writes data then there is nothing to receive...

The code you posted has a few problems. The on_data_available_callback is the DataReaderListener installed on a DDS DataReader correct? If so, then it will only be called when the DataReader has received some data. So someone needs to be writing it.  Moreover the on_data_available_callback is called from inside an internal DDS thread. This is intended as a "short" notification that should never delay its return, block or "sleep" as you do in your code.  Sleeping or blocking while on this call will prevent the DDS middleware from receiving any other data.  Take a look at this best practice tip for additional background: https://community.rti.com/best-practices/never-block-listener-callback

Gerardo