Enable new callback API

- OnData() channel data handler.
 - ConditionalRun() for devices without incoming data.
 - Header file with common main(), to be extended with getDevice/addCustomOptions.
 - Update examples (MQ/Tutorial3) to use the new API and config.
 - NewSimpleMessage() for simpler creation of small messages (additional copy).
 - Replace SetProperty/GetProperty with fConfig access.
 - Runtime configurable channel names for common devices.
 - Configurable logging interval per channel.
 - FairMQMultiplier for distributing same data to multiple outputs.
 - Cleanup state machine messages.
 - Cmd option to toggle signal handling.
 - Simpler API for send/receive timeouts.
 - Enable --log-to-file.
 - Fix coverity issues, warnings.
 - Various code cleanup and minor tweaks.
This commit is contained in:
Alexey Rybalchenko
2016-08-10 09:47:53 +02:00
committed by Mohammad Al-Turany
parent d5b98567db
commit da10c64800
53 changed files with 501 additions and 1263 deletions

View File

@@ -56,7 +56,7 @@ Set(DEPENDENCIES
${DEPENDENCIES}
FairMQ
${DDS_INTERCOM_LIBRARY_SHARED}
${DDS_PROTOCOL_LIBRARY_SHARED} # also link the two DDS dependency libraries to avoid linking issues on some osx systems
${DDS_PROTOCOL_LIBRARY_SHARED}
${DDS_USER_DEFAULTS_LIBRARY_SHARED}
)

View File

@@ -5,15 +5,6 @@
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* FairMQExample3Processor.cpp
*
* @since 2014-10-10
* @author A. Rybalchenko
*/
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include "FairMQExample3Processor.h"
#include "FairMQLogger.h"
@@ -22,37 +13,33 @@ using namespace std;
FairMQExample3Processor::FairMQExample3Processor()
{
OnData("data1", &FairMQExample3Processor::HandleData);
}
void FairMQExample3Processor::CustomCleanup(void *data, void *object)
bool FairMQExample3Processor::HandleData(FairMQMessagePtr& msg, int /*index*/)
{
delete static_cast<string*>(object);
}
LOG(INFO) << "Received data, processing...";
void FairMQExample3Processor::Run()
{
while (CheckCurrentState(RUNNING))
// Modify the received string
string* text = new std::string(static_cast<char*>(msg->GetData()), msg->GetSize());
*text += " (modified by " + fId + ")";
// create message object with a pointer to the data buffer,
// its size,
// custom deletion function (called when transfer is done),
// and pointer to the object managing the data buffer
FairMQMessagePtr msg2(NewMessage(const_cast<char*>(text->c_str()),
text->length(),
[](void* /*data*/, void* object) { delete static_cast<string*>(object); },
text));
// Send out the output message
if (Send(msg2, "data2") < 0)
{
// Create empty message to hold the input
unique_ptr<FairMQMessage> input(NewMessage());
// Receive the message (blocks until received or interrupted (e.g. by state change)).
// Returns size of the received message or -1 if interrupted.
if (Receive(input, "data1") >= 0)
{
LOG(INFO) << "Received data, processing...";
// Modify the received string
string* text = new string(static_cast<char*>(input->GetData()), input->GetSize());
*text += " (modified by " + fId + ")";
// Create output message
unique_ptr<FairMQMessage> msg(NewMessage(const_cast<char*>(text->c_str()), text->length(), CustomCleanup, text));
// Send out the output message
Send(msg, "data2");
}
return false;
}
return true;
}
FairMQExample3Processor::~FairMQExample3Processor()

View File

@@ -5,17 +5,9 @@
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* FairMQExample3Processor.h
*
* @since 2014-10-10
* @author A. Rybalchenko
*/
#ifndef FAIRMQEXAMPLE3PROCESSOR_H_
#define FAIRMQEXAMPLE3PROCESSOR_H_
#include <string>
#ifndef FAIRMQEXAMPLE2PROCESSOR_H_
#define FAIRMQEXAMPLE2PROCESSOR_H_
#include "FairMQDevice.h"
@@ -25,10 +17,8 @@ class FairMQExample3Processor : public FairMQDevice
FairMQExample3Processor();
virtual ~FairMQExample3Processor();
static void CustomCleanup(void* data, void* hint);
protected:
virtual void Run();
bool HandleData(FairMQMessagePtr&, int);
};
#endif /* FAIRMQEXAMPLE3PROCESSOR_H_ */

View File

@@ -12,11 +12,12 @@
* @author A. Rybalchenko
*/
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <thread> // this_thread::sleep_for
#include <chrono>
#include "FairMQExample3Sampler.h"
#include "FairMQLogger.h"
#include "FairMQProgOptions.h" // device->fConfig
using namespace std;
@@ -24,25 +25,24 @@ FairMQExample3Sampler::FairMQExample3Sampler()
{
}
void FairMQExample3Sampler::CustomCleanup(void *data, void *object)
bool FairMQExample3Sampler::ConditionalRun()
{
delete static_cast<string*>(object);
}
std::this_thread::sleep_for(std::chrono::seconds(1));
void FairMQExample3Sampler::Run()
{
while (CheckCurrentState(RUNNING))
// NewSimpleMessage creates a copy of the data and takes care of its destruction (after the transfer takes place).
// Should only be used for small data because of the cost of an additional copy
FairMQMessagePtr msg(NewSimpleMessage("Data"));
LOG(INFO) << "Sending \"Data\"";
// in case of error or transfer interruption, return false to go to IDLE state
// successfull transfer will return number of bytes transfered (can be 0 if sending an empty message).
if (Send(msg, "data1") < 0)
{
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
string* text = new string("Data");
unique_ptr<FairMQMessage> msg(NewMessage(const_cast<char*>(text->c_str()), text->length(), CustomCleanup, text));
LOG(INFO) << "Sending \"Data\"";
Send(msg, "data1");
return false;
}
return true;
}
FairMQExample3Sampler::~FairMQExample3Sampler()

