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