如何使用 RTI DDS 确定已删除 DataWriter 的 type_name

How to determine that type_name of a deleted DataWriter using RTI DDS

我正在使用 RTI DDS 5.2 在 c++ 中编写一个工具,它需要检测 DataWriters 何时被删除并知道相关数据的 type_name。我使用的代码类似于 this and this.

我正在使用 DDSWaitSet,当使用 delete_datawriter 删除 DataWriter 时会触发它,但 SampleInfo 指示数据无效并且确实如此,数据样本 type_name 为空。

有没有一种方法可以删除 DataWriter,从而使内置主题订阅获得 type_name?或者是否有我可以设置的 QOS 设置来修复此行为?

找到解决此问题的方法。我仍然不知道如何使数据样本在 DataWriter 消失时有效,但是 SampleInfo 确实有字段 instance_state 可以唯一标识编写器。解决方案是在数据有效时跟踪 type_names,在数据无效时查找它。这是我用来解决问题的代码的要点:

struct cmp_instance_handle {
    bool operator()(const DDS_InstanceHandle_t& a, const DDS_InstanceHandle_t& b) const {
        return !DDS_InstanceHandle_equals(&a, &b);
    }
};

void wait_for_data_writer_samples()
{
    ConditionSeq cs;
    DDSWaitSet* ws = new DDSWaitSet();
    StatusCondition* condition = _publication_dr->get_statuscondition();
    DDS_Duration_t timeout = {DDS_DURATION_INFINITE_SEC, DDS_DURATION_INFINITE_NSEC};
    std::map<DDS_InstanceHandle_t, std::string, cmp_instance_handle> instance_handle_map;

    ws->attach_condition(condition);
    condition->set_enabled_statuses(DDS_STATUS_MASK_ALL);

    while(true) {
        ws->wait(cs, timeout);

        PublicationBuiltinTopicDataSeq data_seq;
        SampleInfoSeq info_seq;

        _publication_dr->take(
            data_seq,
            info_seq,
            DDS_LENGTH_UNLIMITED,
            ANY_SAMPLE_STATE,
            ANY_VIEW_STATE,
            ANY_INSTANCE_STATE
        );

        int len = data_seq.length();
        for(int i = 0; i < len; ++i) {
            DDS_InstanceHandle_t instance_handle = info_seq[i].instance_handle;

            if(info_seq[i].valid_data) {
                std::string type_name(data_seq[i].type_name);

                // store the type_name in the map for future use
                instance_handle_map[instance_handle] = type_name;
                if(info_seq[i].instance_state == DDS_InstanceStateKind::DDS_ALIVE_INSTANCE_STATE) {
                    do_data_writer_alive_callback(type_name);
                }
                else {
                    // If the data is valid, but not DDS_ALIVE_INSTANCE_STATE, the DataWriter died *and* we can 
                    // directly access the type_name so we can handle that case here
                    do_data_writer_dead_callback(type_name);
                }
            }
            else {
                // If the data is not valid then the DataWriter is definitely not alive but we can't directly access
                // the type_name. Fortunately we can look it up in our map.
                do_data_writer_dead_callback(instance_handle_map[instance_handle]);

                // at this point the DataWriter is gone so we remove it from the map.
                instance_handle_map.erase(instance_handle);

            }
        }
        _publication_dr->return_loan(data_seq, info_seq);
    }
}