Here we will show how to write data to a data feed using MeRMaID::support.
The first thing that has to be done in order to enable a service to write data to a data feed is to describe the type of data being written. This is done by filling in the data description file. The data description file consists of a series of data-structure
items, each describing a different data structure. Each data-structure
is composed by:
data-name
: the name of the data structuredata-composition
: the description of how the data structure is composed. This is further composed by:data-item
: description of each item that is part of the data structure. This is composed by:name
: name of the item inside the data structure. Each name has to be unique.type
: the type of the data item. This can be any of the following: integer
, double
or string
For our example we will just want to write an integer named "a"
to the data feed. We shall call this data structure as data-producer-output
. The data description file for this data type is as follows:
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE data-description-file SYSTEM "data-description-file.dtd"> <data-description-file> <data-structure> <data-name>data-producer-output</data-name> <data-composition> <data-item> <name>a</name> <type>integer</type> </data-item> </data-composition> </data-structure> </data-description-file>
After specifying the data structure that will be written, we have to declare that our Service will have a new data feed. This is done in the service type description file. We will have to add a data-feed
item to our service type description. This data-feed
item is composed of:
name
: name of the data feedcomment
: description of what the data feed is used forout-data
: name of the data structure that will be written to the data feedIn our example, we will name the data feed simply as "testDataFeed"
and the ooutput data name is the same as the one created in the data type description file: "data-producer-output"
. The service type description file now looks like this:
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE service-type-description-list SYSTEM "service-type-description-list.dtd"> <service-type-description-list> <service-type-description> <name>DataProducer</name> <comment>This is an example Service that has a Data Feed</comment> <data-feed> <name>testDataFeed</name> <comment>This data feed sends random data</comment> <out-data>data-producer-output</out-data> </data-feed> </service-type-description> </service-type-description-list>
With the changes in the description files done, now we have to change the code of our Service. For this example we will use the integer available in a variable named count_
which we increment each time we read it.
MeRMaID::support uses a special type of data container to carry data around. It is the mermaid::support::data::DataBox
. The DataBox has the ability to carry several data items of diferent types. DataBoxes are built according to what has been specified in the data structure description file. This way, DataBoxes can only hold data for which they were built, trying to put anything else in the box simply "won't fit". The items inside the box are catalogued, so they can be accessed by name.
In order to write data to a DataFeed, the first thing to do is to get a suitable DataBox. We can get a suitable DataBox for a specific data feed by using the mermaid::support::service::Service::getDataBoxForDataFeed()
method. It takes as a parameter the name of the data feed for which a DataBox is needed. After getting the DataBox, it should be filled with the data that is intended to be written to the data feed.
In our DataProducer
example, all of this is translated in the following piece of code:
CountedPtr<DataBox> db = getDataBoxForDataFeed("testDataFeed"); db->setInteger("a", count_++);
When the DataBox is ready to be written to the data feed, it is just a matter of calling the mermaid::support::service::Service::sendToDataFeed()
method, with the data feed name and the DataBox as parameters.
In our example this translates to:
sendToDataFeed("testDataFeed", db);
The full code for this tutorial is available in the DataProducer
example Service
.