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