Line data Source code
1 : /**
2 : * @file dummy_command_facility_.cxx Dummy command facility
3 : * implementation of the CommandFacility interface
4 : *
5 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
6 : * Licensing/copyright details are in the COPYING file that you should have
7 : * received with this code.
8 : */
9 : #include "cmdlib/CommandFacility.hpp"
10 :
11 : #include "logging/Logging.hpp"
12 : #include <nlohmann/json.hpp>
13 : #include <cetlib/BasicPluginFactory.h>
14 :
15 : #include <thread>
16 : #include <chrono>
17 : #include <memory>
18 : #include <string>
19 :
20 : using namespace dunedaq::cmdlib;
21 : using namespace std::chrono_literals;
22 :
23 : class dummyCommandFacility : public CommandFacility
24 : {
25 : public:
26 0 : explicit dummyCommandFacility(std::string uri, int n) :
27 : CommandFacility(uri),
28 0 : n_command{n} {
29 0 : }
30 :
31 0 : void run(std::atomic<bool>& end_marker) {
32 0 : TLOG() << "Going for a run... " << n_command;
33 :
34 0 : bool once = true;
35 0 : while (end_marker) {
36 0 : if (once) {
37 : // execute n_command quick commands
38 0 : for (auto i=0; i<n_command; ++i) {
39 0 : auto democmd = nlohmann::json::parse("{\"happy\": true, \"pi\": 3.141, \"count\": " + std::to_string(i) + "}");
40 0 : inherited::execute_command(democmd, cmd::CommandReply());
41 0 : }
42 :
43 : // execute 1 slow command
44 0 : auto slowcmd = nlohmann::json::parse("{\"asd\": true}");
45 0 : inherited::execute_command(slowcmd, cmd::CommandReply());
46 :
47 : // execute again n_command quick command
48 0 : for (auto i=0; i<n_command; ++i) {
49 0 : auto democmd = nlohmann::json::parse("{\"happy\": true, \"pi\": 3.141, \"count\": " + std::to_string(i) + "}");
50 0 : inherited::execute_command(democmd, cmd::CommandReply());
51 0 : }
52 0 : once = false;
53 0 : }
54 : }
55 :
56 0 : TLOG_DEBUG(1) <<"Finished.";
57 0 : }
58 :
59 : protected:
60 : typedef CommandFacility inherited;
61 : int n_command;
62 :
63 0 : void completion_callback(const cmdobj_t& cmd, cmd::CommandReply& meta) {
64 0 : TLOG() << "Command " << cmd << "\nexecution resulted with: " << meta.result;
65 0 : }
66 :
67 : };
68 :
69 : extern "C" {
70 0 : std::shared_ptr<dunedaq::cmdlib::CommandFacility> make(std::string uri, int n) {
71 0 : return std::shared_ptr<dunedaq::cmdlib::CommandFacility>(new dummyCommandFacility(uri, n));
72 : }
73 : }
|