Пример #1
0
  /**
   * 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;
  }
  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();
    }
  }