13.1.2. Statistics Domain Participant
为了开始收集某个统计主题(统计主题名称)中的数据,必须启用相应的统计 DataWriter。实际上,Fast DDS 统计模块可以在运行时启用和禁用。为此,Fast DDS 统计模块提供了一个扩展的 DDS DomainParticipant API:
- 启用统计 DataWriters
- 禁用统计 DataWriters
- 获取指向扩展 DomainParticipant 类的指针
示例
自动启用统计 DataWriters
13.1.2.1. Enable statistics DataWriters
可以通过不同方式启用统计 DataWriters。这可以自动完成(见“自动启用统计 DataWriters”)。或者,可以在运行时使用以下两种方法之一手动启用统计 DataWriters:enable_statistics_datawriter()
或 enable_statistics_datawriter_with_profile()
。
enable_statistics_datawriter()
方法需要以下参数:
- 要启用的统计主题名称(参见“Statistics Topic names”以获取主题列表)。
- DataWriter QoS 配置文件(参见“Statistics DataWriter recommended QoS”以获取推荐配置)。
可以通过 FASTDDS_DEFAULT_PROFILES_FILE 中的 DataWriter 配置文件定义特定所需 QoS(参见 XML profiles)。enable_statistics_datawriter_with_profile()
方法通过查找特定的 DataWriter XML 配置文件来启用一个 DataWriter。在这些配置文件中,可以设置特定的 QoS。
enable_statistics_datawriter_with_profile()
方法需要以下参数:
- 用于填充 DataWriter 的 QoS 结构的 XML 配置文件名称。
- 要启用的统计主题名称。(参见“Statistics Topic names”以获取主题列表)。
13.1.2.2. Disable statistics DataWriters
使用 disable_statistics_datawriter()
方法禁用统计 DataWriters。此方法需要以下参数:
- 要禁用的统计主题名称(参见“Statistics Topic names”以获取主题列表)。
13.1.2.3. Obtain pointer to the extended DomainParticipant class
DomainParticipant 是通过 create_participant()
方法由 DomainParticipantFactory 创建的。该方法返回创建的 DDS 标准 DomainParticipant 的指针。为了获得指向扩展 DDS API 的子类 DomainParticipant 的指针,提供了静态方法 narrow()
。
13.1.2.4. Example
以下示例展示了如何使用扩展 DDS API 的 Statistics 模块:
// 创建一个 DomainParticipant
DomainParticipant* participant =
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
if (nullptr == participant)
{
// 错误处理
return;
}
// 获取指向子类的指针
eprosima::fastdds::statistics::dds::DomainParticipant* statistics_participant =
eprosima::fastdds::statistics::dds::DomainParticipant::narrow(participant);
// 启用统计 DataWriter
if (statistics_participant->enable_statistics_datawriter(eprosima::fastdds::statistics::GAP_COUNT_TOPIC,
eprosima::fastdds::statistics::dds::STATISTICS_DATAWRITER_QOS) != RETCODE_OK)
{
// 错误处理
return;
}
// 使用 DomainParticipant 进行通信
// (...)
// 禁用统计 DataWriter
if (statistics_participant->disable_statistics_datawriter(eprosima::fastdds::statistics::GAP_COUNT_TOPIC) !=
RETCODE_OK)
{
// 错误处理
return;
}
// 删除 DomainParticipant
if (DomainParticipantFactory::get_instance()->delete_participant(participant) != RETCODE_OK)
{
// 错误处理
return;
}
以上代码演示了如何创建一个 DomainParticipant
、启用和禁用相应的统计数据写入器,以及最后删除参与者对象。这一过程体现了 Fast DDS 中扩展 API 的使用以及如何有效地管理数据通信。
内容由零声教学AI助手提供,问题来源于学员提问