artdaq  v2_03_02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Commandable.cc
1 #include "artdaq/Application/Commandable.hh"
2 #include "artdaq/DAQdata/Globals.hh"
3 
5  , primary_mutex_() {}
6 
7 // **********************************************************************
8 // *** The following methods implement the externally available commands.
9 // **********************************************************************
10 
11 bool artdaq::Commandable::initialize(fhicl::ParameterSet const& pset, uint64_t timeout, uint64_t timestamp)
12 {
13  std::lock_guard<std::mutex> lk(primary_mutex_);
14  external_request_status_ = true;
15  report_string_ = "All is OK.";
16 
17  std::string initialState = fsm_.getState().getName();
18  fsm_.init(pset, timeout, timestamp);
19  if (external_request_status_)
20  {
21  std::string finalState = fsm_.getState().getName();
22  TLOG_DEBUG("CommandableInterface")
23  << "States before and after an init transition: "
24  << initialState << " and " << finalState << TLOG_ENDL;
25  }
26 
27  return (external_request_status_);
28 }
29 
30 bool artdaq::Commandable::start(art::RunID id, uint64_t timeout, uint64_t timestamp)
31 {
32  std::lock_guard<std::mutex> lk(primary_mutex_);
33  external_request_status_ = true;
34  report_string_ = "All is OK.";
35 
36  std::string initialState = fsm_.getState().getName();
37  fsm_.start(id, timeout, timestamp);
38  if (external_request_status_)
39  {
40  std::string finalState = fsm_.getState().getName();
41  TLOG_DEBUG("CommandableInterface")
42  << "States before and after a start transition: "
43  << initialState << " and " << finalState << TLOG_ENDL;
44  }
45 
46  return (external_request_status_);
47 }
48 
49 bool artdaq::Commandable::stop(uint64_t timeout, uint64_t timestamp)
50 {
51  std::lock_guard<std::mutex> lk(primary_mutex_);
52  external_request_status_ = true;
53  report_string_ = "All is OK.";
54 
55  std::string initialState = fsm_.getState().getName();
56  fsm_.stop(timeout, timestamp);
57  if (external_request_status_)
58  {
59  std::string finalState = fsm_.getState().getName();
60  TLOG_DEBUG("CommandableInterface")
61  << "States before and after a stop transition: "
62  << initialState << " and " << finalState << TLOG_ENDL;
63  }
64 
65  return (external_request_status_);
66 }
67 
68 bool artdaq::Commandable::pause(uint64_t timeout, uint64_t timestamp)
69 {
70  std::lock_guard<std::mutex> lk(primary_mutex_);
71  external_request_status_ = true;
72  report_string_ = "All is OK.";
73 
74  std::string initialState = fsm_.getState().getName();
75  fsm_.pause(timeout, timestamp);
76  if (external_request_status_)
77  {
78  std::string finalState = fsm_.getState().getName();
79  TLOG_DEBUG("CommandableInterface")
80  << "States before and after a pause transition: "
81  << initialState << " and " << finalState << TLOG_ENDL;
82  }
83 
84  return (external_request_status_);
85 }
86 
87 bool artdaq::Commandable::resume(uint64_t timeout, uint64_t timestamp)
88 {
89  std::lock_guard<std::mutex> lk(primary_mutex_);
90  external_request_status_ = true;
91  report_string_ = "All is OK.";
92 
93  std::string initialState = fsm_.getState().getName();
94  fsm_.resume(timeout, timestamp);
95  if (external_request_status_)
96  {
97  std::string finalState = fsm_.getState().getName();
98  TLOG_DEBUG("CommandableInterface")
99  << "States before and after a resume transition: "
100  << initialState << " and " << finalState << TLOG_ENDL;
101  }
102 
103  return (external_request_status_);
104 }
105 
106 bool artdaq::Commandable::shutdown(uint64_t timeout)
107 {
108  std::lock_guard<std::mutex> lk(primary_mutex_);
109  external_request_status_ = true;
110  report_string_ = "All is OK.";
111 
112  std::string initialState = fsm_.getState().getName();
113  fsm_.shutdown(timeout);
114  if (external_request_status_)
115  {
116  std::string finalState = fsm_.getState().getName();
117  TLOG_DEBUG("CommandableInterface")
118  << "States before and after a shutdown transition: "
119  << initialState << " and " << finalState << TLOG_ENDL;
120  }
121 
122  return (external_request_status_);
123 }
124 
125 bool artdaq::Commandable::soft_initialize(fhicl::ParameterSet const& pset, uint64_t timeout, uint64_t timestamp)
126 {
127  std::lock_guard<std::mutex> lk(primary_mutex_);
128  external_request_status_ = true;
129  report_string_ = "All is OK.";
130 
131  std::string initialState = fsm_.getState().getName();
132  fsm_.soft_init(pset, timeout, timestamp);
133  if (external_request_status_)
134  {
135  std::string finalState = fsm_.getState().getName();
136  TLOG_DEBUG("CommandableInterface")
137  << "States before and after a soft_init transition: "
138  << initialState << " and " << finalState << TLOG_ENDL;
139  }
140 
141  return (external_request_status_);
142 }
143 
144 bool artdaq::Commandable::reinitialize(fhicl::ParameterSet const& pset, uint64_t timeout, uint64_t timestamp)
145 {
146  std::lock_guard<std::mutex> lk(primary_mutex_);
147  external_request_status_ = true;
148  report_string_ = "All is OK.";
149 
150  std::string initialState = fsm_.getState().getName();
151  fsm_.reinit(pset, timeout, timestamp);
152  if (external_request_status_)
153  {
154  std::string finalState = fsm_.getState().getName();
155  TLOG_DEBUG("CommandableInterface")
156  << "States before and after a reinit transition: "
157  << initialState << " and " << finalState << TLOG_ENDL;
158  }
159 
160  return (external_request_status_);
161 }
162 
164 {
165  std::lock_guard<std::mutex> lk(primary_mutex_);
166  external_request_status_ = true;
167  report_string_ = "An error condition was reported while running.";
168 
169  std::string initialState = fsm_.getState().getName();
170  fsm_.in_run_failure();
171  if (external_request_status_)
172  {
173  std::string finalState = fsm_.getState().getName();
174  TLOG_DEBUG("CommandableInterface")
175  << "States before and after an in_run_failure transition: "
176  << initialState << " and " << finalState << TLOG_ENDL;
177  }
178 
179  return (external_request_status_);
180 }
181 
182 std::string artdaq::Commandable::status() const
183 {
184  std::lock_guard<std::mutex> lk(primary_mutex_);
185  std::string currentState = this->current_state();
186  if (currentState == "InRunError")
187  {
188  return "Error";
189  }
190 
191  return currentState;
192 }
193 
194 std::vector<std::string> artdaq::Commandable::legal_commands() const
195 {
196  std::lock_guard<std::mutex> lk(primary_mutex_);
197  std::string currentState = this->current_state();
198 
199  if (currentState == "Ready")
200  {
201  return {"init", "soft_init", "start", "shutdown"};
202  }
203  if (currentState == "Running")
204  {
205  // 12-May-2015, KAB: in_run_failure is also possible, but it
206  // shouldn't be requested externally.
207  return {"pause", "stop"};
208  }
209  if (currentState == "Paused")
210  {
211  return {"resume", "stop"};
212  }
213  if (currentState == "InRunError")
214  {
215  return {"pause", "stop"};
216  }
217 
218  // Booted and Error
219  return {"init", "shutdown"};
220 }
221 
222 // *******************************************************************
223 // *** The following methods implement the state machine operations.
224 // *******************************************************************
225 
226 bool artdaq::Commandable::do_initialize(fhicl::ParameterSet const&, uint64_t, uint64_t)
227 {
228  TLOG_DEBUG("CommandableInterface") << "do_initialize called." << TLOG_ENDL;
229  external_request_status_ = true;
230  return external_request_status_;
231 }
232 
233 bool artdaq::Commandable::do_start(art::RunID, uint64_t, uint64_t)
234 {
235  TLOG_DEBUG("CommandableInterface") << "do_start called." << TLOG_ENDL;
236  external_request_status_ = true;
237  return external_request_status_;
238 }
239 
240 bool artdaq::Commandable::do_stop(uint64_t, uint64_t)
241 {
242  TLOG_DEBUG("CommandableInterface") << "do_stop called." << TLOG_ENDL;
243  external_request_status_ = true;
244  return external_request_status_;
245 }
246 
247 bool artdaq::Commandable::do_pause(uint64_t, uint64_t)
248 {
249  TLOG_DEBUG("CommandableInterface") << "do_pause called." << TLOG_ENDL;
250  external_request_status_ = true;
251  return external_request_status_;
252 }
253 
254 bool artdaq::Commandable::do_resume(uint64_t, uint64_t)
255 {
256  TLOG_DEBUG("CommandableInterface") << "do_resume called." << TLOG_ENDL;
257  external_request_status_ = true;
258  return external_request_status_;
259 }
260 
262 {
263  TLOG_DEBUG("CommandableInterface") << "do_shutdown called." << TLOG_ENDL;
264  external_request_status_ = true;
265  return external_request_status_;
266 }
267 
268 bool artdaq::Commandable::do_reinitialize(fhicl::ParameterSet const&, uint64_t, uint64_t)
269 {
270  TLOG_DEBUG("CommandableInterface") << "do_reinitialize called." << TLOG_ENDL;
271  external_request_status_ = true;
272  return external_request_status_;
273 }
274 
275 bool artdaq::Commandable::do_soft_initialize(fhicl::ParameterSet const&, uint64_t, uint64_t)
276 {
277  TLOG_DEBUG("CommandableInterface") << "do_soft_initialize called." << TLOG_ENDL;
278  external_request_status_ = true;
279  return external_request_status_;
280 }
281 
282 void artdaq::Commandable::badTransition(const std::string& trans)
283 {
284  std::string currentState = this->current_state();
285  if (currentState == "InRunError")
286  {
287  currentState = "Error";
288  }
289 
290  report_string_ = "An invalid transition (";
291  report_string_.append(trans);
292  report_string_.append(") was requested; transition not allowed from this process's current state of \"");
293  report_string_.append(currentState);
294  report_string_.append("\"");
295 
296  TLOG_WARNING("CommandableInterface") << report_string_ << TLOG_ENDL;
297 
298  external_request_status_ = false;
299 }
300 
302 {
303  TLOG_DEBUG("CommandableInterface") << "BootedEnter called." << TLOG_ENDL;
304 }
305 
307 {
308  TLOG_DEBUG("CommandableInterface") << "InRunExit called." << TLOG_ENDL;
309 }
310 
311 // *********************
312 // *** Utility methods.
313 // *********************
314 
316 {
317  std::string fullStateName = (const_cast<Commandable*>(this))->fsm_.getState().getName();
318  size_t pos = fullStateName.rfind("::");
319  if (pos != std::string::npos)
320  {
321  return fullStateName.substr(pos + 2);
322  }
323  else
324  {
325  return fullStateName;
326  }
327 }
bool initialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp)
Processes the initialize request.
Definition: Commandable.cc:11
virtual bool do_start(art::RunID, uint64_t, uint64_t)
Perform the start transition.
Definition: Commandable.cc:233
Commandable is the base class for all artdaq components which implement the artdaq state machine...
Definition: Commandable.hh:20
virtual void badTransition(const std::string &trans)
This function is called when an attempt is made to call an illegal transition.
Definition: Commandable.cc:282
virtual void InRunExit()
Perform actions upon leaving the InRun state.
Definition: Commandable.cc:306
virtual bool do_pause(uint64_t, uint64_t)
Perform the pause transition.
Definition: Commandable.cc:247
bool reinitialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp)
Processes the reinitialize request.
Definition: Commandable.cc:144
std::string current_state() const
Return the name of the current state.
Definition: Commandable.cc:315
virtual void BootedEnter()
Perform actions upon entering the Booted state.
Definition: Commandable.cc:301
std::string status() const
Returns the current state of the Commandable.
Definition: Commandable.cc:182
virtual bool do_shutdown(uint64_t)
Perform the shutdown transition.
Definition: Commandable.cc:261
virtual bool do_soft_initialize(fhicl::ParameterSet const &, uint64_t, uint64_t)
Perform the soft_initialize transition.
Definition: Commandable.cc:275
bool resume(uint64_t timeout, uint64_t timestamp)
Processes the resume transition.
Definition: Commandable.cc:87
bool pause(uint64_t timeout, uint64_t timestamp)
Processes the pause transition.
Definition: Commandable.cc:68
virtual bool do_stop(uint64_t, uint64_t)
Perform the stop transition.
Definition: Commandable.cc:240
bool in_run_failure()
Actions taken when the in_run_failure state is set.
Definition: Commandable.cc:163
std::vector< std::string > legal_commands() const
Get the legal transition commands from the current state.
Definition: Commandable.cc:194
bool soft_initialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp)
Processes the soft-initialize request.
Definition: Commandable.cc:125
virtual bool do_resume(uint64_t, uint64_t)
Perform the resume transition.
Definition: Commandable.cc:254
bool start(art::RunID id, uint64_t timeout, uint64_t timestamp)
Processes the start transition.
Definition: Commandable.cc:30
virtual bool do_reinitialize(fhicl::ParameterSet const &, uint64_t, uint64_t)
Perform the reinitialize transition.
Definition: Commandable.cc:268
bool stop(uint64_t timeout, uint64_t timestamp)
Processes the stop transition.
Definition: Commandable.cc:49
virtual bool do_initialize(fhicl::ParameterSet const &, uint64_t, uint64_t)
Perform the initialize transition.
Definition: Commandable.cc:226
bool shutdown(uint64_t timeout)
Processes the shutdown transition.
Definition: Commandable.cc:106