View File

@@ -15,8 +15,6 @@
#ifndef FAIRMQEXAMPLE3SAMPLER_H_
#define FAIRMQEXAMPLE3SAMPLER_H_
#include <string>
#include "FairMQDevice.h"
class FairMQExample3Sampler : public FairMQDevice
@@ -25,10 +23,8 @@ class FairMQExample3Sampler : public FairMQDevice
FairMQExample3Sampler();
virtual ~FairMQExample3Sampler();
static void CustomCleanup(void* data, void* hint);
protected:
virtual void Run();
virtual bool ConditionalRun();
};
#endif /* FAIRMQEXAMPLE3SAMPLER_H_ */

View File

@@ -12,9 +12,6 @@
* @author A. Rybalchenko
*/
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include "FairMQExample3Sink.h"
#include "FairMQLogger.h"
@@ -22,21 +19,17 @@ using namespace std;
FairMQExample3Sink::FairMQExample3Sink()
{
// register a handler for data arriving on "data2" channel
OnData("data2", &FairMQExample3Sink::HandleData);
}
void FairMQExample3Sink::Run()
// handler is called whenever a message arrives on "data2", with a reference to the message and a sub-channel index (here 0)
bool FairMQExample3Sink::HandleData(FairMQMessagePtr& msg, int /*index*/)
{
while (CheckCurrentState(RUNNING))
{
unique_ptr<FairMQMessage> msg(NewMessage());
LOG(INFO) << "Received: \"" << string(static_cast<char*>(msg->GetData()), msg->GetSize()) << "\"";
if (Receive(msg, "data2") >= 0)
{
LOG(INFO) << "Received message: \""
<< string(static_cast<char*>(msg->GetData()), msg->GetSize())
<< "\"";
}
}
// return true if want to be called again (otherwise go to IDLE state)
return true;
}
FairMQExample3Sink::~FairMQExample3Sink()

View File

@@ -24,7 +24,7 @@ class FairMQExample3Sink : public FairMQDevice
virtual ~FairMQExample3Sink();
protected:
virtual void Run();
bool HandleData(FairMQMessagePtr&, int);
};
#endif /* FAIRMQEXAMPLE3SINK_H_ */

View File

@@ -5,34 +5,17 @@
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* runExample2Processor.cxx
*
* @since 2013-04-23
* @author D. Klein, A. Rybalchenko
*/
#include "FairMQLogger.h"
#include "FairMQProgOptions.h"
#include "runFairMQDevice.h"
#include "FairMQExample3Processor.h"
#include "runSimpleMQStateMachine.h"
int main(int argc, char** argv)
namespace bpo = boost::program_options;
void addCustomOptions(bpo::options_description& /*options*/)
{
try
{
FairMQProgOptions config;
config.ParseAll(argc, argv);
FairMQExample3Processor processor;
runStateMachine(processor, config);
}
catch (std::exception& e)
{
LOG(ERROR) << "Unhandled Exception reached the top of main: "
<< e.what() << ", application will now exit";
return 1;
}
return 0;
}
FairMQDevice* getDevice(const FairMQProgOptions& /*config*/)
{
return new FairMQExample3Processor();
}

View File

@@ -5,34 +5,17 @@
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* runExample2Sampler.cxx
*
* @since 2013-04-23
* @author D. Klein, A. Rybalchenko
*/
#include "FairMQLogger.h"
#include "FairMQProgOptions.h"
#include "runFairMQDevice.h"
#include "FairMQExample3Sampler.h"
#include "runSimpleMQStateMachine.h"
int main(int argc, char** argv)
namespace bpo = boost::program_options;
void addCustomOptions(bpo::options_description& /*options*/)
{
try
{
FairMQProgOptions config;
config.ParseAll(argc, argv);
FairMQExample3Sampler sampler;
runStateMachine(sampler, config);
}
catch (std::exception& e)
{
LOG(ERROR) << "Unhandled Exception reached the top of main: "
<< e.what() << ", application will now exit";
return 1;
}
return 0;
}
FairMQDevice* getDevice(const FairMQProgOptions& /*config*/)
{
return new FairMQExample3Sampler();
}

View File

@@ -5,34 +5,17 @@
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* runExample2Sink.cxx
*
* @since 2013-04-23
* @author D. Klein, A. Rybalchenko
*/
#include "FairMQLogger.h"
#include "FairMQProgOptions.h"
#include "runFairMQDevice.h"
#include "FairMQExample3Sink.h"
#include "runSimpleMQStateMachine.h"
int main(int argc, char** argv)
namespace bpo = boost::program_options;
void addCustomOptions(bpo::options_description& /*options*/)
{
try
{
FairMQProgOptions config;
config.ParseAll(argc, argv);
FairMQExample3Sink sink;
runStateMachine(sink, config);
}
catch (std::exception& e)
{
LOG(ERROR) << "Unhandled Exception reached the top of main: "
<< e.what() << ", application will now exit";
return 1;
}
return 0;
}
FairMQDevice* getDevice(const FairMQProgOptions& /*config*/)
{
return new FairMQExample3Sink();
}