eclipse-cyclonedds / cyclonedds

Eclipse Cyclone DDS project
https://projects.eclipse.org/projects/iot.cyclonedds
Other
888 stars 363 forks source link

dds discovery #2108

Closed Gummum closed 1 month ago

Gummum commented 1 month ago

Can a publisher know how many subscriber subscribe to a topic? If so, could you please tell me where he did it? thank you!

eboasson commented 1 month ago

dds_get_matched_subscriptions(writer, NULL, 0) should do the trick if you have a writer; if you want to find out without having a writer, then you need to subscribe to the DCPSSubscription topic like

dds_entity_t reader = dds_create_reader (participant, DDS_BUILTIN_TOPIC_DCPSSUBSCRIPTION, NULL, NULL);

then use something like:

uint32_t count_readers (dds_entity_t reader, const char *topic_name)
{
  uint32_t count = 0;
  void *sampleptrs[10] = { NULL };
  dds_sample_info_t infos[10];
  int32_t n;
  do {
    n = dds_take (reader, sampleptrs, infos, 10, 10);
    for (int32_t i = 0; i < n; i++) {
      dds_builtintopic_endpoint_t const * const sample = sampleptrs[i];
      if (infos[i].valid_data &&
          infos[i].instance_state == DDS_ALIVE_INSTANCE_STATE && 
          strcmp (sample->topic_name, topic_name) == 0)
        count++;
    }
    (void) dds_return_loan (reader, sampleptrs, n);
  } while (n > 0);
  return count;
}
Gummum commented 1 month ago

thanks a lot!!