00001 #include "otsdaq/AggregatorApp/AggregatorApp.h"
00002 #include "otsdaq/AggregatorApp/AggregatorInterface.h"
00003 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00004 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00005 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h"
00006 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
00007 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
00008
00009 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationGroupKey.h"
00010 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
00011 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
00012
00013 #include <toolbox/fsm/FailedEvent.h>
00014
00015 #include <xdaq/NamespaceURI.h>
00016 #include <xoap/Method.h>
00017
00018 #include <memory>
00019 #include "messagefacility/MessageLogger/MessageLogger.h"
00020 #include "artdaq/DAQdata/configureMessageFacility.hh"
00021 #include "artdaq/DAQrate/quiet_mpi.hh"
00022 #include "cetlib/exception.h"
00023 #include "artdaq/BuildInfo/GetPackageBuildInfo.hh"
00024 #include "fhiclcpp/make_ParameterSet.h"
00025
00026 #include <fstream>
00027 #include <iostream>
00028 #include <cassert>
00029
00030 using namespace ots;
00031
00032 XDAQ_INSTANTIATOR_IMPL(AggregatorApp)
00033
00034
00035 AggregatorApp::AggregatorApp(xdaq::ApplicationStub * s) throw (xdaq::exception::Exception)
00036 : xdaq::Application (s)
00037 , SOAPMessenger (this)
00038 , stateMachineWorkLoopManager_(toolbox::task::bind(this, &AggregatorApp::stateMachineThread, "StateMachine"))
00039 , stateMachineSemaphore_ (toolbox::BSem::FULL)
00040 , theConfigurationManager_ (new ConfigurationManager)
00041 , XDAQContextConfigurationName_(theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getConfigurationName())
00042 , supervisorConfigurationPath_ ("INITIALIZED INSIDE THE CONTRUCTOR BECAUSE IT NEEDS supervisorContextUID_ and supervisorApplicationUID_")
00043 , supervisorContextUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
00044 , supervisorApplicationUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
00045 {
00046 INIT_MF("AggregatorApp");
00047 xgi::bind (this, &AggregatorApp::Default, "Default" );
00048 xgi::bind (this, &AggregatorApp::stateMachineXgiHandler, "StateMachineXgiHandler");
00049
00050 xoap::bind(this, &AggregatorApp::stateMachineStateRequest, "StateMachineStateRequest", XDAQ_NS_URI );
00051 xoap::bind(this, &AggregatorApp::stateMachineErrorMessageRequest, "StateMachineErrorMessageRequest", XDAQ_NS_URI );
00052
00053 try
00054 {
00055 supervisorContextUID_ = theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL());
00056 }
00057 catch(...)
00058 {
00059 __MOUT_ERR__ << "XDAQ Supervisor could not access it's configuration through the Configuration Context Group." <<
00060 " The XDAQContextConfigurationName = " << XDAQContextConfigurationName_ <<
00061 ". The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
00062 throw;
00063 }
00064 try
00065 {
00066 supervisorApplicationUID_ = theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getApplicationUID
00067 (
00068 getApplicationContext()->getContextDescriptor()->getURL(),
00069 getApplicationDescriptor()->getLocalId()
00070 );
00071 }
00072 catch(...)
00073 {
00074 __MOUT_ERR__ << "XDAQ Supervisor could not access it's configuration through the Configuration Application Group."
00075 << " The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
00076 throw;
00077 }
00078 supervisorConfigurationPath_ = "/" + supervisorContextUID_ + "/LinkToApplicationConfiguration/" + supervisorApplicationUID_ + "/LinkToSupervisorConfiguration";
00079
00080 setStateMachineName(supervisorApplicationUID_);
00081 init();
00082 }
00083
00084
00085 AggregatorApp::~AggregatorApp(void)
00086 {
00087 destroy();
00088 }
00089
00090 void AggregatorApp::init(void)
00091 {
00092 std::cout << __COUT_HDR_FL__ << "ARTDAQAGGREGATOR SUPERVISOR INIT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00093 theSupervisorDescriptorInfo_.init(getApplicationContext());
00094 artdaq::configureMessageFacility("aggregator");
00095
00096
00097
00098 int const wanted_threading_level { MPI_THREAD_MULTIPLE };
00099
00100
00101 MPI_Comm local_group_comm;
00102
00103 try
00104 {
00105 mpiSentry_.reset( new artdaq::MPISentry(0, 0, wanted_threading_level, artdaq::TaskType::AggregatorTask, local_group_comm) );
00106 }
00107 catch (cet::exception& errormsg)
00108 {
00109 mf::LogError("AggregatorMain") << errormsg ;
00110 mf::LogError("AggregatorMain") << "MPISentry error encountered in AggregatorMain; exiting...";
00111 throw errormsg;
00112 }
00113
00114 std::cout << __COUT_HDR_FL__ << "ARTDAQAGGREGATOR SUPERVISOR INIT4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00115 std::string name = "Aggregator";
00116 unsigned short port = 5300;
00117
00118 artdaq::setMsgFacAppName(name, port);
00119
00120 mf::LogDebug(name + "Supervisor") << "artdaq version " <<
00121 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getPackageVersion()
00122 << ", built " <<
00123 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getBuildTimestamp();
00124
00125
00126 theAggregatorInterface_ = new AggregatorInterface(mpiSentry_->rank(), name );
00127
00128 }
00129
00130
00131 void AggregatorApp::destroy(void)
00132 {
00133 delete theAggregatorInterface_;
00134
00135 mpiSentry_.reset();
00136 }
00137
00138
00139 void AggregatorApp::Default(xgi::Input * in, xgi::Output * out ) throw (xgi::exception::Exception)
00140 {
00141
00142 *out << "<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/AggregatorApp.html?urn=" <<
00143 this->getApplicationDescriptor()->getLocalId() << "'></frameset></html>";
00144 }
00145
00146
00147 void AggregatorApp::stateMachineXgiHandler(xgi::Input * in, xgi::Output * out ) throw (xgi::exception::Exception)
00148 {}
00149
00150
00151 void AggregatorApp::stateMachineResultXgiHandler(xgi::Input* in, xgi::Output* out ) throw (xgi::exception::Exception)
00152 {}
00153
00154
00155 xoap::MessageReference AggregatorApp::stateMachineXoapHandler(xoap::MessageReference message ) throw (xoap::exception::Exception)
00156 {
00157 std::cout << __COUT_HDR_FL__ << "Soap Handler!" << std::endl;
00158 stateMachineWorkLoopManager_.removeProcessedRequests();
00159 stateMachineWorkLoopManager_.processRequest(message);
00160 std::cout << __COUT_HDR_FL__ << "Done - Soap Handler!" << std::endl;
00161 return message;
00162 }
00163
00164
00165 xoap::MessageReference AggregatorApp::stateMachineResultXoapHandler(xoap::MessageReference message ) throw (xoap::exception::Exception)
00166 {
00167 std::cout << __COUT_HDR_FL__ << "Soap Handler!" << std::endl;
00168
00169
00170 std::cout << __COUT_HDR_FL__ << "Done - Soap Handler!" << std::endl;
00171 return message;
00172 }
00173
00174
00175 bool AggregatorApp::stateMachineThread(toolbox::task::WorkLoop* workLoop)
00176 {
00177 stateMachineSemaphore_.take();
00178 std::cout << __COUT_HDR_FL__ << "Re-sending message..." << SOAPUtilities::translate(stateMachineWorkLoopManager_.getMessage(workLoop)).getCommand() << std::endl;
00179 std::string reply = send(this->getApplicationDescriptor(),stateMachineWorkLoopManager_.getMessage(workLoop));
00180 stateMachineWorkLoopManager_.report(workLoop, reply, 100, true);
00181 std::cout << __COUT_HDR_FL__ << "Done with message" << std::endl;
00182 stateMachineSemaphore_.give();
00183 return false;
00184
00185 }
00186
00187
00188 xoap::MessageReference AggregatorApp::stateMachineStateRequest(xoap::MessageReference message) throw (xoap::exception::Exception)
00189 {
00190 std::cout << __COUT_HDR_FL__ << theStateMachine_.getCurrentStateName() << std::endl;
00191 return SOAPUtilities::makeSOAPMessageReference(theStateMachine_.getCurrentStateName());
00192 }
00193
00194
00195 xoap::MessageReference AggregatorApp::stateMachineErrorMessageRequest(xoap::MessageReference message)
00196 throw (xoap::exception::Exception)
00197 {
00198 __MOUT__<< "theStateMachine_.getErrorMessage() = " << theStateMachine_.getErrorMessage() << std::endl;
00199
00200 SOAPParameters retParameters;
00201 retParameters.addParameter("ErrorMessage",theStateMachine_.getErrorMessage());
00202 return SOAPUtilities::makeSOAPMessageReference("stateMachineErrorMessageRequestReply",retParameters);
00203 }
00204
00205
00206 void AggregatorApp::stateInitial(toolbox::fsm::FiniteStateMachine& fsm) throw (toolbox::fsm::exception::Exception)
00207 {
00208
00209 }
00210
00211
00212 void AggregatorApp::stateHalted(toolbox::fsm::FiniteStateMachine& fsm) throw (toolbox::fsm::exception::Exception)
00213 {
00214
00215 }
00216
00217
00218 void AggregatorApp::stateRunning(toolbox::fsm::FiniteStateMachine& fsm) throw (toolbox::fsm::exception::Exception)
00219 {
00220
00221 }
00222
00223
00224 void AggregatorApp::stateConfigured(toolbox::fsm::FiniteStateMachine& fsm) throw (toolbox::fsm::exception::Exception)
00225 {
00226
00227 }
00228
00229
00230 void AggregatorApp::statePaused(toolbox::fsm::FiniteStateMachine& fsm) throw (toolbox::fsm::exception::Exception)
00231 {
00232
00233 }
00234
00235
00236 void AggregatorApp::inError (toolbox::fsm::FiniteStateMachine & fsm) throw (toolbox::fsm::exception::Exception)
00237 {
00238 std::cout << __COUT_HDR_FL__ << "Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
00239
00240 }
00241
00242
00243 void AggregatorApp::enteringError (toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
00244 {
00245 std::cout << __COUT_HDR_FL__ << "Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
00246 toolbox::fsm::FailedEvent& failedEvent = dynamic_cast<toolbox::fsm::FailedEvent&>(*e);
00247 std::ostringstream error;
00248 error << "Failure performing transition from "
00249 << failedEvent.getFromState()
00250 << " to "
00251 << failedEvent.getToState()
00252 << " exception: " << failedEvent.getException().what();
00253 std::cout << __COUT_HDR_FL__ << error.str() << std::endl;
00254
00255
00256 }
00257
00258
00259 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
00260 #define ARTDAQ_FILE_PREAMBLE "aggregator"
00261
00262 void AggregatorApp::transitionConfiguring(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
00263 {
00264
00265 std::cout << __COUT_HDR_FL__ << "ARTDAQAGGREGATOR SUPERVISOR CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00266 std::cout << __COUT_HDR_FL__ << SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) << std::endl;
00267
00268 std::pair<std::string , ConfigurationGroupKey> theGroup(
00269 SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
00270 getParameters().getValue("ConfigurationGroupName"),
00271 ConfigurationGroupKey(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
00272 getParameters().getValue("ConfigurationGroupKey")));
00273
00274 __MOUT__ << "Configuration group name: " << theGroup.first << " key: " <<
00275 theGroup.second << std::endl;
00276
00277 theConfigurationManager_->loadConfigurationGroup(
00278 theGroup.first,
00279 theGroup.second, true);
00280
00281
00282 std::string path = "";
00283 char* dirMRB = getenv("MRB_BUILDDIR");
00284 char* dirP = getenv("OTSDAQ_DIR");
00285
00286 if(dirMRB) { path = std::string(dirMRB) + "/otsdaq_demo/"; }
00287 else if(dirP) { path = std::string(dirP) + "/"; }
00288
00289
00290
00291
00292
00293
00294
00295
00296 fhicl::ParameterSet pset;
00297
00298
00299 std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE + "-";
00300 std::string uid = theConfigurationManager_->getNode(XDAQContextConfigurationName_).getNode(supervisorConfigurationPath_).getValue();
00301
00302 __MOUT__ << "uid: " << uid << std::endl;
00303 for(unsigned int i=0;i<uid.size();++i)
00304 if((uid[i] >= 'a' && uid[i] <= 'z') ||
00305 (uid[i] >= 'A' && uid[i] <= 'Z') ||
00306 (uid[i] >= '0' && uid[i] <= '9'))
00307 filename += uid[i];
00308 filename += ".fcl";
00309
00310
00311 __MOUT__ << std::endl;
00312 __MOUT__ << std::endl;
00313 __MOUT__ << "filename: " << filename << std::endl;
00314
00315 std::string fileFclString;
00316 {
00317 std::ifstream in(filename, std::ios::in | std::ios::binary);
00318 if (in)
00319 {
00320 std::string contents;
00321 in.seekg(0, std::ios::end);
00322 fileFclString.resize(in.tellg());
00323 in.seekg(0, std::ios::beg);
00324 in.read(&fileFclString[0], fileFclString.size());
00325 in.close();
00326 }
00327 }
00328
00329 __MOUT__ << fileFclString << std::endl;
00330 fhicl::make_ParameterSet(fileFclString, pset);
00331
00332
00333
00334 theAggregatorInterface_->configure(pset);
00335 mf::LogInfo("AggregatorInterface") << "ARTDAQAGGREGATOR SUPERVISOR DONE CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
00336
00337 }
00338
00339
00340 void AggregatorApp::transitionHalting(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
00341 {
00342 theAggregatorInterface_->halt();
00343 }
00344
00345
00346 void AggregatorApp::transitionInitializing(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
00347 {
00348
00349 }
00350
00351
00352 void AggregatorApp::transitionPausing(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
00353 {
00354 theAggregatorInterface_->pause();
00355 }
00356
00357
00358 void AggregatorApp::transitionResuming(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
00359 {
00360 theAggregatorInterface_->resume();
00361 }
00362
00363
00364 void AggregatorApp::transitionStarting(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
00365 {
00366 theAggregatorInterface_->start(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue("RunNumber"));
00367 }
00368
00369
00370 void AggregatorApp::transitionStopping(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
00371 {
00372 theAggregatorInterface_->stop();
00373 theAggregatorInterface_->halt();
00374 }