/******************************************************************************** * 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 Readout : public FairMQDevice { public: Readout() {} void InitTask() override { fMsgSize = fConfig->GetProperty("msg-size"); fMaxIterations = fConfig->GetProperty("max-iterations"); fRegion = FairMQUnmanagedRegionPtr(NewUnmanagedRegionFor("rb", 0, 10000000, [this](const std::vector& blocks) { // callback to be called when message buffers no longer needed by transport fNumUnackedMsgs -= blocks.size(); if (fMaxIterations > 0) { LOG(debug) << "Received " << blocks.size() << " acks"; } } )); } bool ConditionalRun() override { FairMQMessagePtr msg(NewMessageFor("rb", // channel 0, // sub-channel fRegion, // region fRegion->GetData(), // ptr within region fMsgSize, // offset from ptr nullptr // hint )); if (Send(msg, "rb", 0) > 0) { ++fNumUnackedMsgs; if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) { LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state."; return false; } } return true; } void ResetTask() override { // if not all messages acknowledged, wait for a bit. But only once, since receiver could be already dead. if (fNumUnackedMsgs != 0) { LOG(debug) << "waiting for all acknowledgements... (" << fNumUnackedMsgs << ")"; std::this_thread::sleep_for(std::chrono::milliseconds(500)); LOG(debug) << "done, still unacked: " << fNumUnackedMsgs; } fRegion.reset(); } private: int fMsgSize = 10000; uint64_t fMaxIterations = 0; uint64_t fNumIterations = 0; FairMQUnmanagedRegionPtr fRegion = nullptr; std::atomic fNumUnackedMsgs = 0; }; 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)"); } std::unique_ptr getDevice(fair::mq::ProgOptions& /*config*/) { return std::make_unique(); }