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 );
51 xoap::bind(
this, &AggregatorApp::stateMachineErrorMessageRequest,
"StateMachineErrorMessageRequest", XDAQ_NS_URI );
55 supervisorContextUID_ = theConfigurationManager_->__GET_CONFIG__(
XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL());
59 __COUT_ERR__ <<
"XDAQ Supervisor could not access it's configuration through the Configuration Context Group." <<
60 " The XDAQContextConfigurationName = " << XDAQContextConfigurationName_ <<
61 ". The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
68 getApplicationContext()->getContextDescriptor()->getURL(),
69 getApplicationDescriptor()->getLocalId()
74 __COUT_ERR__ <<
"XDAQ Supervisor could not access it's configuration through the Configuration Application Group."
75 <<
" The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
78 supervisorConfigurationPath_ =
"/" + supervisorContextUID_ +
"/LinkToApplicationConfiguration/" + supervisorApplicationUID_ +
"/LinkToSupervisorConfiguration";
80 setStateMachineName(supervisorApplicationUID_);
85 AggregatorApp::~AggregatorApp(
void)
90 void AggregatorApp::init(
void)
92 std::cout << __COUT_HDR_FL__ <<
"ARTDAQAGGREGATOR SUPERVISOR INIT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
93 theSupervisorDescriptorInfo_.init(getApplicationContext());
94 artdaq::configureMessageFacility(
"aggregator");
98 int const wanted_threading_level { MPI_THREAD_MULTIPLE };
101 MPI_Comm local_group_comm;
105 mpiSentry_.reset(
new artdaq::MPISentry(0, 0, wanted_threading_level, artdaq::TaskType::AggregatorTask, local_group_comm) );
107 catch (cet::exception& errormsg)
109 mf::LogError(
"AggregatorMain") << errormsg ;
110 mf::LogError(
"AggregatorMain") <<
"MPISentry error encountered in AggregatorMain; exiting...";
114 std::cout << __COUT_HDR_FL__ <<
"ARTDAQAGGREGATOR SUPERVISOR INIT4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
115 std::string name =
"Aggregator";
116 unsigned short port = 5300;
118 artdaq::setMsgFacAppName(name, port);
120 mf::LogDebug(name +
"Supervisor") <<
"artdaq version " <<
121 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getPackageVersion()
123 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getBuildTimestamp();
131 void AggregatorApp::destroy(
void)
133 delete theAggregatorInterface_;
139 void AggregatorApp::Default(xgi::Input * in, xgi::Output * out )
throw (xgi::exception::Exception)
142 *out <<
"<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/AggregatorApp.html?urn=" <<
143 this->getApplicationDescriptor()->getLocalId() <<
"'></frameset></html>";
147 void AggregatorApp::stateMachineXgiHandler(xgi::Input * in, xgi::Output * out )
throw (xgi::exception::Exception)
151 void AggregatorApp::stateMachineResultXgiHandler(xgi::Input* in, xgi::Output* out )
throw (xgi::exception::Exception)
155 xoap::MessageReference AggregatorApp::stateMachineXoapHandler(xoap::MessageReference message )
throw (xoap::exception::Exception)
157 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
158 stateMachineWorkLoopManager_.removeProcessedRequests();
159 stateMachineWorkLoopManager_.processRequest(message);
160 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
165 xoap::MessageReference AggregatorApp::stateMachineResultXoapHandler(xoap::MessageReference message )
throw (xoap::exception::Exception)
167 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
170 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
175 bool AggregatorApp::stateMachineThread(toolbox::task::WorkLoop* workLoop)
177 stateMachineSemaphore_.take();
178 std::cout << __COUT_HDR_FL__ <<
"Re-sending message..." << SOAPUtilities::translate(stateMachineWorkLoopManager_.getMessage(workLoop)).getCommand() << std::endl;
179 std::string reply = send(this->getApplicationDescriptor(),stateMachineWorkLoopManager_.getMessage(workLoop));
180 stateMachineWorkLoopManager_.report(workLoop, reply, 100,
true);
181 std::cout << __COUT_HDR_FL__ <<
"Done with message" << std::endl;
182 stateMachineSemaphore_.give();
188 xoap::MessageReference AggregatorApp::stateMachineStateRequest(xoap::MessageReference message)
throw (xoap::exception::Exception)
190 std::cout << __COUT_HDR_FL__ << theStateMachine_.getCurrentStateName() << std::endl;
191 return SOAPUtilities::makeSOAPMessageReference(theStateMachine_.getCurrentStateName());
195 xoap::MessageReference AggregatorApp::stateMachineErrorMessageRequest(xoap::MessageReference message)
196 throw (xoap::exception::Exception)
198 __COUT__<<
"theStateMachine_.getErrorMessage() = " << theStateMachine_.getErrorMessage() << std::endl;
201 retParameters.addParameter(
"ErrorMessage",theStateMachine_.getErrorMessage());
202 return SOAPUtilities::makeSOAPMessageReference(
"stateMachineErrorMessageRequestReply",retParameters);
206 void AggregatorApp::stateInitial(toolbox::fsm::FiniteStateMachine& fsm)
throw (toolbox::fsm::exception::Exception)
212 void AggregatorApp::stateHalted(toolbox::fsm::FiniteStateMachine& fsm)
throw (toolbox::fsm::exception::Exception)
218 void AggregatorApp::stateRunning(toolbox::fsm::FiniteStateMachine& fsm)
throw (toolbox::fsm::exception::Exception)
224 void AggregatorApp::stateConfigured(toolbox::fsm::FiniteStateMachine& fsm)
throw (toolbox::fsm::exception::Exception)
230 void AggregatorApp::statePaused(toolbox::fsm::FiniteStateMachine& fsm)
throw (toolbox::fsm::exception::Exception)
236 void AggregatorApp::inError (toolbox::fsm::FiniteStateMachine & fsm)
throw (toolbox::fsm::exception::Exception)
238 std::cout << __COUT_HDR_FL__ <<
"Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
243 void AggregatorApp::enteringError (toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
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;
259 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
260 #define ARTDAQ_FILE_PREAMBLE "aggregator"
262 void AggregatorApp::transitionConfiguring(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
265 std::cout << __COUT_HDR_FL__ <<
"ARTDAQAGGREGATOR SUPERVISOR CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
266 std::cout << __COUT_HDR_FL__ << SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) << std::endl;
269 SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
270 getParameters().getValue(
"ConfigurationGroupName"),
272 getParameters().getValue(
"ConfigurationGroupKey")));
274 __COUT__ <<
"Configuration group name: " << theGroup.first <<
" key: " <<
275 theGroup.second << std::endl;
277 theConfigurationManager_->loadConfigurationGroup(
279 theGroup.second,
true);
282 std::string path =
"";
283 char* dirMRB = getenv(
"MRB_BUILDDIR");
284 char* dirP = getenv(
"OTSDAQ_DIR");
286 if(dirMRB) { path = std::string(dirMRB) +
"/otsdaq_demo/"; }
287 else if(dirP) { path = std::string(dirP) +
"/"; }
296 fhicl::ParameterSet pset;
299 std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE +
"-";
300 std::string uid = theConfigurationManager_->getNode(XDAQContextConfigurationName_).getNode(supervisorConfigurationPath_).getValue();
302 __COUT__ <<
"uid: " << uid << std::endl;
303 for(
unsigned int i=0;i<uid.size();++i)
304 if((uid[i] >=
'a' && uid[i] <=
'z') ||
305 (uid[i] >=
'A' && uid[i] <=
'Z') ||
306 (uid[i] >=
'0' && uid[i] <=
'9'))
311 __COUT__ << std::endl;
312 __COUT__ << std::endl;
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());
329 __COUT__ << fileFclString << std::endl;
330 fhicl::make_ParameterSet(fileFclString, pset);
334 theAggregatorInterface_->configure(pset);
335 mf::LogInfo(
"AggregatorInterface") <<
"ARTDAQAGGREGATOR SUPERVISOR DONE CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
340 void AggregatorApp::transitionHalting(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
342 theAggregatorInterface_->halt();
346 void AggregatorApp::transitionInitializing(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
352 void AggregatorApp::transitionPausing(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
354 theAggregatorInterface_->pause();
358 void AggregatorApp::transitionResuming(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
360 theAggregatorInterface_->resume();
364 void AggregatorApp::transitionStarting(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
366 theAggregatorInterface_->start(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue(
"RunNumber"));
370 void AggregatorApp::transitionStopping(toolbox::Event::Reference e)
throw (toolbox::fsm::exception::Exception)
372 theAggregatorInterface_->stop();
373 theAggregatorInterface_->halt();