Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't set both a listener with StatusMask::all() and waitset #499

Open
tmayoff opened this issue Jul 18, 2024 · 2 comments
Open

Can't set both a listener with StatusMask::all() and waitset #499

tmayoff opened this issue Jul 18, 2024 · 2 comments

Comments

@tmayoff
Copy link

tmayoff commented Jul 18, 2024

Is it possible to have both a listener listening for all statuses and have a wait set waiting for just one. The WaitSet just times out when the listener has the all status, or a matching status. Should this be possible?

DataWriter setup:

template <typename T>
inline Writer<T>::Writer(const ::dds::pub::Publisher& publisher, const ::dds::topic::Topic<T>& topic, ::dds::pub::qos::DataWriterQos qos)
    // FIXME: Can't listen to all statuses here and create a WaitSet
    : listener_(std::make_shared<WriterListener<T>>()), writer_(publisher, topic, qos, listener_.get(), ::dds::core::status::StatusMask::all()) {}

Code with WaitSet:

template <typename T>
inline auto Writer<T>::WaitForSubscribers(int32_t number_of_subscriber, const ::dds::core::Duration& timeout) -> result_t<bool> {
  ::dds::core::cond::StatusCondition condition(writer_);
  condition.enabled_statuses(::dds::core::status::StatusMask::publication_matched());

  ::dds::core::cond::WaitSet ws;
  ws.attach_condition(condition);

  while (true) {
    auto result = ws.wait(timeout);
    if (result.empty()) {  // No publications
      continue;
    }

    const auto current_count = writer_.publication_matched_status().current_count();
    if (current_count >= number_of_subscriber) break;
  }

  ws.detach_condition(condition);
  return true;
}
@eboasson
Copy link
Contributor

The DDS spec has a very brief note on combining waitsets and listeners:

2.2.4.2.1 Changes in Plain Communication Status

The communication status is also reset to FALSE whenever the associated listener operation is called as the listener implicitly accesses the status which is passed as a parameter to the operation. The fact that the status is reset prior to calling the listener means that if the application calls the get_ from inside the listener it will see the status already reset.

An exception to this rule is when the associated listener is the ‘nil’ listener. As described in 2.2.4.3.1 the ‘nil’ listener is treaded as a NOOP and the act of calling the ‘nil’ listener does not reset the communication status.

(2.2.4.2.2 is similar for "read communication status")

2.2.4.6 Combination

Those two mechanisms may be combined in the application (e.g., using wait-sets and conditions to access the data and listeners to be warned asynchronously of erroneous communication statuses).

It is likely that the application will choose one or the other mechanism for each particular communication status (not both). However, if both mechanisms are enabled, then the listener mechanism is used first and then the WaitSet objects are signalled.

I think you're running into this. The listener gets invoked, therefore the status gets reset before the waitset gets a chance. Is that possible?

(The Cyclone C API allows controlling whether the status is reset on invocation, the C++ binding uses this internally, but doesn't expose it. I believe that also allows it to skimp a bit on the various masks and listener combinations in the spec that are too confusing for me. I'll try to find the spec's bits about all of those combinations if you need me to: it'll be a good thing for me try again to wrap my head around the definitions ...)

@tmayoff
Copy link
Author

tmayoff commented Jul 22, 2024

That sounds like it's what I'm seeing. Is there a way from C++ to avoid resetting the status (getting the C handles to call whatever relevant functions). Or since we're really only using most of the listener just for logging, can it be done without hitting this issue.

template <typename T>
class WriterListener : public ::dds::pub::DataWriterListener<T> {
 public:
  void on_offered_deadline_missed(::dds::pub::DataWriter<T>& writer, const ::dds::core::status::OfferedDeadlineMissedStatus& status) override;
  void on_offered_incompatible_qos(::dds::pub::DataWriter<T>& writer, const ::dds::core::status::OfferedIncompatibleQosStatus& status) override;
  void on_liveliness_lost(::dds::pub::DataWriter<T>& writer, const ::dds::core::status::LivelinessLostStatus& status) override;
  void on_publication_matched(::dds::pub::DataWriter<T>& writer, const ::dds::core::status::PublicationMatchedStatus& status) override;
};

template <typename T>
inline void WriterListener<T>::on_offered_deadline_missed(::dds::pub::DataWriter<T>& writer,
                                                          const ::dds::core::status::OfferedDeadlineMissedStatus&) {
  if (Log::Logger::GlobalLogger() != nullptr) {
    Log::Logger::GlobalLogger()->warning_tag({Log::TAGS::DDS}, "Writer Topic ({}): On Offered Deadline Missed", writer->topic_description().name());
  }
}

template <typename T>
inline void WriterListener<T>::on_offered_incompatible_qos(::dds::pub::DataWriter<T>& writer,
                                                           const ::dds::core::status::OfferedIncompatibleQosStatus& status) {
  if (Log::Logger::GlobalLogger() != nullptr) {
    auto policy = status->last_policy_id();
    Log::Logger::GlobalLogger()->error_tag({Log::TAGS::DDS}, "Writer Topic ({}): On Offered Incompatible QoS {}", writer->topic_description().name(),
                                           policy);
  }
}

template <typename T>
inline void WriterListener<T>::on_liveliness_lost(::dds::pub::DataWriter<T>& writer, const ::dds::core::status::LivelinessLostStatus& /*status*/) {
  if (Log::Logger::GlobalLogger() != nullptr) {
    Log::Logger::GlobalLogger()->trace_tag({Log::TAGS::DDS}, "Writer Topic ({}): On Liveliness lost", writer->topic_description().name());
  }
}

template <typename T>
inline void WriterListener<T>::on_publication_matched(::dds::pub::DataWriter<T>& writer,
                                                      const ::dds::core::status::PublicationMatchedStatus& status) {
  if (Log::Logger::GlobalLogger() != nullptr) {
    Log::Logger::GlobalLogger()->trace_tag({Log::TAGS::DDS}, "Writer Topic ({}): On Publication Matched (currently matched: {}, change: {})",
                                           writer->topic_description().name(), status.current_count(), status.current_count_change());
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants