0

I'm writing a tool in c++ using RTI DDS 5.2 that needs to detect when DataWriters are deleted and know the type_name of the related data. I'm using code similar to this and this.

I'm using a DDSWaitSet and it is getting triggered when a DataWriter is deleted with delete_datawriter but the SampleInfo indicates that the data is not valid and sure enough, the data sample type_name is empty.

Is there a way to delete a DataWriter in such a was as to cause the built in topic subscription to get the type_name? Or is there a QOS setting I can set to fix this behavior?

Stephen
  • 2,613
  • 1
  • 24
  • 42

1 Answers1

0

Found a workaround for this problem. I still don't know how to make it so the data sample is valid when a DataWriter goes away, but the SampleInfo does have the field instance_state which uniquely identifies the writer. The solution is to keep track of type_names when the data is valid and just look it up when it's not. Here is the gist of the code I am using to solve the issue:

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);
    }
}
Stephen
  • 2,613
  • 1
  • 24
  • 42