Here we will show how to read data from a data feed using MeRMaID::support.
The first thing you should do is to create the handler that will process data received through the data feed. The handler is defined as a method of your service which has to follow the following C++ signature:
void(YourService*)(CountedPtr<DataBox> dataBox);
where "YourService" is the class name of the Service you're developing.
This means that you should create a method on your Service class which has one parameter (CountedPtr<DataBox> dataBox
) and which returns void
.
For instance, a suitable method for handling data received from the DataProducer
service (described in the Writing data to a data feed tutorial) would be declared as:
void processDataFeedInput(CountedPtr<DataBox> dataBox);
The handler will be called every time data is received through the data feed. This meens that reception is assynchronous. Concurrency problems within a Service
are avoided by MeRMaID::support as it assures that no other Service
code is running while the handler method is being executed. Please note that this means that if, for whatever reason, your code inside the handler blocks you will effectively hang the Service
and it will stop working.
Whenever the handler is called, you will receive a DataBox that is similar to the one which was built in the Writing data to a data feed tutorial. Therefore, to access the value that was stored in it, it is just a matter of doing:
int a = dataBox->getInteger("a");
At last, you should tell MeRMaID::support which data feed is the handler associated with. For this handler, we want to read the data feed provided by the DataProducer
Service
. We can do this by using the mermaid::support::service::Service::registerDataFeedInputHandler() method. In order to do this, we first have to store a pointer to the handler method in a DataFeedInputHandlerMethodBase
object. For our DataConsumer
example it looks like this:
CountedPtr<DataFeedInputHandlerMethodBase> dataFeedHandler(new DataFeedInputHandlerMethod<DataConsumer>(this, &DataConsumer::processDataFeedInput));
This code may seem a bit overly complicated, but it is necessary given several peculiar characteristics of C++. Please note that the object that we end up with is a pointer to a base class of the created DataFeedInputHandlerMethod
object. The DataFeedInputHandlerMethod
class is a template that is specialized with the class of the object pointed to by the constructor's first argument (in our case they are, respectively, DataConsumer
and this
.)
Having the pointer to the handler method, it's a matter of registering it for the desired Data Feed:
registerDataFeedInputHandler("TestEntity", "myDataProducer", "testDataFeed", dataFeedHandler);
The full code for this tutorial is available in the DataConsumer
example Service
.