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.
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
Nevermind, it was a Qt Creator build issue, nothing to do with DDS :)
Anyway thanks again for your answer !
Stephane
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.
Hi,
No. You do not need different
Listener
orbjects on each DDSDataReader
. You can install the sameDataReaderListener
object. This is the reason why theDataReaderListener
operation get passed theDataReader
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 DDSDataReaders
belonging to different DDSSubcriber
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 theListener
is installed belong to the sameSubscriber
then the DDS core guarantees mutual exclusion in the callback. i.e. DDS internally maintains a mutex-per-subscriber to ensure callbacks toListeners
associated with theSubscriber
(or itsDataReaders
) are serialized.Gerardo
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.
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:
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
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.