DataReaderListener not called

1 post / 0 new
Offline
Last seen: 2 weeks 4 days ago
Joined: 08/22/2019
Posts: 4
DataReaderListener not called

Hi,

I have successfully set up a publisher in a Java application, and its data is received by another participant. Registering the same participant that is publishing the data as a subscriber seems to be successful, too, but it seems my DataReaderListener is never called when something is published on the topic. Here's the code that is setting up the subscriber:

import com.rti.dds.domain.DomainParticipant;
import static com.rti.dds.domain.DomainParticipant.SUBSCRIBER_QOS_DEFAULT;
import com.rti.dds.infrastructure.StatusKind;
import com.rti.dds.subscription.DataReader;
import com.rti.dds.subscription.DataReaderAdapter;
import com.rti.dds.subscription.Subscriber;
import static com.rti.dds.subscription.Subscriber.DATAREADER_QOS_DEFAULT;
import com.rti.dds.topic.Topic;
import static java.util.Objects.requireNonNull;
import org.opentcs.components.Lifecycle;
import static org.opentcs.util.Assertions.checkState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyStatusSubscriber
    implements Lifecycle {

  private static final Logger LOG = LoggerFactory.getLogger(VehicleStatusSubscriber.class);
  private static final String TOPIC_NAME = "my_status";
  private final DomainParticipant domainParticipant;
  private Subscriber subscriber;
  private Topic myStatusTopic;
  private MyStatusDataReader myStatusDataReader;
  private StatusDataReaderListener myStatusDataReaderListener;
  private boolean initialized;

  public MyStatusSubscriber(DomainParticipant domainParticipant) {
    this.domainParticipant = requireNonNull(domainParticipant, "domainParticipant");
  }

  @Override
  public void initialize() {
    if (isInitialized()) {
      return;
    }

    subscriber = domainParticipant.create_subscriber(SUBSCRIBER_QOS_DEFAULT,
                                                     null,
                                                     StatusKind.STATUS_MASK_NONE);
    checkState(subscriber != null, "Subscriber creation failed.");

    MyStatusTypeSupport.register_type(domainParticipant, MyStatusTypeSupport.get_type_name());

    myStatusDataReader = createDataReader(subscriber);

    initialized = true;
  }

  @Override
  public void terminate() {
    if (!isInitialized()) {
      return;
    }

    subscriber.delete_datareader(myStatusDataReader);
    myStatusDataReader = null;
    myStatusDataReaderListener = null;

    domainParticipant.delete_subscriber(subscriber);
    subscriber = null;

    domainParticipant.delete_topic(myStatusTopic);

    initialized = false;
  }

  @Override
  public boolean isInitialized() {
    return initialized;
  }

  private MyStatusDataReader createDataReader(Subscriber subscriber) {
    myStatusTopic = domainParticipant.create_topic(TOPIC_NAME,
                                                   MyStatusTypeSupport.get_type_name(),
                                                   DomainParticipant.TOPIC_QOS_DEFAULT,
                                                   null,
                                                   StatusKind.STATUS_MASK_NONE);

    myStatusDataReaderListener = new StatusDataReaderListener();
    MyStatusDataReader dataReader
        = (MyStatusDataReader) subscriber.create_datareader(myStatusTopic,
                                                            DATAREADER_QOS_DEFAULT,
                                                            myStatusDataReaderListener,
                                                            StatusKind.STATUS_MASK_ALL);

    checkState(dataReader != null, "Could not create data reader for topic '" + myStatusTopic + "'");
    return dataReader;
  }

  private class StatusDataReaderListener
      extends DataReaderAdapter {

    @Override
    public void on_data_available(DataReader reader) {
      LOG.info("Data available.");

      // XXX Process data.
    }
  }
}

I have not been able to identify any apparent errors here, so far. I'm wondering what could be the problem, especially as the participant is the very same that successfully publishes messages. Any ideas/suggestions why on_data_available() is not called?

Best regards, Stefan