DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::cmdlib::CommandFacility Class Referenceabstract

Interface needed by DAQ apps and services for command handling. More...

#include <CommandFacility.hpp>

Collaboration diagram for dunedaq::cmdlib::CommandFacility:
[legend]

Public Member Functions

 CommandFacility (std::string)
virtual ~CommandFacility ()
 CommandFacility (const CommandFacility &)=delete
 CommandFacility is not copy-constructible.
CommandFacility & operator= (const CommandFacility &)=delete
 CommandFacility is not copy-assignable.
 CommandFacility (CommandFacility &&)=delete
 CommandFacility is not move-constructible.
CommandFacility & operator= (CommandFacility &&)=delete
 CommandFacility is not move-assignable.
void set_commanded (CommandedObject &commanded, std::string name)
 Meant to be called once from main.
virtual void run (std::atomic< bool > &end_marker)=0
 Meant to be called once from main (implementation specific).
void execute_command (const cmdobj_t &cmd, cmd::CommandReply meta)
 Feed commands from the implementation.

Protected Member Functions

virtual void completion_callback (const cmdobj_t &cmd, cmd::CommandReply &meta)=0
 Must be implemented to handling the results of the commands.

Protected Attributes

std::string m_name
 name of the commanded object

Private Types

typedef tbb::concurrent_queue< std::future< void > > CompletionQueue
 Completion queue for reqistered tasks.
typedef std::function< void(const cmdobj_t &, cmd::CommandReply)> CommandCallback
 Request callback function signature.

Private Member Functions

void handle_command (const cmdobj_t &cmd, cmd::CommandReply meta)
 The glue between commanded and completion callback.
void executor ()

Private Attributes

CommandedObject * m_commanded_object = nullptr
 Commanded Object to run execute with received commands as parameters.
CompletionQueue m_completion_queue
CommandCallback m_command_callback = nullptr
std::atomic< bool > m_active
 Single thrad is responsible to trigger tasks.
std::thread m_executor

Detailed Description

Interface needed by DAQ apps and services for command handling.

Definition at line 49 of file CommandFacility.hpp.

Member Typedef Documentation

◆ CommandCallback

Request callback function signature.

Definition at line 89 of file CommandFacility.hpp.

◆ CompletionQueue

typedef tbb::concurrent_queue<std::future<void> > dunedaq::cmdlib::CommandFacility::CompletionQueue
private

Completion queue for reqistered tasks.

Definition at line 85 of file CommandFacility.hpp.

Constructor & Destructor Documentation

◆ CommandFacility() [1/3]

dunedaq::cmdlib::CommandFacility::CommandFacility ( std::string )
inlineexplicit

Definition at line 52 of file CommandFacility.hpp.

52{}

◆ ~CommandFacility()

CommandFacility::~CommandFacility ( )
virtual

Definition at line 21 of file CommandFacility.cpp.

22{
23 if (m_active.load()) {
24 m_active.store(false);
25 if (m_executor.joinable()) {
26 m_executor.join();
27 }
28 }
29}
std::atomic< bool > m_active
Single thrad is responsible to trigger tasks.

◆ CommandFacility() [2/3]

dunedaq::cmdlib::CommandFacility::CommandFacility ( const CommandFacility & )
delete

CommandFacility is not copy-constructible.

◆ CommandFacility() [3/3]

dunedaq::cmdlib::CommandFacility::CommandFacility ( CommandFacility && )
delete

CommandFacility is not move-constructible.

Member Function Documentation

◆ completion_callback()

virtual void dunedaq::cmdlib::CommandFacility::completion_callback ( const cmdobj_t & cmd,
cmd::CommandReply & meta )
protectedpure virtual

Must be implemented to handling the results of the commands.

◆ execute_command()

void CommandFacility::execute_command ( const cmdobj_t & cmd,
cmd::CommandReply meta )

Feed commands from the implementation.

Definition at line 47 of file CommandFacility.cpp.

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}

◆ executor()

void CommandFacility::executor ( )
private

Definition at line 81 of file CommandFacility.cpp.

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 error(const Issue &issue)
Definition ers.hpp:101

◆ handle_command()

void CommandFacility::handle_command ( const cmdobj_t & cmd,
cmd::CommandReply meta )
private

The glue between commanded and completion callback.

Definition at line 54 of file CommandFacility.cpp.

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 }
77 completion_callback(cmd, meta);
78}
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
virtual void execute(const cmdobj_t &command)=0
Pure virtual execute member.
const char * what() const noexcept
General cause of the issue.
Definition Issue.hpp:133
Unsupported std::string uri CommandExecutionFailed
Definition Issues.hpp:62

◆ operator=() [1/2]

CommandFacility & dunedaq::cmdlib::CommandFacility::operator= ( CommandFacility && )
delete

CommandFacility is not move-assignable.

◆ operator=() [2/2]

CommandFacility & dunedaq::cmdlib::CommandFacility::operator= ( const CommandFacility & )
delete

CommandFacility is not copy-assignable.

◆ run()

virtual void dunedaq::cmdlib::CommandFacility::run ( std::atomic< bool > & end_marker)
pure virtual

Meant to be called once from main (implementation specific).

◆ set_commanded()

void CommandFacility::set_commanded ( CommandedObject & commanded,
std::string name )

Meant to be called once from main.

Definition at line 32 of file CommandFacility.cpp.

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}
void handle_command(const cmdobj_t &cmd, cmd::CommandReply meta)
The glue between commanded and completion callback.

Member Data Documentation

◆ m_active

std::atomic<bool> dunedaq::cmdlib::CommandFacility::m_active
private

Single thrad is responsible to trigger tasks.

Definition at line 93 of file CommandFacility.hpp.

◆ m_command_callback

CommandCallback dunedaq::cmdlib::CommandFacility::m_command_callback = nullptr
private

Definition at line 90 of file CommandFacility.hpp.

◆ m_commanded_object

CommandedObject* dunedaq::cmdlib::CommandFacility::m_commanded_object = nullptr
mutableprivate

Commanded Object to run execute with received commands as parameters.

Definition at line 82 of file CommandFacility.hpp.

◆ m_completion_queue

CompletionQueue dunedaq::cmdlib::CommandFacility::m_completion_queue
private

Definition at line 86 of file CommandFacility.hpp.

◆ m_executor

std::thread dunedaq::cmdlib::CommandFacility::m_executor
private

Definition at line 95 of file CommandFacility.hpp.

◆ m_name

std::string dunedaq::cmdlib::CommandFacility::m_name
protected

name of the commanded object

Definition at line 73 of file CommandFacility.hpp.


The documentation for this class was generated from the following files: