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.
 
CommandFacilityoperator= (const CommandFacility &)=delete
 CommandFacility is not copy-assignable.
 
 CommandFacility (CommandFacility &&)=delete
 CommandFacility is not move-constructible.
 
CommandFacilityoperator= (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

CommandedObjectm_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

std::function<void(const cmdobj_t&, cmd::CommandReply)> dunedaq::cmdlib::CommandFacility::CommandCallback
private

Request callback function signature.

Definition at line 95 of file CommandFacility.hpp.

◆ CompletionQueue

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

Completion queue for reqistered tasks.

Definition at line 91 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 46 of file CommandFacility.cpp.

47{
48 auto execfut = std::async(std::launch::deferred, m_command_callback, std::move(cmd), std::move(meta));
49 m_completion_queue.push(std::move(execfut));
50}

◆ executor()

void CommandFacility::executor ( )
private

Definition at line 80 of file CommandFacility.cpp.

81{
82 std::future<void> fut;
83 while (m_active.load()) {
84 if (m_completion_queue.empty()) {
85 std::this_thread::sleep_for(std::chrono::milliseconds(10));
86 } else {
87 bool success = m_completion_queue.try_pop(fut);
88 if (!success) {
89 ers::error(CompletionQueueIssue(ERS_HERE, "Can't get from completion queue."));
90 } else {
91 fut.wait(); // trigger execution
92 }
93 }
94 }
95}
#define ERS_HERE
void error(const Issue &issue)
Definition ers.hpp:81

◆ handle_command()

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

The glue between commanded and completion callback.

Definition at line 53 of file CommandFacility.cpp.

54{
55 try {
57 meta.success = true;
58 meta.result = "OK";
59 meta.appname = m_name;
60 } catch (const ers::Issue& ei ) {
61 meta.success = false;
62 meta.result = ei.what();
63 meta.appname = m_name;
64 ers::error(CommandExecutionFailed(ERS_HERE, "Caught ers::Issue", ei));
65 } catch (const std::exception& exc) {
66 meta.success = false;
67 meta.result = exc.what();
68 meta.appname = m_name;
69 ers::error(CommandExecutionFailed(ERS_HERE, "Caught std::exception", exc));
70 } catch (...) { // NOLINT JCF Jan-27-2021 violates letter of the law but not the spirit
71 meta.success = false;
72 meta.result = "Caught unknown exception";
73 meta.appname = m_name;
75 }
76 completion_callback(cmd, meta);
77}
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.
Base class for any user define issue.
Definition Issue.hpp:69
const char * what() const noexcept
General cause of the issue.
Definition Issue.hpp:133
Unsupported std::string uri CommandExecutionFailed
Definition Issues.hpp:68

◆ 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;
37 m_command_callback = std::bind(&CommandFacility::handle_command, this, std::placeholders::_1, std::placeholders::_2);
38 m_active.store(true);
39 m_executor = std::thread(&CommandFacility::executor, this);
40 } else {
41 ers::error(CommandFacilityInitialization(ERS_HERE, "set_commanded shall be called once."));
42 }
43}
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 99 of file CommandFacility.hpp.

◆ m_command_callback

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

Definition at line 96 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 87 of file CommandFacility.hpp.

◆ m_completion_queue

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

Definition at line 92 of file CommandFacility.hpp.

◆ m_executor

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

Definition at line 101 of file CommandFacility.hpp.

◆ m_name

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

name of the commanded object

Definition at line 77 of file CommandFacility.hpp.


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