DDS(Data Distribution Service)————PublisherListener(2)
4.PublisherListener
在 DDS(Data Distribution Service)中,PublisherListener
是一个接口类,允许应用程序对与发布者相关的事件进行响应。通过实现并注册一个 PublisherListener
,你可以处理发布者的各种状态变化和事件,例如数据写入、匹配的 DataReader 发生变化等。
4.1 创建Linstener(使用Qos实例)
DomainParticipant* particioant = DomainParticipantFactory::get_instance()->create_participant(0,PARTICIPANT_QOS_DEFAULT);
CustomPublisherListener cusListener;
/* 创建含有PublisherLinster(监听器) 的 publisher */
Publisher* pub1 = particioant->create_publisher(PUBLISHER_QOS_DEFAULT,&cusListener);
4.2 创建 Linstener(使用XML File)
DomainParticipantFactory::get_instance()->load_XML_profiles_file("profiles.xml");
DomainParticipant* participant =
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
/* Create a Publisher using a profile and no Listener */
Publisher* publisher_with_profile =
participant->create_publisher_with_profile("publisher_profile");
/* 创建含有linstener的publisher */
CustomPublisherListener custom_listener;
Publisher* publisher_with_profile_and_custom_listener =
participant->create_publisher_with_profile("publisher_profile", &custom_listener);
4.3 删除所属的实例,防止内存泄露
- 先删除Publisher所创建的实例
- 再删除对应的Publisher
// Delete the entities the Publisher created.
if (publisher->delete_contained_entities() != ReturnCode_t::RETCODE_OK)
{
// Publisher failed to delete the entities it created.
return;
}
// Delete the Publisher
if (participant->delete_publisher(publisher) != ReturnCode_t::RETCODE_OK)
{
// Error
return;
}