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 );
55 xoap::bind(
this, &EventBuilderApp::stateMachineErrorMessageRequest,
"StateMachineErrorMessageRequest", XDAQ_NS_URI );
59 supervisorContextUID_ = theConfigurationManager_->__GET_CONFIG__(
XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL());
63 __COUT_ERR__ <<
"XDAQ Supervisor could not access it's configuration through the Configuration Context Group." <<
64 " The XDAQContextConfigurationName = " << XDAQContextConfigurationName_ <<
65 ". The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
72 getApplicationContext()->getContextDescriptor()->getURL(),
73 getApplicationDescriptor()->getLocalId()
78 __COUT_ERR__ <<
"XDAQ Supervisor could not access it's configuration through the Configuration Application Group."
79 <<
" The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
82 supervisorConfigurationPath_ =
"/" + supervisorContextUID_ +
"/LinkToApplicationConfiguration/" + supervisorApplicationUID_ +
"/LinkToSupervisorConfiguration";
84 setStateMachineName(supervisorApplicationUID_);
89 EventBuilderApp::~EventBuilderApp(
void)
94 void EventBuilderApp::init(
void)
96 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR INIT START!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
97 theSupervisorDescriptorInfo_.init(getApplicationContext());
98 artdaq::configureMessageFacility(
"eventbuilder");
102 int const wanted_threading_level { MPI_THREAD_MULTIPLE };
105 MPI_Comm local_group_comm;
108 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR TRYING MPISENTRY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
109 mpiSentry_.reset(
new artdaq::MPISentry(0, 0, wanted_threading_level, artdaq::TaskType::EventBuilderTask, local_group_comm) );
110 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR DONE MPISENTRY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
112 catch (cet::exception& errormsg)
114 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR INIT ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
115 mf::LogError(
"EventBuilderMain") << errormsg ;
116 mf::LogError(
"EventBuilderMain") <<
"MPISentry error encountered in EventBuilderMain; exiting...";
120 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR NO ERRORS MAKING MSG FACILITY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
121 std::string name =
"Builder";
122 unsigned short port = 5200;
124 artdaq::setMsgFacAppName(name, port);
125 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR MSG FACILITY DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
126 mf::LogDebug(name +
"Supervisor") <<
"artdaq version " <<
128 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getPackageVersion()
130 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getBuildTimestamp();
136 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR INIT DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
140 void EventBuilderApp::destroy(
void)
147 void EventBuilderApp::Default(xgi::Input * in, xgi::Output * out )
148 throw (xgi::exception::Exception)
151 *out <<
"<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/EventBuilderApp.html?urn=" <<
152 this->getApplicationDescriptor()->getLocalId() <<
"'></frameset></html>";
156 void EventBuilderApp::stateMachineXgiHandler(xgi::Input * in, xgi::Output * out )
157 throw (xgi::exception::Exception)
161 void EventBuilderApp::stateMachineResultXgiHandler(xgi::Input* in, xgi::Output* out )
162 throw (xgi::exception::Exception)
166 xoap::MessageReference EventBuilderApp::stateMachineXoapHandler(xoap::MessageReference message )
167 throw (xoap::exception::Exception)
169 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
170 stateMachineWorkLoopManager_.removeProcessedRequests();
171 stateMachineWorkLoopManager_.processRequest(message);
172 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
177 xoap::MessageReference EventBuilderApp::stateMachineResultXoapHandler(xoap::MessageReference message )
178 throw (xoap::exception::Exception)
180 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
183 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
188 bool EventBuilderApp::stateMachineThread(toolbox::task::WorkLoop* workLoop)
190 stateMachineSemaphore_.take();
191 std::cout << __COUT_HDR_FL__ <<
"Re-sending message..." << SOAPUtilities::translate(stateMachineWorkLoopManager_.getMessage(workLoop)).getCommand() << std::endl;
192 std::string reply = send(this->getApplicationDescriptor(),stateMachineWorkLoopManager_.getMessage(workLoop));
193 stateMachineWorkLoopManager_.report(workLoop, reply, 100,
true);
194 std::cout << __COUT_HDR_FL__ <<
"Done with message" << std::endl;
195 stateMachineSemaphore_.give();
201 xoap::MessageReference EventBuilderApp::stateMachineStateRequest(xoap::MessageReference message)
202 throw (xoap::exception::Exception)
204 std::cout << __COUT_HDR_FL__ << theStateMachine_.getCurrentStateName() << std::endl;
205 return SOAPUtilities::makeSOAPMessageReference(theStateMachine_.getCurrentStateName());
209 xoap::MessageReference EventBuilderApp::stateMachineErrorMessageRequest(xoap::MessageReference message)
210 throw (xoap::exception::Exception)
212 __COUT__<<
"theStateMachine_.getErrorMessage() = " << theStateMachine_.getErrorMessage() << std::endl;
215 retParameters.addParameter(
"ErrorMessage",theStateMachine_.getErrorMessage());
216 return SOAPUtilities::makeSOAPMessageReference(
"stateMachineErrorMessageRequestReply",retParameters);
220 void EventBuilderApp::stateInitial(toolbox::fsm::FiniteStateMachine& fsm)
221 throw (toolbox::fsm::exception::Exception)
227 void EventBuilderApp::stateHalted(toolbox::fsm::FiniteStateMachine& fsm)
228 throw (toolbox::fsm::exception::Exception)
234 void EventBuilderApp::stateRunning(toolbox::fsm::FiniteStateMachine& fsm)
235 throw (toolbox::fsm::exception::Exception)
241 void EventBuilderApp::stateConfigured(toolbox::fsm::FiniteStateMachine& fsm)
242 throw (toolbox::fsm::exception::Exception)
248 void EventBuilderApp::statePaused(toolbox::fsm::FiniteStateMachine& fsm)
249 throw (toolbox::fsm::exception::Exception)
255 void EventBuilderApp::inError (toolbox::fsm::FiniteStateMachine & fsm)
256 throw (toolbox::fsm::exception::Exception)
258 std::cout << __COUT_HDR_FL__ <<
"Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
263 void EventBuilderApp::enteringError (toolbox::Event::Reference e)
264 throw (toolbox::fsm::exception::Exception)
266 std::cout << __COUT_HDR_FL__ <<
"Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
267 toolbox::fsm::FailedEvent& failedEvent =
dynamic_cast<toolbox::fsm::FailedEvent&
>(*e);
268 std::ostringstream error;
269 error <<
"Failure performing transition from "
270 << failedEvent.getFromState()
272 << failedEvent.getToState()
273 <<
" exception: " << failedEvent.getException().what();
274 std::cout << __COUT_HDR_FL__ << error.str() << std::endl;
279 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
280 #define ARTDAQ_FILE_PREAMBLE "builder"
283 void EventBuilderApp::transitionConfiguring(toolbox::Event::Reference e)
284 throw (toolbox::fsm::exception::Exception)
287 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
288 std::cout << __COUT_HDR_FL__ << SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) << std::endl;
292 SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
293 getParameters().getValue(
"ConfigurationGroupName"),
295 getParameters().getValue(
"ConfigurationGroupKey")));
297 __COUT__ <<
"Configuration group name: " << theGroup.first <<
" key: " <<
298 theGroup.second << std::endl;
300 theConfigurationManager_->loadConfigurationGroup(
302 theGroup.second,
true);
316 __COUT__ << std::endl;
317 __COUT__ << std::endl;
319 fhicl::ParameterSet pset;
323 std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE +
"-";
324 std::string uid = theConfigurationManager_->getNode(XDAQContextConfigurationName_).getNode(supervisorConfigurationPath_).getValue();
326 __COUT__ <<
"uid: " << uid << std::endl;
327 for(
unsigned int i=0;i<uid.size();++i)
328 if((uid[i] >=
'a' && uid[i] <=
'z') ||
329 (uid[i] >=
'A' && uid[i] <=
'Z') ||
330 (uid[i] >=
'0' && uid[i] <=
'9'))
334 __COUT__ <<
"filename: " << filename << std::endl;
336 std::string fileFclString;
338 std::ifstream in(filename, std::ios::in | std::ios::binary);
341 std::string contents;
342 in.seekg(0, std::ios::end);
343 fileFclString.resize(in.tellg());
344 in.seekg(0, std::ios::beg);
345 in.read(&fileFclString[0], fileFclString.size());
349 __COUT__ << fileFclString << std::endl;
353 fhicl::make_ParameterSet(fileFclString, pset);
357 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
358 it->second->configure(pset);
360 catch(
const cet::coded_exception<fhicl::error, &fhicl::detail::translate>& e)
362 __SS__ <<
"Error was caught while configuring: " << e.what() << std::endl;
363 __COUT_ERR__ <<
"\n" << ss.str();
364 theStateMachine_.setErrorMessage(ss.str());
365 throw toolbox::fsm::exception::Exception(
368 "EventBuilderApp::transitionConfiguring" ,
374 std::cout << __COUT_HDR_FL__ <<
"ARTDAQBUILDER SUPERVISOR DONE CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
379 void EventBuilderApp::transitionHalting(toolbox::Event::Reference e)
380 throw (toolbox::fsm::exception::Exception)
383 for(
auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
388 void EventBuilderApp::transitionInitializing(toolbox::Event::Reference e)
389 throw (toolbox::fsm::exception::Exception)
395 void EventBuilderApp::transitionPausing(toolbox::Event::Reference e)
396 throw (toolbox::fsm::exception::Exception)
399 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
404 void EventBuilderApp::transitionResuming(toolbox::Event::Reference e)
405 throw (toolbox::fsm::exception::Exception)
408 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
409 it->second->resume();
413 void EventBuilderApp::transitionStarting(toolbox::Event::Reference e)
414 throw (toolbox::fsm::exception::Exception)
417 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
418 it->second->start(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue(
"RunNumber"));
422 void EventBuilderApp::transitionStopping(toolbox::Event::Reference e)
423 throw (toolbox::fsm::exception::Exception)
426 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
429 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)