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
00313
00314
00315 std::string artdaq::Commandable::current_state() const
00316 {
00317 std::string fullStateName = (const_cast<Commandable*>(this))->fsm_.getState().getName();
00318 size_t pos = fullStateName.rfind("::");
00319 if (pos != std::string::npos)
00320 {
00321 return fullStateName.substr(pos + 2);
00322 }
00323 else
00324 {
00325 return fullStateName;
00326 }
00327 }