artdaq  v3_12_02
commander_test.cc
1 #include "TRACE/tracemf.h"
2 #define TRACE_NAME "commander_test"
3 
4 #include "artdaq-core/Utilities/configureMessageFacility.hh"
5 #include "artdaq/Application/LoadParameterSet.hh"
6 #include "artdaq/DAQdata/Globals.hh"
7 #include "artdaq/ExternalComms/CommanderInterface.hh"
8 #include "artdaq/ExternalComms/MakeCommanderPlugin.hh"
9 
10 #include "fhiclcpp/types/Atom.h"
11 #include "fhiclcpp/types/Comment.h"
12 #include "fhiclcpp/types/Name.h"
13 #include "fhiclcpp/types/TableFragment.h"
14 
15 #include <boost/thread.hpp>
16 
17 // NOLINTNEXTLINE(readability-function-size)
18 int main(int argc, char** argv)
19 try
20 {
21  struct Config
22  {
23  fhicl::TableFragment<artdaq::CommanderInterface::Config> commanderPluginConfig;
24 
25  fhicl::Atom<std::string> partition_number{fhicl::Name{"partition_number"}, fhicl::Comment{"Partition to run in"}, ""};
26  };
27  artdaq::configureMessageFacility("commander_test", true, true);
28  fhicl::ParameterSet config_ps = LoadParameterSet<Config>(argc, argv, "commander_test", "A test driver for CommanderInterface plugins");
29 
30  artdaq::Globals::partition_number_ = config_ps.get<int>("partition_number", 1);
31 
32  auto id_rand = seedAndRandom();
33  if (config_ps.has_key("id"))
34  {
35  TLOG(TLVL_DEBUG) << "Ignoring set id and using random!";
36  config_ps.erase("id");
37  }
38  config_ps.put("id", artdaq::Globals::partition_number_ * 1000 + (id_rand % 1000));
39 
40  std::unique_ptr<artdaq::Commandable> cmdble(new artdaq::Commandable());
41 
42  auto commander = artdaq::MakeCommanderPlugin(config_ps, *cmdble);
43 
44  // Start server thread
45  boost::thread commanderThread([&] { commander->run_server(); });
46  while (!commander->GetStatus())
47  {
48  usleep(10000);
49  }
50  sleep(1);
51 
52  uint64_t arg = 0;
53  fhicl::ParameterSet pset;
54 
55  TLOG(TLVL_INFO) << "START";
56 
57  TLOG(TLVL_DEBUG) << "Sending init";
58  std::string sts = commander->send_init(pset, arg, arg);
59  TLOG(TLVL_DEBUG) << "init res=" << sts;
60  if (sts != "Success")
61  {
62  TLOG(TLVL_ERROR) << "init returned " << sts << ", exiting with error";
63  exit(1);
64  }
65 
66  TLOG(TLVL_DEBUG) << "Sending soft_init";
67  sts = commander->send_soft_init(pset, arg, arg);
68  TLOG(TLVL_DEBUG) << "soft_init res=" << sts;
69  if (sts != "Success")
70  {
71  TLOG(TLVL_ERROR) << "soft_init returned " << sts << ", exiting with error";
72  exit(2);
73  }
74 
75  TLOG(TLVL_DEBUG) << "Sending legal_commands";
76  sts = commander->send_legal_commands();
77  TLOG(TLVL_DEBUG) << "legal_commands res=" << sts;
78  if (sts.empty() || sts.find("Exception", 0) != std::string::npos || sts.find("Error", 0) != std::string::npos)
79  {
80  TLOG(TLVL_ERROR) << "legal_commands returned " << sts << ", exiting with error";
81  exit(3);
82  }
83 
84  TLOG(TLVL_DEBUG) << "Sending meta_command";
85  sts = commander->send_meta_command("test", "test");
86  TLOG(TLVL_DEBUG) << "meta_command res=" << sts;
87  if (sts != "Success")
88  {
89  TLOG(TLVL_ERROR) << "meta_command returned " << sts << ", exiting with error";
90  exit(4);
91  }
92 
93  TLOG(TLVL_DEBUG) << "Sending report";
94  sts = commander->send_report("test");
95  TLOG(TLVL_DEBUG) << "report res=" << sts;
96  if (sts.empty() || sts.find("Exception", 0) != std::string::npos || sts.find("Error", 0) != std::string::npos)
97  {
98  TLOG(TLVL_ERROR) << "report returned " << sts << ", exiting with error";
99  exit(5);
100  }
101 
102  TLOG(TLVL_DEBUG) << "Sending start";
103  sts = commander->send_start(art::RunID(0x7357), arg, arg);
104  TLOG(TLVL_DEBUG) << "start res=" << sts;
105  if (sts != "Success")
106  {
107  TLOG(TLVL_ERROR) << "start returned " << sts << ", exiting with error";
108  exit(6);
109  }
110 
111  TLOG(TLVL_DEBUG) << "Sending status";
112  sts = commander->send_status();
113  TLOG(TLVL_DEBUG) << "status res=" << sts;
114  if (sts.empty() || sts.find("Exception", 0) != std::string::npos || sts.find("Error", 0) != std::string::npos)
115  {
116  TLOG(TLVL_ERROR) << "status returned " << sts << ", exiting with error";
117  exit(7);
118  }
119 
120  TLOG(TLVL_DEBUG) << "Sending pause";
121  sts = commander->send_pause(arg, arg);
122  TLOG(TLVL_DEBUG) << "pause res=" << sts;
123  if (sts != "Success")
124  {
125  TLOG(TLVL_ERROR) << "pause returned " << sts << ", exiting with error";
126  exit(8);
127  }
128 
129  TLOG(TLVL_DEBUG) << "Sending resume";
130  sts = commander->send_resume(arg, arg);
131  TLOG(TLVL_DEBUG) << "resume res=" << sts;
132  if (sts != "Success")
133  {
134  TLOG(TLVL_ERROR) << "resume returned " << sts << ", exiting with error";
135  exit(9);
136  }
137 
138  TLOG(TLVL_DEBUG) << "Sending rollover_subrun";
139  sts = commander->send_rollover_subrun(arg, static_cast<uint32_t>(arg));
140  TLOG(TLVL_DEBUG) << "rollover_subrun res=" << sts;
141  if (sts != "Success")
142  {
143  TLOG(TLVL_ERROR) << "rollover_subrun returned " << sts << ", exiting with error";
144  exit(10);
145  }
146 
147  TLOG(TLVL_DEBUG) << "Sending stop";
148  sts = commander->send_stop(arg, arg);
149  TLOG(TLVL_DEBUG) << "stop res=" << sts;
150  if (sts != "Success")
151  {
152  TLOG(TLVL_ERROR) << "stop returned " << sts << ", exiting with error";
153  exit(11);
154  }
155 
156  TLOG(TLVL_DEBUG) << "Sending reinit";
157  sts = commander->send_reinit(pset, arg, arg);
158  TLOG(TLVL_DEBUG) << "reinit res=" << sts;
159  if (sts != "Success")
160  {
161  TLOG(TLVL_ERROR) << "reinit returned " << sts << ", exiting with error";
162  exit(12);
163  }
164 
165  TLOG(TLVL_DEBUG) << "Sending trace_set";
166  sts = commander->send_trace_set("TRACE", "M", "0x7357AAAABBBBCCCC");
167  TLOG(TLVL_DEBUG) << "trace_set res=" << sts;
168  if (sts != "Success")
169  {
170  TLOG(TLVL_ERROR) << "trace_set returned " << sts << ", exiting with error";
171  exit(13);
172  }
173 
174  TLOG(TLVL_DEBUG) << "Sending trace_get";
175  sts = commander->send_trace_get("TRACE");
176  TLOG(TLVL_DEBUG) << "trace_get res=" << sts;
177  if (sts.empty() || sts.find("Exception", 0) != std::string::npos || sts.find("Error", 0) != std::string::npos)
178  {
179  TLOG(TLVL_ERROR) << "trace_get returned " << sts << ", exiting with error";
180  exit(14);
181  }
182 
183  TLOG(TLVL_DEBUG) << "Sending register_monitor";
184  sts = commander->send_register_monitor("unqiue_label: test");
185  TLOG(TLVL_DEBUG) << "register_monitor res=" << sts;
186  if (sts.empty() || sts.find("Exception", 0) != std::string::npos || sts.find("Error", 0) != std::string::npos)
187  {
188  TLOG(TLVL_ERROR) << "register_monitor returned " << sts << ", exiting with error";
189  exit(15);
190  }
191 
192  TLOG(TLVL_DEBUG) << "Sending unregister_monitor";
193  sts = commander->send_unregister_monitor("test");
194  TLOG(TLVL_DEBUG) << "unregister_monitor res=" << sts;
195  if (sts.empty() || sts.find("Exception", 0) != std::string::npos || sts.find("Error", 0) != std::string::npos)
196  {
197  TLOG(TLVL_ERROR) << "unregister_monitor returned " << sts << ", exiting with error";
198  exit(16);
199  }
200 
201  TLOG(TLVL_DEBUG) << "Sending shutdown";
202  sts = commander->send_shutdown(arg);
203  TLOG(TLVL_DEBUG) << "shutdown res=" << sts;
204  if (sts != "Success")
205  {
206  TLOG(TLVL_ERROR) << "shutdown returned " << sts << ", exiting with error";
207  exit(17);
208  }
209 
210  TLOG(TLVL_INFO) << "DONE";
211 
212  if (commanderThread.joinable())
213  {
214  commanderThread.join();
215  }
217 
218  return 0;
219 }
220 catch (...)
221 {
222  return -1;
223 }
Commandable is the base class for all artdaq components which implement the artdaq state machine...
Definition: Commandable.hh:22
static void CleanUpGlobals()
Clean up statically-allocated Manager class instances.
Definition: Globals.hh:156
Configuration for simple_metric_sender.
std::unique_ptr< artdaq::CommanderInterface > MakeCommanderPlugin(const fhicl::ParameterSet &commander_pset, artdaq::Commandable &commandable)
Load a CommanderInterface plugin.
static int partition_number_
The partition number of the current application.
Definition: Globals.hh:45