< 이전 | 다음 >

학습 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)
    		{   			
    //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);
    	}
    }
    #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로 끝납니다.
    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 토픽 요소에 대한 업데이트를 수신하도록 코드를 추가하는 방법에 대해 살펴보았습니다.
다음 학습에서는 컨텐츠 필터링된 토픽을 사용하여 업데이트된 데이터를 필터링합니다.
< 이전 | 다음 >

피드백