Multiple listeners issue

8 posts / 0 new
Last post
Offline
Last seen: 10 years 4 months ago
Joined: 04/10/2014
Posts: 6
Multiple listeners issue

Hello,

I'm currently using two Datawriters belonging to the same Publisher. Each Datawriter has its own topic to write on.

On the other side, I have two Datareaders belonging to the same Subscriber.

What I'd like to do is to set one listener per Datareader, so each Datareader can listen to one topic.

I assume this is possible, but probably because of my implementation I'm getting an error.

Basically, I created two listener classes ( myListener1 and myListener2)  inherithing from DDSDataReaderListener (where I implemented the  on_data_available() function), and in my subscriber_main() function I did something like :

    reader_listener_1 = new myListener1();

  reader_listener_2 = new myListener2();

    reader1=subscriber->create_datareader(topic1,datareader_qos,reader_listener_1,DDS_STATUS_MASK_ALL);

  reader2=subscriber->create_datareader(topic2,datareader_qos,reader_listener_2,DDS_STATUS_MASK_ALL);

 

So my questions are : Is my approach correct from a DDS point of view  ? If yes that must be an implementation error, if not what would be the most efficient way to listen to two different topics within my subscriber ?

Thanks a lot,

Stephane.

  

           

 

Keywords:
rip
rip's picture
Offline
Last seen: 2 days 23 hours ago
Joined: 04/06/2012
Posts: 324

Hi Stephane,

That looks (prima facie) correct.  What error are you getting?

One question I have is are the two Topics using the same data Type?  If not, I'm assuming that the error you are getting is Type related.

rip

Offline
Last seen: 10 years 4 months ago
Joined: 04/10/2014
Posts: 6

Nevermind, it was a Qt Creator build issue, nothing to do with DDS :)

Anyway thanks again for your answer !

Stephane

Offline
Last seen: 8 years 9 months ago
Joined: 07/22/2014
Posts: 9

Hi,

I had a question regarding mutliple listeners. Let's say i have more than 15 topics to listen to. Does that mean i need to create 15 listeners classes?

Im not using DDS dynamic data reader, im using typed data readers.

Please advise.

 

 

 

Gerardo Pardo's picture
Offline
Last seen: 1 month 1 week ago
Joined: 06/02/2010
Posts: 602

Hi,

No. You do not need different Listener orbjects on each DDS DataReader. You can install the same DataReaderListener object. This is the reason why the DataReaderListener operation get passed the DataReader as the first argument. That way the listener operations can distinghish what DataReader triggered the callback...

One thing to keep in mind is that DataReaderListener objects in DDS DataReaders belonging to different DDS Subcriber objects may be called concurrently from multiple threads so if you install the same Listener object you need to protect any shared internal state with a Mutex. If all the DataReaders where the Listener is installed belong to the same Subscriber then the DDS core guarantees mutual exclusion in the callback. i.e. DDS internally maintains a mutex-per-subscriber to ensure callbacks to Listeners associated with the Subscriber (or its DataReaders) are serialized.

Gerardo

 

Offline
Last seen: 8 years 9 months ago
Joined: 07/22/2014
Posts: 9

Hi Gerardo,

I got your points. My codes in Publisher & Subscriber are finally running. Can i seek your help to look through my subscriber code. Though it is already running, I would like to know whether any portion i can do better.

I will be adding 10 over more topics in future. 

File Attachments: 
Gerardo Pardo's picture
Offline
Last seen: 1 month 1 week ago
Joined: 06/02/2010
Posts: 602

Hi Jeslene,

There seems to be a logical error in your code. You have two main loops. These are the sections similar to what is shown below:

       // --- Wait for data --- //

        /* Main loop */
        const System.Int32 receive_period = 4000; // milliseconds
        for (int count=0;
             (sample_count == 0) || (count < sample_count);
             ++count) {
            ...
        }

You only need one of this at the end of the program after you have created all your DataReaders and installed all your listeners... Otherwise when your program gets there it will just spend all its time in that loop and never get to the instructions that follow. In your example it would never create the second Topic called  "Example Hums_Parameter_Info_T". Does this make sense?

Gerardo

Offline
Last seen: 8 years 9 months ago
Joined: 07/22/2014
Posts: 9

Gerardo,

It makes sense and i had make the changes. However, i encountered segmentation fault when running the J_subscriber. The only difference is that i had created many topics and listening to many topics.

Attached is my publisher and subscriber codes.

File Attachments: