1 #include "artdaq/Application/BoardReaderApp.hh"
2 #include "artdaq-core/Utilities/ExceptionHandler.hh"
4 #include "artdaq/DAQdata/Globals.hh"
5 #define TRACE_NAME (app_name + "_BoardReaderApp").c_str() // NOLINT
11 : fragment_receiver_ptr_(nullptr)
22 external_request_status_ =
true;
28 TLOG(TLVL_DEBUG) <<
"Initializing first deleting old instance " <<
static_cast<void*
>(fragment_receiver_ptr_.get());
29 fragment_receiver_ptr_.reset(
nullptr);
30 fragment_receiver_ptr_ = std::make_unique<BoardReaderCore>(*this);
31 TLOG(TLVL_DEBUG) <<
"Initializing new BoardReaderCore at " <<
static_cast<void*
>(fragment_receiver_ptr_.get()) <<
" with pset " << pset.to_string();
32 external_request_status_ = fragment_receiver_ptr_->initialize(pset, timeout, timestamp);
33 if (!external_request_status_)
35 report_string_ =
"Error initializing ";
36 report_string_.append(app_name +
" ");
37 report_string_.append(
"with ParameterSet = \"" + pset.to_string() +
"\".");
40 TLOG(TLVL_DEBUG) <<
"do_initialize(fhicl::ParameterSet, uint64_t, uint64_t): "
41 <<
"Done initializing.";
42 return external_request_status_;
52 fragment_receiver_ptr_->SetStartTransitionTimeout(timeout);
53 external_request_status_ =
true;
55 boost::thread::attributes attrs;
56 attrs.set_stack_size(4096 * 2000);
62 catch (
const boost::exception& e)
64 std::stringstream exception_string;
65 exception_string <<
"Caught boost::exception starting Fragment Processing threads: " << boost::diagnostic_information(e) <<
", errno=" << errno;
67 ExceptionHandler(ExceptionHandlerRethrow::yes, exception_string.str());
70 auto start_wait = std::chrono::steady_clock::now();
71 while (!fragment_receiver_ptr_->GetSenderThreadActive() || !fragment_receiver_ptr_->GetReceiverThreadActive())
73 if (TimeUtils::GetElapsedTime(start_wait) > timeout)
75 TLOG(TLVL_ERROR) <<
"Timeout occurred waiting for BoardReaderCore threads to start. Timeout = " << timeout <<
" s, Time waited = " << TimeUtils::GetElapsedTime(start_wait) <<
" s,"
76 <<
" Receiver ready: " << std::boolalpha << fragment_receiver_ptr_->GetReceiverThreadActive() <<
", Sender ready: " << fragment_receiver_ptr_->GetSenderThreadActive();
77 external_request_status_ =
false;
84 if (external_request_status_)
86 external_request_status_ = fragment_receiver_ptr_->start(
id, timeout, timestamp);
89 if (!external_request_status_)
91 report_string_ =
"Error starting ";
92 report_string_.append(app_name +
" ");
93 report_string_.append(
"for run number ");
94 report_string_.append(boost::lexical_cast<std::string>(
id.run()));
95 report_string_.append(
", timeout ");
96 report_string_.append(boost::lexical_cast<std::string>(timeout));
97 report_string_.append(
", timestamp ");
98 report_string_.append(boost::lexical_cast<std::string>(timestamp));
99 report_string_.append(
".");
102 return external_request_status_;
108 external_request_status_ = fragment_receiver_ptr_->stop(timeout, timestamp);
109 if (!external_request_status_)
111 report_string_ =
"Error stopping ";
112 report_string_.append(app_name +
".");
115 if (fragment_input_thread_.joinable())
117 TLOG(TLVL_DEBUG) <<
"Joining fragment input (Generator) thread";
118 fragment_input_thread_.join();
120 if (fragment_output_thread_.joinable())
122 TLOG(TLVL_DEBUG) <<
"Joining fragment output (Sender) thread";
123 fragment_output_thread_.join();
126 TLOG(TLVL_DEBUG) <<
"BoardReader Stopped. Getting run statistics";
127 int number_of_fragments_sent = -1;
128 if (fragment_receiver_ptr_)
130 number_of_fragments_sent = fragment_receiver_ptr_->GetFragmentsProcessed();
132 TLOG(TLVL_DEBUG) <<
"do_stop(uint64_t, uint64_t): "
133 <<
"Number of fragments sent = " << number_of_fragments_sent
136 return external_request_status_;
142 external_request_status_ = fragment_receiver_ptr_->pause(timeout, timestamp);
143 if (!external_request_status_)
145 report_string_ =
"Error pausing ";
146 report_string_.append(app_name +
".");
149 if (fragment_input_thread_.joinable()) fragment_input_thread_.join();
150 if (fragment_output_thread_.joinable()) fragment_output_thread_.join();
151 int number_of_fragments_sent = fragment_receiver_ptr_->GetFragmentsProcessed();
152 TLOG(TLVL_DEBUG) <<
"do_pause(uint64_t, uint64_t): "
153 <<
"Number of fragments sent = " << number_of_fragments_sent
156 return external_request_status_;
166 external_request_status_ =
true;
168 boost::thread::attributes attrs;
169 attrs.set_stack_size(4096 * 2000);
175 catch (
const boost::exception& e)
177 std::stringstream exception_string;
178 exception_string <<
"Caught boost::exception starting Fragment Processing threads: " << boost::diagnostic_information(e) <<
", errno=" << errno;
180 ExceptionHandler(ExceptionHandlerRethrow::yes, exception_string.str());
183 auto start_wait = std::chrono::steady_clock::now();
184 while (!fragment_receiver_ptr_->GetSenderThreadActive() || !fragment_receiver_ptr_->GetReceiverThreadActive())
186 if (TimeUtils::GetElapsedTimeMicroseconds(start_wait) > timeout * 1000000)
188 TLOG(TLVL_ERROR) <<
"Timeout occurred waiting for BoardReaderCore threads to start. Timeout = " << timeout <<
" s, Time waited = " << TimeUtils::GetElapsedTime(start_wait) <<
" s,"
189 <<
" Receiver ready: " << std::boolalpha << fragment_receiver_ptr_->GetReceiverThreadActive() <<
", Sender ready: " << fragment_receiver_ptr_->GetSenderThreadActive();
190 external_request_status_ =
false;
197 if (external_request_status_)
199 external_request_status_ = fragment_receiver_ptr_->resume(timeout, timestamp);
201 if (!external_request_status_)
203 report_string_ =
"Error resuming ";
204 report_string_.append(app_name +
".");
206 return external_request_status_;
212 external_request_status_ = fragment_receiver_ptr_->shutdown(timeout);
215 if (fragment_input_thread_.joinable()) fragment_input_thread_.join();
216 if (fragment_output_thread_.joinable()) fragment_output_thread_.join();
217 if (!external_request_status_)
219 report_string_ =
"Error shutting down ";
220 report_string_.append(app_name +
".");
222 return external_request_status_;
228 external_request_status_ = fragment_receiver_ptr_->soft_initialize(pset, timeout, timestamp);
229 if (!external_request_status_)
231 report_string_ =
"Error soft-initializing ";
232 report_string_.append(app_name +
" ");
233 report_string_.append(
"with ParameterSet = \"" + pset.to_string() +
"\".");
235 return external_request_status_;
240 external_request_status_ = fragment_receiver_ptr_->reinitialize(pset, timeout, timestamp);
241 if (!external_request_status_)
243 report_string_ =
"Error reinitializing ";
244 report_string_.append(app_name +
" ");
245 report_string_.append(
"with ParameterSet = \"" + pset.to_string() +
"\".");
247 return external_request_status_;
252 TLOG(TLVL_DEBUG) <<
"Booted state entry action called.";
258 fragment_receiver_ptr_.reset(
nullptr);
263 external_request_status_ = fragment_receiver_ptr_->metaCommand(command, arg);
264 if (!external_request_status_)
266 report_string_ =
"Error running meta-command on ";
267 report_string_.append(app_name +
" ");
268 report_string_.append(
"with command = \"" + command +
"\", arg = \"" + arg +
"\".");
270 return external_request_status_;
275 std::string resultString;
278 if (which ==
"transition_status")
280 if (report_string_.length() > 0) {
return report_string_; }
294 if (fragment_receiver_ptr_ !=
nullptr)
296 resultString.append(fragment_receiver_ptr_->report(which));
300 resultString.append(
"This BoardReader has not yet been initialized and ");
301 resultString.append(
"therefore can not provide reporting.");
bool do_soft_initialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp) override
Soft-Initialize the BoardReaderCore.
bool do_resume(uint64_t timeout, uint64_t timestamp) override
Resume the BoardReaderCore.
bool do_stop(uint64_t timeout, uint64_t timestamp) override
Stop the BoardReaderCore.
bool do_meta_command(std::string const &command, std::string const &arg) override
Perform a user-defined command (passed to CommandableFragmentGenerator)
bool do_shutdown(uint64_t timeout) override
Shutdown the BoardReaderCore.
bool do_reinitialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp) override
Reinitialize the BoardReaderCore.
std::string report(std::string const &which) const override
If which is "transition_status", report the status of the last transition. Otherwise pass through to ...
void send_fragments()
Main working loop of the BoardReaderCore, pt. 2.
BoardReaderApp()
BoardReaderApp Constructor.
void BootedEnter() override
Action taken upon entering the "Booted" state.
void receive_fragments()
Main working loop of the BoardReaderCore.
bool do_start(art::RunID id, uint64_t timeout, uint64_t timestamp) override
Start the BoardReaderCore.
bool do_initialize(fhicl::ParameterSet const &pset, uint64_t timeout, uint64_t timestamp) override
Initialize the BoardReaderCore.
bool do_pause(uint64_t timeout, uint64_t timestamp) override
Pause the BoardReaderCore.