00001 #include "artdaq/Application/Commandable.hh"
00002 #include "artdaq/DAQdata/Globals.hh"
00003
00004 artdaq::Commandable::Commandable() : fsm_(*this)
00005 , primary_mutex_() {}
00006
00007
00008
00009
00010
00011 bool artdaq::Commandable::initialize(fhicl::ParameterSet const& pset, uint64_t timeout, uint64_t timestamp)
00012 {
00013 std::lock_guard<std::mutex> lk(primary_mutex_);
00014 external_request_status_ = true;
00015 report_string_ = "All is OK.";
00016
00017 std::string initialState = fsm_.getState().getName();
00018 fsm_.init(pset, timeout, timestamp);
00019 if (external_request_status_)
00020 {
00021 std::string finalState = fsm_.getState().getName();
00022 TLOG_DEBUG("CommandableInterface")
00023 << "States before and after an init transition: "
00024 << initialState << " and " << finalState << TLOG_ENDL;
00025 }
00026
00027 return (external_request_status_);
00028 }
00029
00030 bool artdaq::Commandable::start(art::RunID id, uint64_t timeout, uint64_t timestamp)
00031 {
00032 std::lock_guard<std::mutex> lk(primary_mutex_);
00033 external_request_status_ = true;
00034 report_string_ = "All is OK.";
00035
00036 std::string initialState = fsm_.getState().getName();
00037 fsm_.start(id, timeout, timestamp);
00038 if (external_request_status_)
00039 {
00040 std::string finalState = fsm_.getState().getName();
00041 TLOG_DEBUG("CommandableInterface")
00042 << "States before and after a start transition: "
00043 << initialState << " and " << finalState << TLOG_ENDL;
00044 }
00045
00046 return (external_request_status_);
00047 }
00048
00049 bool artdaq::Commandable::stop(uint64_t timeout, uint64_t timestamp)
00050 {
00051 std::lock_guard<std::mutex> lk(primary_mutex_);
00052 external_request_status_ = true;
00053 report_string_ = "All is OK.";
00054
00055 std::string initialState = fsm_.getState().getName();
00056 fsm_.stop(timeout, timestamp);
00057 if (external_request_status_)
00058 {
00059 std::string finalState = fsm_.getState().getName();
00060 TLOG_DEBUG("CommandableInterface")
00061 << "States before and after a stop transition: "
00062 << initialState << " and " << finalState << TLOG_ENDL;
00063 }
00064
00065 return (external_request_status_);
00066 }
00067
00068 bool artdaq::Commandable::pause(uint64_t timeout, uint64_t timestamp)
00069 {
00070 std::lock_guard<std::mutex> lk(primary_mutex_);
00071 external_request_status_ = true;
00072 report_string_ = "All is OK.";
00073
00074 std::string initialState = fsm_.getState().getName();
00075 fsm_.pause(timeout, timestamp);
00076 if (external_request_status_)
00077 {
00078 std::string finalState = fsm_.getState().getName();
00079 TLOG_DEBUG("CommandableInterface")
00080 << "States before and after a pause transition: "
00081 << initialState << " and " << finalState << TLOG_ENDL;
00082 }
00083
00084 return (external_request_status_);
00085 }
00086
00087 bool artdaq::Commandable::resume(uint64_t timeout, uint64_t timestamp)
00088 {
00089 std::lock_guard<std::mutex> lk(primary_mutex_);
00090 external_request_status_ = true;
00091 report_string_ = "All is OK.";
00092
00093 std::string initialState = fsm_.getState().getName();
00094 fsm_.resume(timeout, timestamp);
00095 if (external_request_status_)
00096 {
00097 std::string finalState = fsm_.getState().getName();
00098 TLOG_DEBUG("CommandableInterface")
00099 << "States before and after a resume transition: "
00100 << initialState << " and " << finalState << TLOG_ENDL;
00101 }
00102
00103 return (external_request_status_);
00104 }
00105
00106 bool artdaq::Commandable::shutdown(uint64_t timeout)
00107 {
00108 std::lock_guard<std::mutex> lk(primary_mutex_);
00109 external_request_status_ = true;
00110 report_string_ = "All is OK.";
00111
00112 std::string initialState = fsm_.getState().getName();
00113 fsm_.shutdown(timeout);
00114 if (external_request_status_)
00115 {
00116 std::string finalState = fsm_.getState().getName();
00117 TLOG_DEBUG("CommandableInterface")
00118 << "States before and after a shutdown transition: "
00119 << initialState << " and " << finalState << TLOG_ENDL;
00120 }
00121
00122 return (external_request_status_);
00123 }
00124
00125 bool artdaq::Commandable::soft_initialize(fhicl::ParameterSet const& pset, uint64_t timeout, uint64_t timestamp)
00126 {
00127 std::lock_guard<std::mutex> lk(primary_mutex_);
00128 external_request_status_ = true;
00129 report_string_ = "All is OK.";
00130
00131 std::string initialState = fsm_.getState().getName();
00132 fsm_.soft_init(pset, timeout, timestamp);
00133 if (external_request_status_)
00134 {
00135 std::string finalState = fsm_.getState().getName();
00136 TLOG_DEBUG("CommandableInterface")
00137 << "States before and after a soft_init transition: "
00138 << initialState << " and " << finalState << TLOG_ENDL;
00139 }
00140
00141 return (external_request_status_);
00142 }
00143
00144 bool artdaq::Commandable::reinitialize(fhicl::ParameterSet const& pset, uint64_t timeout, uint64_t timestamp)
00145 {
00146 std::lock_guard<std::mutex> lk(primary_mutex_);
00147 external_request_status_ = true;
00148 report_string_ = "All is OK.";
00149
00150 std::string initialState = fsm_.getState().getName();
00151 fsm_.reinit(pset, timeout, timestamp);
00152 if (external_request_status_)
00153 {
00154 std::string finalState = fsm_.getState().getName();
00155 TLOG_DEBUG("CommandableInterface")
00156 << "States before and after a reinit transition: "
00157 << initialState << " and " << finalState << TLOG_ENDL;
00158 }
00159
00160 return (external_request_status_);
00161 }
00162
00163 bool artdaq::Commandable::in_run_failure()
00164 {
00165 std::lock_guard<std::mutex> lk(primary_mutex_);
00166 external_request_status_ = true;
00167 report_string_ = "An error condition was reported while running.";
00168
00169 std::string initialState = fsm_.getState().getName();
00170 fsm_.in_run_failure();
00171 if (external_request_status_)
00172 {
00173 std::string finalState = fsm_.getState().getName();
00174 TLOG_DEBUG("CommandableInterface")
00175 << "States before and after an in_run_failure transition: "
00176 << initialState << " and " << finalState << TLOG_ENDL;
00177 }
00178
00179 return (external_request_status_);
00180 }
00181
00182 std::string artdaq::Commandable::status() const
00183 {
00184 std::lock_guard<std::mutex> lk(primary_mutex_);
00185 std::string currentState = this->current_state();
00186 if (currentState == "InRunError")
00187 {
00188 return "Error";
00189 }
00190
00191 return currentState;
00192 }
00193
00194 std::vector<std::string> artdaq::Commandable::legal_commands() const
00195 {
00196 std::lock_guard<std::mutex> lk(primary_mutex_);
00197 std::string currentState = this->current_state();
00198
00199 if (currentState == "Ready")
00200 {
00201 return { "init", "soft_init", "start", "shutdown" };
00202 }
00203 if (currentState == "Running")
00204 {
00205
00206
00207 return { "pause", "stop" };
00208 }
00209 if (currentState == "Paused")
00210 {
00211 return { "resume", "stop" };
00212 }
00213 if (currentState == "InRunError")
00214 {
00215 return { "pause", "stop" };
00216 }
00217
00218
00219 return { "init", "shutdown" };
00220 }
00221
00222
00223
00224
00225
00226 bool artdaq::Commandable::do_initialize(fhicl::ParameterSet const&, uint64_t, uint64_t)
00227 {
00228 TLOG_DEBUG("CommandableInterface") << "do_initialize called." << TLOG_ENDL;
00229 external_request_status_ = true;
00230 return external_request_status_;
00231 }
00232
00233 bool artdaq::Commandable::do_start(art::RunID, uint64_t, uint64_t)
00234 {
00235 TLOG_DEBUG("CommandableInterface") << "do_start called." << TLOG_ENDL;
00236 external_request_status_ = true;
00237 return external_request_status_;
00238 }
00239
00240 bool artdaq::Commandable::do_stop(uint64_t, uint64_t)
00241 {
00242 TLOG_DEBUG("CommandableInterface") << "do_stop called." << TLOG_ENDL;
00243 external_request_status_ = true;
00244 return external_request_status_;
00245 }
00246
00247 bool artdaq::Commandable::do_pause(uint64_t, uint64_t)
00248 {
00249 TLOG_DEBUG("CommandableInterface") << "do_pause called." << TLOG_ENDL;
00250 external_request_status_ = true;
00251 return external_request_status_;
00252 }
00253
00254 bool artdaq::Commandable::do_resume(uint64_t, uint64_t)
00255 {
00256 TLOG_DEBUG("CommandableInterface") << "do_resume called." << TLOG_ENDL;
00257 external_request_status_ = true;
00258 return external_request_status_;
00259 }
00260
00261 bool artdaq::Commandable::do_shutdown(uint64_t)
00262 {
00263 TLOG_DEBUG("CommandableInterface") << "do_shutdown called." << TLOG_ENDL;
00264 external_request_status_ = true;
00265 return external_request_status_;
00266 }
00267
00268 bool artdaq::Commandable::do_reinitialize(fhicl::ParameterSet const&, uint64_t, uint64_t)
00269 {
00270 TLOG_DEBUG("CommandableInterface") << "do_reinitialize called." << TLOG_ENDL;
00271 external_request_status_ = true;
00272 return external_request_status_;
00273 }
00274
00275 bool artdaq::Commandable::do_soft_initialize(fhicl::ParameterSet const&, uint64_t, uint64_t)
00276 {
00277 TLOG_DEBUG("CommandableInterface") << "do_soft_initialize called." << TLOG_ENDL;
00278 external_request_status_ = true;
00279 return external_request_status_;
00280 }
00281
00282 void artdaq::Commandable::badTransition(const std::string& trans)
00283 {
00284 std::string currentState = this->current_state();
00285 if (currentState == "InRunError")
00286 {
00287 currentState = "Error";
00288 }
00289
00290 report_string_ = "An invalid transition (";
00291 report_string_.append(trans);
00292 report_string_.append(") was requested; transition not allowed from this process's current state of \"");
00293 report_string_.append(currentState);
00294 report_string_.append("\"");
00295
00296 TLOG_WARNING("CommandableInterface") << report_string_ << TLOG_ENDL;
00297
00298 external_request_status_ = false;
00299 }
00300
00301 void artdaq::Commandable::BootedEnter()
00302 {
00303 TLOG_DEBUG("CommandableInterface") << "BootedEnter called." << TLOG_ENDL;
00304 }
00305
00306 void artdaq::Commandable::InRunExit()
00307 {
00308 TLOG_DEBUG("CommandableInterface") << "InRunExit called." << TLOG_ENDL;
00309 }
00310
00311
00312 std::string artdaq::Commandable::do_trace_get(std::string const& name)
00313 {
00314 TLOG_DEBUG("CommandableInterface") << "Getting masks for name " << name << TLOG_ENDL;
00315 std::ostringstream ss;
00316 if (name == "ALL")
00317 {
00318 unsigned ii = 0;
00319 unsigned ee = traceControl_p->num_namLvlTblEnts;
00320 for (ii = 0; ii < ee; ++ii)
00321 {
00322 if (traceNamLvls_p[ii].name[0])
00323 ss << traceNamLvls_p[ii].name << " " << std::hex << std::showbase << traceNamLvls_p[ii].M << " " << traceNamLvls_p[ii].S << " " << traceNamLvls_p[ii].T << " " << std::endl;
00324 }
00325 }
00326 else
00327 {
00328 unsigned ii = 0;
00329 unsigned ee = traceControl_p->num_namLvlTblEnts;
00330 for (ii = 0; ii < ee; ++ii)
00331 {
00332 if (traceNamLvls_p[ii].name[0] && TMATCHCMP(name.c_str(), traceNamLvls_p[ii].name)) break;
00333 }
00334 if (ii == ee) return "";
00335
00336 ss << std::hex << traceNamLvls_p[ii].M << " " << traceNamLvls_p[ii].S << " " << traceNamLvls_p[ii].T;
00337 }
00338 return ss.str();
00339 }
00340
00341 bool artdaq::Commandable::do_trace_set(std::string const& type, std::string const& name, uint64_t mask)
00342 {
00343 TLOG_DEBUG("CommandableInterface") << "Setting msk " << type << " for name " << name << " to " << std::hex << std::showbase << mask << TLOG_ENDL;
00344 if (name != "ALL")
00345 {
00346 if (type.size() > 0) {
00347 switch (type[0])
00348 {
00349 case 'M':
00350 TRACE_CNTL("lvlmsknM", name.c_str(), mask);
00351 break;
00352 case 'S':
00353 TRACE_CNTL("lvlmsknS", name.c_str(), mask);
00354 break;
00355 case 'T':
00356 TRACE_CNTL("lvlmsknT", name.c_str(), mask);
00357 break;
00358 }
00359 }
00360 else
00361 {
00362 TLOG_ERROR("CommandableInterface") << "Cannot set mask: no type specified!" << TLOG_ENDL;
00363 }
00364 }
00365 else
00366 {
00367 if (type.size() > 0) {
00368 switch (type[0])
00369 {
00370 case 'M':
00371 TRACE_CNTL("lvlmskMg", mask);
00372 break;
00373 case 'S':
00374 TRACE_CNTL("lvlmskSg", mask);
00375 break;
00376 case 'T':
00377 TRACE_CNTL("lvlmskTg", mask);
00378 break;
00379 }
00380 }
00381 else
00382 {
00383 TLOG_ERROR("CommandableInterface") << "Cannot set mask: no type specified!" << TLOG_ENDL;
00384 }
00385 }
00386 return true;
00387 }
00388
00389 bool artdaq::Commandable::do_meta_command(std::string const& cmd, std::string const& arg)
00390 {
00391 TLOG_DEBUG("CommandableInterface") << "Meta-Command called: cmd=" << cmd << ", arg=" << arg << TLOG_ENDL;
00392 return true;
00393 }
00394
00395
00396
00397
00398
00399 std::string artdaq::Commandable::current_state() const
00400 {
00401 std::string fullStateName = (const_cast<Commandable*>(this))->fsm_.getState().getName();
00402 size_t pos = fullStateName.rfind("::");
00403 if (pos != std::string::npos)
00404 {
00405 return fullStateName.substr(pos + 2);
00406 }
00407 else
00408 {
00409 return fullStateName;
00410 }
00411 }