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