DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
CommandFacility.cpp
Go to the documentation of this file.
1
9#include "cmdlib/Issues.hpp"
10#include "logging/Logging.hpp"
11
12#include <atomic>
13#include <chrono>
14#include <functional>
15#include <future>
16#include <string>
17#include <utility>
18
19using namespace dunedaq::cmdlib;
20
22{
23 if (m_active.load()) {
24 m_active.store(false);
25 if (m_executor.joinable()) {
26 m_executor.join();
27 }
28 }
29}
30
31void
33{
34 if (m_commanded_object == nullptr) {
35 m_name = name;
36 m_commanded_object = &commanded;
38 std::bind(&CommandFacility::handle_command, this, std::placeholders::_1, std::placeholders::_2);
39 m_active.store(true);
40 m_executor = std::thread(&CommandFacility::executor, this);
41 } else {
42 ers::error(CommandFacilityInitialization(ERS_HERE, "set_commanded shall be called once."));
43 }
44}
45
46void
48{
49 auto execfut = std::async(std::launch::deferred, m_command_callback, std::move(cmd), std::move(meta));
50 m_completion_queue.push(std::move(execfut));
51}
52
53void
55{
56 try {
58 meta.success = true;
59 meta.result = "OK";
60 meta.appname = m_name;
61 } catch (const ers::Issue& ei) {
62 meta.success = false;
63 meta.result = ei.what();
64 meta.appname = m_name;
65 ers::error(CommandExecutionFailed(ERS_HERE, "Caught ers::Issue", ei));
66 } catch (const std::exception& exc) {
67 meta.success = false;
68 meta.result = exc.what();
69 meta.appname = m_name;
70 ers::error(CommandExecutionFailed(ERS_HERE, "Caught std::exception", exc));
71 } catch (...) { // NOLINT JCF Jan-27-2021 violates letter of the law but not the spirit
72 meta.success = false;
73 meta.result = "Caught unknown exception";
74 meta.appname = m_name;
76 }
78}
79
80void
82{
83 std::future<void> fut;
84 while (m_active.load()) {
85 if (m_completion_queue.empty()) {
86 std::this_thread::sleep_for(std::chrono::milliseconds(10));
87 } else {
88 bool success = m_completion_queue.try_pop(fut);
89 if (!success) {
90 ers::error(CompletionQueueIssue(ERS_HERE, "Can't get from completion queue."));
91 } else {
92 fut.wait(); // trigger execution
93 }
94 }
95 }
96}
#define ERS_HERE
void handle_command(const cmdobj_t &cmd, cmd::CommandReply meta)
The glue between commanded and completion callback.
virtual void completion_callback(const cmdobj_t &cmd, cmd::CommandReply &meta)=0
Must be implemented to handling the results of the commands.
CommandedObject * m_commanded_object
Commanded Object to run execute with received commands as parameters.
std::string m_name
name of the commanded object
std::atomic< bool > m_active
Single thrad is responsible to trigger tasks.
void set_commanded(CommandedObject &commanded, std::string name)
Meant to be called once from main.
void execute_command(const cmdobj_t &cmd, cmd::CommandReply meta)
Feed commands from the implementation.
Interface needed by commanded objects in the DAQ.
virtual void execute(const cmdobj_t &command)=0
Pure virtual execute member.
Base class for any user define issue.
Definition Issue.hpp:76
const char * what() const noexcept
General cause of the issue.
Definition Issue.hpp:133
nlohmann::json cmdobj_t
Unsupported std::string uri CommandExecutionFailed
Definition Issues.hpp:62
void error(const Issue &issue)
Definition ers.hpp:101