mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 17:41:45 +00:00
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:
committed by
Mohammad Al-Turany
parent
d5b98567db
commit
da10c64800
@@ -12,10 +12,8 @@
|
||||
* @author A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include <memory> // unique_ptr
|
||||
#include <string>
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
#include <thread> // this_thread::sleep_for
|
||||
#include <chrono>
|
||||
|
||||
#include "FairMQExample6Broadcaster.h"
|
||||
#include "FairMQLogger.h"
|
||||
@@ -26,22 +24,19 @@ FairMQExample6Broadcaster::FairMQExample6Broadcaster()
|
||||
{
|
||||
}
|
||||
|
||||
void FairMQExample6Broadcaster::CustomCleanup(void* /*data*/, void *object)
|
||||
bool FairMQExample6Broadcaster::ConditionalRun()
|
||||
{
|
||||
delete static_cast<string*>(object);
|
||||
}
|
||||
this_thread::sleep_for(chrono::seconds(1));
|
||||
|
||||
void FairMQExample6Broadcaster::Run()
|
||||
{
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
|
||||
// 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("OK"));
|
||||
|
||||
string* text = new string("OK");
|
||||
unique_ptr<FairMQMessage> msg(NewMessage(const_cast<char*>(text->c_str()), text->length(), CustomCleanup, text));
|
||||
LOG(INFO) << "Sending OK";
|
||||
Send(msg, "broadcast");
|
||||
}
|
||||
LOG(INFO) << "Sending OK";
|
||||
|
||||
Send(msg, "broadcast");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
FairMQExample6Broadcaster::~FairMQExample6Broadcaster()
|
||||
|
@@ -23,10 +23,8 @@ class FairMQExample6Broadcaster : public FairMQDevice
|
||||
FairMQExample6Broadcaster();
|
||||
virtual ~FairMQExample6Broadcaster();
|
||||
|
||||
static void CustomCleanup(void* data, void* hint);
|
||||
|
||||
protected:
|
||||
virtual void Run();
|
||||
virtual bool ConditionalRun();
|
||||
};
|
||||
|
||||
#endif /* FAIRMQEXAMPLE6BROADCASTER_H_ */
|
||||
|
@@ -13,12 +13,13 @@
|
||||
*/
|
||||
|
||||
#include <memory> // unique_ptr
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
#include <thread> // this_thread::sleep_for
|
||||
#include <chrono>
|
||||
|
||||
#include "FairMQExample6Sampler.h"
|
||||
#include "FairMQPoller.h"
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQProgOptions.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -27,42 +28,39 @@ FairMQExample6Sampler::FairMQExample6Sampler()
|
||||
{
|
||||
}
|
||||
|
||||
void FairMQExample6Sampler::CustomCleanup(void* /*data*/, void *object)
|
||||
void FairMQExample6Sampler::InitTask()
|
||||
{
|
||||
delete static_cast<string*>(object);
|
||||
fText = fConfig->GetValue<string>("text");
|
||||
}
|
||||
|
||||
void FairMQExample6Sampler::Run()
|
||||
{
|
||||
std::unique_ptr<FairMQPoller> poller(fTransportFactory->CreatePoller(fChannels, { "data", "broadcast" }));
|
||||
unique_ptr<FairMQPoller> poller(fTransportFactory->CreatePoller(fChannels, { "data", "broadcast" }));
|
||||
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
poller->Poll(-1);
|
||||
poller->Poll(100);
|
||||
|
||||
if (poller->CheckInput("broadcast", 0))
|
||||
{
|
||||
unique_ptr<FairMQMessage> msg(NewMessage());
|
||||
FairMQMessagePtr msg(NewMessage());
|
||||
|
||||
if (Receive(msg, "broadcast") > 0)
|
||||
{
|
||||
LOG(INFO) << "Received broadcast: \""
|
||||
<< string(static_cast<char*>(msg->GetData()), msg->GetSize())
|
||||
<< "\"";
|
||||
LOG(INFO) << "Received broadcast: \"" << string(static_cast<char*>(msg->GetData()), msg->GetSize()) << "\"";
|
||||
}
|
||||
}
|
||||
|
||||
if (poller->CheckOutput("data", 0))
|
||||
{
|
||||
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
|
||||
this_thread::sleep_for(chrono::seconds(1));
|
||||
|
||||
string* text = new string(fText);
|
||||
FairMQMessagePtr msg(NewSimpleMessage(fText));
|
||||
|
||||
unique_ptr<FairMQMessage> msg(NewMessage(const_cast<char*>(text->c_str()), text->length(), CustomCleanup, text));
|
||||
|
||||
LOG(INFO) << "Sending \"" << fText << "\"";
|
||||
|
||||
Send(msg, "data");
|
||||
if (Send(msg, "data") > 0)
|
||||
{
|
||||
LOG(INFO) << "Sent \"" << fText << "\"";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,47 +68,3 @@ void FairMQExample6Sampler::Run()
|
||||
FairMQExample6Sampler::~FairMQExample6Sampler()
|
||||
{
|
||||
}
|
||||
|
||||
void FairMQExample6Sampler::SetProperty(const int key, const string& value)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case Text:
|
||||
fText = value;
|
||||
break;
|
||||
default:
|
||||
FairMQDevice::SetProperty(key, value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
string FairMQExample6Sampler::GetProperty(const int key, const string& default_ /*= ""*/)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case Text:
|
||||
return fText;
|
||||
break;
|
||||
default:
|
||||
return FairMQDevice::GetProperty(key, default_);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQExample6Sampler::SetProperty(const int key, const int value)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
default:
|
||||
FairMQDevice::SetProperty(key, value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int FairMQExample6Sampler::GetProperty(const int key, const int default_ /*= 0*/)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
default:
|
||||
return FairMQDevice::GetProperty(key, default_);
|
||||
}
|
||||
}
|
||||
|
@@ -22,25 +22,14 @@
|
||||
class FairMQExample6Sampler : public FairMQDevice
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
Text = FairMQDevice::Last,
|
||||
Last
|
||||
};
|
||||
FairMQExample6Sampler();
|
||||
virtual ~FairMQExample6Sampler();
|
||||
|
||||
static void CustomCleanup(void* data, void* hint);
|
||||
|
||||
virtual void SetProperty(const int key, const std::string& value);
|
||||
virtual std::string GetProperty(const int key, const std::string& default_ = "");
|
||||
virtual void SetProperty(const int key, const int value);
|
||||
virtual int GetProperty(const int key, const int default_ = 0);
|
||||
|
||||
protected:
|
||||
std::string fText;
|
||||
|
||||
virtual void Run();
|
||||
virtual void InitTask();
|
||||
};
|
||||
|
||||
#endif /* FAIRMQEXAMPLE6SAMPLER_H_ */
|
||||
|
@@ -12,6 +12,8 @@
|
||||
* @author A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include <memory> // unique_ptr
|
||||
|
||||
#include "FairMQExample6Sink.h"
|
||||
#include "FairMQPoller.h"
|
||||
#include "FairMQLogger.h"
|
||||
@@ -28,11 +30,11 @@ void FairMQExample6Sink::Run()
|
||||
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
poller->Poll(-1);
|
||||
poller->Poll(100);
|
||||
|
||||
if (poller->CheckInput("broadcast", 0))
|
||||
{
|
||||
unique_ptr<FairMQMessage> msg(NewMessage());
|
||||
FairMQMessagePtr msg(NewMessage());
|
||||
|
||||
if (Receive(msg, "broadcast") > 0)
|
||||
{
|
||||
@@ -42,7 +44,7 @@ void FairMQExample6Sink::Run()
|
||||
|
||||
if (poller->CheckInput("data", 0))
|
||||
{
|
||||
unique_ptr<FairMQMessage> msg(NewMessage());
|
||||
FairMQMessagePtr msg(NewMessage());
|
||||
|
||||
if (Receive(msg, "data") > 0)
|
||||
{
|
||||
|
@@ -5,43 +5,17 @@
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* runExample6Broadcaster.cxx
|
||||
*
|
||||
* @since 2013-04-23
|
||||
* @author A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQProgOptions.h"
|
||||
#include "runFairMQDevice.h"
|
||||
#include "FairMQExample6Broadcaster.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);
|
||||
|
||||
FairMQExample6Broadcaster broadcaster;
|
||||
broadcaster.CatchSignals();
|
||||
broadcaster.SetConfig(config);
|
||||
|
||||
broadcaster.ChangeState("INIT_DEVICE");
|
||||
broadcaster.WaitForEndOfState("INIT_DEVICE");
|
||||
|
||||
broadcaster.ChangeState("INIT_TASK");
|
||||
broadcaster.WaitForEndOfState("INIT_TASK");
|
||||
|
||||
broadcaster.ChangeState("RUN");
|
||||
broadcaster.InteractiveStateLoop();
|
||||
}
|
||||
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 FairMQExample6Broadcaster();
|
||||
}
|
||||
|
@@ -5,55 +5,19 @@
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* runExample6Sampler.cxx
|
||||
*
|
||||
* @since 2013-04-23
|
||||
* @author D. Klein, A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include "boost/program_options.hpp"
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQProgOptions.h"
|
||||
#include "runFairMQDevice.h"
|
||||
#include "FairMQExample6Sampler.h"
|
||||
|
||||
using namespace boost::program_options;
|
||||
namespace bpo = boost::program_options;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
void addCustomOptions(bpo::options_description& options)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string text;
|
||||
|
||||
options_description samplerOptions("Sampler options");
|
||||
samplerOptions.add_options()
|
||||
("text", value<std::string>(&text)->default_value("Hello"), "Text to send out");
|
||||
|
||||
FairMQProgOptions config;
|
||||
config.AddToCmdLineOptions(samplerOptions);
|
||||
config.ParseAll(argc, argv);
|
||||
|
||||
FairMQExample6Sampler sampler;
|
||||
sampler.CatchSignals();
|
||||
sampler.SetConfig(config);
|
||||
sampler.SetProperty(FairMQExample6Sampler::Text, text);
|
||||
|
||||
sampler.ChangeState("INIT_DEVICE");
|
||||
sampler.WaitForEndOfState("INIT_DEVICE");
|
||||
|
||||
sampler.ChangeState("INIT_TASK");
|
||||
sampler.WaitForEndOfState("INIT_TASK");
|
||||
|
||||
sampler.ChangeState("RUN");
|
||||
sampler.InteractiveStateLoop();
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
LOG(ERROR) << "Unhandled Exception reached the top of main: "
|
||||
<< e.what() << ", application will now exit";
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
options.add_options()
|
||||
("text", bpo::value<std::string>()->default_value("Hello"), "Text to send out");
|
||||
}
|
||||
|
||||
FairMQDevice* getDevice(const FairMQProgOptions& /*config*/)
|
||||
{
|
||||
return new FairMQExample6Sampler();
|
||||
}
|
||||
|
@@ -5,43 +5,17 @@
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* runExample6Sink.cxx
|
||||
*
|
||||
* @since 2013-04-23
|
||||
* @author D. Klein, A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQProgOptions.h"
|
||||
#include "runFairMQDevice.h"
|
||||
#include "FairMQExample6Sink.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);
|
||||
|
||||
FairMQExample6Sink sink;
|
||||
sink.CatchSignals();
|
||||
sink.SetConfig(config);
|
||||
|
||||
sink.ChangeState("INIT_DEVICE");
|
||||
sink.WaitForEndOfState("INIT_DEVICE");
|
||||
|
||||
sink.ChangeState("INIT_TASK");
|
||||
sink.WaitForEndOfState("INIT_TASK");
|
||||
|
||||
sink.ChangeState("RUN");
|
||||
sink.InteractiveStateLoop();
|
||||
}
|
||||
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 FairMQExample6Sink();
|
||||
}
|
||||
|
Reference in New Issue
Block a user