예제 #1
0
  /**
   * Creates abstract DataReader for subscriber and topic
   *
   * @param subscriber DDS subscriber to attach reader
   * @param topic Topic to read from
   * @return DDS DataReader
   * @throws IOException
   */
  public DataReader createReader(final Subscriber subscriber, final Topic topic)
      throws IOException {
    final DataReader parentReader =
        subscriber.create_datareader(
            topic, Subscriber.DATAREADER_QOS_USE_TOPIC_QOS, null, StatusKind.STATUS_MASK_NONE);

    if (parentReader == null) {
      throw new IOException("Could not create DataReader");
    }

    return parentReader;
  }
  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();
    }
  }