mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 09:31:45 +00:00
reduce noise in examples
This commit is contained in:
committed by
Dennis Klein
parent
a7dbeadd1c
commit
aaf74ad93f
@@ -6,25 +6,14 @@
|
||||
# copied verbatim in the file "LICENSE" #
|
||||
################################################################################
|
||||
|
||||
add_library(ExampleMultipleTransportsLib STATIC
|
||||
"Sampler1.cxx"
|
||||
"Sampler1.h"
|
||||
"Sampler2.cxx"
|
||||
"Sampler2.h"
|
||||
"Sink.cxx"
|
||||
"Sink.h"
|
||||
)
|
||||
add_executable(fairmq-ex-multiple-transports-sampler1 sampler1.cxx)
|
||||
target_link_libraries(fairmq-ex-multiple-transports-sampler1 PRIVATE FairMQ)
|
||||
|
||||
target_link_libraries(ExampleMultipleTransportsLib PUBLIC FairMQ)
|
||||
add_executable(fairmq-ex-multiple-transports-sampler2 sampler2.cxx)
|
||||
target_link_libraries(fairmq-ex-multiple-transports-sampler2 PRIVATE FairMQ)
|
||||
|
||||
add_executable(fairmq-ex-multiple-transports-sampler1 runSampler1.cxx)
|
||||
target_link_libraries(fairmq-ex-multiple-transports-sampler1 PRIVATE ExampleMultipleTransportsLib)
|
||||
|
||||
add_executable(fairmq-ex-multiple-transports-sampler2 runSampler2.cxx)
|
||||
target_link_libraries(fairmq-ex-multiple-transports-sampler2 PRIVATE ExampleMultipleTransportsLib)
|
||||
|
||||
add_executable(fairmq-ex-multiple-transports-sink runSink.cxx)
|
||||
target_link_libraries(fairmq-ex-multiple-transports-sink PRIVATE ExampleMultipleTransportsLib)
|
||||
add_executable(fairmq-ex-multiple-transports-sink sink.cxx)
|
||||
target_link_libraries(fairmq-ex-multiple-transports-sink PRIVATE FairMQ)
|
||||
|
||||
add_custom_target(ExampleMultipleTransports DEPENDS fairmq-ex-multiple-transports-sampler1 fairmq-ex-multiple-transports-sampler2 fairmq-ex-multiple-transports-sink)
|
||||
|
||||
|
@@ -1,80 +0,0 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include "Sampler1.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace example_multiple_transports
|
||||
{
|
||||
|
||||
Sampler1::Sampler1()
|
||||
: fAckListener()
|
||||
, fMaxIterations(0)
|
||||
, fNumIterations(0)
|
||||
{
|
||||
}
|
||||
|
||||
void Sampler1::InitTask()
|
||||
{
|
||||
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
|
||||
}
|
||||
|
||||
void Sampler1::PreRun()
|
||||
{
|
||||
fAckListener = thread(&Sampler1::ListenForAcks, this);
|
||||
}
|
||||
|
||||
bool Sampler1::ConditionalRun()
|
||||
{
|
||||
// Creates a message using the transport of channel data1
|
||||
FairMQMessagePtr msg(NewMessageFor("data1", 0, 1000000));
|
||||
|
||||
// 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)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations)
|
||||
{
|
||||
LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Sampler1::PostRun()
|
||||
{
|
||||
fAckListener.join();
|
||||
}
|
||||
|
||||
void Sampler1::ListenForAcks()
|
||||
{
|
||||
uint64_t numAcks = 0;
|
||||
|
||||
while (!NewStatePending())
|
||||
{
|
||||
FairMQMessagePtr ack(NewMessageFor("ack", 0));
|
||||
if (Receive(ack, "ack") < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
++numAcks;
|
||||
}
|
||||
|
||||
LOG(info) << "Acknowledged " << numAcks << " messages";
|
||||
}
|
||||
|
||||
Sampler1::~Sampler1()
|
||||
{
|
||||
}
|
||||
|
||||
} // namespace example_multiple_transports
|
@@ -1,39 +0,0 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#ifndef FAIRMQEXAMPLEMULTIPLETRANSPORTSSAMPLER1_H
|
||||
#define FAIRMQEXAMPLEMULTIPLETRANSPORTSSAMPLER1_H
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include "FairMQDevice.h"
|
||||
|
||||
namespace example_multiple_transports
|
||||
{
|
||||
|
||||
class Sampler1 : public FairMQDevice
|
||||
{
|
||||
public:
|
||||
Sampler1();
|
||||
virtual ~Sampler1();
|
||||
|
||||
protected:
|
||||
virtual void InitTask();
|
||||
virtual void PreRun();
|
||||
virtual bool ConditionalRun();
|
||||
virtual void PostRun();
|
||||
void ListenForAcks();
|
||||
|
||||
std::thread fAckListener;
|
||||
uint64_t fMaxIterations;
|
||||
uint64_t fNumIterations;
|
||||
};
|
||||
|
||||
} // namespace example_multiple_transports
|
||||
|
||||
#endif /* FAIRMQEXAMPLEMULTIPLETRANSPORTSSAMPLER1_H */
|
@@ -1,51 +0,0 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include "Sampler2.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace example_multiple_transports
|
||||
{
|
||||
|
||||
Sampler2::Sampler2()
|
||||
: fMaxIterations(0)
|
||||
, fNumIterations(0)
|
||||
{
|
||||
}
|
||||
|
||||
void Sampler2::InitTask()
|
||||
{
|
||||
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
|
||||
}
|
||||
|
||||
bool Sampler2::ConditionalRun()
|
||||
{
|
||||
FairMQMessagePtr msg(NewMessage(1000));
|
||||
|
||||
// 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, "data2") < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations)
|
||||
{
|
||||
LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Sampler2::~Sampler2()
|
||||
{
|
||||
}
|
||||
|
||||
} // namespace example_multiple_transports
|
@@ -1,34 +0,0 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#ifndef FAIRMQEXAMPLEMULTIPLETRANSPORTSSAMPLER2_H
|
||||
#define FAIRMQEXAMPLEMULTIPLETRANSPORTSSAMPLER2_H
|
||||
|
||||
#include "FairMQDevice.h"
|
||||
|
||||
namespace example_multiple_transports
|
||||
{
|
||||
|
||||
class Sampler2 : public FairMQDevice
|
||||
{
|
||||
public:
|
||||
Sampler2();
|
||||
virtual ~Sampler2();
|
||||
|
||||
protected:
|
||||
virtual void InitTask();
|
||||
virtual bool ConditionalRun();
|
||||
|
||||
private:
|
||||
uint64_t fMaxIterations;
|
||||
uint64_t fNumIterations;
|
||||
};
|
||||
|
||||
} // namespace example_multiple_transports
|
||||
|
||||
#endif /* FAIRMQEXAMPLEMULTIPLETRANSPORTSSAMPLER2_H */
|
@@ -1,78 +0,0 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* Sink.cxx
|
||||
*
|
||||
* @since 2014-10-10
|
||||
* @author A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include "Sink.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace example_multiple_transports
|
||||
{
|
||||
|
||||
Sink::Sink()
|
||||
: fMaxIterations(0)
|
||||
, fNumIterations1(0)
|
||||
, fNumIterations2(0)
|
||||
{
|
||||
// register a handler for data arriving on "data" channel
|
||||
OnData("data1", &Sink::HandleData1);
|
||||
OnData("data2", &Sink::HandleData2);
|
||||
}
|
||||
|
||||
void Sink::InitTask()
|
||||
{
|
||||
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
|
||||
}
|
||||
|
||||
// handler is called whenever a message arrives on "data", with a reference to the message and a sub-channel index (here 0)
|
||||
bool Sink::HandleData1(FairMQMessagePtr& /*msg*/, int /*index*/)
|
||||
{
|
||||
fNumIterations1++;
|
||||
// Creates a message using the transport of channel ack
|
||||
FairMQMessagePtr ack(NewMessageFor("ack", 0));
|
||||
if (Send(ack, "ack") < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// return true if want to be called again (otherwise go to IDLE state)
|
||||
return CheckIterations();
|
||||
}
|
||||
|
||||
// handler is called whenever a message arrives on "data", with a reference to the message and a sub-channel index (here 0)
|
||||
bool Sink::HandleData2(FairMQMessagePtr& /*msg*/, int /*index*/)
|
||||
{
|
||||
fNumIterations2++;
|
||||
// return true if want to be called again (otherwise go to IDLE state)
|
||||
return CheckIterations();
|
||||
}
|
||||
|
||||
bool Sink::CheckIterations()
|
||||
{
|
||||
if (fMaxIterations > 0)
|
||||
{
|
||||
if (fNumIterations1 >= fMaxIterations && fNumIterations2 >= fMaxIterations)
|
||||
{
|
||||
LOG(info) << "Configured maximum number of iterations reached & Received messages from both sources. Leaving RUNNING state.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Sink::~Sink()
|
||||
{
|
||||
}
|
||||
|
||||
} // namespace example_multiple_transports
|
@@ -1,43 +0,0 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* Sink.h
|
||||
*
|
||||
* @since 2014-10-10
|
||||
* @author A. Rybalchenko
|
||||
*/
|
||||
|
||||
#ifndef FAIRMQEXAMPLEMULTIPLETRANSPORTSSINK_H
|
||||
#define FAIRMQEXAMPLEMULTIPLETRANSPORTSSINK_H
|
||||
|
||||
#include "FairMQDevice.h"
|
||||
|
||||
namespace example_multiple_transports
|
||||
{
|
||||
|
||||
class Sink : public FairMQDevice
|
||||
{
|
||||
public:
|
||||
Sink();
|
||||
virtual ~Sink();
|
||||
|
||||
protected:
|
||||
virtual void InitTask();
|
||||
bool HandleData1(FairMQMessagePtr&, int);
|
||||
bool HandleData2(FairMQMessagePtr&, int);
|
||||
bool CheckIterations();
|
||||
|
||||
private:
|
||||
uint64_t fMaxIterations;
|
||||
uint64_t fNumIterations1;
|
||||
uint64_t fNumIterations2;
|
||||
};
|
||||
|
||||
} // namespace example_multiple_transports
|
||||
|
||||
#endif /* FAIRMQEXAMPLEMULTIPLETRANSPORTSSINK_H */
|
@@ -1,23 +0,0 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include "runFairMQDevice.h"
|
||||
#include "Sampler1.h"
|
||||
|
||||
namespace bpo = boost::program_options;
|
||||
|
||||
void addCustomOptions(bpo::options_description& options)
|
||||
{
|
||||
options.add_options()
|
||||
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
|
||||
}
|
||||
|
||||
FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/)
|
||||
{
|
||||
return new example_multiple_transports::Sampler1();
|
||||
}
|
@@ -1,23 +0,0 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include "runFairMQDevice.h"
|
||||
#include "Sampler2.h"
|
||||
|
||||
namespace bpo = boost::program_options;
|
||||
|
||||
void addCustomOptions(bpo::options_description& options)
|
||||
{
|
||||
options.add_options()
|
||||
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
|
||||
}
|
||||
|
||||
FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/)
|
||||
{
|
||||
return new example_multiple_transports::Sampler2();
|
||||
}
|
@@ -1,23 +0,0 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include "runFairMQDevice.h"
|
||||
#include "Sink.h"
|
||||
|
||||
namespace bpo = boost::program_options;
|
||||
|
||||
void addCustomOptions(bpo::options_description& options)
|
||||
{
|
||||
options.add_options()
|
||||
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
|
||||
}
|
||||
|
||||
FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/)
|
||||
{
|
||||
return new example_multiple_transports::Sink();
|
||||
}
|
89
examples/multiple-transports/sampler1.cxx
Normal file
89
examples/multiple-transports/sampler1.cxx
Normal file
@@ -0,0 +1,89 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include <fairmq/Device.h>
|
||||
#include <fairmq/runDevice.h>
|
||||
|
||||
#include <thread>
|
||||
|
||||
namespace bpo = boost::program_options;
|
||||
|
||||
class Sampler1 : public FairMQDevice
|
||||
{
|
||||
public:
|
||||
Sampler1()
|
||||
: fAckListener()
|
||||
, fMaxIterations(0)
|
||||
, fNumIterations(0)
|
||||
{}
|
||||
|
||||
protected:
|
||||
void InitTask() override
|
||||
{
|
||||
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
|
||||
}
|
||||
|
||||
void PreRun() override
|
||||
{
|
||||
fAckListener = std::thread(&Sampler1::ListenForAcks, this);
|
||||
}
|
||||
|
||||
bool ConditionalRun() override
|
||||
{
|
||||
// Creates a message using the transport of channel data1
|
||||
FairMQMessagePtr msg(NewMessageFor("data1", 0, 1000000));
|
||||
|
||||
// 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) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) {
|
||||
LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void PostRun() override
|
||||
{
|
||||
fAckListener.join();
|
||||
}
|
||||
|
||||
void ListenForAcks()
|
||||
{
|
||||
uint64_t numAcks = 0;
|
||||
|
||||
while (!NewStatePending()) {
|
||||
FairMQMessagePtr ack(NewMessageFor("ack", 0));
|
||||
if (Receive(ack, "ack") < 0) {
|
||||
break;
|
||||
}
|
||||
++numAcks;
|
||||
}
|
||||
|
||||
LOG(info) << "Acknowledged " << numAcks << " messages";
|
||||
}
|
||||
|
||||
std::thread fAckListener;
|
||||
uint64_t fMaxIterations;
|
||||
uint64_t fNumIterations;
|
||||
};
|
||||
|
||||
void addCustomOptions(bpo::options_description& options)
|
||||
{
|
||||
options.add_options()
|
||||
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
|
||||
}
|
||||
|
||||
std::unique_ptr<fair::mq::Device> getDevice(fair::mq::ProgOptions& /*config*/)
|
||||
{
|
||||
return std::make_unique<Sampler1>();
|
||||
}
|
63
examples/multiple-transports/sampler2.cxx
Normal file
63
examples/multiple-transports/sampler2.cxx
Normal file
@@ -0,0 +1,63 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include <fairmq/Device.h>
|
||||
#include <fairmq/runDevice.h>
|
||||
|
||||
#include <cstdint> // uint64_t
|
||||
|
||||
namespace bpo = boost::program_options;
|
||||
|
||||
class Sampler2 : public FairMQDevice
|
||||
{
|
||||
public:
|
||||
Sampler2()
|
||||
: fMaxIterations(0)
|
||||
, fNumIterations(0)
|
||||
{}
|
||||
|
||||
protected:
|
||||
void InitTask() override
|
||||
{
|
||||
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
|
||||
}
|
||||
|
||||
bool ConditionalRun() override
|
||||
{
|
||||
FairMQMessagePtr msg(NewMessage(1000));
|
||||
|
||||
// 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, "data2") < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) {
|
||||
LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
uint64_t fMaxIterations;
|
||||
uint64_t fNumIterations;
|
||||
};
|
||||
|
||||
void addCustomOptions(bpo::options_description& options)
|
||||
{
|
||||
options.add_options()
|
||||
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
|
||||
}
|
||||
|
||||
std::unique_ptr<fair::mq::Device> getDevice(fair::mq::ProgOptions& /*config*/)
|
||||
{
|
||||
return std::make_unique<Sampler2>();
|
||||
}
|
82
examples/multiple-transports/sink.cxx
Normal file
82
examples/multiple-transports/sink.cxx
Normal file
@@ -0,0 +1,82 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include <fairmq/Device.h>
|
||||
#include <fairmq/runDevice.h>
|
||||
|
||||
namespace bpo = boost::program_options;
|
||||
|
||||
class Sink : public FairMQDevice
|
||||
{
|
||||
public:
|
||||
Sink()
|
||||
: fMaxIterations(0)
|
||||
, fNumIterations1(0)
|
||||
, fNumIterations2(0)
|
||||
{
|
||||
// register a handler for data arriving on "data" channel
|
||||
OnData("data1", &Sink::HandleData1);
|
||||
OnData("data2", &Sink::HandleData2);
|
||||
}
|
||||
|
||||
protected:
|
||||
void InitTask() override
|
||||
{
|
||||
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
|
||||
}
|
||||
|
||||
// handler is called whenever a message arrives on "data", with a reference to the message and a sub-channel index (here 0)
|
||||
bool HandleData1(FairMQMessagePtr& /*msg*/, int /*index*/)
|
||||
{
|
||||
fNumIterations1++;
|
||||
// Creates a message using the transport of channel ack
|
||||
FairMQMessagePtr ack(NewMessageFor("ack", 0));
|
||||
if (Send(ack, "ack") < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// return true if want to be called again (otherwise go to IDLE state)
|
||||
return CheckIterations();
|
||||
}
|
||||
|
||||
// handler is called whenever a message arrives on "data", with a reference to the message and a sub-channel index (here 0)
|
||||
bool HandleData2(FairMQMessagePtr& /*msg*/, int /*index*/)
|
||||
{
|
||||
fNumIterations2++;
|
||||
// return true if want to be called again (otherwise go to IDLE state)
|
||||
return CheckIterations();
|
||||
}
|
||||
|
||||
bool CheckIterations()
|
||||
{
|
||||
if (fMaxIterations > 0) {
|
||||
if (fNumIterations1 >= fMaxIterations && fNumIterations2 >= fMaxIterations) {
|
||||
LOG(info) << "Configured maximum number of iterations reached & Received messages from both sources. Leaving RUNNING state.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t fMaxIterations;
|
||||
uint64_t fNumIterations1;
|
||||
uint64_t fNumIterations2;
|
||||
};
|
||||
|
||||
void addCustomOptions(bpo::options_description& options)
|
||||
{
|
||||
options.add_options()
|
||||
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
|
||||
}
|
||||
|
||||
std::unique_ptr<fair::mq::Device> getDevice(fair::mq::ProgOptions& /*config*/)
|
||||
{
|
||||
return std::make_unique<Sink>();
|
||||
}
|
Reference in New Issue
Block a user