DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
CommandFacility.hpp
Go to the documentation of this file.
1
8#ifndef CMDLIB_INCLUDE_CMDLIB_COMMANDFACILITY_HPP_
9#define CMDLIB_INCLUDE_CMDLIB_COMMANDFACILITY_HPP_
10
11#include "cmdlib/cmd/Nljs.hpp"
12#include "CommandedObject.hpp"
13#include "Issues.hpp"
14
15#include <cetlib/BasicPluginFactory.h>
16#include <cetlib/compiler_macros.h>
17
18#include <tbb/concurrent_queue.h>
19
20#include <future>
21#include <functional>
22#include <atomic>
23#include <memory>
24#include <string>
25
26#ifndef EXTERN_C_FUNC_DECLARE_START
27#define EXTERN_C_FUNC_DECLARE_START \
28 extern "C" \
29 {
30#endif
31
36#define DEFINE_DUNE_COMMAND_FACILITY(klass) \
37 EXTERN_C_FUNC_DECLARE_START \
38 std::unique_ptr<dunedaq::cmdlib::CommandFacility> make() \
39 { \
40 return std::unique_ptr<dunedaq::cmdlib::CommandFacility>(new klass()); \
41 } \
42 }
43
44namespace dunedaq::cmdlib {
45
50{
51public:
52 explicit CommandFacility(std::string /*uri*/) {}
53 virtual ~CommandFacility();
55 delete;
57 delete;
59 delete;
61 delete;
62
64 void set_commanded(CommandedObject& commanded, std::string name);
65
67 virtual void run(std::atomic<bool>& end_marker) = 0;
68
70 void execute_command(const cmdobj_t& cmd, cmd::CommandReply meta);
71
72protected:
74 virtual void completion_callback(const cmdobj_t& cmd, cmd::CommandReply& meta) = 0;
75
77 std::string m_name;
78
79private:
80
82 void handle_command(const cmdobj_t& cmd, cmd::CommandReply meta);
83
84 void executor();
85
88
89
91 typedef tbb::concurrent_queue<std::future<void>> CompletionQueue;
93
95 typedef std::function<void(const cmdobj_t&, cmd::CommandReply)> CommandCallback;
97
99 std::atomic<bool> m_active;
100
101 std::thread m_executor;
102
103};
104template <typename... ARGS>
105std::shared_ptr<CommandFacility>
107 std::string const& uri,
108 ARGS&&... args)
109{
110 auto sep = uri.find("://");
111 std::string scheme;
112 if (sep == std::string::npos) { // simple path
113 scheme = "stdin";
114 } else { // with scheme
115 scheme = uri.substr(0, sep);
116 }
117 std::string plugin_name = scheme + "CommandFacility";
118 static cet::BasicPluginFactory bpf("duneCommandFacility", "make");
119 std::shared_ptr<CommandFacility> cf_ptr;
120 try {
121 cf_ptr = bpf.makePlugin<std::shared_ptr<CommandFacility>>(
122 plugin_name,
123 uri,
124 std::forward<ARGS>(args)...
125 );
126 } catch (const cet::exception &cexpt) {
128 } catch (const ers::Issue &iexpt) {
130 } catch (...) { // NOLINT JCF Jan-27-2021 violates letter of the law but not the spirit
131 throw CommandFacilityCreationFailed(ERS_HERE, uri, "Unknown error.");
132 }
133 return cf_ptr;
134}
135
136} // namespace dunedaq::cmdlib
137
138#endif // CMDLIB_INCLUDE_CMDLIB_COMMANDFACILITY_HPP_
#define ERS_HERE
Interface needed by DAQ apps and services for command handling.
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.
CommandFacility & operator=(CommandFacility &&)=delete
CommandFacility is not move-assignable.
virtual void run(std::atomic< bool > &end_marker)=0
Meant to be called once from main (implementation specific)
std::string m_name
name of the commanded object
CommandFacility & operator=(const CommandFacility &)=delete
CommandFacility is not copy-assignable.
CommandFacility(CommandFacility &&)=delete
CommandFacility is not move-constructible.
std::atomic< bool > m_active
Single thrad is responsible to trigger tasks.
tbb::concurrent_queue< std::future< void > > CompletionQueue
Completion queue for reqistered tasks.
std::function< void(const cmdobj_t &, cmd::CommandReply)> CommandCallback
Request callback function signature.
void set_commanded(CommandedObject &commanded, std::string name)
Meant to be called once from main.
CommandFacility(const CommandFacility &)=delete
CommandFacility is not copy-constructible.
void execute_command(const cmdobj_t &cmd, cmd::CommandReply meta)
Feed commands from the implementation.
Interface needed by commanded objects in the DAQ.
Base class for any user define issue.
Definition Issue.hpp:69
#define ARGS(...)
std::shared_ptr< CommandFacility > make_command_facility(std::string const &uri, ARGS &&... args)
Unsupported std::string uri Execution of command std::string error Failed to create CommandFacility uri
Definition Issues.hpp:77
Unsupported std::string uri Execution of command std::string error CommandFacilityCreationFailed
Definition Issues.hpp:76