3.2.2. DomainParticipantListener
DomainParticipantListener
is an abstract class defining the callbacks that will be triggered
in response to state changes on the DomainParticipant.
By default, all these callbacks are empty and do nothing.
The user should implement a specialization of this class overriding the callbacks
that are needed on the application.
Callbacks that are not overridden will maintain their empty implementation.
DomainParticipantListener inherits from TopicListener,
PublisherListener, and SubscriberListener.
Therefore, it has the ability to react to every kind of event that is
reported to any of its attached Entities.
Since events are always notified to the most specific Entity Listener that can handle the event,
callbacks that DomainParticipantListener inherits from other Listeners will only be called
if no other Entity was able to handle the event,
either because it has no Listener attached,
or because the callback is disabled by the StatusMask
on the Entity.
Additionally, DomainParticipantListener adds the following non-standard callbacks:
on_participant_discovery()
: A new DomainParticipant is discovered in the same domain, a previously known DomainParticipant has been removed, or some DomainParticipant has changed its QoS. This method provides an overload with an additional boolean output parameter so a discovery callback can tell the middleware if a newly discovered participant has to be ignored via the use of theignore_participant()
. This overload should be used when there is a need to ignore participants inside the discovery callback, since callingignore_participant()
inside the listener might deadlock. If both callbacks are implemented, the discovery callback with theshould_be_ignored
boolean flag takes precedence. The second discovery callback is only executed if the discovered DomainParticipant is not ignored in the first callback (should_be_ignored
parameter returnsfalse
).
on_data_reader_discovery()
: A new DataReader is discovered in the same domain, a previously known DataReader has been removed, or some DataReader has changed its QoS.
on_data_writer_discovery()
: A new DataWriter is discovered in the same domain, a previously known DataWriter has been removed, or some DataWriter has changed its QoS.
onParticipantAuthentication()
: Informs about the result of the authentication process of a remote DomainParticipant (either on failure or success).
Important
For more information about callbacks and its hierarchy, please refer to Listener.
class CustomDomainParticipantListener : public DomainParticipantListener
{
public:
CustomDomainParticipantListener()
: DomainParticipantListener()
{
}
virtual ~CustomDomainParticipantListener()
{
}
void on_participant_discovery(
DomainParticipant* participant,
eprosima::fastdds::rtps::ParticipantDiscoveryStatus status,
const ParticipantBuiltinTopicData& info,
bool& should_be_ignored) override
{
should_be_ignored = false;
if (status == eprosima::fastdds::rtps::ParticipantDiscoveryStatus::DISCOVERED_PARTICIPANT)
{
std::cout << "New participant discovered" << std::endl;
// The following line can be modified to evaluate whether the discovered participant should be ignored
// (usually based on fields present in the discovery information)
bool ignoring_condition = false;
if (ignoring_condition)
{
should_be_ignored = true; // Request the ignoring of the discovered participant
}
}
else if (status == eprosima::fastdds::rtps::ParticipantDiscoveryStatus::REMOVED_PARTICIPANT ||
status == eprosima::fastdds::rtps::ParticipantDiscoveryStatus::DROPPED_PARTICIPANT)
{
std::cout << "Participant lost" << std::endl;
}
}
#if HAVE_SECURITY
void onParticipantAuthentication(
DomainParticipant* participant,
eprosima::fastdds::rtps::ParticipantAuthenticationInfo&& info) override
{
if (info.status == eprosima::fastdds::rtps::ParticipantAuthenticationInfo::AUTHORIZED_PARTICIPANT)
{
std::cout << "A participant was authorized" << std::endl;
}
else if (info.status == eprosima::fastdds::rtps::ParticipantAuthenticationInfo::UNAUTHORIZED_PARTICIPANT)
{
std::cout << "A participant failed authorization" << std::endl;
}
}
#endif // if HAVE_SECURITY
void on_data_reader_discovery(
DomainParticipant* participant,
eprosima::fastdds::rtps::ReaderDiscoveryStatus reason,
const eprosima::fastdds::rtps::SubscriptionBuiltinTopicData& info,
bool& should_be_ignored) override
{
should_be_ignored = false;
if (reason == eprosima::fastdds::rtps::ReaderDiscoveryStatus::DISCOVERED_READER)
{
std::cout << "New datareader discovered" << std::endl;
// The following line can be modified to evaluate whether the discovered datareader should be ignored
// (usually based on fields present in the discovery information)
bool ignoring_condition = false;
if (ignoring_condition)
{
should_be_ignored = true; // Request the ignoring of the discovered datareader
}
}
else if (reason == eprosima::fastdds::rtps::ReaderDiscoveryStatus::REMOVED_READER)
{
std::cout << "Datareader lost" << std::endl;
}
}
void on_data_writer_discovery(
DomainParticipant* participant,
eprosima::fastdds::rtps::WriterDiscoveryStatus reason,
const eprosima::fastdds::dds::PublicationBuiltinTopicData& info,
bool& should_be_ignored) override
{
static_cast<void>(participant);
static_cast<void>(info);
should_be_ignored = false;
if (reason == eprosima::fastdds::rtps::WriterDiscoveryStatus::DISCOVERED_WRITER)
{
std::cout << "New datawriter discovered" << std::endl;
// The following line can be modified to evaluate whether the discovered datawriter should be ignored
// (usually based on fields present in the discovery information)
bool ignoring_condition = false;
if (ignoring_condition)
{
should_be_ignored = true; // Request the ignoring of the discovered datawriter
}
}
else if (reason == eprosima::fastdds::rtps::WriterDiscoveryStatus::REMOVED_WRITER)
{
std::cout << "Datawriter lost" << std::endl;
}
}
};