< Previous | Next >

Lesson 8: Add code in order to publish data to your topic

In this lesson, you add some code so that you can publish data to your MyTopic topic element.

In this lesson, you publish data to MyTopic by using MyPublisher::publishMyDataWriter. In order to publish data, you must initialize an instance of MyTopicStruct and complete its attributes. The code that is related to the struct initialization and that adds value to it is not standard DDS API code. It is specific to the DDS implementation you are using - RTI or OpenSplice. You base your code on the generated code from the DDSMyTopicStructLib.idl file.

To add the code so that you can publish data to your topic:

  1. To the MyPublisher publisher, add a publishData operation:
    1. Right-click MyPublisher and select Add New > Operation
    2. Open the Features window for the operation and name it publishData
  2. On the Implementation tab, enter the following code and click OK:
    char buff[32];
    for (int i = 1; i <= 10; ++i)
    {
    	OMDelay(1000);
    	MyTopicStruct myTopicStruct;
    #ifndef USE_OSPL_IMPLEMENTATION
    	MyTopicStruct_initialize(&myTopicStruct;);
    #endif
    	myTopicStruct.myKeyAttribute = i;
    	myTopicStruct.myLongAttribute = i;
    	myTopicStruct.myBooleanAttribute = (i % 2);
    	myTopicStruct.myDoubleAttribute = 1./i;
    	
    	//Initializing myStringAttribute
    	sprintf(buff, "message number %d", i);
    #ifdef USE_OSPL_IMPLEMENTATION
        myTopicStruct.myStringAttribute = DDS::string_dup(buff);
    #else //RTI
    	unsigned int buffLength = strlen(buff);
        if (!RTICdrType_copyString(myTopicStruct.myStringAttribute, buff, buffLength + 1)) 
        {
            return;
        }
    #endif
    
     	//Initializing myLongSequence
    #ifdef USE_OSPL_IMPLEMENTATION
     	myTopicStruct.myLongSequenceAttribute.length(3); 	
    	myTopicStruct.myLongSequenceAttribute[0] = i;
    	myTopicStruct.myLongSequenceAttribute[1] = i+1;
    	myTopicStruct.myLongSequenceAttribute[2] = i+2;
    #else //RTI
     	const signed int ar[3] = {i, i+1, i+2};
     	if (!myTopicStruct.myLongSequenceAttribute.from_array(ar, 3))
     	{
     		 return;
     	}
    #endif	
    	
    	//publish myTopicStruct
    	publishMyDataWriter(myTopicStruct);
    #ifndef USE_OSPL_IMPLEMENTATION
    	MyTopicStruct_finalize(&myTopicStruct;);
    #endif
    }

    To understand how to initialize MyTopicStruct with data, in the DefaultConfig folder under the MyTopicStructLib domain participant folder (for example, <Rational® Rhapsody® installation folder>\DDSProject3\MyTopicStructLib\DefaultConfig), open the DDSMyTopicStructLib.cxx and DDSMyTopicStructLib.h files with any text editor. These files were generated from the DDSMyTopicStructLib.idl file in the process of building MyTopicStructLib. By reviewing the DDSMyTopicStructLib.h file, you find the MyTopicStruct_initialize and MyTopicStruct_finalize functions that we use in our sample code for initializing and finalizing our instance of MyTopicStruct. The from_array function that initializes myLongSequenceAttribute from an array was discovered after doing some research about the definition of DDS_LongSeq, which is the type of myLongSequenceAttribute in the DDSMyTopicStructLib.h file. If you are using the RTI DDS implementation, then by viewing the MyTopicStruct_copy function in the DDSMyTopicStructLib.cxx file you find the call to the RTICdrType_copyString function that we use in our sample code for initializing the myStringAttribute attribute.

  3. After you added the publishData operation to the MyPublisher publisher, enter code to call it:
    1. Under the MyPublishingApplication domain participant, open the Features window for the DefaultConfig configuration
    2. On the Initialization tab, in the Initialization code field, add the following code:
      p_MyPublisher->publishData();
    3. Click OK
  4. Generate code for the MyPublishingApplication domain participant and build it:
    • To generate code: Select Code > Generate > DefaultConfig. If you are asked if you want to reload, click Yes.
    • To build your application: Select Code > Build > BuildMyPublishingApplication.exe

Lesson checkpoint

In this lesson, you learned how to add code in order to publish data to your MyTopic topic element.
In the next lesson, you will create a subscriber entity and a dataReader entity.
< Previous | Next >

Feedback