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:
- To the MyPublisher publisher, add a publishData operation:
- Right-click MyPublisher and select
- Open the Features window for the operation and name
it publishData
- 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.
- After you added the publishData operation to the MyPublisher
publisher, enter code to call it:
- Under the MyPublishingApplication domain participant,
open the Features window for the DefaultConfig configuration
- On the Initialization tab, in
the Initialization code field, add the following
code:
p_MyPublisher->publishData();
- Click OK
- Generate code for the MyPublishingApplication domain participant
and build it:
- To generate code: Select . If you are asked if you want to reload, click Yes.
- To build your application: Select