< 上一個課程 | 下一個課程 >

第 12 課:新增程式碼以接收主題相關的更新項目

在這一課,您將新增程式碼以接收 MyTopic 主題元素的相關更新項目。
註:publishData 作業的程式碼相似,下列程式碼包含特定於 DDS 實作的部分。
  1. 若要接收 MyTopic 的相關更新項目,請對 MyDataReaderListener 類別的 on_data_available 作業(位於 SubscriberPkg 套件下)開啟「特性」視窗,然後在實作標籤上新增下列程式碼,然後按一下確定
    #ifdef USE_OSPL_IMPLEMENTATION
    MyTopicStructDataReader* myTopicStructDataReader = dynamic_cast<MyTopicStructDataReader*>(reader);
    if (myTopicStructDataReader != NULL)
    {
    	DDS::SampleInfoSeq info;
    	DDS::ReturnCode_t retcode;
    	MyTopicStructSeq_var myTopicStruct = new MyTopicStructSeq;	
    	for(;;)
    	{
    		//MyTopicStruct_initialize(&myTopicStruct);
    		//retcode = myTopicStructDataReader->take_next_sample(myTopicStruct, info);				
    		retcode = myTopicStructDataReader->take(myTopicStruct, info, 1, DDS::ANY_SAMPLE_STATE, DDS::ANY_VIEW_STATE, DDS::ANY_INSTANCE_STATE);
    		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[0].valid_data)
       		{   						
    			printf("myKeyAttribute: %ld, myLongAttribute: %ld, myBooleanAttribute: %d, myDoubleAttribute: %.3f, myStringAttribute: %s, myLongSequence: {%ld, %ld, %ld}\n", myTopicStruct[0].myKeyAttribute, myTopicStruct[0].myLongAttribute, myTopicStruct[0].myBooleanAttribute, myTopicStruct[0].myDoubleAttribute, myTopicStruct[0].myStringAttribute.m_ptr, myTopicStruct[0].myLongSequenceAttribute[0], myTopicStruct[0].myLongSequenceAttribute[1], myTopicStruct[0].myLongSequenceAttribute[2]);
    			break;
       		}		
       	}
    }
    #else //RTI
    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)
       		{   			
    			//將順序元素複製到本端陣列。
    			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);
       	}
    }
    #endif
  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 結束
    準備接收資料
    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 主題元素的相關更新項目。
在下一課,您將使用內容過濾主題過濾更新資料。
< 上一個課程 | 下一個課程 >

回饋