1 #include "otsdaq/AggregatorApp/AggregatorApp.h"
2 #include "otsdaq/AggregatorApp/AggregatorInterface.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 : xdaq::Application (s)
38 , stateMachineWorkLoopManager_(toolbox::task::bind(this, &
AggregatorApp::stateMachineThread, "StateMachine"))
39 , stateMachineSemaphore_ (toolbox::BSem::FULL)
41 , XDAQContextConfigurationName_(theConfigurationManager_->__GET_CONFIG__(
XDAQContextConfiguration)->getConfigurationName())
42 , supervisorConfigurationPath_ ("INITIALIZED INSIDE THE CONTRUCTOR BECAUSE IT NEEDS supervisorContextUID_ and supervisorApplicationUID_")
43 , supervisorContextUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
44 , supervisorApplicationUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
46 INIT_MF(
"AggregatorApp");
47 xgi::bind (
this, &AggregatorApp::Default,
"Default" );
48 xgi::bind (
this, &AggregatorApp::stateMachineXgiHandler,
"StateMachineXgiHandler");
50 xoap::bind(
this, &AggregatorApp::stateMachineStateRequest,
"StateMachineStateRequest", XDAQ_NS_URI );
53 supervisorContextUID_ = theConfigurationManager_->__GET_CONFIG__(
XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL());
57 __MOUT_ERR__ <<
"XDAQ Supervisor could not access it's configuration through the Configuration Context Group." <<
58 " The XDAQContextConfigurationName = " << XDAQContextConfigurationName_ <<
59 ". The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
66 getApplicationContext()->getContextDescriptor()->getURL(),
67 getApplicationDescriptor()->getLocalId()
72 __MOUT_ERR__ <<
"XDAQ Supervisor could not access it's configuration through the Configuration Application Group."
73 <<
" The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
76 supervisorConfigurationPath_ =
"/" + supervisorContextUID_ +
"/LinkToApplicationConfiguration/" + supervisorApplicationUID_ +
"/LinkToSupervisorConfiguration";
78 setStateMachineName(supervisorApplicationUID_);
83 AggregatorApp::~AggregatorApp(
void)
88 void AggregatorApp::init(
void)
90 std::cout << __COUT_HDR_FL__ <<
"ARTDAQAGGREGATOR SUPERVISOR INIT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
91 theSupervisorDescriptorInfo_.init(getApplicationContext());
92 artdaq::configureMessageFacility(
"aggregator");
96 int const wanted_threading_level { MPI_THREAD_MULTIPLE };
99 MPI_Comm local_group_comm;
103 mpiSentry_.reset(
new artdaq::MPISentry(0, 0, wanted_threading_level, artdaq::TaskType::AggregatorTask, local_group_comm) );
105 catch (cet::exception& errormsg)
107 mf::LogError(
"AggregatorMain") << errormsg ;
108 mf::LogError(
"AggregatorMain") <<
"MPISentry error encountered in AggregatorMain; exiting...";
112 std::cout << __COUT_HDR_FL__ <<
"ARTDAQAGGREGATOR SUPERVISOR INIT4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
113 std::string name =
"Aggregator";
114 unsigned short port = 5300;
116 artdaq::setMsgFacAppName(name, port);
118 mf::LogDebug(name +
"Supervisor") <<
"artdaq version " <<
119 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getPackageVersion()
121 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getBuildTimestamp();
129 void AggregatorApp::destroy(
void)
131 delete theAggregatorInterface_;
137 void AggregatorApp::Default(xgi::Input * in, xgi::Output * out )
throw (xgi::exception::Exception)
140 *out <<
"<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/AggregatorApp.html?urn=" <<
141 this->getApplicationDescriptor()->getLocalId() <<
"'></frameset></html>";
145 void AggregatorApp::stateMachineXgiHandler(xgi::Input * in, xgi::Output * out )
throw (xgi::exception::Exception)
149 void AggregatorApp::stateMachineResultXgiHandler(xgi::Input* in, xgi::Output* out )
throw (xgi::exception::Exception)
153 xoap::MessageReference AggregatorApp::stateMachineXoapHandler(xoap::MessageReference message )
throw (xoap::exception::Exception)
155 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
156 stateMachineWorkLoopManager_.removeProcessedRequests();
157 stateMachineWorkLoopManager_.processRequest(message);
158 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
163 xoap::MessageReference AggregatorApp::stateMachineResultXoapHandler(xoap::MessageReference message )
throw (xoap::exception::Exception)
165 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
168 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
173 bool AggregatorApp::stateMachineThread(toolbox::task::WorkLoop* workLoop)
175 stateMachineSemaphore_.take();
176 std::cout << __COUT_HDR_FL__ <<
"Re-sending message..." << SOAPUtilities::translate(stateMachineWorkLoopManager_.getMessage(workLoop)).getCommand() << std::endl;
177 std::string reply = send(this->getApplicationDescriptor(),stateMachineWorkLoopManager_.getMessage(workLoop));
178 stateMachineWorkLoopManager_.report(workLoop, reply, 100,
true);
179 std::cout << __COUT_HDR_FL__ <<
"Done with message" << std::endl;
180 stateMachineSemaphore_.give();
186 xoap::MessageReference AggregatorApp::stateMachineStateRequest(xoap::MessageReference message)
throw (xoap::exception::Exception)
188 std::cout << __COUT_HDR_FL__ << theStateMachine_.getCurrentStateName() << std::endl;
189 return SOAPUtilities::makeSOAPMessageReference(theStateMachine_.getCurrentStateName());
193 void AggregatorApp::stateInitial(toolbox::fsm::FiniteStateMachine& fsm)
throw (toolbox::fsm::exception::Exception)
199 void AggregatorApp::stateHalted(toolbox::fsm::FiniteStateMachine& fsm)
throw (toolbox::fsm::exception::Exception)
205 void AggregatorApp::stateRunning(toolbox::fsm::FiniteStateMachine& fsm)
throw (toolbox::fsm::exception::Exception)
211 void AggregatorApp::stateConfigured(toolbox::fsm::FiniteStateMachine& fsm)
throw (toolbox::fsm::exception::Exception)
217 void AggregatorApp::statePaused(toolbox::fsm::FiniteStateMachine& fsm)
throw (toolbox::fsm::exception::Exception)
223 void AggregatorApp::inError (toolbox::fsm::FiniteStateMachine & fsm)
throw (toolbox::fsm::exception::Exception)
225 std::cout << __COUT_HDR_FL__ <<
"Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
230 void AggregatorApp::enteringError (toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
232 std::cout << __COUT_HDR_FL__ <<
"Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
233 toolbox::fsm::FailedEvent& failedEvent =
dynamic_cast<toolbox::fsm::FailedEvent&
>(*e);
234 std::ostringstream error;
235 error <<
"Failure performing transition from "
236 << failedEvent.getFromState()
238 << failedEvent.getToState()
239 <<
" exception: " << failedEvent.getException().what();
240 std::cout << __COUT_HDR_FL__ << error.str() << std::endl;
246 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
247 #define ARTDAQ_FILE_PREAMBLE "aggregator"
249 void AggregatorApp::transitionConfiguring(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
252 std::cout << __COUT_HDR_FL__ <<
"ARTDAQAGGREGATOR SUPERVISOR CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
253 std::cout << __COUT_HDR_FL__ << SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) << std::endl;
256 SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
257 getParameters().getValue(
"ConfigurationGroupName"),
259 getParameters().getValue(
"ConfigurationGroupKey")));
261 __MOUT__ <<
"Configuration group name: " << theGroup.first <<
" key: " <<
262 theGroup.second << std::endl;
264 theConfigurationManager_->loadConfigurationGroup(
266 theGroup.second,
true);
269 std::string path =
"";
270 char* dirMRB = getenv(
"MRB_BUILDDIR");
271 char* dirP = getenv(
"OTSDAQ_DIR");
273 if(dirMRB) { path = std::string(dirMRB) +
"/otsdaq_demo/"; }
274 else if(dirP) { path = std::string(dirP) +
"/"; }
283 fhicl::ParameterSet pset;
286 std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE +
"-";
287 std::string uid = theConfigurationManager_->getNode(XDAQContextConfigurationName_).getNode(supervisorConfigurationPath_).getValue();
289 __MOUT__ <<
"uid: " << uid << std::endl;
290 for(
unsigned int i=0;i<uid.size();++i)
291 if((uid[i] >=
'a' && uid[i] <=
'z') ||
292 (uid[i] >=
'A' && uid[i] <=
'Z') ||
293 (uid[i] >=
'0' && uid[i] <=
'9'))
298 __MOUT__ << std::endl;
299 __MOUT__ << std::endl;
300 __MOUT__ <<
"filename: " << filename << std::endl;
302 std::string fileFclString;
304 std::ifstream in(filename, std::ios::in | std::ios::binary);
307 std::string contents;
308 in.seekg(0, std::ios::end);
309 fileFclString.resize(in.tellg());
310 in.seekg(0, std::ios::beg);
311 in.read(&fileFclString[0], fileFclString.size());
316 __MOUT__ << fileFclString << std::endl;
317 fhicl::make_ParameterSet(fileFclString, pset);
321 theAggregatorInterface_->configure(pset);
322 mf::LogInfo(
"AggregatorInterface") <<
"ARTDAQAGGREGATOR SUPERVISOR DONE CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
327 void AggregatorApp::transitionHalting(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
329 theAggregatorInterface_->halt();
333 void AggregatorApp::transitionInitializing(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
339 void AggregatorApp::transitionPausing(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
341 theAggregatorInterface_->pause();
345 void AggregatorApp::transitionResuming(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
347 theAggregatorInterface_->resume();
351 void AggregatorApp::transitionStarting(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
353 theAggregatorInterface_->start(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue(
"RunNumber"));
357 void AggregatorApp::transitionStopping(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
359 theAggregatorInterface_->stop();
360 theAggregatorInterface_->halt();