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