1 #include "otsdaq/EventBuilderApp/EventBuilderApp.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutMacros.h"
4 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h"
5 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
6 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
8 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationGroupKey.h"
9 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
10 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
12 #include <toolbox/fsm/FailedEvent.h>
14 #include <xdaq/NamespaceURI.h>
15 #include <xoap/Method.h>
18 #include "messagefacility/MessageLogger/MessageLogger.h"
19 #include "artdaq-core/Utilities/configureMessageFacility.hh"
20 #include "cetlib/exception.h"
21 #include "artdaq/BuildInfo/GetPackageBuildInfo.hh"
22 #include "fhiclcpp/make_ParameterSet.h"
35 : xdaq::Application (stub)
37 , stateMachineWorkLoopManager_ (toolbox::task::bind(this, &
EventBuilderApp::stateMachineThread, "StateMachine"))
38 , stateMachineSemaphore_ (toolbox::BSem::FULL)
40 , XDAQContextConfigurationName_(theConfigurationManager_->__GET_CONFIG__(
XDAQContextConfiguration)->getConfigurationName())
41 , supervisorConfigurationPath_ ("INITIALIZED INSIDE THE CONTRUCTOR BECAUSE IT NEEDS supervisorContextUID_ and supervisorApplicationUID_")
42 , supervisorContextUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
43 , supervisorApplicationUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
48 INIT_MF(
"EventBuilderApp");
49 xgi::bind (
this, &EventBuilderApp::Default,
"Default" );
50 xgi::bind (
this, &EventBuilderApp::stateMachineXgiHandler,
"StateMachineXgiHandler");
52 xoap::bind(
this, &EventBuilderApp::stateMachineStateRequest,
"StateMachineStateRequest", XDAQ_NS_URI );
53 xoap::bind(
this, &EventBuilderApp::stateMachineErrorMessageRequest,
"StateMachineErrorMessageRequest", XDAQ_NS_URI );
57 supervisorContextUID_ = theConfigurationManager_->__GET_CONFIG__(
XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL());
61 __COUT_ERR__ <<
"XDAQ Supervisor could not access it's configuration through the Configuration Context Group." <<
62 " The XDAQContextConfigurationName = " << XDAQContextConfigurationName_ <<
63 ". The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
70 getApplicationContext()->getContextDescriptor()->getURL(),
71 getApplicationDescriptor()->getLocalId()
76 __COUT_ERR__ <<
"XDAQ Supervisor could not access it's configuration through the Configuration Application Group."
77 <<
" The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
80 supervisorConfigurationPath_ =
"/" + supervisorContextUID_ +
"/LinkToApplicationConfiguration/" + supervisorApplicationUID_ +
"/LinkToSupervisorConfiguration";
82 setStateMachineName(supervisorApplicationUID_);
87 EventBuilderApp::~EventBuilderApp(
void)
92 void EventBuilderApp::init(
void)
94 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR INIT START!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
95 allSupervisorInfo_.init(getApplicationContext());
97 artdaq::configureMessageFacility(
"eventbuilder");
101 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR NO ERRORS MAKING MSG FACILITY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
102 std::string name =
"Builder";
103 unsigned short port = 5200;
105 artdaq::setMsgFacAppName(name, port);
106 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR MSG FACILITY DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
107 mf::LogDebug(name +
"Supervisor") <<
"artdaq version " <<
109 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getPackageVersion()
111 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getBuildTimestamp();
115 my_rank = this->getApplicationDescriptor()->getLocalId();
116 theARTDAQEventBuilderInterfaces_[0] =
new artdaq::EventBuilderApp( );
117 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR INIT DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
121 void EventBuilderApp::destroy(
void)
126 void EventBuilderApp::Default(xgi::Input * in, xgi::Output * out )
130 *out <<
"<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/EventBuilderApp.html?urn=" <<
131 this->getApplicationDescriptor()->getLocalId() <<
"'></frameset></html>";
135 void EventBuilderApp::stateMachineXgiHandler(xgi::Input * in, xgi::Output * out )
140 void EventBuilderApp::stateMachineResultXgiHandler(xgi::Input* in, xgi::Output* out )
145 xoap::MessageReference EventBuilderApp::stateMachineXoapHandler(xoap::MessageReference message )
148 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
149 stateMachineWorkLoopManager_.removeProcessedRequests();
150 stateMachineWorkLoopManager_.processRequest(message);
151 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
156 xoap::MessageReference EventBuilderApp::stateMachineResultXoapHandler(xoap::MessageReference message )
159 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
162 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
167 bool EventBuilderApp::stateMachineThread(toolbox::task::WorkLoop* workLoop)
169 stateMachineSemaphore_.take();
170 std::cout << __COUT_HDR_FL__ <<
"Re-sending message..." << SOAPUtilities::translate(stateMachineWorkLoopManager_.getMessage(workLoop)).getCommand() << std::endl;
171 std::string reply = send(this->getApplicationDescriptor(),stateMachineWorkLoopManager_.getMessage(workLoop));
172 stateMachineWorkLoopManager_.report(workLoop, reply, 100,
true);
173 std::cout << __COUT_HDR_FL__ <<
"Done with message" << std::endl;
174 stateMachineSemaphore_.give();
180 xoap::MessageReference EventBuilderApp::stateMachineStateRequest(xoap::MessageReference message)
183 std::cout << __COUT_HDR_FL__ << theStateMachine_.getCurrentStateName() << std::endl;
184 return SOAPUtilities::makeSOAPMessageReference(theStateMachine_.getCurrentStateName());
188 xoap::MessageReference EventBuilderApp::stateMachineErrorMessageRequest(xoap::MessageReference message)
191 __COUT__<<
"theStateMachine_.getErrorMessage() = " << theStateMachine_.getErrorMessage() << std::endl;
194 retParameters.addParameter(
"ErrorMessage",theStateMachine_.getErrorMessage());
195 return SOAPUtilities::makeSOAPMessageReference(
"stateMachineErrorMessageRequestReply",retParameters);
199 void EventBuilderApp::stateInitial(toolbox::fsm::FiniteStateMachine& fsm)
206 void EventBuilderApp::stateHalted(toolbox::fsm::FiniteStateMachine& fsm)
213 void EventBuilderApp::stateRunning(toolbox::fsm::FiniteStateMachine& fsm)
220 void EventBuilderApp::stateConfigured(toolbox::fsm::FiniteStateMachine& fsm)
227 void EventBuilderApp::statePaused(toolbox::fsm::FiniteStateMachine& fsm)
234 void EventBuilderApp::inError (toolbox::fsm::FiniteStateMachine & fsm)
237 std::cout << __COUT_HDR_FL__ <<
"Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
242 void EventBuilderApp::enteringError (toolbox::Event::Reference e)
245 std::cout << __COUT_HDR_FL__ <<
"Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
246 toolbox::fsm::FailedEvent& failedEvent =
dynamic_cast<toolbox::fsm::FailedEvent&
>(*e);
247 std::ostringstream error;
248 error <<
"Failure performing transition from "
249 << failedEvent.getFromState()
251 << failedEvent.getToState()
252 <<
" exception: " << failedEvent.getException().what();
253 std::cout << __COUT_HDR_FL__ << error.str() << std::endl;
258 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
259 #define ARTDAQ_FILE_PREAMBLE "builder"
262 void EventBuilderApp::transitionConfiguring(toolbox::Event::Reference e)
266 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
267 std::cout << __COUT_HDR_FL__ << SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) << std::endl;
271 SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
272 getParameters().getValue(
"ConfigurationGroupName"),
274 getParameters().getValue(
"ConfigurationGroupKey")));
276 __COUT__ <<
"Configuration group name: " << theGroup.first <<
" key: " <<
277 theGroup.second << std::endl;
279 theConfigurationManager_->loadConfigurationGroup(
281 theGroup.second,
true);
295 __COUT__ << std::endl;
296 __COUT__ << std::endl;
298 fhicl::ParameterSet pset;
302 std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE +
"-";
303 std::string uid = theConfigurationManager_->getNode(XDAQContextConfigurationName_).getNode(supervisorConfigurationPath_).getValue();
305 __COUT__ <<
"uid: " << uid << std::endl;
306 for(
unsigned int i=0;i<uid.size();++i)
307 if((uid[i] >=
'a' && uid[i] <=
'z') ||
308 (uid[i] >=
'A' && uid[i] <=
'Z') ||
309 (uid[i] >=
'0' && uid[i] <=
'9'))
313 __COUT__ <<
"filename: " << filename << std::endl;
315 std::string fileFclString;
317 std::ifstream in(filename, std::ios::in | std::ios::binary);
320 std::string contents;
321 in.seekg(0, std::ios::end);
322 fileFclString.resize(in.tellg());
323 in.seekg(0, std::ios::beg);
324 in.read(&fileFclString[0], fileFclString.size());
328 __COUT__ << fileFclString << std::endl;
332 fhicl::make_ParameterSet(fileFclString, pset);
336 for(
auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
337 it->second->initialize(pset,0,0);
339 catch(
const cet::coded_exception<fhicl::error, &fhicl::detail::translate>& e)
341 __SS__ <<
"Error was caught while configuring: " << e.what() << std::endl;
342 __COUT_ERR__ <<
"\n" << ss.str();
343 theStateMachine_.setErrorMessage(ss.str());
344 throw toolbox::fsm::exception::Exception(
347 "EventBuilderApp::transitionConfiguring" ,
353 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR DONE CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
358 void EventBuilderApp::transitionHalting(toolbox::Event::Reference e)
367 void EventBuilderApp::transitionInitializing(toolbox::Event::Reference e)
374 void EventBuilderApp::transitionPausing(toolbox::Event::Reference e)
378 for(
auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
379 it->second->pause(0, 0);
383 void EventBuilderApp::transitionResuming(toolbox::Event::Reference e)
387 for(
auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
388 it->second->resume(0, 0);
392 void EventBuilderApp::transitionStarting(toolbox::Event::Reference e)
396 art::RunID runId((art::RunNumber_t)boost::lexical_cast<art::RunNumber_t>(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue(
"RunNumber")));
397 for(
auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
398 it->second->start(runId, 0, 0);
402 void EventBuilderApp::transitionStopping(toolbox::Event::Reference e)
406 for(
auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
407 it->second->stop(45, 0);
409 for(
auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
410 it->second->shutdown(45);