DomainParticipants may optionally have Listeners. Listeners are essentially callback routines and are how Connext DDS will notify your application of specific events (changes in status) for entities Topics, Publishers, Subscribers, DataWriters, and DataReaders. Each Entity may have a Listener installed and enabled to process the events for itself and all of the sub-Entities created from it. If an Entity does not have a Listener installed or is not enabled to listen for a particular event, then Connext DDS will propagate the event to the Entity’s parent. If the parent Entity does not process the event, Connext DDS will continue to propagate the event up the object hierarchy until either a Listener is invoked or the event is dropped.
The DomainParticipantListener is the last chance that an event can be processed for the Entities descended from a DomainParticipant. The DomainParticipantListener is used only if an event is not handled by any of the Entities contained by the participant.
A Listener is typically set up when the DomainParticipant is created (see Creating a DomainParticipant). You can also set one up after creation time by using the set_listener() operation, as illustrated in Figure 5. The get_listener() operation can be used to retrieve the current DomainParticipantListener.
Figure 5 Setting up DomainParticipantListener
// MyDomainParticipantListener only handles PUBLICATION_MATCHED and // SUBSCRIPTION_MATCHED status for DomainParticipant Entities class MyDomainParticipantListener : public DDSDomainParticipantListener { public: virtual void on_publication_matched(DDSDataWriter *writer, const DDS_PublicationMatchedStatus &status); virtual void on_subscription_matched(DDSDataReader *reader, const DDS_SubscriptionMatchedStatus &status); }; void MyDomainParticipantListener::on_publication_matched( DDSDataWriter *writer, const DDS_PublicationMatchedStatus &status) { const char *name = writer->get_topic()->get_name(); printf(“Number of matching DataReaders for Topic %s is %d\n”, name, status.current_count); }; void MyDomainParticipantListener::on_subscription_matched( DDSDataReader *reader, const DDS_SubscriptionMatchedStatus &status) { const char *name = reader->get_topicdescription()->get_name(); printf(“Number of matching DataWriters for Topic %s is %d\n”, name, status.current_count); }; // Set up participant listener MyDomainParticipantListener* participant_listener = new MyDomainParticipantListener(); if (participant_listener == NULL) { // ... handle error } // Create the participant with a listener DDSDomainParticipant* participant = factory->create_participant( domain_id, participant_qos, participant_listener, DDS_PUBLICATION_MATCHED_STATUS | DDS_SUBSCRIPTION_MATCHED_STATUS ); if (participant == NULL) { // ... handle error }
If a Listener is set for a DomainParticipant, the Listener needs to exist as long as the DomainParticipant exists. It is unsafe to destroy the Listener while it is attached to a participant. However, you may remove the DomainParticipantListener from a DomainParticipant by calling set_listener() with a NULL value. Once the Listener has been removed from the participant, you may safely destroy it (see Types of Listeners).
Notes:
See also:
© 2017 RTI