1 #include "otsdaq/EventBuilderApp/EventBuilderApp.h"
2 #include "otsdaq/EventBuilderApp/EventBuilderInterface.h"
3 #include "otsdaq-core/MessageFacility/MessageFacility.h"
4 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
5 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h"
6 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
7 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
9 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationGroupKey.h"
10 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
11 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
13 #include <toolbox/fsm/FailedEvent.h>
15 #include <xdaq/NamespaceURI.h>
16 #include <xoap/Method.h>
19 #include "messagefacility/MessageLogger/MessageLogger.h"
20 #include "artdaq/DAQdata/configureMessageFacility.hh"
21 #include "artdaq/DAQrate/quiet_mpi.hh"
22 #include "cetlib/exception.h"
23 #include "artdaq/BuildInfo/GetPackageBuildInfo.hh"
24 #include "fhiclcpp/make_ParameterSet.h"
36 throw (xdaq::exception::Exception)
37 : xdaq::Application (stub)
39 , stateMachineWorkLoopManager_ (toolbox::task::bind(this, &
EventBuilderApp::stateMachineThread, "StateMachine"))
40 , stateMachineSemaphore_ (toolbox::BSem::FULL)
42 , XDAQContextConfigurationName_(theConfigurationManager_->__GET_CONFIG__(
XDAQContextConfiguration)->getConfigurationName())
43 , supervisorConfigurationPath_ ("INITIALIZED INSIDE THE CONTRUCTOR BECAUSE IT NEEDS supervisorContextUID_ and supervisorApplicationUID_")
44 , supervisorContextUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
45 , supervisorApplicationUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
50 INIT_MF(
"EventBuilderApp");
51 xgi::bind (
this, &EventBuilderApp::Default,
"Default" );
52 xgi::bind (
this, &EventBuilderApp::stateMachineXgiHandler,
"StateMachineXgiHandler");
54 xoap::bind(
this, &EventBuilderApp::stateMachineStateRequest,
"StateMachineStateRequest", XDAQ_NS_URI );
57 supervisorContextUID_ = theConfigurationManager_->__GET_CONFIG__(
XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL());
61 __MOUT_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 __MOUT_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 theSupervisorDescriptorInfo_.init(getApplicationContext());
96 artdaq::configureMessageFacility(
"eventbuilder");
100 int const wanted_threading_level { MPI_THREAD_MULTIPLE };
103 MPI_Comm local_group_comm;
106 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR TRYING MPISENTRY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
107 mpiSentry_.reset(
new artdaq::MPISentry(0, 0, wanted_threading_level, artdaq::TaskType::EventBuilderTask, local_group_comm) );
108 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR DONE MPISENTRY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
110 catch (cet::exception& errormsg)
112 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR INIT ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
113 mf::LogError(
"EventBuilderMain") << errormsg ;
114 mf::LogError(
"EventBuilderMain") <<
"MPISentry error encountered in EventBuilderMain; exiting...";
118 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR NO ERRORS MAKING MSG FACILITY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
119 std::string name =
"Builder";
120 unsigned short port = 5200;
122 artdaq::setMsgFacAppName(name, port);
123 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR MSG FACILITY DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
124 mf::LogDebug(name +
"Supervisor") <<
"artdaq version " <<
126 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getPackageVersion()
128 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getBuildTimestamp();
134 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR INIT DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
138 void EventBuilderApp::destroy(
void)
145 void EventBuilderApp::Default(xgi::Input * in, xgi::Output * out )
146 throw (xgi::exception::Exception)
149 *out <<
"<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/EventBuilderApp.html?urn=" <<
150 this->getApplicationDescriptor()->getLocalId() <<
"'></frameset></html>";
154 void EventBuilderApp::stateMachineXgiHandler(xgi::Input * in, xgi::Output * out )
155 throw (xgi::exception::Exception)
159 void EventBuilderApp::stateMachineResultXgiHandler(xgi::Input* in, xgi::Output* out )
160 throw (xgi::exception::Exception)
164 xoap::MessageReference EventBuilderApp::stateMachineXoapHandler(xoap::MessageReference message )
165 throw (xoap::exception::Exception)
167 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
168 stateMachineWorkLoopManager_.removeProcessedRequests();
169 stateMachineWorkLoopManager_.processRequest(message);
170 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
175 xoap::MessageReference EventBuilderApp::stateMachineResultXoapHandler(xoap::MessageReference message )
176 throw (xoap::exception::Exception)
178 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
181 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
186 bool EventBuilderApp::stateMachineThread(toolbox::task::WorkLoop* workLoop)
188 stateMachineSemaphore_.take();
189 std::cout << __COUT_HDR_FL__ <<
"Re-sending message..." << SOAPUtilities::translate(stateMachineWorkLoopManager_.getMessage(workLoop)).getCommand() << std::endl;
190 std::string reply = send(this->getApplicationDescriptor(),stateMachineWorkLoopManager_.getMessage(workLoop));
191 stateMachineWorkLoopManager_.report(workLoop, reply, 100,
true);
192 std::cout << __COUT_HDR_FL__ <<
"Done with message" << std::endl;
193 stateMachineSemaphore_.give();
199 xoap::MessageReference EventBuilderApp::stateMachineStateRequest(xoap::MessageReference message)
200 throw (xoap::exception::Exception)
202 std::cout << __COUT_HDR_FL__ << theStateMachine_.getCurrentStateName() << std::endl;
203 return SOAPUtilities::makeSOAPMessageReference(theStateMachine_.getCurrentStateName());
207 void EventBuilderApp::stateInitial(toolbox::fsm::FiniteStateMachine& fsm)
208 throw (toolbox::fsm::exception::Exception)
214 void EventBuilderApp::stateHalted(toolbox::fsm::FiniteStateMachine& fsm)
215 throw (toolbox::fsm::exception::Exception)
221 void EventBuilderApp::stateRunning(toolbox::fsm::FiniteStateMachine& fsm)
222 throw (toolbox::fsm::exception::Exception)
228 void EventBuilderApp::stateConfigured(toolbox::fsm::FiniteStateMachine& fsm)
229 throw (toolbox::fsm::exception::Exception)
235 void EventBuilderApp::statePaused(toolbox::fsm::FiniteStateMachine& fsm)
236 throw (toolbox::fsm::exception::Exception)
242 void EventBuilderApp::inError (toolbox::fsm::FiniteStateMachine & fsm)
243 throw (toolbox::fsm::exception::Exception)
245 std::cout << __COUT_HDR_FL__ <<
"Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
250 void EventBuilderApp::enteringError (toolbox::Event::Reference e)
251 throw (toolbox::fsm::exception::Exception)
253 std::cout << __COUT_HDR_FL__ <<
"Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
254 toolbox::fsm::FailedEvent& failedEvent =
dynamic_cast<toolbox::fsm::FailedEvent&
>(*e);
255 std::ostringstream error;
256 error <<
"Failure performing transition from "
257 << failedEvent.getFromState()
259 << failedEvent.getToState()
260 <<
" exception: " << failedEvent.getException().what();
261 std::cout << __COUT_HDR_FL__ << error.str() << std::endl;
266 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
267 #define ARTDAQ_FILE_PREAMBLE "builder"
270 void EventBuilderApp::transitionConfiguring(toolbox::Event::Reference e)
271 throw (toolbox::fsm::exception::Exception)
274 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
275 std::cout << __COUT_HDR_FL__ << SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) << std::endl;
279 SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
280 getParameters().getValue(
"ConfigurationGroupName"),
282 getParameters().getValue(
"ConfigurationGroupKey")));
284 __MOUT__ <<
"Configuration group name: " << theGroup.first <<
" key: " <<
285 theGroup.second << std::endl;
287 theConfigurationManager_->loadConfigurationGroup(
289 theGroup.second,
true);
303 __MOUT__ << std::endl;
304 __MOUT__ << std::endl;
306 fhicl::ParameterSet pset;
310 std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE +
"-";
311 std::string uid = theConfigurationManager_->getNode(XDAQContextConfigurationName_).getNode(supervisorConfigurationPath_).getValue();
313 __MOUT__ <<
"uid: " << uid << std::endl;
314 for(
unsigned int i=0;i<uid.size();++i)
315 if((uid[i] >=
'a' && uid[i] <=
'z') ||
316 (uid[i] >=
'A' && uid[i] <=
'Z') ||
317 (uid[i] >=
'0' && uid[i] <=
'9'))
321 __MOUT__ <<
"filename: " << filename << std::endl;
323 std::string fileFclString;
325 std::ifstream in(filename, std::ios::in | std::ios::binary);
328 std::string contents;
329 in.seekg(0, std::ios::end);
330 fileFclString.resize(in.tellg());
331 in.seekg(0, std::ios::beg);
332 in.read(&fileFclString[0], fileFclString.size());
336 __MOUT__ << fileFclString << std::endl;
337 fhicl::make_ParameterSet(fileFclString, pset);
341 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
342 it->second->configure(pset);
343 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR DONE CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
348 void EventBuilderApp::transitionHalting(toolbox::Event::Reference e)
349 throw (toolbox::fsm::exception::Exception)
352 for(
auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
357 void EventBuilderApp::transitionInitializing(toolbox::Event::Reference e)
358 throw (toolbox::fsm::exception::Exception)
364 void EventBuilderApp::transitionPausing(toolbox::Event::Reference e)
365 throw (toolbox::fsm::exception::Exception)
368 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
373 void EventBuilderApp::transitionResuming(toolbox::Event::Reference e)
374 throw (toolbox::fsm::exception::Exception)
377 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
378 it->second->resume();
382 void EventBuilderApp::transitionStarting(toolbox::Event::Reference e)
383 throw (toolbox::fsm::exception::Exception)
386 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
387 it->second->start(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue(
"RunNumber"));
391 void EventBuilderApp::transitionStopping(toolbox::Event::Reference e)
392 throw (toolbox::fsm::exception::Exception)
395 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
398 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)