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

@@ -0,0 +1,17 @@
/********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#ifndef FAIRMQEX8HEADER_H_
#define FAIRMQEX8HEADER_H_
struct Ex8Header
{
int32_t stopFlag;
};
#endif /* FAIRMQEX8HEADER_H_ */

View File

@@ -12,58 +12,48 @@
* @author A. Rybalchenko
*/
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <thread> // this_thread::sleep_for
#include <chrono>
#include "FairMQExample8Sampler.h"
#include "FairMQEx8Header.h"
#include "FairMQLogger.h"
using namespace std;
struct Ex8Header {
int32_t stopFlag;
};
FairMQExample8Sampler::FairMQExample8Sampler()
: fCounter(0)
{
}
void FairMQExample8Sampler::Run()
bool FairMQExample8Sampler::ConditionalRun()
{
int counter = 0;
// Wait a second to keep the output readable.
this_thread::sleep_for(chrono::seconds(1));
// Check if we are still in the RUNNING state.
while (CheckCurrentState(RUNNING))
Ex8Header header;
// Set stopFlag to 1 for the first 4 messages, and to 0 for the 5th.
fCounter < 5 ? header.stopFlag = 0 : header.stopFlag = 1;
LOG(INFO) << "Sending header with stopFlag: " << header.stopFlag;
FairMQParts parts;
// 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
parts.AddPart(NewSimpleMessage(header));
parts.AddPart(NewMessage(1000));
LOG(INFO) << "Sending body of size: " << parts.At(1)->GetSize();
Send(parts, "data-out");
// Go out of the sending loop if the stopFlag was sent.
if (fCounter++ == 5)
{
Ex8Header* header = new Ex8Header;
// Set stopFlag to 1 for the first 4 messages, and to 0 for the 5th.
counter < 5 ? header->stopFlag = 0 : header->stopFlag = 1;
LOG(INFO) << "Sending header with stopFlag: " << header->stopFlag;
FairMQParts parts;
parts.AddPart(NewMessage(header, // data pointer
sizeof(Ex8Header), // data size
[](void* data, void* /*hint*/) { delete static_cast<Ex8Header*>(data); } // callback to deallocate after the transfer
));
parts.AddPart(NewMessage(1000));
LOG(INFO) << "Sending body of size: " << parts.At(1)->GetSize();
Send(parts, "data-out");
// Go out of the sending loop if the stopFlag was sent.
if (counter == 5)
{
break;
}
counter++;
// Wait a second to keep the output readable.
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
return false;
}
LOG(INFO) << "Going out of RUNNING state.";
return true;
}
FairMQExample8Sampler::~FairMQExample8Sampler()

View File

@@ -15,8 +15,6 @@
#ifndef FAIRMQEXAMPLE8SAMPLER_H_
#define FAIRMQEXAMPLE8SAMPLER_H_
#include <string>
#include "FairMQDevice.h"
class FairMQExample8Sampler : public FairMQDevice
@@ -26,7 +24,10 @@ class FairMQExample8Sampler : public FairMQDevice
virtual ~FairMQExample8Sampler();
protected:
virtual void Run();
virtual bool ConditionalRun();
private:
int fCounter;
};
#endif /* FAIRMQEXAMPLE8SAMPLER_H_ */

View File

@@ -12,41 +12,30 @@
* @author A. Rybalchenko
*/
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include "FairMQExample8Sink.h"
#include "FairMQEx8Header.h"
#include "FairMQLogger.h"
using namespace std;
struct Ex8Header {
int32_t stopFlag;
};
FairMQExample8Sink::FairMQExample8Sink()
{
OnData("data-in", &FairMQExample8Sink::HandleData);
}
void FairMQExample8Sink::Run()
bool FairMQExample8Sink::HandleData(FairMQParts& parts, int /*index*/)
{
while (CheckCurrentState(RUNNING))
Ex8Header header;
header.stopFlag = (static_cast<Ex8Header*>(parts.At(0)->GetData()))->stopFlag;
LOG(INFO) << "Received header with stopFlag: " << header.stopFlag;
LOG(INFO) << "Received body of size: " << parts.At(1)->GetSize();
if (header.stopFlag == 1)
{
FairMQParts parts;
if (Receive(parts, "data-in") >= 0)
{
Ex8Header header;
header.stopFlag = (static_cast<Ex8Header*>(parts.At(0)->GetData()))->stopFlag;
LOG(INFO) << "Received header with stopFlag: " << header.stopFlag;
LOG(INFO) << "Received body of size: " << parts.At(1)->GetSize();
if (header.stopFlag == 1)
{
LOG(INFO) << "Flag is 0, exiting Run()";
break;
}
}
LOG(INFO) << "stopFlag is 0, going IDLE";
return false;
}
return true;
}
FairMQExample8Sink::~FairMQExample8Sink()

View File

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

View File

@@ -5,43 +5,17 @@
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* runExample8Sampler.cxx
*
* @since 2013-04-23
* @author D. Klein, A. Rybalchenko
*/
#include "FairMQLogger.h"
#include "FairMQProgOptions.h"
#include "runFairMQDevice.h"
#include "FairMQExample8Sampler.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);
FairMQExample8Sampler sampler;
sampler.CatchSignals();
sampler.SetConfig(config);
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;
}
FairMQDevice* getDevice(const FairMQProgOptions& /*config*/)
{
return new FairMQExample8Sampler();
}

View File

@@ -5,43 +5,17 @@
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* runExample8Sink.cxx
*
* @since 2013-04-23
* @author D. Klein, A. Rybalchenko
*/
#include "FairMQLogger.h"
#include "FairMQProgOptions.h"
#include "runFairMQDevice.h"
#include "FairMQExample8Sink.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);
FairMQExample8Sink 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 FairMQExample8Sink();
}