/**
   * Registers topic with QoS for test cases
   *
   * @param name topic name
   * @param typeName type for topic
   * @param reliable reliable (true) or best effort (false) QoS
   * @return DDS topic object
   * @throws IOException
   */
  public Topic registerTopic(final String name, final String typeName, final boolean reliable)
      throws IOException {
    TopicQos reliableTopicQos = new TopicQos();

    participant.get_default_topic_qos(reliableTopicQos);

    if (reliable) {
      reliableTopicQos.reliability.kind = ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS;
    } else {
      reliableTopicQos.reliability.kind = ReliabilityQosPolicyKind.BEST_EFFORT_RELIABILITY_QOS;
    }

    reliableTopicQos.history.kind = HistoryQosPolicyKind.KEEP_ALL_HISTORY_QOS;

    reliableTopicQos.resource_limits.max_samples = 50000;

    reliableTopicQos.durability.kind = DurabilityQosPolicyKind.VOLATILE_DURABILITY_QOS;

    participant.set_default_topic_qos(reliableTopicQos);

    Topic topic =
        participant.create_topic(
            name, typeName, reliableTopicQos, null, StatusKind.STATUS_MASK_NONE);

    if (topic == null) {
      throw new IOException("Could not create Topic");
    }

    return topic;
  }
  /**
   * Create publisher using default QoS
   *
   * @return DDS publisher
   * @throws IOException
   */
  public Publisher createPublisher() throws IOException {

    final Publisher publisher =
        participant.create_publisher(
            DomainParticipant.PUBLISHER_QOS_DEFAULT, null, StatusKind.STATUS_MASK_NONE);

    if (publisher == null) {
      throw new IOException("Could not create Publisher");
    }
    return publisher;
  }
  /**
   * Create subscriber with default QoS
   *
   * @return DDS Subscriber
   * @throws IOException
   */
  public Subscriber createSubscriber() throws IOException {

    final Subscriber subscriber =
        participant.create_subscriber(
            DomainParticipant.SUBSCRIBER_QOS_DEFAULT, null, StatusKind.STATUS_MASK_NONE);

    if (subscriber == null) {
      throw new IOException("Could not create Subscriber");
    }

    return subscriber;
  }
  /**
   * Create abstract data writer and check for errors
   *
   * @param publisher Publisher to build data writer from
   * @param topic Topic to write to
   * @return DDS DataWriter
   * @throws IOException
   */
  public DataWriter createWriter(final Publisher publisher, final Topic topic) throws IOException {

    DataWriterQos dataWriterQos;

    if (System.getProperty("dds.batch") != null) {

      int batchMicros = Integer.parseInt(System.getProperty("dds.batch"));

      dataWriterQos = new DataWriterQos();

      participant.get_default_datawriter_qos(dataWriterQos);

      dataWriterQos.batch.enable = true;

      dataWriterQos.batch.max_flush_delay.sec = 0; // = new Duration_t(0, 5000000);
      dataWriterQos.batch.max_flush_delay.nanosec = batchMicros * 1000;

      if (System.getProperty("dds.batch.data") != null) {
        dataWriterQos.batch.max_data_bytes = Integer.parseInt(System.getProperty("dds.batch.data"));
      }
      if (System.getProperty("dds.batch.samples") != null) {
        dataWriterQos.batch.max_samples = Integer.parseInt(System.getProperty("dds.batch.samples"));
      }

      dataWriterQos.history.kind = HistoryQosPolicyKind.KEEP_ALL_HISTORY_QOS;

      System.err.println("Flush delay: " + dataWriterQos.batch.max_flush_delay);

    } else {

      dataWriterQos = Publisher.DATAWRITER_QOS_USE_TOPIC_QOS;
    }

    final DataWriter parentWriter =
        publisher.create_datawriter(topic, dataWriterQos, null, StatusKind.STATUS_MASK_NONE);

    if (parentWriter == null) {
      throw new IOException("Could not create DataWriter");
    }

    return parentWriter;
  }
 /** Delete all entities, letting peers know you are disconnecting */
 public void shutdown() {
   participant.delete_contained_entities();
 }
  private static void subscriberMain(int domainId, int sampleCount) {

    DomainParticipant participant = null;
    Subscriber subscriber = null;
    Topic topic = null;
    DataReaderListener listener = null;
    DoubleArrayDataDataReader reader = null;

    try {

      // --- Create participant --- //

      /* To customize participant QoS, use
      the configuration file
      USER_QOS_PROFILES.xml */

      participant =
          DomainParticipantFactory.TheParticipantFactory.create_participant(
              domainId,
              DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
              null /* listener */,
              StatusKind.STATUS_MASK_NONE);
      if (participant == null) {
        System.err.println("create_participant error\n");
        return;
      }

      // --- Create subscriber --- //

      /* To customize subscriber QoS, use
      the configuration file USER_QOS_PROFILES.xml */

      subscriber =
          participant.create_subscriber(
              DomainParticipant.SUBSCRIBER_QOS_DEFAULT,
              null /* listener */,
              StatusKind.STATUS_MASK_NONE);
      if (subscriber == null) {
        System.err.println("create_subscriber error\n");
        return;
      }

      // --- Create topic --- //

      /* Register type before creating topic */
      String typeName = DoubleArrayDataTypeSupport.get_type_name();
      DoubleArrayDataTypeSupport.register_type(participant, typeName);

      /* To customize topic QoS, use
      the configuration file USER_QOS_PROFILES.xml */

      topic =
          participant.create_topic(
              "Example DoubleArrayData",
              typeName,
              DomainParticipant.TOPIC_QOS_DEFAULT,
              null /* listener */,
              StatusKind.STATUS_MASK_NONE);
      if (topic == null) {
        System.err.println("create_topic error\n");
        return;
      }

      // --- Create reader --- //

      listener = new DoubleArrayDataListener();

      /* To customize data reader QoS, use
      the configuration file USER_QOS_PROFILES.xml */

      reader =
          (DoubleArrayDataDataReader)
              subscriber.create_datareader(
                  topic, Subscriber.DATAREADER_QOS_DEFAULT, listener, StatusKind.STATUS_MASK_ALL);
      if (reader == null) {
        System.err.println("create_datareader error\n");
        return;
      }

      // --- Wait for data --- //

      final long receivePeriodSec = 4;

      for (int count = 0; (sampleCount == 0) || (count < sampleCount); ++count) {
        System.out.println(
            "DoubleArrayData subscriber sleeping for " + receivePeriodSec + " sec...");
        try {
          Thread.sleep(receivePeriodSec * 1000); // in millisec
        } catch (InterruptedException ix) {
          System.err.println("INTERRUPTED");
          break;
        }
      }
    } finally {

      // --- Shutdown --- //

      if (participant != null) {
        participant.delete_contained_entities();

        DomainParticipantFactory.TheParticipantFactory.delete_participant(participant);
      }
      /* RTI Data Distribution Service provides the finalize_instance()
      method for users who want to release memory used by the
      participant factory singleton. Uncomment the following block of
      code for clean destruction of the participant factory
      singleton. */
      // DomainParticipantFactory.finalize_instance();
    }
  }