< 前へ | 次へ >

レッスン 12: トピックに関する更新を受信するためのコードを追加する

このレッスンでは、MyTopic トピック要素に関する更新を受信するためのコードを追加します。
注: publishData 操作のコードと同様に、以下のコードには DDS 実装固有のパーツが含まれています。 この例のコードは、このチュートリアルのソフトウェア要件である RTI Data Distribution Service 4.4d または 4.5d に関連しています。
  1. MyDataReaderListener クラスの MyTopic に関する更新を受信するには、on_data_available 操作の「フィーチャー」ウィンドウを開き (SubscriberPkg パッケージ下)、「実装」タブで以下のコードを追加して、「OK」をクリックします。
    注: このチュートリアル・トピックに合わせるために、次のコード・サンプルでは、2 番目の printf 行の終わりで使用された円記号 (¥) は、コードの次のセグメントがコードの前の行の一部であることを示します。 「実装」タブで入力する場合、2 番目の printf 行は、ar[2]); で終わる、コードの非常に長い 1 行です。
    MyTopicStructDataReader* myTopicStructDataReader =
    dynamic_cast<MyTopicStructDataReader*>(reader);
    if (myTopicStructDataReader != NULL)
    {
       DDS::SampleInfo info;
       DDS::ReturnCode_t retcode;
       MyTopicStruct myTopicStruct;
       for(;;)
       {
          MyTopicStruct_initialize(&myTopicStruct);
          retcode = myTopicStructDataReader->take_next_sample(myTopicStruct, info);
          if (retcode == DDS::RETCODE_NO_DATA)
          {
              break;
          }
          else if (retcode != DDS::RETCODE_OK)
          {
              printf("take_next_sample returned error %d\n", retcode);
              return;
          }
          if (info.valid_data)
          {
              //Copy the sequence elements to a local array.
              signed int ar[3];
              myTopicStruct.myLongSequenceAttribute.to_array(ar, 3);
              printf("myKeyAttribute: %ld, myLongAttribute: %ld, ¥
                 myBooleanAttribute: %d, myDoubleAttribute: %.3f, ¥
                 myStringAttribute: %s, myLongSequence: {%ld, %ld, %ld}\n", ¥
                 myTopicStruct.myKeyAttribute, myTopicStruct.myLongAttribute, ¥
                 myTopicStruct.myBooleanAttribute, ¥
                 myTopicStruct.myDoubleAttribute, myTopicStruct.myStringAttribute, ¥
                 ar[0], ar[1], ar[2]);
          }
          MyTopicStruct_finalize(&myTopicStruct);
       }
    } 
  2. MySubscribingApplication のコードを生成してビルドし、実行します。
    1. 「コード」 > 「生成」 > 「DefaultConfig」と選択します。
    2. 「コード」 > 「ビルド」 > 「BuildMySubscribingApplication.exe」と選択します。
    3. 「コード」 > 「MySubscribingApplication.exe の実行」と選択します。
      注: ファイアウォールをインストールしている場合、MySubscribingApplication.exe でインターネット接続の確立を試みるという警告が表示されることがあります。この試みに同意します。 MyPublishingApplication.exe についても同様にします。MyPublishingApplication.exe の最初の実行では、接続の承認要求があるため、パブリッシャーが送信したいくつかのメッセージが失われ、出力がこのレッスンで示したサンプル出力より小さくなる場合があります。
  3. アクティブ・コンポーネントを MyPublishingApplication に設定します。そのためには、PublishingPkg パッケージの下の DefaultConfig構成を右クリックして、「アクティブ構成として設定」を選択します。
  4. MyPublishingApplication を実行します。MySubscribingApplication の出力は、以下のようになります。
    注: このチュートリアル・トピックに合わせるために、以下のサンプル出力は、このレッスンの際に表示されるものと異なってフォーマットされます。サンプル出力は、各行の出力を判別できるように改行を挿入してフォーマットされています。この改行は、このチュートリアルでの出力には表示されません。各行は、myKeyAttribute で始まり、myLongSequence で終わります。
    ready to receive data
    myKeyAttribute: 1, myLongAttribute: 1, myBooleanAttribute: 1, 
    myDoubleAttribute: 1.000, myStringAttribute: message number 1, 
    myLongSequence: {1, 2, 3}
    myKeyAttribute: 2, myLongAttribute: 2, myBooleanAttribute: 0, 
    myDoubleAttribute: 0.500, myStringAttribute: message number 2, 
    myLongSequence: {2, 3, 4}
    myKeyAttribute: 3, myLongAttribute: 3, myBooleanAttribute: 1, 
    myDoubleAttribute: 0.333, myStringAttribute: message number 3, 
    myLongSequence: {3, 4, 5}
    myKeyAttribute: 4, myLongAttribute: 4, myBooleanAttribute: 0, 
    myDoubleAttribute: 0.250, myStringAttribute: message number 4, 
    myLongSequence: {4, 5, 6}
    myKeyAttribute: 5, myLongAttribute: 5, myBooleanAttribute: 1, 
    myDoubleAttribute: 0.200, myStringAttribute: message number 5, 
    myLongSequence: {5, 6, 7}
    myKeyAttribute: 6, myLongAttribute: 6, myBooleanAttribute: 0, 
    myDoubleAttribute: 0.167, myStringAttribute: message number 6, 
    myLongSequence: {6, 7, 8}
    myKeyAttribute: 7, myLongAttribute: 7, myBooleanAttribute: 1, 
    myDoubleAttribute: 0.143, myStringAttribute: message number 7, 
    myLongSequence: {7, 8, 9}
    myKeyAttribute: 8, myLongAttribute: 8, myBooleanAttribute: 0, 
    myDoubleAttribute: 0.125, myStringAttribute: message number 8, 
    myLongSequence: {8, 9, 10}
    myKeyAttribute: 9, myLongAttribute: 9, myBooleanAttribute: 1, 
    myDoubleAttribute: 0.111, myStringAttribute: message number 9, 
    myLongSequence: {9, 10, 11}
    myKeyAttribute: 10, myLongAttribute: 10, myBooleanAttribute: 0, 
    myDoubleAttribute: 0.100, myStringAttribute: message number 10, 
    myLongSequence: {10, 11, 12}

レッスンのチェックポイント

このレッスンでは、MyTopic トピック要素に関する更新を受信するためのコードを追加する方法について学習しました。
次のレッスンでは、コンテンツ・フィルター・トピックを使用して更新データをフィルターに掛けます。
< 前へ | 次へ >

フィードバック