From aaf74ad93f7471efba7cf367b382717ecde351c7 Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Thu, 13 May 2021 22:22:49 +0200 Subject: [PATCH] reduce noise in examples --- examples/1-1/CMakeLists.txt | 17 +-- examples/1-1/README.md | 2 +- examples/1-1/Sampler.cxx | 59 --------- examples/1-1/Sampler.h | 42 ------ examples/1-1/Sink.cxx | 54 -------- examples/1-1/Sink.h | 40 ------ examples/1-1/runSampler.cxx | 24 ---- examples/1-1/runSink.cxx | 23 ---- examples/1-1/sampler.cxx | 75 +++++++++++ examples/1-1/sink.cxx | 62 +++++++++ examples/1-n-1/CMakeLists.txt | 23 +--- examples/1-n-1/Processor.cxx | 53 -------- examples/1-n-1/Processor.h | 29 ---- examples/1-n-1/Sampler.cxx | 68 ---------- examples/1-n-1/Sampler.h | 42 ------ examples/1-n-1/Sink.cxx | 55 -------- examples/1-n-1/Sink.h | 40 ------ examples/1-n-1/processor.cxx | 58 ++++++++ examples/1-n-1/runProcessor.cxx | 21 --- examples/1-n-1/runSampler.cxx | 24 ---- examples/1-n-1/runSink.cxx | 23 ---- examples/1-n-1/sampler.cxx | 71 ++++++++++ examples/1-n-1/sink.cxx | 61 +++++++++ examples/copypush/CMakeLists.txt | 17 +-- examples/copypush/Sampler.cxx | 68 ---------- examples/copypush/Sampler.h | 43 ------ examples/copypush/Sink.cxx | 51 ------- examples/copypush/Sink.h | 42 ------ examples/copypush/runSampler.cxx | 23 ---- examples/copypush/runSink.cxx | 23 ---- examples/copypush/sampler.cxx | 74 +++++++++++ examples/copypush/sink.cxx | 60 +++++++++ examples/dds/CMakeLists.txt | 23 +--- examples/dds/Processor.cxx | 52 -------- examples/dds/Processor.h | 29 ---- examples/dds/Sampler.cxx | 64 --------- examples/dds/Sampler.h | 40 ------ examples/dds/Sink.cxx | 54 -------- examples/dds/Sink.h | 40 ------ examples/dds/processor.cxx | 58 ++++++++ examples/dds/runProcessor.cxx | 21 --- examples/dds/runSampler.cxx | 25 ---- examples/dds/runSink.cxx | 25 ---- examples/dds/sampler.cxx | 71 ++++++++++ examples/dds/sink.cxx | 62 +++++++++ examples/multipart/CMakeLists.txt | 17 +-- examples/multipart/Header.h | 4 +- examples/multipart/Sampler.cxx | 86 ------------ examples/multipart/Sampler.h | 40 ------ examples/multipart/Sink.cxx | 52 -------- examples/multipart/Sink.h | 34 ----- examples/multipart/runSampler.cxx | 23 ---- examples/multipart/runSink.cxx | 21 --- examples/multipart/sampler.cxx | 97 ++++++++++++++ examples/multipart/sink.cxx | 63 +++++++++ examples/multiple-channels/Broadcaster.cxx | 48 ------- examples/multiple-channels/Broadcaster.h | 35 ----- examples/multiple-channels/CMakeLists.txt | 23 +--- examples/multiple-channels/Sampler.cxx | 82 ------------ examples/multiple-channels/Sampler.h | 42 ------ examples/multiple-channels/Sink.cxx | 71 ---------- examples/multiple-channels/Sink.h | 44 ------ examples/multiple-channels/broadcaster.cxx | 46 +++++++ examples/multiple-channels/runBroadcaster.cxx | 21 --- examples/multiple-channels/runSampler.cxx | 24 ---- examples/multiple-channels/runSink.cxx | 23 ---- examples/multiple-channels/sampler.cxx | 81 ++++++++++++ examples/multiple-channels/sink.cxx | 79 +++++++++++ examples/multiple-transports/CMakeLists.txt | 23 +--- examples/multiple-transports/Sampler1.cxx | 80 ----------- examples/multiple-transports/Sampler1.h | 39 ------ examples/multiple-transports/Sampler2.cxx | 51 ------- examples/multiple-transports/Sampler2.h | 34 ----- examples/multiple-transports/Sink.cxx | 78 ----------- examples/multiple-transports/Sink.h | 43 ------ examples/multiple-transports/runSampler1.cxx | 23 ---- examples/multiple-transports/runSampler2.cxx | 23 ---- examples/multiple-transports/runSink.cxx | 23 ---- examples/multiple-transports/sampler1.cxx | 89 +++++++++++++ examples/multiple-transports/sampler2.cxx | 63 +++++++++ examples/multiple-transports/sink.cxx | 82 ++++++++++++ examples/n-m/CMakeLists.txt | 6 +- .../n-m/{runReceiver.cxx => receiver.cxx} | 9 +- examples/n-m/{runSender.cxx => sender.cxx} | 9 +- .../{runSynchronizer.cxx => synchronizer.cxx} | 9 +- examples/qc/CMakeLists.txt | 8 +- .../{runQCDispatcher.cxx => qCDispatcher.cxx} | 9 +- examples/qc/{runQCTask.cxx => qCTask.cxx} | 9 +- examples/qc/{runSampler.cxx => sampler.cxx} | 9 +- examples/qc/{runSink.cxx => sink.cxx} | 9 +- examples/readout/CMakeLists.txt | 10 +- examples/readout/{Builder.h => builder.cxx} | 19 ++- .../readout/{Processor.h => processor.cxx} | 16 ++- examples/readout/{Readout.h => readout.cxx} | 22 +-- examples/readout/{Receiver.h => receiver.cxx} | 19 ++- examples/readout/runBuilder.cxx | 23 ---- examples/readout/runProcessor.cxx | 20 --- examples/readout/runReadout.cxx | 24 ---- examples/readout/runReceiver.cxx | 23 ---- examples/readout/runSender.cxx | 23 ---- examples/readout/{Sender.h => sender.cxx} | 19 ++- examples/region/CMakeLists.txt | 17 +-- examples/region/Sampler.cxx | 104 --------------- examples/region/Sampler.h | 49 ------- examples/region/Sink.cxx | 70 ---------- examples/region/Sink.h | 43 ------ examples/region/runSampler.cxx | 25 ---- examples/region/runSink.cxx | 23 ---- examples/region/sampler.cxx | 125 ++++++++++++++++++ examples/region/sink.cxx | 73 ++++++++++ examples/req-rep/CMakeLists.txt | 17 +-- examples/req-rep/Client.cxx | 80 ----------- examples/req-rep/Client.h | 42 ------ examples/req-rep/Server.cxx | 66 --------- examples/req-rep/Server.h | 40 ------ examples/req-rep/client.cxx | 83 ++++++++++++ examples/req-rep/runClient.cxx | 24 ---- examples/req-rep/runServer.cxx | 23 ---- examples/req-rep/server.cxx | 72 ++++++++++ 119 files changed, 1766 insertions(+), 3143 deletions(-) delete mode 100644 examples/1-1/Sampler.cxx delete mode 100644 examples/1-1/Sampler.h delete mode 100644 examples/1-1/Sink.cxx delete mode 100644 examples/1-1/Sink.h delete mode 100644 examples/1-1/runSampler.cxx delete mode 100644 examples/1-1/runSink.cxx create mode 100644 examples/1-1/sampler.cxx create mode 100644 examples/1-1/sink.cxx delete mode 100644 examples/1-n-1/Processor.cxx delete mode 100644 examples/1-n-1/Processor.h delete mode 100644 examples/1-n-1/Sampler.cxx delete mode 100644 examples/1-n-1/Sampler.h delete mode 100644 examples/1-n-1/Sink.cxx delete mode 100644 examples/1-n-1/Sink.h create mode 100644 examples/1-n-1/processor.cxx delete mode 100644 examples/1-n-1/runProcessor.cxx delete mode 100644 examples/1-n-1/runSampler.cxx delete mode 100644 examples/1-n-1/runSink.cxx create mode 100644 examples/1-n-1/sampler.cxx create mode 100644 examples/1-n-1/sink.cxx delete mode 100644 examples/copypush/Sampler.cxx delete mode 100644 examples/copypush/Sampler.h delete mode 100644 examples/copypush/Sink.cxx delete mode 100644 examples/copypush/Sink.h delete mode 100644 examples/copypush/runSampler.cxx delete mode 100644 examples/copypush/runSink.cxx create mode 100644 examples/copypush/sampler.cxx create mode 100644 examples/copypush/sink.cxx delete mode 100644 examples/dds/Processor.cxx delete mode 100644 examples/dds/Processor.h delete mode 100644 examples/dds/Sampler.cxx delete mode 100644 examples/dds/Sampler.h delete mode 100644 examples/dds/Sink.cxx delete mode 100644 examples/dds/Sink.h create mode 100644 examples/dds/processor.cxx delete mode 100644 examples/dds/runProcessor.cxx delete mode 100644 examples/dds/runSampler.cxx delete mode 100644 examples/dds/runSink.cxx create mode 100644 examples/dds/sampler.cxx create mode 100644 examples/dds/sink.cxx delete mode 100644 examples/multipart/Sampler.cxx delete mode 100644 examples/multipart/Sampler.h delete mode 100644 examples/multipart/Sink.cxx delete mode 100644 examples/multipart/Sink.h delete mode 100644 examples/multipart/runSampler.cxx delete mode 100644 examples/multipart/runSink.cxx create mode 100644 examples/multipart/sampler.cxx create mode 100644 examples/multipart/sink.cxx delete mode 100644 examples/multiple-channels/Broadcaster.cxx delete mode 100644 examples/multiple-channels/Broadcaster.h delete mode 100644 examples/multiple-channels/Sampler.cxx delete mode 100644 examples/multiple-channels/Sampler.h delete mode 100644 examples/multiple-channels/Sink.cxx delete mode 100644 examples/multiple-channels/Sink.h create mode 100644 examples/multiple-channels/broadcaster.cxx delete mode 100644 examples/multiple-channels/runBroadcaster.cxx delete mode 100644 examples/multiple-channels/runSampler.cxx delete mode 100644 examples/multiple-channels/runSink.cxx create mode 100644 examples/multiple-channels/sampler.cxx create mode 100644 examples/multiple-channels/sink.cxx delete mode 100644 examples/multiple-transports/Sampler1.cxx delete mode 100644 examples/multiple-transports/Sampler1.h delete mode 100644 examples/multiple-transports/Sampler2.cxx delete mode 100644 examples/multiple-transports/Sampler2.h delete mode 100644 examples/multiple-transports/Sink.cxx delete mode 100644 examples/multiple-transports/Sink.h delete mode 100644 examples/multiple-transports/runSampler1.cxx delete mode 100644 examples/multiple-transports/runSampler2.cxx delete mode 100644 examples/multiple-transports/runSink.cxx create mode 100644 examples/multiple-transports/sampler1.cxx create mode 100644 examples/multiple-transports/sampler2.cxx create mode 100644 examples/multiple-transports/sink.cxx rename examples/n-m/{runReceiver.cxx => receiver.cxx} (95%) rename examples/n-m/{runSender.cxx => sender.cxx} (93%) rename examples/n-m/{runSynchronizer.cxx => synchronizer.cxx} (87%) rename examples/qc/{runQCDispatcher.cxx => qCDispatcher.cxx} (90%) rename examples/qc/{runQCTask.cxx => qCTask.cxx} (83%) rename examples/qc/{runSampler.cxx => sampler.cxx} (85%) rename examples/qc/{runSink.cxx => sink.cxx} (83%) rename examples/readout/{Builder.h => builder.cxx} (73%) rename examples/readout/{Processor.h => processor.cxx} (76%) rename examples/readout/{Readout.h => readout.cxx} (86%) rename examples/readout/{Receiver.h => receiver.cxx} (77%) delete mode 100644 examples/readout/runBuilder.cxx delete mode 100644 examples/readout/runProcessor.cxx delete mode 100644 examples/readout/runReadout.cxx delete mode 100644 examples/readout/runReceiver.cxx delete mode 100644 examples/readout/runSender.cxx rename examples/readout/{Sender.h => sender.cxx} (73%) delete mode 100644 examples/region/Sampler.cxx delete mode 100644 examples/region/Sampler.h delete mode 100644 examples/region/Sink.cxx delete mode 100644 examples/region/Sink.h delete mode 100644 examples/region/runSampler.cxx delete mode 100644 examples/region/runSink.cxx create mode 100644 examples/region/sampler.cxx create mode 100644 examples/region/sink.cxx delete mode 100644 examples/req-rep/Client.cxx delete mode 100644 examples/req-rep/Client.h delete mode 100644 examples/req-rep/Server.cxx delete mode 100644 examples/req-rep/Server.h create mode 100644 examples/req-rep/client.cxx delete mode 100644 examples/req-rep/runClient.cxx delete mode 100644 examples/req-rep/runServer.cxx create mode 100644 examples/req-rep/server.cxx diff --git a/examples/1-1/CMakeLists.txt b/examples/1-1/CMakeLists.txt index 7ad8a6bd..8f414b2a 100644 --- a/examples/1-1/CMakeLists.txt +++ b/examples/1-1/CMakeLists.txt @@ -6,20 +6,11 @@ # copied verbatim in the file "LICENSE" # ################################################################################ -add_library(Example11Lib STATIC - "Sampler.cxx" - "Sampler.h" - "Sink.cxx" - "Sink.h" -) +add_executable(fairmq-ex-1-1-sampler sampler.cxx) +target_link_libraries(fairmq-ex-1-1-sampler PRIVATE FairMQ) -target_link_libraries(Example11Lib PUBLIC FairMQ) - -add_executable(fairmq-ex-1-1-sampler runSampler.cxx) -target_link_libraries(fairmq-ex-1-1-sampler PRIVATE Example11Lib) - -add_executable(fairmq-ex-1-1-sink runSink.cxx) -target_link_libraries(fairmq-ex-1-1-sink PRIVATE Example11Lib) +add_executable(fairmq-ex-1-1-sink sink.cxx) +target_link_libraries(fairmq-ex-1-1-sink PRIVATE FairMQ) add_custom_target(Example11 DEPENDS fairmq-ex-1-1-sampler fairmq-ex-1-1-sink) diff --git a/examples/1-1/README.md b/examples/1-1/README.md index 6dd28462..799a4d6d 100644 --- a/examples/1-1/README.md +++ b/examples/1-1/README.md @@ -3,7 +3,7 @@ A simple topology of two devices - **Sampler** and **Sink**. **Sampler** sends data to **Sink** via the **PUSH-PULL** pattern. -`runSampler.cxx` and `runSink.cxx` configure and run the devices. +`sampler.cxx` and `sink.cxx` configure and run the devices. The executables take two command line parameters: `--id` and `--channel-config`. The value of `--id` should be a unique identifier and the value for `--channel-config` is the configuration of the communication channel. . diff --git a/examples/1-1/Sampler.cxx b/examples/1-1/Sampler.cxx deleted file mode 100644 index ec3ce45e..00000000 --- a/examples/1-1/Sampler.cxx +++ /dev/null @@ -1,59 +0,0 @@ -/******************************************************************************** - * Copyright (C) 2014-2018 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 "Sampler.h" - -using namespace std; - -namespace example_1_1 -{ - -Sampler::Sampler() - : fText() - , fMaxIterations(0) - , fNumIterations(0) -{} - -void Sampler::InitTask() -{ - // Get the fText and fMaxIterations values from the command line options (via fConfig) - fText = fConfig->GetProperty("text"); - fMaxIterations = fConfig->GetProperty("max-iterations"); -} - -bool Sampler::ConditionalRun() -{ - // create a copy of the data with new(), that will be deleted after the transfer is complete - string* text = new string(fText); - - // 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 msg(NewMessage( - const_cast(text->c_str()), - text->length(), - [](void* /*data*/, void* object) { delete static_cast(object); }, - text)); - - LOG(info) << "Sending \"" << fText << "\""; - - // in case of error or transfer interruption, return false to go to the Ready state - // successfull transfer will return number of bytes transfered (can be 0 if sending an empty message). - if (Send(msg, "data") < 0) { - return false; - } else if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - return false; - } - - return true; -} - -Sampler::~Sampler() {} - -} // namespace example_1_1 diff --git a/examples/1-1/Sampler.h b/examples/1-1/Sampler.h deleted file mode 100644 index 7a3922ae..00000000 --- a/examples/1-1/Sampler.h +++ /dev/null @@ -1,42 +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" * - ********************************************************************************/ -/** - * Sampler.h - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#ifndef FAIRMQEXAMPLE11SAMPLER_H -#define FAIRMQEXAMPLE11SAMPLER_H - -#include - -#include "FairMQDevice.h" - -namespace example_1_1 -{ - -class Sampler : public FairMQDevice -{ - public: - Sampler(); - virtual ~Sampler(); - - protected: - std::string fText; - uint64_t fMaxIterations; - uint64_t fNumIterations; - - void InitTask() override; - bool ConditionalRun() override; -}; - -} // namespace example_1_1 - -#endif /* FAIRMQEXAMPLE11SAMPLER_H */ diff --git a/examples/1-1/Sink.cxx b/examples/1-1/Sink.cxx deleted file mode 100644 index 44d95645..00000000 --- a/examples/1-1/Sink.cxx +++ /dev/null @@ -1,54 +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_1_1 -{ - -Sink::Sink() - : fMaxIterations(0) - , fNumIterations(0) -{ - // register a handler for data arriving on "data" channel - OnData("data", &Sink::HandleData); -} - -void Sink::InitTask() -{ - // Get the fMaxIterations value from the command line options (via fConfig) - fMaxIterations = fConfig->GetProperty("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::HandleData(FairMQMessagePtr& msg, int /*index*/) -{ - LOG(info) << "Received: \"" << string(static_cast(msg->GetData()), msg->GetSize()) << "\""; - - if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - return false; - } - - // return true if you want the handler to be called again (otherwise return false go to the - // Ready state) - return true; -} - -Sink::~Sink() {} - -} // namespace example_1_1 diff --git a/examples/1-1/Sink.h b/examples/1-1/Sink.h deleted file mode 100644 index 35563760..00000000 --- a/examples/1-1/Sink.h +++ /dev/null @@ -1,40 +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 FAIRMQEXAMPLE11SINK_H -#define FAIRMQEXAMPLE11SINK_H - -#include "FairMQDevice.h" - -namespace example_1_1 -{ - -class Sink : public FairMQDevice -{ - public: - Sink(); - virtual ~Sink(); - - protected: - virtual void InitTask(); - bool HandleData(FairMQMessagePtr&, int); - - private: - uint64_t fMaxIterations; - uint64_t fNumIterations; -}; - -} // namespace example_1_1 - -#endif /* FAIRMQEXAMPLE11SINK_H */ diff --git a/examples/1-1/runSampler.cxx b/examples/1-1/runSampler.cxx deleted file mode 100644 index b9ef19ed..00000000 --- a/examples/1-1/runSampler.cxx +++ /dev/null @@ -1,24 +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 "Sampler.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("text", bpo::value()->default_value("Hello"), "Text to send out") - ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_1_1::Sampler(); -} diff --git a/examples/1-1/runSink.cxx b/examples/1-1/runSink.cxx deleted file mode 100644 index b7405caa..00000000 --- a/examples/1-1/runSink.cxx +++ /dev/null @@ -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()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_1_1::Sink(); -} diff --git a/examples/1-1/sampler.cxx b/examples/1-1/sampler.cxx new file mode 100644 index 00000000..e212a66c --- /dev/null +++ b/examples/1-1/sampler.cxx @@ -0,0 +1,75 @@ +/******************************************************************************** + * 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 +#include + +#include + +namespace bpo = boost::program_options; + +class Sampler : public FairMQDevice +{ + public: + Sampler() + : fMaxIterations(0) + , fNumIterations(0) + {} + + protected: + std::string fText; + uint64_t fMaxIterations; + uint64_t fNumIterations; + + void InitTask() override + { + // Get the fText and fMaxIterations values from the command line options (via fConfig) + fText = fConfig->GetProperty("text"); + fMaxIterations = fConfig->GetProperty("max-iterations"); + } + + bool ConditionalRun() override + { + // create a copy of the data with new(), that will be deleted after the transfer is complete + std::string* text = new std::string(fText); + + // 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 msg(NewMessage( + const_cast(text->c_str()), + text->length(), + [](void* /*data*/, void* object) { delete static_cast(object); }, + text)); + + LOG(info) << "Sending \"" << fText << "\""; + + // in case of error or transfer interruption, return false to go to the Ready state + // successfull transfer will return number of bytes transfered (can be 0 if sending an empty message). + if (Send(msg, "data") < 0) { + return false; + } else if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + return false; + } + + return true; + } +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("text", bpo::value()->default_value("Hello"), "Text to send out") + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/1-1/sink.cxx b/examples/1-1/sink.cxx new file mode 100644 index 00000000..10e77ec4 --- /dev/null +++ b/examples/1-1/sink.cxx @@ -0,0 +1,62 @@ +/******************************************************************************** + * 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 +#include + +#include + +namespace bpo = boost::program_options; + +class Sink : public FairMQDevice +{ + public: + Sink() + : fMaxIterations(0) + , fNumIterations(0) + { + // register a handler for data arriving on "data" channel + OnData("data", &Sink::HandleData); + } + + protected: + void InitTask() override + { + // Get the fMaxIterations value from the command line options (via fConfig) + fMaxIterations = fConfig->GetProperty("max-iterations"); + } + + bool HandleData(FairMQMessagePtr& msg, int) + { + LOG(info) << "Received: \"" << std::string(static_cast(msg->GetData()), msg->GetSize()) << "\""; + + if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + return false; + } + + // return true if you want the handler to be called again (otherwise return false go to the + // Ready state) + return true; + } + + private: + uint64_t fMaxIterations; + uint64_t fNumIterations; +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/1-n-1/CMakeLists.txt b/examples/1-n-1/CMakeLists.txt index 4ba7df72..aca5572f 100644 --- a/examples/1-n-1/CMakeLists.txt +++ b/examples/1-n-1/CMakeLists.txt @@ -6,25 +6,14 @@ # copied verbatim in the file "LICENSE" # ################################################################################ -add_library(Example1N1Lib STATIC - "Sampler.cxx" - "Sampler.h" - "Processor.cxx" - "Processor.h" - "Sink.cxx" - "Sink.h" -) +add_executable(fairmq-ex-1-n-1-sampler sampler.cxx) +target_link_libraries(fairmq-ex-1-n-1-sampler PRIVATE FairMQ) -target_link_libraries(Example1N1Lib PUBLIC FairMQ) +add_executable(fairmq-ex-1-n-1-processor processor.cxx) +target_link_libraries(fairmq-ex-1-n-1-processor PRIVATE FairMQ) -add_executable(fairmq-ex-1-n-1-sampler runSampler.cxx) -target_link_libraries(fairmq-ex-1-n-1-sampler PRIVATE Example1N1Lib) - -add_executable(fairmq-ex-1-n-1-processor runProcessor.cxx) -target_link_libraries(fairmq-ex-1-n-1-processor PRIVATE Example1N1Lib) - -add_executable(fairmq-ex-1-n-1-sink runSink.cxx) -target_link_libraries(fairmq-ex-1-n-1-sink PRIVATE Example1N1Lib) +add_executable(fairmq-ex-1-n-1-sink sink.cxx) +target_link_libraries(fairmq-ex-1-n-1-sink PRIVATE FairMQ) add_custom_target(Example1N1 DEPENDS fairmq-ex-1-n-1-sampler fairmq-ex-1-n-1-processor fairmq-ex-1-n-1-sink) diff --git a/examples/1-n-1/Processor.cxx b/examples/1-n-1/Processor.cxx deleted file mode 100644 index b74ba282..00000000 --- a/examples/1-n-1/Processor.cxx +++ /dev/null @@ -1,53 +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 "Processor.h" - -#include - -using namespace std; - -namespace example_1_n_1 -{ - -Processor::Processor() -{ - OnData("data1", &Processor::HandleData); -} - -bool Processor::HandleData(FairMQMessagePtr& msg, int /*index*/) -{ - LOG(info) << "Received data, processing..."; - - // Modify the received string - string* text = new std::string(static_cast(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(text->c_str()), - text->length(), - [](void* /*data*/, void* object) { delete static_cast(object); }, - text)); - - // Send out the output message - if (Send(msg2, "data2") < 0) - { - return false; - } - - return true; -} - -Processor::~Processor() -{ -} - -} // namespace example_1_n_1 diff --git a/examples/1-n-1/Processor.h b/examples/1-n-1/Processor.h deleted file mode 100644 index 87670831..00000000 --- a/examples/1-n-1/Processor.h +++ /dev/null @@ -1,29 +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 FAIRMQEXAMPLE1N1PROCESSOR_H_ -#define FAIRMQEXAMPLE1N1PROCESSOR_H_ - -#include "FairMQDevice.h" - -namespace example_1_n_1 -{ - -class Processor : public FairMQDevice -{ - public: - Processor(); - virtual ~Processor(); - - protected: - bool HandleData(FairMQMessagePtr&, int); -}; - -} // namespace example_1_n_1 - -#endif /* FAIRMQEXAMPLE1N1PROCESSOR_H_ */ diff --git a/examples/1-n-1/Sampler.cxx b/examples/1-n-1/Sampler.cxx deleted file mode 100644 index 8aac7e28..00000000 --- a/examples/1-n-1/Sampler.cxx +++ /dev/null @@ -1,68 +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" * - ********************************************************************************/ -/** - * Sampler.cpp - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#include // this_thread::sleep_for -#include - -#include "Sampler.h" - -using namespace std; - -namespace example_1_n_1 -{ - -Sampler::Sampler() - : fText() - , fMaxIterations(0) - , fNumIterations(0) -{ -} - -void Sampler::InitTask() -{ - // Get the fText and fMaxIterations values from the command line options (via fConfig) - fText = fConfig->GetProperty("text"); - fMaxIterations = fConfig->GetProperty("max-iterations"); -} - -bool Sampler::ConditionalRun() -{ - // Initializing message with NewStaticMessage will avoid copy - // but won't delete the data after the sending is completed. - FairMQMessagePtr msg(NewStaticMessage(fText)); - - LOG(info) << "Sending \"" << fText << "\""; - - // 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; - } - else if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) - { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - return false; - } - - this_thread::sleep_for(chrono::seconds(1)); - - return true; -} - -Sampler::~Sampler() -{ -} - -} // namespace example_1_n_1 diff --git a/examples/1-n-1/Sampler.h b/examples/1-n-1/Sampler.h deleted file mode 100644 index 9d428110..00000000 --- a/examples/1-n-1/Sampler.h +++ /dev/null @@ -1,42 +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" * - ********************************************************************************/ -/** - * Sampler.h - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#ifndef FAIRMQEXAMPLE1N1SAMPLER_H_ -#define FAIRMQEXAMPLE1N1SAMPLER_H_ - -#include - -#include "FairMQDevice.h" - -namespace example_1_n_1 -{ - -class Sampler : public FairMQDevice -{ - public: - Sampler(); - virtual ~Sampler(); - - protected: - std::string fText; - uint64_t fMaxIterations; - uint64_t fNumIterations; - - virtual void InitTask(); - virtual bool ConditionalRun(); -}; - -} // namespace example_1_n_1 - -#endif /* FAIRMQEXAMPLE1N1SAMPLER_H_ */ diff --git a/examples/1-n-1/Sink.cxx b/examples/1-n-1/Sink.cxx deleted file mode 100644 index 7587f1b3..00000000 --- a/examples/1-n-1/Sink.cxx +++ /dev/null @@ -1,55 +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_1_n_1 -{ - -Sink::Sink() - : fMaxIterations(0) - , fNumIterations(0) -{ - // register a handler for data arriving on "data2" channel - OnData("data2", &Sink::HandleData); -} - -void Sink::InitTask() -{ - // Get the fMaxIterations value from the command line options (via fConfig) - fMaxIterations = fConfig->GetProperty("max-iterations"); -} - -// handler is called whenever a message arrives on "data2", with a reference to the message and a sub-channel index (here 0) -bool Sink::HandleData(FairMQMessagePtr& msg, int /*index*/) -{ - LOG(info) << "Received: \"" << string(static_cast(msg->GetData()), msg->GetSize()) << "\""; - - if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) - { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - return false; - } - - // return true if want to be called again (otherwise return false go to IDLE state) - return true; -} - -Sink::~Sink() -{ -} - -} // namespace example_1_n_1 diff --git a/examples/1-n-1/Sink.h b/examples/1-n-1/Sink.h deleted file mode 100644 index 85c97462..00000000 --- a/examples/1-n-1/Sink.h +++ /dev/null @@ -1,40 +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 FAIRMQEXAMPLE1N1SINK_H -#define FAIRMQEXAMPLE1N1SINK_H - -#include "FairMQDevice.h" - -namespace example_1_n_1 -{ - -class Sink : public FairMQDevice -{ - public: - Sink(); - virtual ~Sink(); - - protected: - virtual void InitTask(); - bool HandleData(FairMQMessagePtr&, int); - - private: - uint64_t fMaxIterations; - uint64_t fNumIterations; -}; - -} // namespace example_1_n_1 - -#endif /* FAIRMQEXAMPLE1N1SINK_H */ diff --git a/examples/1-n-1/processor.cxx b/examples/1-n-1/processor.cxx new file mode 100644 index 00000000..39aedbdb --- /dev/null +++ b/examples/1-n-1/processor.cxx @@ -0,0 +1,58 @@ +/******************************************************************************** + * 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 +#include + +#include + +namespace bpo = boost::program_options; + +class Processor : public FairMQDevice +{ + public: + Processor() + { + OnData("data1", &Processor::HandleData); + } + + protected: + bool HandleData(FairMQMessagePtr& msg, int) + { + LOG(info) << "Received data, processing..."; + + // Modify the received string + std::string* text = new std::string(static_cast(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(text->c_str()), + text->length(), + [](void* /*data*/, void* object) { delete static_cast(object); }, + text)); + + // Send out the output message + if (Send(msg2, "data2") < 0) { + return false; + } + + return true; + } +}; + +void addCustomOptions(bpo::options_description& /*options*/) +{ +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/1-n-1/runProcessor.cxx b/examples/1-n-1/runProcessor.cxx deleted file mode 100644 index cfebe051..00000000 --- a/examples/1-n-1/runProcessor.cxx +++ /dev/null @@ -1,21 +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 "Processor.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& /*options*/) -{ -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_1_n_1::Processor(); -} diff --git a/examples/1-n-1/runSampler.cxx b/examples/1-n-1/runSampler.cxx deleted file mode 100644 index d8c991f7..00000000 --- a/examples/1-n-1/runSampler.cxx +++ /dev/null @@ -1,24 +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 "Sampler.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("text", bpo::value()->default_value("Hello"), "Text to send out") - ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_1_n_1::Sampler(); -} diff --git a/examples/1-n-1/runSink.cxx b/examples/1-n-1/runSink.cxx deleted file mode 100644 index f3b6c0df..00000000 --- a/examples/1-n-1/runSink.cxx +++ /dev/null @@ -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()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_1_n_1::Sink(); -} diff --git a/examples/1-n-1/sampler.cxx b/examples/1-n-1/sampler.cxx new file mode 100644 index 00000000..5202c7f2 --- /dev/null +++ b/examples/1-n-1/sampler.cxx @@ -0,0 +1,71 @@ +/******************************************************************************** + * 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 +#include + +#include +#include // this_thread::sleep_for +#include + +namespace bpo = boost::program_options; + +class Sampler : public FairMQDevice +{ + public: + Sampler() + : fMaxIterations(0) + , fNumIterations(0) + {} + + protected: + std::string fText; + uint64_t fMaxIterations; + uint64_t fNumIterations; + + void InitTask() override + { + // Get the fText and fMaxIterations values from the command line options (via fConfig) + fText = fConfig->GetProperty("text"); + fMaxIterations = fConfig->GetProperty("max-iterations"); + } + + bool ConditionalRun() override + { + // Initializing message with NewStaticMessage will avoid copy + // but won't delete the data after the sending is completed. + FairMQMessagePtr msg(NewStaticMessage(fText)); + + LOG(info) << "Sending \"" << fText << "\""; + + // 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; + } else if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + return false; + } + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + return true; + } +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("text", bpo::value()->default_value("Hello"), "Text to send out") + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/1-n-1/sink.cxx b/examples/1-n-1/sink.cxx new file mode 100644 index 00000000..f1fcaca3 --- /dev/null +++ b/examples/1-n-1/sink.cxx @@ -0,0 +1,61 @@ +/******************************************************************************** + * 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 +#include + +#include + +namespace bpo = boost::program_options; + +class Sink : public FairMQDevice +{ + public: + Sink() + : fMaxIterations(0) + , fNumIterations(0) + { + // register a handler for data arriving on "data2" channel + OnData("data2", &Sink::HandleData); + } + + protected: + virtual void InitTask() override + { + // Get the fMaxIterations value from the command line options (via fConfig) + fMaxIterations = fConfig->GetProperty("max-iterations"); + } + + bool HandleData(FairMQMessagePtr& msg, int) + { + LOG(info) << "Received: \"" << std::string(static_cast(msg->GetData()), msg->GetSize()) << "\""; + + if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + return false; + } + + // return true if want to be called again (otherwise return false go to IDLE state) + return true; + } + + private: + uint64_t fMaxIterations; + uint64_t fNumIterations; +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/copypush/CMakeLists.txt b/examples/copypush/CMakeLists.txt index 8b6a353e..130109f0 100644 --- a/examples/copypush/CMakeLists.txt +++ b/examples/copypush/CMakeLists.txt @@ -6,21 +6,12 @@ # copied verbatim in the file "LICENSE" # ################################################################################ -add_library(ExampleCopyPushLib STATIC - "Sampler.cxx" - "Sampler.h" - "Sink.cxx" - "Sink.h" -) -target_link_libraries(ExampleCopyPushLib PUBLIC FairMQ) +add_executable(fairmq-ex-copypush-sampler sampler.cxx) +target_link_libraries(fairmq-ex-copypush-sampler PRIVATE FairMQ) -add_executable(fairmq-ex-copypush-sampler runSampler.cxx) -target_link_libraries(fairmq-ex-copypush-sampler PRIVATE ExampleCopyPushLib) - - -add_executable(fairmq-ex-copypush-sink runSink.cxx) -target_link_libraries(fairmq-ex-copypush-sink PRIVATE ExampleCopyPushLib) +add_executable(fairmq-ex-copypush-sink sink.cxx) +target_link_libraries(fairmq-ex-copypush-sink PRIVATE FairMQ) add_custom_target(ExampleCopyPush DEPENDS fairmq-ex-copypush-sampler fairmq-ex-copypush-sink) diff --git a/examples/copypush/Sampler.cxx b/examples/copypush/Sampler.cxx deleted file mode 100644 index 92ca98ae..00000000 --- a/examples/copypush/Sampler.cxx +++ /dev/null @@ -1,68 +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" * - ********************************************************************************/ -/** - * Sampler.cpp - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#include // this_thread::sleep_for -#include - -#include "Sampler.h" - -using namespace std; - -namespace example_copypush -{ - -Sampler::Sampler() - : fNumDataChannels(0) - , fCounter(0) - , fMaxIterations(0) - , fNumIterations(0) -{ -} - -void Sampler::InitTask() -{ - fNumDataChannels = fChannels.at("data").size(); - fMaxIterations = fConfig->GetProperty("max-iterations"); -} - -bool Sampler::ConditionalRun() -{ - // 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(fCounter++)); - - for (int i = 0; i < fNumDataChannels - 1; ++i) - { - FairMQMessagePtr msgCopy(NewMessage()); - msgCopy->Copy(*msg); - Send(msgCopy, "data", i); - } - Send(msg, "data", fNumDataChannels - 1); - - if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) - { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - return false; - } - - this_thread::sleep_for(chrono::seconds(1)); - - return true; -} - -Sampler::~Sampler() -{ -} - -} // namespace example_copypush diff --git a/examples/copypush/Sampler.h b/examples/copypush/Sampler.h deleted file mode 100644 index 66bbb1dd..00000000 --- a/examples/copypush/Sampler.h +++ /dev/null @@ -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" * - ********************************************************************************/ -/** - * Sampler.h - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#ifndef FAIRMQEXAMPLECOPYPUSHSAMPLER_H -#define FAIRMQEXAMPLECOPYPUSHSAMPLER_H - -#include "FairMQDevice.h" - -#include // uint64_t - -namespace example_copypush -{ - -class Sampler : public FairMQDevice -{ - public: - Sampler(); - virtual ~Sampler(); - - protected: - virtual void InitTask(); - virtual bool ConditionalRun(); - - int fNumDataChannels; - uint64_t fCounter; - uint64_t fMaxIterations; - uint64_t fNumIterations; -}; - -} // namespace example_copypush - -#endif /* FAIRMQEXAMPLECOPYPUSHSAMPLER_H */ diff --git a/examples/copypush/Sink.cxx b/examples/copypush/Sink.cxx deleted file mode 100644 index 1f490069..00000000 --- a/examples/copypush/Sink.cxx +++ /dev/null @@ -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" * - ********************************************************************************/ -/** - * Sink.cxx - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#include "Sink.h" - -namespace example_copypush -{ - -Sink::Sink() - : fMaxIterations(0) - , fNumIterations(0) -{ - OnData("data", &Sink::HandleData); -} - -void Sink::InitTask() -{ - // Get the fMaxIterations value from the command line options (via fConfig) - fMaxIterations = fConfig->GetProperty("max-iterations"); -} - -bool Sink::HandleData(FairMQMessagePtr& msg, int /*index*/) -{ - LOG(info) << "Received message: \"" << *(static_cast(msg->GetData())) << "\""; - - if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) - { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - return false; - } - - // return true if want to be called again (otherwise go to IDLE state) - return true; -} - -Sink::~Sink() -{ -} - -} // namespace example_copypush diff --git a/examples/copypush/Sink.h b/examples/copypush/Sink.h deleted file mode 100644 index acbf80a4..00000000 --- a/examples/copypush/Sink.h +++ /dev/null @@ -1,42 +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 FAIRMQEXAMPLECOPYPUSHSINK_H -#define FAIRMQEXAMPLECOPYPUSHSINK_H - -#include "FairMQDevice.h" - -#include // uint64_t - -namespace example_copypush -{ - -class Sink : public FairMQDevice -{ - public: - Sink(); - virtual ~Sink(); - - protected: - virtual void InitTask(); - bool HandleData(FairMQMessagePtr&, int); - - private: - uint64_t fMaxIterations; - uint64_t fNumIterations; -}; - -} // namespace example_copypush - -#endif /* FAIRMQEXAMPLECOPYPUSHSINK_H */ diff --git a/examples/copypush/runSampler.cxx b/examples/copypush/runSampler.cxx deleted file mode 100644 index aad63884..00000000 --- a/examples/copypush/runSampler.cxx +++ /dev/null @@ -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 "Sampler.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_copypush::Sampler(); -} diff --git a/examples/copypush/runSink.cxx b/examples/copypush/runSink.cxx deleted file mode 100644 index e5704688..00000000 --- a/examples/copypush/runSink.cxx +++ /dev/null @@ -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()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_copypush::Sink(); -} diff --git a/examples/copypush/sampler.cxx b/examples/copypush/sampler.cxx new file mode 100644 index 00000000..b262731e --- /dev/null +++ b/examples/copypush/sampler.cxx @@ -0,0 +1,74 @@ +/******************************************************************************** + * 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 +#include + +#include +#include // uint64_t +#include // this_thread::sleep_for + +namespace bpo = boost::program_options; + +class Sampler : public FairMQDevice +{ + public: + Sampler() + : fNumDataChannels(0) + , fCounter(0) + , fMaxIterations(0) + , fNumIterations(0) + {} + + protected: + void InitTask() override + { + fNumDataChannels = fChannels.at("data").size(); + fMaxIterations = fConfig->GetProperty("max-iterations"); + } + + bool ConditionalRun() override + { + // 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(fCounter++)); + + for (int i = 0; i < fNumDataChannels - 1; ++i) { + FairMQMessagePtr msgCopy(NewMessage()); + msgCopy->Copy(*msg); + Send(msgCopy, "data", i); + } + Send(msg, "data", fNumDataChannels - 1); + + if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + return false; + } + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + return true; + } + + + int fNumDataChannels; + uint64_t fCounter; + uint64_t fMaxIterations; + uint64_t fNumIterations; +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/copypush/sink.cxx b/examples/copypush/sink.cxx new file mode 100644 index 00000000..cbc0fe78 --- /dev/null +++ b/examples/copypush/sink.cxx @@ -0,0 +1,60 @@ +/******************************************************************************** + * 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 +#include + +#include // uint64_t + +namespace bpo = boost::program_options; + +class Sink : public FairMQDevice +{ + public: + Sink() + : fMaxIterations(0) + , fNumIterations(0) + { + OnData("data", &Sink::HandleData); + } + + protected: + void InitTask() override + { + // Get the fMaxIterations value from the command line options (via fConfig) + fMaxIterations = fConfig->GetProperty("max-iterations"); + } + + bool HandleData(FairMQMessagePtr& msg, int) + { + LOG(info) << "Received message: \"" << *(static_cast(msg->GetData())) << "\""; + + if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + return false; + } + + // return true if want to be called again (otherwise go to IDLE state) + return true; + } + + private: + uint64_t fMaxIterations; + uint64_t fNumIterations; +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/dds/CMakeLists.txt b/examples/dds/CMakeLists.txt index 4b40e0c2..feea4e67 100644 --- a/examples/dds/CMakeLists.txt +++ b/examples/dds/CMakeLists.txt @@ -6,25 +6,14 @@ # copied verbatim in the file "LICENSE" # ################################################################################ -add_library(ExampleDDSLib STATIC - "Sampler.cxx" - "Sampler.h" - "Processor.cxx" - "Processor.h" - "Sink.cxx" - "Sink.h" -) +add_executable(fairmq-ex-dds-sampler sampler.cxx) +target_link_libraries(fairmq-ex-dds-sampler PRIVATE FairMQ) -target_link_libraries(ExampleDDSLib PUBLIC FairMQ) +add_executable(fairmq-ex-dds-processor processor.cxx) +target_link_libraries(fairmq-ex-dds-processor PRIVATE FairMQ) -add_executable(fairmq-ex-dds-sampler runSampler.cxx) -target_link_libraries(fairmq-ex-dds-sampler PRIVATE ExampleDDSLib) - -add_executable(fairmq-ex-dds-processor runProcessor.cxx) -target_link_libraries(fairmq-ex-dds-processor PRIVATE ExampleDDSLib) - -add_executable(fairmq-ex-dds-sink runSink.cxx) -target_link_libraries(fairmq-ex-dds-sink PRIVATE ExampleDDSLib) +add_executable(fairmq-ex-dds-sink sink.cxx) +target_link_libraries(fairmq-ex-dds-sink PRIVATE FairMQ) add_custom_target(ExampleDDS DEPENDS fairmq-ex-dds-sampler fairmq-ex-dds-processor fairmq-ex-dds-sink) diff --git a/examples/dds/Processor.cxx b/examples/dds/Processor.cxx deleted file mode 100644 index 53a96c95..00000000 --- a/examples/dds/Processor.cxx +++ /dev/null @@ -1,52 +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 "Processor.h" -#include "FairMQLogger.h" - -using namespace std; - -namespace example_dds -{ - -Processor::Processor() -{ - OnData("data1", &Processor::HandleData); -} - -bool Processor::HandleData(FairMQMessagePtr& msg, int /*index*/) -{ - LOG(info) << "Received data, processing..."; - - // Modify the received string - string* text = new std::string(static_cast(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(text->c_str()), - text->length(), - [](void* /*data*/, void* object) { delete static_cast(object); }, - text)); - - // Send out the output message - if (Send(msg2, "data2") < 0) - { - return false; - } - - return true; -} - -Processor::~Processor() -{ -} - -} // namespace example_dds diff --git a/examples/dds/Processor.h b/examples/dds/Processor.h deleted file mode 100644 index ede74087..00000000 --- a/examples/dds/Processor.h +++ /dev/null @@ -1,29 +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 FAIRMQEXAMPLEDDSPROCESSOR_H -#define FAIRMQEXAMPLEDDSPROCESSOR_H - -#include "FairMQDevice.h" - -namespace example_dds -{ - -class Processor : public FairMQDevice -{ - public: - Processor(); - virtual ~Processor(); - - protected: - bool HandleData(FairMQMessagePtr&, int); -}; - -} - -#endif /* FAIRMQEXAMPLEDDSPROCESSOR_H */ diff --git a/examples/dds/Sampler.cxx b/examples/dds/Sampler.cxx deleted file mode 100644 index 7e0a8a02..00000000 --- a/examples/dds/Sampler.cxx +++ /dev/null @@ -1,64 +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" * - ********************************************************************************/ -/** - * Sampler.cpp - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#include // this_thread::sleep_for -#include - -#include "Sampler.h" - -using namespace std; - -namespace example_dds -{ - -Sampler::Sampler() -{ -} - -void Sampler::InitTask() -{ - fIterations = fConfig->GetValue("iterations"); - fCounter = 0; -} - -bool Sampler::ConditionalRun() -{ - // 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) { - return false; - } - - if (fIterations > 0) { - ++fCounter; - if (fCounter >= fIterations) { - LOG(info) << "Sent " << fCounter << " messages. Finished."; - return false; - } - } - - return true; -} - -Sampler::~Sampler() -{ -} - -} diff --git a/examples/dds/Sampler.h b/examples/dds/Sampler.h deleted file mode 100644 index 7d74736a..00000000 --- a/examples/dds/Sampler.h +++ /dev/null @@ -1,40 +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" * - ********************************************************************************/ -/** - * Sampler.h - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#ifndef FAIRMQEXAMPLEDDSSAMPLER_H -#define FAIRMQEXAMPLEDDSSAMPLER_H - -#include "FairMQDevice.h" - -namespace example_dds -{ - -class Sampler : public FairMQDevice -{ - public: - Sampler(); - virtual ~Sampler(); - void InitTask() override; - - protected: - bool ConditionalRun() override; - - private: - uint64_t fIterations; - uint64_t fCounter; -}; - -} // namespace example_dds - -#endif /* FAIRMQEXAMPLEDDSSAMPLER_H */ diff --git a/examples/dds/Sink.cxx b/examples/dds/Sink.cxx deleted file mode 100644 index db19e873..00000000 --- a/examples/dds/Sink.cxx +++ /dev/null @@ -1,54 +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_dds -{ - -Sink::Sink() -{ - // register a handler for data arriving on "data2" channel - OnData("data2", &Sink::HandleData); -} - -void Sink::InitTask() -{ - fIterations = fConfig->GetValue("iterations"); - fCounter = 0; -} - -// handler is called whenever a message arrives on "data2", with a reference to the message and a sub-channel index (here 0) -bool Sink::HandleData(FairMQMessagePtr& msg, int /*index*/) -{ - LOG(info) << "Received: \"" << string(static_cast(msg->GetData()), msg->GetSize()) << "\""; - - if (fIterations > 0) { - ++fCounter; - if (fCounter >= fIterations) { - LOG(info) << "Received " << fCounter << " messages. Finished."; - return false; - } - } - // return true if want to be called again (otherwise go to IDLE state) - return true; -} - -Sink::~Sink() -{ -} - -} // namespace example_dds diff --git a/examples/dds/Sink.h b/examples/dds/Sink.h deleted file mode 100644 index 5a5e9344..00000000 --- a/examples/dds/Sink.h +++ /dev/null @@ -1,40 +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 FAIRMQEXAMPLEDDSSINK_H -#define FAIRMQEXAMPLEDDSSINK_H - -#include "FairMQDevice.h" - -namespace example_dds -{ - -class Sink : public FairMQDevice -{ - public: - Sink(); - virtual ~Sink(); - void InitTask() override; - - protected: - bool HandleData(FairMQMessagePtr&, int); - - private: - uint64_t fIterations; - uint64_t fCounter; -}; - -} // namespace example_dds - -#endif /* FAIRMQEXAMPLEDDSSINK_H */ diff --git a/examples/dds/processor.cxx b/examples/dds/processor.cxx new file mode 100644 index 00000000..39aedbdb --- /dev/null +++ b/examples/dds/processor.cxx @@ -0,0 +1,58 @@ +/******************************************************************************** + * 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 +#include + +#include + +namespace bpo = boost::program_options; + +class Processor : public FairMQDevice +{ + public: + Processor() + { + OnData("data1", &Processor::HandleData); + } + + protected: + bool HandleData(FairMQMessagePtr& msg, int) + { + LOG(info) << "Received data, processing..."; + + // Modify the received string + std::string* text = new std::string(static_cast(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(text->c_str()), + text->length(), + [](void* /*data*/, void* object) { delete static_cast(object); }, + text)); + + // Send out the output message + if (Send(msg2, "data2") < 0) { + return false; + } + + return true; + } +}; + +void addCustomOptions(bpo::options_description& /*options*/) +{ +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/dds/runProcessor.cxx b/examples/dds/runProcessor.cxx deleted file mode 100644 index da3ef777..00000000 --- a/examples/dds/runProcessor.cxx +++ /dev/null @@ -1,21 +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 "Processor.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& /*options*/) -{ -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_dds::Processor(); -} diff --git a/examples/dds/runSampler.cxx b/examples/dds/runSampler.cxx deleted file mode 100644 index 5374d5a4..00000000 --- a/examples/dds/runSampler.cxx +++ /dev/null @@ -1,25 +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 "Sampler.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options()( - "iterations,i", - bpo::value()->default_value(1000), - "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_dds::Sampler(); -} diff --git a/examples/dds/runSink.cxx b/examples/dds/runSink.cxx deleted file mode 100644 index 63cad938..00000000 --- a/examples/dds/runSink.cxx +++ /dev/null @@ -1,25 +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()( - "iterations,i", - bpo::value()->default_value(1000), - "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_dds::Sink(); -} diff --git a/examples/dds/sampler.cxx b/examples/dds/sampler.cxx new file mode 100644 index 00000000..3409e9cd --- /dev/null +++ b/examples/dds/sampler.cxx @@ -0,0 +1,71 @@ +/******************************************************************************** + * 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 +#include + +#include // uint64_t + +namespace bpo = boost::program_options; + +class Sampler : public FairMQDevice +{ + public: + Sampler() + : fIterations(0) + , fCounter(0) + {} + + void InitTask() override + { + fIterations = fConfig->GetValue("iterations"); + } + + protected: + bool ConditionalRun() override + { + // 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) { + return false; + } + + if (fIterations > 0) { + ++fCounter; + if (fCounter >= fIterations) { + LOG(info) << "Sent " << fCounter << " messages. Finished."; + return false; + } + } + + return true; + } + + private: + uint64_t fIterations; + uint64_t fCounter; +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options()( + "iterations,i", + bpo::value()->default_value(1000), + "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/dds/sink.cxx b/examples/dds/sink.cxx new file mode 100644 index 00000000..a6586847 --- /dev/null +++ b/examples/dds/sink.cxx @@ -0,0 +1,62 @@ +/******************************************************************************** + * 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 +#include + +#include + +namespace bpo = boost::program_options; + +class Sink : public FairMQDevice +{ + public: + Sink() + : fIterations(0) + , fCounter(0) + { + OnData("data2", &Sink::HandleData); + } + + void InitTask() override + { + fIterations = fConfig->GetValue("iterations"); + } + + protected: + bool HandleData(FairMQMessagePtr& msg, int) + { + LOG(info) << "Received: \"" << std::string(static_cast(msg->GetData()), msg->GetSize()) << "\""; + + if (fIterations > 0) { + ++fCounter; + if (fCounter >= fIterations) { + LOG(info) << "Received " << fCounter << " messages. Finished."; + return false; + } + } + // return true if want to be called again (otherwise go to IDLE state) + return true; + } + + private: + uint64_t fIterations; + uint64_t fCounter; +}; +void addCustomOptions(bpo::options_description& options) +{ + options.add_options()( + "iterations,i", + bpo::value()->default_value(1000), + "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/multipart/CMakeLists.txt b/examples/multipart/CMakeLists.txt index bb3ef930..281fade0 100644 --- a/examples/multipart/CMakeLists.txt +++ b/examples/multipart/CMakeLists.txt @@ -6,20 +6,11 @@ # copied verbatim in the file "LICENSE" # ################################################################################ -add_library(ExampleMultipartLib STATIC - "Sampler.cxx" - "Sampler.h" - "Sink.cxx" - "Sink.h" -) +add_executable(fairmq-ex-multipart-sampler sampler.cxx) +target_link_libraries(fairmq-ex-multipart-sampler PRIVATE FairMQ) -target_link_libraries(ExampleMultipartLib PUBLIC FairMQ) - -add_executable(fairmq-ex-multipart-sampler runSampler.cxx) -target_link_libraries(fairmq-ex-multipart-sampler PRIVATE ExampleMultipartLib) - -add_executable(fairmq-ex-multipart-sink runSink.cxx) -target_link_libraries(fairmq-ex-multipart-sink PRIVATE ExampleMultipartLib) +add_executable(fairmq-ex-multipart-sink sink.cxx) +target_link_libraries(fairmq-ex-multipart-sink PRIVATE FairMQ) add_custom_target(ExampleMultipart DEPENDS fairmq-ex-multipart-sampler fairmq-ex-multipart-sink) diff --git a/examples/multipart/Header.h b/examples/multipart/Header.h index c827dd81..d6e53dc9 100644 --- a/examples/multipart/Header.h +++ b/examples/multipart/Header.h @@ -9,6 +9,8 @@ #ifndef FAIRMQEXAMPLEMULTIPARTHEADER_H #define FAIRMQEXAMPLEMULTIPARTHEADER_H +#include + namespace example_multipart { @@ -19,4 +21,4 @@ struct Header } -#endif /* FAIRMQEXAMPLEMULTIPARTHEADER_H */ \ No newline at end of file +#endif /* FAIRMQEXAMPLEMULTIPARTHEADER_H */ diff --git a/examples/multipart/Sampler.cxx b/examples/multipart/Sampler.cxx deleted file mode 100644 index 2939144d..00000000 --- a/examples/multipart/Sampler.cxx +++ /dev/null @@ -1,86 +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" * - ********************************************************************************/ -/** - * Sampler.cpp - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#include // this_thread::sleep_for -#include - -#include "Sampler.h" -#include "Header.h" - -using namespace std; - -namespace example_multipart -{ - -Sampler::Sampler() - : fMaxIterations(5) - , fNumIterations(0) -{ -} - -void Sampler::InitTask() -{ - fMaxIterations = fConfig->GetProperty("max-iterations"); -} - -bool Sampler::ConditionalRun() -{ - Header header; - header.stopFlag = 0; - - // Set stopFlag to 1 for last message. - if (fMaxIterations > 0 && fNumIterations == fMaxIterations - 1) { - 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)); - - // create more data parts, testing the FairMQParts in-place constructor - FairMQParts auxData{ NewMessage(500), NewMessage(600), NewMessage(700) }; - assert(auxData.Size() == 3); - parts.AddPart(std::move(auxData)); - assert(auxData.Size() == 0); - assert(parts.Size() == 5); - - parts.AddPart(NewMessage()); - - assert(parts.Size() == 6); - - parts.AddPart(NewMessage(100)); - - assert(parts.Size() == 7); - - LOG(info) << "Sending body of size: " << parts.At(1)->GetSize(); - - Send(parts, "data"); - - // Go out of the sending loop if the stopFlag was sent. - if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - return false; - } - - // Wait a second to keep the output readable. - this_thread::sleep_for(chrono::seconds(1)); - - return true; -} - -} // namespace example_multipart diff --git a/examples/multipart/Sampler.h b/examples/multipart/Sampler.h deleted file mode 100644 index 6d5904fe..00000000 --- a/examples/multipart/Sampler.h +++ /dev/null @@ -1,40 +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" * - ********************************************************************************/ -/** - * Sampler.h - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#ifndef FAIRMQEXAMPLEDDSSAMPLER_H -#define FAIRMQEXAMPLEDDSSAMPLER_H - -#include "FairMQDevice.h" - -namespace example_multipart -{ - -class Sampler : public FairMQDevice -{ - public: - Sampler(); - virtual ~Sampler() {} - - protected: - virtual void InitTask(); - virtual bool ConditionalRun(); - - private: - uint64_t fMaxIterations; - uint64_t fNumIterations; -}; - -} // namespace example_multipart - -#endif /* FAIRMQEXAMPLEDDSSAMPLER_H */ diff --git a/examples/multipart/Sink.cxx b/examples/multipart/Sink.cxx deleted file mode 100644 index 0cd018cf..00000000 --- a/examples/multipart/Sink.cxx +++ /dev/null @@ -1,52 +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" -#include "Header.h" - -using namespace std; - -namespace example_multipart -{ - -bool Sink::HandleData(FairMQParts& parts, int /*index*/) -{ - LOG(info) << "Received message with " << parts.Size() << " parts"; - - Header header; - header.stopFlag = (static_cast(parts.At(0)->GetData()))->stopFlag; - - LOG(info) << "Received part 1 (header) with stopFlag: " << header.stopFlag; - LOG(info) << "Received part 2 of size: " << parts.At(1)->GetSize(); - assert(parts.At(1)->GetSize() == 1000); - LOG(info) << "Received part 3 of size: " << parts.At(2)->GetSize(); - assert(parts.At(2)->GetSize() == 500); - LOG(info) << "Received part 4 of size: " << parts.At(3)->GetSize(); - assert(parts.At(3)->GetSize() == 600); - LOG(info) << "Received part 5 of size: " << parts.At(4)->GetSize(); - assert(parts.At(4)->GetSize() == 700); - LOG(info) << "Received part 6 of size: " << parts.At(5)->GetSize(); - assert(parts.At(5)->GetSize() == 0); - LOG(info) << "Received part 7 of size: " << parts.At(6)->GetSize(); - assert(parts.At(6)->GetSize() == 100); - - if (header.stopFlag == 1) { - LOG(info) << "stopFlag is 1, going IDLE"; - return false; - } - - return true; -} - -} diff --git a/examples/multipart/Sink.h b/examples/multipart/Sink.h deleted file mode 100644 index 4b24f91e..00000000 --- a/examples/multipart/Sink.h +++ /dev/null @@ -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" * - ********************************************************************************/ -/** - * Sink.h - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#ifndef FAIRMQEXAMPLEMULTIPARTSINK_H -#define FAIRMQEXAMPLEMULTIPARTSINK_H - -#include "FairMQDevice.h" - -namespace example_multipart -{ - -class Sink : public FairMQDevice -{ - public: - Sink() { OnData("data", &Sink::HandleData); } - virtual ~Sink() {} - - protected: - bool HandleData(FairMQParts&, int); -}; - -} -#endif /* FAIRMQEXAMPLEMULTIPARTSINK_H */ diff --git a/examples/multipart/runSampler.cxx b/examples/multipart/runSampler.cxx deleted file mode 100644 index 7bbc1925..00000000 --- a/examples/multipart/runSampler.cxx +++ /dev/null @@ -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 "Sampler.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("max-iterations", bpo::value()->default_value(5), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_multipart::Sampler(); -} diff --git a/examples/multipart/runSink.cxx b/examples/multipart/runSink.cxx deleted file mode 100644 index 13c74558..00000000 --- a/examples/multipart/runSink.cxx +++ /dev/null @@ -1,21 +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*/) -{ -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_multipart::Sink(); -} diff --git a/examples/multipart/sampler.cxx b/examples/multipart/sampler.cxx new file mode 100644 index 00000000..a9570585 --- /dev/null +++ b/examples/multipart/sampler.cxx @@ -0,0 +1,97 @@ +/******************************************************************************** + * 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 "Header.h" + +#include +#include + +#include +#include +#include // this_thread::sleep_for + +namespace bpo = boost::program_options; + +class Sampler : public FairMQDevice +{ + public: + Sampler() + : fMaxIterations(5) + , fNumIterations(0) + {} + + protected: + void InitTask() override + { + fMaxIterations = fConfig->GetProperty("max-iterations"); + } + + bool ConditionalRun() override + { + example_multipart::Header header; + header.stopFlag = 0; + + // Set stopFlag to 1 for last message. + if (fMaxIterations > 0 && fNumIterations == fMaxIterations - 1) { + 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)); + + // create more data parts, testing the FairMQParts in-place constructor + FairMQParts auxData{ NewMessage(500), NewMessage(600), NewMessage(700) }; + assert(auxData.Size() == 3); + parts.AddPart(std::move(auxData)); + assert(auxData.Size() == 0); + assert(parts.Size() == 5); + + parts.AddPart(NewMessage()); + + assert(parts.Size() == 6); + + parts.AddPart(NewMessage(100)); + + assert(parts.Size() == 7); + + LOG(info) << "Sending body of size: " << parts.At(1)->GetSize(); + + Send(parts, "data"); + + // Go out of the sending loop if the stopFlag was sent. + if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + return false; + } + + // Wait a second to keep the output readable. + std::this_thread::sleep_for(std::chrono::seconds(1)); + + return true; + } + + private: + uint64_t fMaxIterations; + uint64_t fNumIterations; +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("max-iterations", bpo::value()->default_value(5), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/multipart/sink.cxx b/examples/multipart/sink.cxx new file mode 100644 index 00000000..f93620f1 --- /dev/null +++ b/examples/multipart/sink.cxx @@ -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 "Header.h" + +#include +#include + +namespace bpo = boost::program_options; + +class Sink : public FairMQDevice +{ + public: + Sink() + { + OnData("data", &Sink::HandleData); + } + + protected: + bool HandleData(FairMQParts& parts, int) + { + LOG(info) << "Received message with " << parts.Size() << " parts"; + + example_multipart::Header header; + header.stopFlag = (static_cast(parts.At(0)->GetData()))->stopFlag; + + LOG(info) << "Received part 1 (header) with stopFlag: " << header.stopFlag; + LOG(info) << "Received part 2 of size: " << parts.At(1)->GetSize(); + assert(parts.At(1)->GetSize() == 1000); + LOG(info) << "Received part 3 of size: " << parts.At(2)->GetSize(); + assert(parts.At(2)->GetSize() == 500); + LOG(info) << "Received part 4 of size: " << parts.At(3)->GetSize(); + assert(parts.At(3)->GetSize() == 600); + LOG(info) << "Received part 5 of size: " << parts.At(4)->GetSize(); + assert(parts.At(4)->GetSize() == 700); + LOG(info) << "Received part 6 of size: " << parts.At(5)->GetSize(); + assert(parts.At(5)->GetSize() == 0); + LOG(info) << "Received part 7 of size: " << parts.At(6)->GetSize(); + assert(parts.At(6)->GetSize() == 100); + + if (header.stopFlag == 1) { + LOG(info) << "stopFlag is 1, going IDLE"; + return false; + } + + return true; + } + +}; + +void addCustomOptions(bpo::options_description& /*options*/) +{ +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/multiple-channels/Broadcaster.cxx b/examples/multiple-channels/Broadcaster.cxx deleted file mode 100644 index 8f8423fe..00000000 --- a/examples/multiple-channels/Broadcaster.cxx +++ /dev/null @@ -1,48 +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" * - ********************************************************************************/ -/** - * Broadcaster.cpp - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#include // this_thread::sleep_for -#include - -#include "Broadcaster.h" - -using namespace std; - -namespace example_multiple_channels -{ - -Broadcaster::Broadcaster() -{ -} - -bool Broadcaster::ConditionalRun() -{ - this_thread::sleep_for(chrono::seconds(1)); - - // 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")); - - LOG(info) << "Sending OK"; - - Send(msg, "broadcast"); - - return true; -} - -Broadcaster::~Broadcaster() -{ -} - -} diff --git a/examples/multiple-channels/Broadcaster.h b/examples/multiple-channels/Broadcaster.h deleted file mode 100644 index 6d29e86b..00000000 --- a/examples/multiple-channels/Broadcaster.h +++ /dev/null @@ -1,35 +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" * - ********************************************************************************/ -/** - * Broadcaster.h - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#ifndef FAIRMQEXAMPLEMULTIPLECHANNELSBROADCASTER_H -#define FAIRMQEXAMPLEMULTIPLECHANNELSBROADCASTER_H - -#include "FairMQDevice.h" - -namespace example_multiple_channels -{ - -class Broadcaster : public FairMQDevice -{ - public: - Broadcaster(); - virtual ~Broadcaster(); - - protected: - virtual bool ConditionalRun(); -}; - -} - -#endif /* FAIRMQEXAMPLEMULTIPLECHANNELSBROADCASTER_H */ diff --git a/examples/multiple-channels/CMakeLists.txt b/examples/multiple-channels/CMakeLists.txt index 39b85e14..f6aafc26 100644 --- a/examples/multiple-channels/CMakeLists.txt +++ b/examples/multiple-channels/CMakeLists.txt @@ -6,25 +6,14 @@ # copied verbatim in the file "LICENSE" # ################################################################################ -add_library(ExampleMultipleChannelsLib STATIC - "Sampler.cxx" - "Sampler.h" - "Broadcaster.cxx" - "Broadcaster.h" - "Sink.cxx" - "Sink.h" -) +add_executable(fairmq-ex-multiple-channels-sampler sampler.cxx) +target_link_libraries(fairmq-ex-multiple-channels-sampler PRIVATE FairMQ) -target_link_libraries(ExampleMultipleChannelsLib PUBLIC FairMQ) +add_executable(fairmq-ex-multiple-channels-broadcaster broadcaster.cxx) +target_link_libraries(fairmq-ex-multiple-channels-broadcaster PRIVATE FairMQ) -add_executable(fairmq-ex-multiple-channels-sampler runSampler.cxx) -target_link_libraries(fairmq-ex-multiple-channels-sampler PRIVATE ExampleMultipleChannelsLib) - -add_executable(fairmq-ex-multiple-channels-broadcaster runBroadcaster.cxx) -target_link_libraries(fairmq-ex-multiple-channels-broadcaster PRIVATE ExampleMultipleChannelsLib) - -add_executable(fairmq-ex-multiple-channels-sink runSink.cxx) -target_link_libraries(fairmq-ex-multiple-channels-sink PRIVATE ExampleMultipleChannelsLib) +add_executable(fairmq-ex-multiple-channels-sink sink.cxx) +target_link_libraries(fairmq-ex-multiple-channels-sink PRIVATE FairMQ) add_custom_target(ExampleMultipleChannels DEPENDS fairmq-ex-multiple-channels-sampler fairmq-ex-multiple-channels-broadcaster fairmq-ex-multiple-channels-sink) diff --git a/examples/multiple-channels/Sampler.cxx b/examples/multiple-channels/Sampler.cxx deleted file mode 100644 index e7f7a758..00000000 --- a/examples/multiple-channels/Sampler.cxx +++ /dev/null @@ -1,82 +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" * - ********************************************************************************/ -/** - * Sampler.cpp - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#include // unique_ptr -#include // this_thread::sleep_for -#include - -#include "Sampler.h" -#include "FairMQPoller.h" - -using namespace std; - -namespace example_multiple_channels -{ - -Sampler::Sampler() - : fText() - , fMaxIterations(0) - , fNumIterations(0) -{ -} - -void Sampler::InitTask() -{ - fText = fConfig->GetProperty("text"); - fMaxIterations = fConfig->GetProperty("max-iterations"); -} - -void Sampler::Run() -{ - FairMQPollerPtr poller(NewPoller("data", "broadcast")); - - while (!NewStatePending()) - { - poller->Poll(100); - - if (poller->CheckInput("broadcast", 0)) - { - FairMQMessagePtr msg(NewMessage()); - - if (Receive(msg, "broadcast") > 0) - { - LOG(info) << "Received broadcast: \"" << string(static_cast(msg->GetData()), msg->GetSize()) << "\""; - } - } - - if (poller->CheckOutput("data", 0)) - { - this_thread::sleep_for(chrono::seconds(1)); - - FairMQMessagePtr msg(NewSimpleMessage(fText)); - - if (Send(msg, "data") > 0) - { - LOG(info) << "Sent \"" << fText << "\""; - - if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) - { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - break; - } - } - } - } -} - -Sampler::~Sampler() -{ -} - -} diff --git a/examples/multiple-channels/Sampler.h b/examples/multiple-channels/Sampler.h deleted file mode 100644 index 8e975430..00000000 --- a/examples/multiple-channels/Sampler.h +++ /dev/null @@ -1,42 +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" * - ********************************************************************************/ -/** - * Sampler.h - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#ifndef FAIRMQEXAMPLEMULTIPLECHANNELSSAMPLER_H -#define FAIRMQEXAMPLEMULTIPLECHANNELSSAMPLER_H - -#include - -#include "FairMQDevice.h" - -namespace example_multiple_channels -{ - -class Sampler : public FairMQDevice -{ - public: - Sampler(); - virtual ~Sampler(); - - protected: - std::string fText; - uint64_t fMaxIterations; - uint64_t fNumIterations; - - virtual void Run(); - virtual void InitTask(); -}; - -} - -#endif /* FAIRMQEXAMPLEMULTIPLECHANNELSSAMPLER_H */ diff --git a/examples/multiple-channels/Sink.cxx b/examples/multiple-channels/Sink.cxx deleted file mode 100644 index 473e4940..00000000 --- a/examples/multiple-channels/Sink.cxx +++ /dev/null @@ -1,71 +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_channels -{ - -Sink::Sink() - : fReceivedData(false) - , fReceivedBroadcast(false) - , fMaxIterations(0) - , fNumIterations(0) -{ - OnData("broadcast", &Sink::HandleBroadcast); - OnData("data", &Sink::HandleData); -} - -void Sink::InitTask() -{ - fMaxIterations = fConfig->GetProperty("max-iterations"); -} - -bool Sink::HandleBroadcast(FairMQMessagePtr& msg, int /*index*/) -{ - LOG(info) << "Received broadcast: \"" << string(static_cast(msg->GetData()), msg->GetSize()) << "\""; - fReceivedBroadcast = true; - - return CheckIterations(); -} - -bool Sink::HandleData(FairMQMessagePtr& msg, int /*index*/) -{ - LOG(info) << "Received message: \"" << string(static_cast(msg->GetData()), msg->GetSize()) << "\""; - fReceivedData = true; - - return CheckIterations(); -} - -bool Sink::CheckIterations() -{ - if (fMaxIterations > 0) - { - if (fReceivedData && fReceivedBroadcast && ++fNumIterations >= fMaxIterations) - { - LOG(info) << "Configured maximum number of iterations reached & Received messages from both sources. Leaving RUNNING state."; - return false; - } - } - - return true; -} - -Sink::~Sink() -{ -} - -} diff --git a/examples/multiple-channels/Sink.h b/examples/multiple-channels/Sink.h deleted file mode 100644 index 775de846..00000000 --- a/examples/multiple-channels/Sink.h +++ /dev/null @@ -1,44 +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 FAIRMQEXAMPLEMULTIPLECHANNELSSINK_H -#define FAIRMQEXAMPLEMULTIPLECHANNELSSINK_H - -#include "FairMQDevice.h" - -namespace example_multiple_channels -{ - -class Sink : public FairMQDevice -{ - public: - Sink(); - virtual ~Sink(); - - protected: - bool HandleBroadcast(FairMQMessagePtr&, int); - bool HandleData(FairMQMessagePtr&, int); - bool CheckIterations(); - virtual void InitTask(); - - private: - bool fReceivedData; - bool fReceivedBroadcast; - uint64_t fMaxIterations; - uint64_t fNumIterations; -}; - -} - -#endif /* FAIRMQEXAMPLEMULTIPLECHANNELSSINK_H */ diff --git a/examples/multiple-channels/broadcaster.cxx b/examples/multiple-channels/broadcaster.cxx new file mode 100644 index 00000000..d9ec921e --- /dev/null +++ b/examples/multiple-channels/broadcaster.cxx @@ -0,0 +1,46 @@ +/******************************************************************************** + * 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 +#include + +#include // this_thread::sleep_for +#include + +namespace bpo = boost::program_options; + +class Broadcaster : public FairMQDevice +{ + public: + Broadcaster() {} + + protected: + bool ConditionalRun() override + { + std::this_thread::sleep_for(std::chrono::seconds(1)); + + // 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")); + + LOG(info) << "Sending OK"; + + Send(msg, "broadcast"); + + return true; + } +}; + +void addCustomOptions(bpo::options_description& /*options*/) +{ +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/multiple-channels/runBroadcaster.cxx b/examples/multiple-channels/runBroadcaster.cxx deleted file mode 100644 index 4fb25a6a..00000000 --- a/examples/multiple-channels/runBroadcaster.cxx +++ /dev/null @@ -1,21 +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 "Broadcaster.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& /*options*/) -{ -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_multiple_channels::Broadcaster(); -} diff --git a/examples/multiple-channels/runSampler.cxx b/examples/multiple-channels/runSampler.cxx deleted file mode 100644 index a8a4fd1b..00000000 --- a/examples/multiple-channels/runSampler.cxx +++ /dev/null @@ -1,24 +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 "Sampler.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("text", bpo::value()->default_value("Hello"), "Text to send out") - ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_multiple_channels::Sampler(); -} diff --git a/examples/multiple-channels/runSink.cxx b/examples/multiple-channels/runSink.cxx deleted file mode 100644 index 756e0da0..00000000 --- a/examples/multiple-channels/runSink.cxx +++ /dev/null @@ -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()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_multiple_channels::Sink(); -} diff --git a/examples/multiple-channels/sampler.cxx b/examples/multiple-channels/sampler.cxx new file mode 100644 index 00000000..6e6a898e --- /dev/null +++ b/examples/multiple-channels/sampler.cxx @@ -0,0 +1,81 @@ +/******************************************************************************** + * 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 +#include +#include + +#include +#include +#include // this_thread::sleep_for + +namespace bpo = boost::program_options; + +class Sampler : public FairMQDevice +{ + public: + Sampler() + : fMaxIterations(0) + , fNumIterations(0) + {} + + void InitTask() override + { + fText = fConfig->GetProperty("text"); + fMaxIterations = fConfig->GetProperty("max-iterations"); + } + + void Run() override + { + FairMQPollerPtr poller(NewPoller("data", "broadcast")); + + while (!NewStatePending()) { + poller->Poll(100); + + if (poller->CheckInput("broadcast", 0)) { + FairMQMessagePtr msg(NewMessage()); + + if (Receive(msg, "broadcast") > 0) { + LOG(info) << "Received broadcast: \"" << std::string(static_cast(msg->GetData()), msg->GetSize()) << "\""; + } + } + + if (poller->CheckOutput("data", 0)) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + + FairMQMessagePtr msg(NewSimpleMessage(fText)); + + if (Send(msg, "data") > 0) { + LOG(info) << "Sent \"" << fText << "\""; + + if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + break; + } + } + } + } + } + + protected: + std::string fText; + uint64_t fMaxIterations; + uint64_t fNumIterations; +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("text", bpo::value()->default_value("Hello"), "Text to send out") + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/multiple-channels/sink.cxx b/examples/multiple-channels/sink.cxx new file mode 100644 index 00000000..fa9f11ec --- /dev/null +++ b/examples/multiple-channels/sink.cxx @@ -0,0 +1,79 @@ +/******************************************************************************** + * 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 +#include + +#include // uint64_t +#include + +namespace bpo = boost::program_options; + +class Sink : public FairMQDevice +{ + public: + Sink() + : fReceivedData(false) + , fReceivedBroadcast(false) + , fMaxIterations(0) + , fNumIterations(0) + { + OnData("broadcast", &Sink::HandleBroadcast); + OnData("data", &Sink::HandleData); + } + + void InitTask() override + { + fMaxIterations = fConfig->GetProperty("max-iterations"); + } + + bool HandleBroadcast(FairMQMessagePtr& msg, int /*index*/) + { + LOG(info) << "Received broadcast: \"" << std::string(static_cast(msg->GetData()), msg->GetSize()) << "\""; + fReceivedBroadcast = true; + + return CheckIterations(); + } + + bool HandleData(FairMQMessagePtr& msg, int /*index*/) + { + LOG(info) << "Received message: \"" << std::string(static_cast(msg->GetData()), msg->GetSize()) << "\""; + fReceivedData = true; + + return CheckIterations(); + } + + bool CheckIterations() + { + if (fMaxIterations > 0) { + if (fReceivedData && fReceivedBroadcast && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached & Received messages from both sources. Leaving RUNNING state."; + return false; + } + } + + return true; + } + + private: + bool fReceivedData; + bool fReceivedBroadcast; + uint64_t fMaxIterations; + uint64_t fNumIterations; +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/multiple-transports/CMakeLists.txt b/examples/multiple-transports/CMakeLists.txt index 08dd987b..61df33a5 100644 --- a/examples/multiple-transports/CMakeLists.txt +++ b/examples/multiple-transports/CMakeLists.txt @@ -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) diff --git a/examples/multiple-transports/Sampler1.cxx b/examples/multiple-transports/Sampler1.cxx deleted file mode 100644 index d9c87c4b..00000000 --- a/examples/multiple-transports/Sampler1.cxx +++ /dev/null @@ -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("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 diff --git a/examples/multiple-transports/Sampler1.h b/examples/multiple-transports/Sampler1.h deleted file mode 100644 index 26a13832..00000000 --- a/examples/multiple-transports/Sampler1.h +++ /dev/null @@ -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 - -#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 */ diff --git a/examples/multiple-transports/Sampler2.cxx b/examples/multiple-transports/Sampler2.cxx deleted file mode 100644 index 9fcc9519..00000000 --- a/examples/multiple-transports/Sampler2.cxx +++ /dev/null @@ -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("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 diff --git a/examples/multiple-transports/Sampler2.h b/examples/multiple-transports/Sampler2.h deleted file mode 100644 index df64e9e5..00000000 --- a/examples/multiple-transports/Sampler2.h +++ /dev/null @@ -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 */ diff --git a/examples/multiple-transports/Sink.cxx b/examples/multiple-transports/Sink.cxx deleted file mode 100644 index 183cc417..00000000 --- a/examples/multiple-transports/Sink.cxx +++ /dev/null @@ -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("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 diff --git a/examples/multiple-transports/Sink.h b/examples/multiple-transports/Sink.h deleted file mode 100644 index 559bb991..00000000 --- a/examples/multiple-transports/Sink.h +++ /dev/null @@ -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 */ diff --git a/examples/multiple-transports/runSampler1.cxx b/examples/multiple-transports/runSampler1.cxx deleted file mode 100644 index 46c602e1..00000000 --- a/examples/multiple-transports/runSampler1.cxx +++ /dev/null @@ -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()->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(); -} diff --git a/examples/multiple-transports/runSampler2.cxx b/examples/multiple-transports/runSampler2.cxx deleted file mode 100644 index e1a0886f..00000000 --- a/examples/multiple-transports/runSampler2.cxx +++ /dev/null @@ -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()->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(); -} diff --git a/examples/multiple-transports/runSink.cxx b/examples/multiple-transports/runSink.cxx deleted file mode 100644 index 4af2d398..00000000 --- a/examples/multiple-transports/runSink.cxx +++ /dev/null @@ -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()->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(); -} diff --git a/examples/multiple-transports/sampler1.cxx b/examples/multiple-transports/sampler1.cxx new file mode 100644 index 00000000..8b4d34d9 --- /dev/null +++ b/examples/multiple-transports/sampler1.cxx @@ -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 +#include + +#include + +namespace bpo = boost::program_options; + +class Sampler1 : public FairMQDevice +{ + public: + Sampler1() + : fAckListener() + , fMaxIterations(0) + , fNumIterations(0) + {} + + protected: + void InitTask() override + { + fMaxIterations = fConfig->GetProperty("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()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/multiple-transports/sampler2.cxx b/examples/multiple-transports/sampler2.cxx new file mode 100644 index 00000000..3e9c5c82 --- /dev/null +++ b/examples/multiple-transports/sampler2.cxx @@ -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 +#include + +#include // uint64_t + +namespace bpo = boost::program_options; + +class Sampler2 : public FairMQDevice +{ + public: + Sampler2() + : fMaxIterations(0) + , fNumIterations(0) + {} + + protected: + void InitTask() override + { + fMaxIterations = fConfig->GetProperty("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()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/multiple-transports/sink.cxx b/examples/multiple-transports/sink.cxx new file mode 100644 index 00000000..f7fa2e30 --- /dev/null +++ b/examples/multiple-transports/sink.cxx @@ -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 +#include + +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("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()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/n-m/CMakeLists.txt b/examples/n-m/CMakeLists.txt index ea7ba202..0a4a1de8 100644 --- a/examples/n-m/CMakeLists.txt +++ b/examples/n-m/CMakeLists.txt @@ -6,13 +6,13 @@ # copied verbatim in the file "LICENSE" # ################################################################################ -add_executable(fairmq-ex-n-m-synchronizer runSynchronizer.cxx) +add_executable(fairmq-ex-n-m-synchronizer synchronizer.cxx) target_link_libraries(fairmq-ex-n-m-synchronizer PRIVATE FairMQ) -add_executable(fairmq-ex-n-m-sender runSender.cxx) +add_executable(fairmq-ex-n-m-sender sender.cxx) target_link_libraries(fairmq-ex-n-m-sender PRIVATE FairMQ) -add_executable(fairmq-ex-n-m-receiver runReceiver.cxx) +add_executable(fairmq-ex-n-m-receiver receiver.cxx) target_link_libraries(fairmq-ex-n-m-receiver PRIVATE FairMQ) add_custom_target(ExampleNM DEPENDS fairmq-ex-n-m-synchronizer fairmq-ex-n-m-sender fairmq-ex-n-m-receiver) diff --git a/examples/n-m/runReceiver.cxx b/examples/n-m/receiver.cxx similarity index 95% rename from examples/n-m/runReceiver.cxx rename to examples/n-m/receiver.cxx index a1ea4b8b..f51599fd 100644 --- a/examples/n-m/runReceiver.cxx +++ b/examples/n-m/receiver.cxx @@ -8,8 +8,8 @@ #include "Header.h" -#include -#include +#include +#include #include #include @@ -116,4 +116,7 @@ void addCustomOptions(bpo::options_description& options) ("max-timeframes", bpo::value()->default_value(0), "Maximum number of timeframes to receive (0 - unlimited)"); } -FairMQDevice* getDevice(const FairMQProgOptions& /* config */) { return new Receiver(); } +std::unique_ptr getDevice(FairMQProgOptions& /* config */) +{ + return std::make_unique(); +} diff --git a/examples/n-m/runSender.cxx b/examples/n-m/sender.cxx similarity index 93% rename from examples/n-m/runSender.cxx rename to examples/n-m/sender.cxx index c51a438a..b9d54b79 100644 --- a/examples/n-m/runSender.cxx +++ b/examples/n-m/sender.cxx @@ -8,8 +8,8 @@ #include "Header.h" -#include -#include +#include +#include #include @@ -76,4 +76,7 @@ void addCustomOptions(bpo::options_description& options) ("subtimeframe-size", bpo::value()->default_value(1000), "Subtimeframe size in bytes") ("num-receivers", bpo::value()->required(), "Number of EPNs"); } -FairMQDevice* getDevice(const FairMQProgOptions& /* config */) { return new Sender(); } +std::unique_ptr getDevice(FairMQProgOptions& /* config */) +{ + return std::make_unique(); +} diff --git a/examples/n-m/runSynchronizer.cxx b/examples/n-m/synchronizer.cxx similarity index 87% rename from examples/n-m/runSynchronizer.cxx rename to examples/n-m/synchronizer.cxx index 58627602..48c7739c 100644 --- a/examples/n-m/runSynchronizer.cxx +++ b/examples/n-m/synchronizer.cxx @@ -6,8 +6,8 @@ * copied verbatim in the file "LICENSE" * ********************************************************************************/ -#include -#include +#include +#include #include #include @@ -43,4 +43,7 @@ class Synchronizer : public FairMQDevice }; void addCustomOptions(bpo::options_description& /* options */) {} -FairMQDevice* getDevice(const FairMQProgOptions& /* config */) { return new Synchronizer(); } +std::unique_ptr getDevice(FairMQProgOptions& /* config */) +{ + return std::make_unique(); +} diff --git a/examples/qc/CMakeLists.txt b/examples/qc/CMakeLists.txt index 5167836b..fd466c28 100644 --- a/examples/qc/CMakeLists.txt +++ b/examples/qc/CMakeLists.txt @@ -6,16 +6,16 @@ # copied verbatim in the file "LICENSE" # ################################################################################ -add_executable(fairmq-ex-qc-sampler runSampler.cxx) +add_executable(fairmq-ex-qc-sampler sampler.cxx) target_link_libraries(fairmq-ex-qc-sampler PRIVATE FairMQ) -add_executable(fairmq-ex-qc-dispatcher runQCDispatcher.cxx) +add_executable(fairmq-ex-qc-dispatcher qCDispatcher.cxx) target_link_libraries(fairmq-ex-qc-dispatcher PRIVATE FairMQ) -add_executable(fairmq-ex-qc-task runQCTask.cxx) +add_executable(fairmq-ex-qc-task qCTask.cxx) target_link_libraries(fairmq-ex-qc-task PRIVATE FairMQ) -add_executable(fairmq-ex-qc-sink runSink.cxx) +add_executable(fairmq-ex-qc-sink sink.cxx) target_link_libraries(fairmq-ex-qc-sink PRIVATE FairMQ) add_custom_target(ExampleQC DEPENDS fairmq-ex-qc-sampler fairmq-ex-qc-dispatcher fairmq-ex-qc-task fairmq-ex-qc-sink) diff --git a/examples/qc/runQCDispatcher.cxx b/examples/qc/qCDispatcher.cxx similarity index 90% rename from examples/qc/runQCDispatcher.cxx rename to examples/qc/qCDispatcher.cxx index d0fedba4..56b9956c 100644 --- a/examples/qc/runQCDispatcher.cxx +++ b/examples/qc/qCDispatcher.cxx @@ -6,8 +6,8 @@ * copied verbatim in the file "LICENSE" * ********************************************************************************/ -#include "runFairMQDevice.h" -#include "FairMQDevice.h" +#include +#include class QCDispatcher : public FairMQDevice { @@ -56,4 +56,7 @@ class QCDispatcher : public FairMQDevice }; void addCustomOptions(boost::program_options::options_description& /*options*/) {} -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) { return new QCDispatcher(); } +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/qc/runQCTask.cxx b/examples/qc/qCTask.cxx similarity index 83% rename from examples/qc/runQCTask.cxx rename to examples/qc/qCTask.cxx index 6cb4d636..54bfed57 100644 --- a/examples/qc/runQCTask.cxx +++ b/examples/qc/qCTask.cxx @@ -6,8 +6,8 @@ * copied verbatim in the file "LICENSE" * ********************************************************************************/ -#include "runFairMQDevice.h" -#include "FairMQDevice.h" +#include +#include class QCTask : public FairMQDevice { @@ -23,4 +23,7 @@ class QCTask : public FairMQDevice namespace bpo = boost::program_options; void addCustomOptions(bpo::options_description& /*options*/) {} -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) { return new QCTask(); } +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/qc/runSampler.cxx b/examples/qc/sampler.cxx similarity index 85% rename from examples/qc/runSampler.cxx rename to examples/qc/sampler.cxx index 952d3549..df05deca 100644 --- a/examples/qc/runSampler.cxx +++ b/examples/qc/sampler.cxx @@ -6,8 +6,8 @@ * copied verbatim in the file "LICENSE" * ********************************************************************************/ -#include "runFairMQDevice.h" -#include "FairMQDevice.h" +#include +#include #include // this_thread::sleep_for #include @@ -33,4 +33,7 @@ class Sampler : public FairMQDevice namespace bpo = boost::program_options; void addCustomOptions(bpo::options_description&) {} -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) { return new Sampler(); } +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/qc/runSink.cxx b/examples/qc/sink.cxx similarity index 83% rename from examples/qc/runSink.cxx rename to examples/qc/sink.cxx index 844e0be5..ece5a35f 100644 --- a/examples/qc/runSink.cxx +++ b/examples/qc/sink.cxx @@ -6,8 +6,8 @@ * copied verbatim in the file "LICENSE" * ********************************************************************************/ -#include "runFairMQDevice.h" -#include "FairMQDevice.h" +#include +#include #include @@ -22,4 +22,7 @@ class Sink : public FairMQDevice namespace bpo = boost::program_options; void addCustomOptions(bpo::options_description&) {} -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) { return new Sink(); } +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/readout/CMakeLists.txt b/examples/readout/CMakeLists.txt index 3ff9d559..288af33a 100644 --- a/examples/readout/CMakeLists.txt +++ b/examples/readout/CMakeLists.txt @@ -6,19 +6,19 @@ # copied verbatim in the file "LICENSE" # ################################################################################ -add_executable(fairmq-ex-readout-readout runReadout.cxx) +add_executable(fairmq-ex-readout-readout readout.cxx) target_link_libraries(fairmq-ex-readout-readout PRIVATE FairMQ) -add_executable(fairmq-ex-readout-builder runBuilder.cxx) +add_executable(fairmq-ex-readout-builder builder.cxx) target_link_libraries(fairmq-ex-readout-builder PRIVATE FairMQ) -add_executable(fairmq-ex-readout-processor runProcessor.cxx) +add_executable(fairmq-ex-readout-processor processor.cxx) target_link_libraries(fairmq-ex-readout-processor PRIVATE FairMQ) -add_executable(fairmq-ex-readout-sender runSender.cxx) +add_executable(fairmq-ex-readout-sender sender.cxx) target_link_libraries(fairmq-ex-readout-sender PRIVATE FairMQ) -add_executable(fairmq-ex-readout-receiver runReceiver.cxx) +add_executable(fairmq-ex-readout-receiver receiver.cxx) target_link_libraries(fairmq-ex-readout-receiver PRIVATE FairMQ) set(EX_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/examples/readout/Builder.h b/examples/readout/builder.cxx similarity index 73% rename from examples/readout/Builder.h rename to examples/readout/builder.cxx index fdbb3bbd..5bdcc31c 100644 --- a/examples/readout/Builder.h +++ b/examples/readout/builder.cxx @@ -5,15 +5,13 @@ * GNU Lesser General Public Licence (LGPL) version 3, * * copied verbatim in the file "LICENSE" * ********************************************************************************/ -#ifndef FAIRMQEXAMPLEREGIONBUILDER_H -#define FAIRMQEXAMPLEREGIONBUILDER_H -#include "FairMQDevice.h" +#include +#include #include -namespace example_readout -{ +namespace bpo = boost::program_options; class Builder : public FairMQDevice { @@ -41,6 +39,13 @@ class Builder : public FairMQDevice std::string fOutputChannelName; }; -} // namespace example_readout +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("output-name", bpo::value()->default_value("bs"), "Output channel name"); +} -#endif /* FAIRMQEXAMPLEREGIONBUILDER_H */ +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/readout/Processor.h b/examples/readout/processor.cxx similarity index 76% rename from examples/readout/Processor.h rename to examples/readout/processor.cxx index 76fcb927..85ce00a7 100644 --- a/examples/readout/Processor.h +++ b/examples/readout/processor.cxx @@ -5,13 +5,11 @@ * GNU Lesser General Public Licence (LGPL) version 3, * * copied verbatim in the file "LICENSE" * ********************************************************************************/ -#ifndef FAIRMQEXAMPLEREGIONPROCESSOR_H -#define FAIRMQEXAMPLEREGIONPROCESSOR_H -#include "FairMQDevice.h" +#include +#include -namespace example_readout -{ +namespace bpo = boost::program_options; class Processor : public FairMQDevice { @@ -32,6 +30,10 @@ class Processor : public FairMQDevice } }; -} // namespace example_readout +void addCustomOptions(bpo::options_description& /* options */) +{} -#endif /* FAIRMQEXAMPLEREGIONPROCESSOR_H */ +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/readout/Readout.h b/examples/readout/readout.cxx similarity index 86% rename from examples/readout/Readout.h rename to examples/readout/readout.cxx index e2424409..aca1d9ea 100644 --- a/examples/readout/Readout.h +++ b/examples/readout/readout.cxx @@ -5,17 +5,15 @@ * GNU Lesser General Public Licence (LGPL) version 3, * * copied verbatim in the file "LICENSE" * ********************************************************************************/ -#ifndef FAIRMQEXAMPLEREADOUTREADOUT_H -#define FAIRMQEXAMPLEREADOUTREADOUT_H + +#include +#include #include #include #include -#include "FairMQDevice.h" - -namespace example_readout -{ +namespace bpo = boost::program_options; class Readout : public FairMQDevice { @@ -86,6 +84,14 @@ class Readout : public FairMQDevice std::atomic fNumUnackedMsgs; }; -} // namespace example_readout +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("msg-size", bpo::value()->default_value(1000), "Message size in bytes") + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} -#endif /* FAIRMQEXAMPLEREADOUTREADOUT_H */ +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/readout/Receiver.h b/examples/readout/receiver.cxx similarity index 77% rename from examples/readout/Receiver.h rename to examples/readout/receiver.cxx index b0bc148b..8bdde0ae 100644 --- a/examples/readout/Receiver.h +++ b/examples/readout/receiver.cxx @@ -5,13 +5,11 @@ * GNU Lesser General Public Licence (LGPL) version 3, * * copied verbatim in the file "LICENSE" * ********************************************************************************/ -#ifndef FAIRMQEXAMPLEREGIONRECEIVER_H -#define FAIRMQEXAMPLEREGIONRECEIVER_H -#include "FairMQDevice.h" +#include +#include -namespace example_readout -{ +namespace bpo = boost::program_options; class Receiver : public FairMQDevice { @@ -49,6 +47,13 @@ class Receiver : public FairMQDevice uint64_t fNumIterations; }; -} // namespace example_readout +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} -#endif /* FAIRMQEXAMPLEREGIONRECEIVER_H */ +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/readout/runBuilder.cxx b/examples/readout/runBuilder.cxx deleted file mode 100644 index 4cbe046d..00000000 --- a/examples/readout/runBuilder.cxx +++ /dev/null @@ -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 "Builder.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("output-name", bpo::value()->default_value("bs"), "Output channel name"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_readout::Builder(); -} diff --git a/examples/readout/runProcessor.cxx b/examples/readout/runProcessor.cxx deleted file mode 100644 index 1c78b11d..00000000 --- a/examples/readout/runProcessor.cxx +++ /dev/null @@ -1,20 +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 "Processor.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& /* options */) -{} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_readout::Processor(); -} diff --git a/examples/readout/runReadout.cxx b/examples/readout/runReadout.cxx deleted file mode 100644 index f3647927..00000000 --- a/examples/readout/runReadout.cxx +++ /dev/null @@ -1,24 +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 "Readout.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("msg-size", bpo::value()->default_value(1000), "Message size in bytes") - ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_readout::Readout(); -} diff --git a/examples/readout/runReceiver.cxx b/examples/readout/runReceiver.cxx deleted file mode 100644 index 1b488cba..00000000 --- a/examples/readout/runReceiver.cxx +++ /dev/null @@ -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 "Receiver.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_readout::Receiver(); -} diff --git a/examples/readout/runSender.cxx b/examples/readout/runSender.cxx deleted file mode 100644 index f59825d3..00000000 --- a/examples/readout/runSender.cxx +++ /dev/null @@ -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 "Sender.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("input-name", bpo::value()->default_value("bs"), "Input channel name"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_readout::Sender(); -} diff --git a/examples/readout/Sender.h b/examples/readout/sender.cxx similarity index 73% rename from examples/readout/Sender.h rename to examples/readout/sender.cxx index 4fd77026..aa80c950 100644 --- a/examples/readout/Sender.h +++ b/examples/readout/sender.cxx @@ -5,15 +5,13 @@ * GNU Lesser General Public Licence (LGPL) version 3, * * copied verbatim in the file "LICENSE" * ********************************************************************************/ -#ifndef FAIRMQEXAMPLEREGIONSENDER_H -#define FAIRMQEXAMPLEREGIONSENDER_H -#include "FairMQDevice.h" +#include +#include #include -namespace example_readout -{ +namespace bpo = boost::program_options; class Sender : public FairMQDevice { @@ -41,6 +39,13 @@ class Sender : public FairMQDevice std::string fInputChannelName; }; -} // namespace example_readout +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("input-name", bpo::value()->default_value("bs"), "Input channel name"); +} -#endif /* FAIRMQEXAMPLEREGIONSENDER_H */ +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/region/CMakeLists.txt b/examples/region/CMakeLists.txt index 76d432e3..4d92d6dc 100644 --- a/examples/region/CMakeLists.txt +++ b/examples/region/CMakeLists.txt @@ -6,20 +6,11 @@ # copied verbatim in the file "LICENSE" # ################################################################################ -add_library(ExampleRegionLib STATIC - "Sampler.cxx" - "Sampler.h" - "Sink.cxx" - "Sink.h" -) +add_executable(fairmq-ex-region-sampler sampler.cxx) +target_link_libraries(fairmq-ex-region-sampler PRIVATE FairMQ) -target_link_libraries(ExampleRegionLib PUBLIC FairMQ) - -add_executable(fairmq-ex-region-sampler runSampler.cxx) -target_link_libraries(fairmq-ex-region-sampler PRIVATE ExampleRegionLib) - -add_executable(fairmq-ex-region-sink runSink.cxx) -target_link_libraries(fairmq-ex-region-sink PRIVATE ExampleRegionLib) +add_executable(fairmq-ex-region-sink sink.cxx) +target_link_libraries(fairmq-ex-region-sink PRIVATE FairMQ) add_custom_target(ExampleRegion DEPENDS fairmq-ex-region-sampler fairmq-ex-region-sink) diff --git a/examples/region/Sampler.cxx b/examples/region/Sampler.cxx deleted file mode 100644 index 70b81add..00000000 --- a/examples/region/Sampler.cxx +++ /dev/null @@ -1,104 +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 "Sampler.h" - -#include - -using namespace std; - -namespace example_region -{ - -Sampler::Sampler() - : fMsgSize(10000) - , fLinger(100) - , fMaxIterations(0) - , fNumIterations(0) - , fRegion(nullptr) - , fNumUnackedMsgs(0) -{} - -void Sampler::InitTask() -{ - fMsgSize = fConfig->GetProperty("msg-size"); - fLinger = fConfig->GetProperty("region-linger"); - fMaxIterations = fConfig->GetProperty("max-iterations"); - - fChannels.at("data").at(0).Transport()->SubscribeToRegionEvents([](FairMQRegionInfo info) { - LOG(info) << "Region event: " << info.event << ": " - << (info.managed ? "managed" : "unmanaged") - << ", id: " << info.id - << ", ptr: " << info.ptr - << ", size: " << info.size - << ", flags: " << info.flags; - }); - - fRegion = FairMQUnmanagedRegionPtr(NewUnmanagedRegionFor("data", // region is created using the transport of this channel... - 0, // ... and this sub-channel - 10000000, // region size - [this](const std::vector& blocks) { // callback to be called when message buffers no longer needed by transport - lock_guard lock(fMtx); - fNumUnackedMsgs -= blocks.size(); - - if (fMaxIterations > 0) { - LOG(info) << "Received " << blocks.size() << " acks"; - } - }, - "", // path, if a region is backed by a file - 0, // flags that are passed for region creation - fair::mq::RegionConfig{true, true} // additional config: { call mlock on the region, zero the region memory } - )); - fRegion->SetLinger(fLinger); -} - -bool Sampler::ConditionalRun() -{ - FairMQMessagePtr msg(NewMessageFor("data", // channel - 0, // sub-channel - fRegion, // region - fRegion->GetData(), // ptr within region - fMsgSize, // offset from ptr - nullptr // hint - )); - - // static_cast(fRegion->GetData())[3] = 97; - // LOG(info) << "check: " << static_cast(fRegion->GetData())[3]; - // std::this_thread::sleep_for(std::chrono::seconds(1)); - - lock_guard lock(fMtx); - ++fNumUnackedMsgs; - if (Send(msg, "data", 0) > 0) { - if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - return false; - } - } - - return true; -} - -void Sampler::ResetTask() -{ - fRegion.reset(); - { - lock_guard lock(fMtx); - if (fNumUnackedMsgs != 0) { - LOG(info) << "Done, still not acknowledged: " << fNumUnackedMsgs; - } else { - LOG(info) << "All acknowledgements received."; - } - } - fChannels.at("data").at(0).Transport()->UnsubscribeFromRegionEvents(); -} - -Sampler::~Sampler() -{ -} - -} // namespace example_region diff --git a/examples/region/Sampler.h b/examples/region/Sampler.h deleted file mode 100644 index 45de28f4..00000000 --- a/examples/region/Sampler.h +++ /dev/null @@ -1,49 +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" * - ********************************************************************************/ -/** - * Sampler.h - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#ifndef FAIRMQEXAMPLEREGIONSAMPLER_H -#define FAIRMQEXAMPLEREGIONSAMPLER_H - -#include -#include - -#include "FairMQDevice.h" - -namespace example_region -{ - -class Sampler : public FairMQDevice -{ - public: - Sampler(); - virtual ~Sampler(); - - protected: - virtual void InitTask(); - virtual bool ConditionalRun(); - virtual void ResetTask(); - - private: - int fMsgSize; - uint32_t fLinger; - uint64_t fMaxIterations; - uint64_t fNumIterations; - FairMQUnmanagedRegionPtr fRegion; - std::mutex fMtx; - uint64_t fNumUnackedMsgs; -}; - -} // namespace example_region - -#endif /* FAIRMQEXAMPLEREGIONSAMPLER_H */ diff --git a/examples/region/Sink.cxx b/examples/region/Sink.cxx deleted file mode 100644 index 8d29231d..00000000 --- a/examples/region/Sink.cxx +++ /dev/null @@ -1,70 +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_region -{ - -Sink::Sink() - : fMaxIterations(0) - , fNumIterations(0) -{ -} - -void Sink::InitTask() -{ - // Get the fMaxIterations value from the command line options (via fConfig) - fMaxIterations = fConfig->GetProperty("max-iterations"); - fChannels.at("data").at(0).Transport()->SubscribeToRegionEvents([](FairMQRegionInfo info) { - LOG(info) << "Region event: " << info.event << ": " - << (info.managed ? "managed" : "unmanaged") - << ", id: " << info.id - << ", ptr: " << info.ptr - << ", size: " << info.size - << ", flags: " << info.flags; - }); -} - -void Sink::Run() -{ - FairMQChannel& dataInChannel = fChannels.at("data").at(0); - - while (!NewStatePending()) { - FairMQMessagePtr msg(dataInChannel.Transport()->CreateMessage()); - dataInChannel.Receive(msg); - - // void* ptr = msg->GetData(); - // char* cptr = static_cast(ptr); - // LOG(info) << "check: " << cptr[3]; - - if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - break; - } - } -} - -void Sink::ResetTask() -{ - fChannels.at("data").at(0).Transport()->UnsubscribeFromRegionEvents(); -} - -Sink::~Sink() -{ -} - -} // namespace example_region diff --git a/examples/region/Sink.h b/examples/region/Sink.h deleted file mode 100644 index 5eb7dd42..00000000 --- a/examples/region/Sink.h +++ /dev/null @@ -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 FAIRMQEXAMPLEREGIONSINK_H -#define FAIRMQEXAMPLEREGIONSINK_H - -#include - -#include "FairMQDevice.h" - -namespace example_region -{ - -class Sink : public FairMQDevice -{ - public: - Sink(); - virtual ~Sink(); - - protected: - virtual void Run(); - virtual void InitTask(); - virtual void ResetTask(); - - private: - uint64_t fMaxIterations; - uint64_t fNumIterations; -}; - -} // namespace example_region - -#endif /* FAIRMQEXAMPLEREGIONSINK_H */ diff --git a/examples/region/runSampler.cxx b/examples/region/runSampler.cxx deleted file mode 100644 index 31f6ad9a..00000000 --- a/examples/region/runSampler.cxx +++ /dev/null @@ -1,25 +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 "Sampler.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("msg-size", bpo::value()->default_value(1000), "Message size in bytes") - ("region-linger", bpo::value()->default_value(100), "Linger period for regions") - ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_region::Sampler(); -} diff --git a/examples/region/runSink.cxx b/examples/region/runSink.cxx deleted file mode 100644 index 40c54308..00000000 --- a/examples/region/runSink.cxx +++ /dev/null @@ -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()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_region::Sink(); -} diff --git a/examples/region/sampler.cxx b/examples/region/sampler.cxx new file mode 100644 index 00000000..a256e071 --- /dev/null +++ b/examples/region/sampler.cxx @@ -0,0 +1,125 @@ +/******************************************************************************** + * 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 +#include + +#include +#include +#include + +namespace bpo = boost::program_options; + +class Sampler : public FairMQDevice +{ + public: + Sampler() + : fMsgSize(10000) + , fLinger(100) + , fMaxIterations(0) + , fNumIterations(0) + , fRegion(nullptr) + , fNumUnackedMsgs(0) + {} + + protected: + void InitTask() override + { + fMsgSize = fConfig->GetProperty("msg-size"); + fLinger = fConfig->GetProperty("region-linger"); + fMaxIterations = fConfig->GetProperty("max-iterations"); + + fChannels.at("data").at(0).Transport()->SubscribeToRegionEvents([](FairMQRegionInfo info) { + LOG(info) << "Region event: " << info.event << ": " + << (info.managed ? "managed" : "unmanaged") + << ", id: " << info.id + << ", ptr: " << info.ptr + << ", size: " << info.size + << ", flags: " << info.flags; + }); + + fRegion = FairMQUnmanagedRegionPtr(NewUnmanagedRegionFor("data", // region is created using the transport of this channel... + 0, // ... and this sub-channel + 10000000, // region size + [this](const std::vector& blocks) { // callback to be called when message buffers no longer needed by transport + std::lock_guard lock(fMtx); + fNumUnackedMsgs -= blocks.size(); + + if (fMaxIterations > 0) { + LOG(info) << "Received " << blocks.size() << " acks"; + } + }, + "", // path, if a region is backed by a file + 0, // flags that are passed for region creation + fair::mq::RegionConfig{true, true} // additional config: { call mlock on the region, zero the region memory } + )); + fRegion->SetLinger(fLinger); + } + + bool ConditionalRun() override + { + FairMQMessagePtr msg(NewMessageFor("data", // channel + 0, // sub-channel + fRegion, // region + fRegion->GetData(), // ptr within region + fMsgSize, // offset from ptr + nullptr // hint + )); + + // static_cast(fRegion->GetData())[3] = 97; + // LOG(info) << "check: " << static_cast(fRegion->GetData())[3]; + // std::this_thread::sleep_for(std::chrono::seconds(1)); + + std::lock_guard lock(fMtx); + ++fNumUnackedMsgs; + if (Send(msg, "data", 0) > 0) { + if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + return false; + } + } + + return true; + } + + void ResetTask() override + { + fRegion.reset(); + { + std::lock_guard lock(fMtx); + if (fNumUnackedMsgs != 0) { + LOG(info) << "Done, still not acknowledged: " << fNumUnackedMsgs; + } else { + LOG(info) << "All acknowledgements received."; + } + } + fChannels.at("data").at(0).Transport()->UnsubscribeFromRegionEvents(); + } + + private: + int fMsgSize; + uint32_t fLinger; + uint64_t fMaxIterations; + uint64_t fNumIterations; + FairMQUnmanagedRegionPtr fRegion; + std::mutex fMtx; + uint64_t fNumUnackedMsgs; +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("msg-size", bpo::value()->default_value(1000), "Message size in bytes") + ("region-linger", bpo::value()->default_value(100), "Linger period for regions") + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/region/sink.cxx b/examples/region/sink.cxx new file mode 100644 index 00000000..55b0c250 --- /dev/null +++ b/examples/region/sink.cxx @@ -0,0 +1,73 @@ +/******************************************************************************** + * 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 +#include + +namespace bpo = boost::program_options; + +class Sink : public FairMQDevice +{ + public: + Sink() + : fMaxIterations(0) + , fNumIterations(0) + {} + + void InitTask() override + { + // Get the fMaxIterations value from the command line options (via fConfig) + fMaxIterations = fConfig->GetProperty("max-iterations"); + fChannels.at("data").at(0).Transport()->SubscribeToRegionEvents([](FairMQRegionInfo info) { + LOG(info) << "Region event: " << info.event << ": " + << (info.managed ? "managed" : "unmanaged") + << ", id: " << info.id + << ", ptr: " << info.ptr + << ", size: " << info.size + << ", flags: " << info.flags; + }); + } + void Run() override + { + FairMQChannel& dataInChannel = fChannels.at("data").at(0); + + while (!NewStatePending()) { + FairMQMessagePtr msg(dataInChannel.Transport()->CreateMessage()); + dataInChannel.Receive(msg); + + // void* ptr = msg->GetData(); + // char* cptr = static_cast(ptr); + // LOG(info) << "check: " << cptr[3]; + + if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + break; + } + } + } + void ResetTask() override + { + fChannels.at("data").at(0).Transport()->UnsubscribeFromRegionEvents(); + } + + private: + uint64_t fMaxIterations; + uint64_t fNumIterations; +}; + + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/req-rep/CMakeLists.txt b/examples/req-rep/CMakeLists.txt index 464c1fd3..08719495 100644 --- a/examples/req-rep/CMakeLists.txt +++ b/examples/req-rep/CMakeLists.txt @@ -6,21 +6,12 @@ # copied verbatim in the file "LICENSE" # ################################################################################ -add_library(ExampleReqRepLib STATIC - "Client.cxx" - "Client.h" - "Server.cxx" - "Server.h" -) - -target_link_libraries(ExampleReqRepLib PUBLIC FairMQ) - -add_executable(fairmq-ex-req-rep-client runClient.cxx) -target_link_libraries(fairmq-ex-req-rep-client PRIVATE ExampleReqRepLib) +add_executable(fairmq-ex-req-rep-client client.cxx) +target_link_libraries(fairmq-ex-req-rep-client PRIVATE FairMQ) -add_executable(fairmq-ex-req-rep-server runServer.cxx) -target_link_libraries(fairmq-ex-req-rep-server PRIVATE ExampleReqRepLib) +add_executable(fairmq-ex-req-rep-server server.cxx) +target_link_libraries(fairmq-ex-req-rep-server PRIVATE FairMQ) add_custom_target(ExampleReqRep DEPENDS fairmq-ex-req-rep-client fairmq-ex-req-rep-server) diff --git a/examples/req-rep/Client.cxx b/examples/req-rep/Client.cxx deleted file mode 100644 index f4c8f640..00000000 --- a/examples/req-rep/Client.cxx +++ /dev/null @@ -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" * - ********************************************************************************/ -/** - * Client.cpp - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#include // this_thread::sleep_for -#include - -#include "Client.h" - -using namespace std; - -namespace example_req_rep -{ - -Client::Client() - : fText() - , fMaxIterations(0) - , fNumIterations(0) -{ -} - -void Client::InitTask() -{ - fText = fConfig->GetProperty("text"); - fMaxIterations = fConfig->GetProperty("max-iterations"); -} - -bool Client::ConditionalRun() -{ - - string* text = new string(fText); - - // 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 req(NewMessage(const_cast(text->c_str()), // data - text->length(), // size - [](void* /*data*/, void* object) { delete static_cast(object); }, // deletion callback - text)); // object that manages the data - FairMQMessagePtr rep(NewMessage()); - - LOG(info) << "Sending \"" << fText << "\" to server."; - - if (Send(req, "data") > 0) - { - if (Receive(rep, "data") >= 0) - { - LOG(info) << "Received reply from server: \"" << string(static_cast(rep->GetData()), rep->GetSize()) << "\""; - - if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) - { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - return false; - } - - this_thread::sleep_for(chrono::seconds(1)); - - return true; - } - } - - return false; -} - -Client::~Client() -{ -} - -} // namespace example_req_rep diff --git a/examples/req-rep/Client.h b/examples/req-rep/Client.h deleted file mode 100644 index 3ea1b286..00000000 --- a/examples/req-rep/Client.h +++ /dev/null @@ -1,42 +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" * - ********************************************************************************/ -/** - * Client.h - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#ifndef FAIRMQEXAMPLEREQREPCLIENT_H -#define FAIRMQEXAMPLEREQREPCLIENT_H - -#include - -#include "FairMQDevice.h" - -namespace example_req_rep -{ - -class Client : public FairMQDevice -{ - public: - Client(); - virtual ~Client(); - - protected: - std::string fText; - uint64_t fMaxIterations; - uint64_t fNumIterations; - - virtual bool ConditionalRun(); - virtual void InitTask(); -}; - -} // namespace example_req_rep - -#endif /* FAIRMQEXAMPLEREQREPCLIENT_H */ diff --git a/examples/req-rep/Server.cxx b/examples/req-rep/Server.cxx deleted file mode 100644 index b32517dc..00000000 --- a/examples/req-rep/Server.cxx +++ /dev/null @@ -1,66 +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" * - ********************************************************************************/ -/** - * Server.cxx - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#include "Server.h" - -using namespace std; - -namespace example_req_rep -{ - -Server::Server() - : fMaxIterations(0) - , fNumIterations(0) -{ - OnData("data", &Server::HandleData); -} - -void Server::InitTask() -{ - // Get the fMaxIterations value from the command line options (via fConfig) - fMaxIterations = fConfig->GetProperty("max-iterations"); -} - -bool Server::HandleData(FairMQMessagePtr& req, int /*index*/) -{ - LOG(info) << "Received request from client: \"" << string(static_cast(req->GetData()), req->GetSize()) << "\""; - - string* text = new string("Thank you for the \"" + string(static_cast(req->GetData()), req->GetSize()) + "\"!"); - - LOG(info) << "Sending reply to client."; - - FairMQMessagePtr rep(NewMessage(const_cast(text->c_str()), // data - text->length(), // size - [](void* /*data*/, void* object) { delete static_cast(object); }, // deletion callback - text)); // object that manages the data - - if (Send(rep, "data") > 0) - { - if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) - { - LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; - return false; - } - - return true; - } - - return false; -} - -Server::~Server() -{ -} - -} // namespace example_req_rep diff --git a/examples/req-rep/Server.h b/examples/req-rep/Server.h deleted file mode 100644 index 73d978ed..00000000 --- a/examples/req-rep/Server.h +++ /dev/null @@ -1,40 +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" * - ********************************************************************************/ -/** - * Server.h - * - * @since 2014-10-10 - * @author A. Rybalchenko - */ - -#ifndef FAIRMQEXAMPLEREQREPSERVER_H -#define FAIRMQEXAMPLEREQREPSERVER_H - -#include "FairMQDevice.h" - -namespace example_req_rep -{ - -class Server : public FairMQDevice -{ - public: - Server(); - virtual ~Server(); - - protected: - virtual void InitTask(); - bool HandleData(FairMQMessagePtr&, int); - - private: - uint64_t fMaxIterations; - uint64_t fNumIterations; -}; - -} // namespace example_req_rep - -#endif /* FAIRMQEXAMPLEREQREPSERVER_H */ diff --git a/examples/req-rep/client.cxx b/examples/req-rep/client.cxx new file mode 100644 index 00000000..d37d34c7 --- /dev/null +++ b/examples/req-rep/client.cxx @@ -0,0 +1,83 @@ +/******************************************************************************** + * 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 +#include + +#include +#include +#include // this_thread::sleep_for + +namespace bpo = boost::program_options; + +class Client : public FairMQDevice +{ + public: + Client() + : fMaxIterations(0) + , fNumIterations(0) + {} + + void InitTask() override + { + fText = fConfig->GetProperty("text"); + fMaxIterations = fConfig->GetProperty("max-iterations"); + } + + bool ConditionalRun() override + { + + std::string* text = new std::string(fText); + + // 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 req(NewMessage(const_cast(text->c_str()), // data + text->length(), // size + [](void* /*data*/, void* object) { delete static_cast(object); }, // deletion callback + text)); // object that manages the data + FairMQMessagePtr rep(NewMessage()); + + LOG(info) << "Sending \"" << fText << "\" to server."; + + if (Send(req, "data") > 0) { + if (Receive(rep, "data") >= 0) { + LOG(info) << "Received reply from server: \"" << std::string(static_cast(rep->GetData()), rep->GetSize()) << "\""; + + if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + return false; + } + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + return true; + } + } + + return false; + } + + private: + std::string fText; + uint64_t fMaxIterations; + uint64_t fNumIterations; +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("text", bpo::value()->default_value("Hello"), "Text to send out") + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +} diff --git a/examples/req-rep/runClient.cxx b/examples/req-rep/runClient.cxx deleted file mode 100644 index 7fd12b03..00000000 --- a/examples/req-rep/runClient.cxx +++ /dev/null @@ -1,24 +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 "Client.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("text", bpo::value()->default_value("Hello"), "Text to send out") - ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_req_rep::Client(); -} diff --git a/examples/req-rep/runServer.cxx b/examples/req-rep/runServer.cxx deleted file mode 100644 index 700bc74b..00000000 --- a/examples/req-rep/runServer.cxx +++ /dev/null @@ -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 "Server.h" - -namespace bpo = boost::program_options; - -void addCustomOptions(bpo::options_description& options) -{ - options.add_options() - ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); -} - -FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) -{ - return new example_req_rep::Server(); -} diff --git a/examples/req-rep/server.cxx b/examples/req-rep/server.cxx new file mode 100644 index 00000000..8465d309 --- /dev/null +++ b/examples/req-rep/server.cxx @@ -0,0 +1,72 @@ +/******************************************************************************** + * 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 +#include + +#include + +namespace bpo = boost::program_options; + +class Server : public FairMQDevice +{ + public: + Server() + : fMaxIterations(0) + , fNumIterations(0) + { + OnData("data", &Server::HandleData); + } + + protected: + void InitTask() override + { + // Get the fMaxIterations value from the command line options (via fConfig) + fMaxIterations = fConfig->GetProperty("max-iterations"); + } + + bool HandleData(FairMQMessagePtr& req, int) + { + LOG(info) << "Received request from client: \"" << std::string(static_cast(req->GetData()), req->GetSize()) << "\""; + + std::string* text = new std::string("Thank you for the \"" + std::string(static_cast(req->GetData()), req->GetSize()) + "\"!"); + + LOG(info) << "Sending reply to client."; + + FairMQMessagePtr rep(NewMessage(const_cast(text->c_str()), // data + text->length(), // size + [](void* /*data*/, void* object) { delete static_cast(object); }, // deletion callback + text)); // object that manages the data + + if (Send(rep, "data") > 0) { + if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { + LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; + return false; + } + + return true; + } + + return false; + } + + private: + uint64_t fMaxIterations; + uint64_t fNumIterations; +}; + +void addCustomOptions(bpo::options_description& options) +{ + options.add_options() + ("max-iterations", bpo::value()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)"); +} + +std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) +{ + return std::make_unique(); +}