00001 #include "otsdaq/EventBuilderApp/EventBuilderApp.h"
00002 #include "otsdaq/EventBuilderApp/EventBuilderInterface.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(EventBuilderApp)
00033
00034
00035 EventBuilderApp::EventBuilderApp(xdaq::ApplicationStub* stub)
00036 throw (xdaq::exception::Exception)
00037 : xdaq::Application (stub)
00038 , SOAPMessenger (this)
00039 , stateMachineWorkLoopManager_ (toolbox::task::bind(this, &EventBuilderApp::stateMachineThread, "StateMachine"))
00040 , stateMachineSemaphore_ (toolbox::BSem::FULL)
00041 , theConfigurationManager_ (new ConfigurationManager)
00042 , XDAQContextConfigurationName_(theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getConfigurationName())
00043 , supervisorConfigurationPath_ ("INITIALIZED INSIDE THE CONTRUCTOR BECAUSE IT NEEDS supervisorContextUID_ and supervisorApplicationUID_")
00044 , supervisorContextUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
00045 , supervisorApplicationUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
00046
00047
00048
00049 {
00050 INIT_MF("EventBuilderApp");
00051 xgi::bind (this, &EventBuilderApp::Default, "Default" );
00052 xgi::bind (this, &EventBuilderApp::stateMachineXgiHandler, "StateMachineXgiHandler");
00053
00054 xoap::bind(this, &EventBuilderApp::stateMachineStateRequest, "StateMachineStateRequest", XDAQ_NS_URI );
00055 xoap::bind(this, &EventBuilderApp::stateMachineErrorMessageRequest, "StateMachineErrorMessageRequest", XDAQ_NS_URI );
00056
00057 try
00058 {
00059 supervisorContextUID_ = theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL());
00060 }
00061 catch(...)
00062 {
00063 __MOUT_ERR__ << "XDAQ Supervisor could not access it's configuration through the Configuration Context Group." <<
00064 " The XDAQContextConfigurationName = " << XDAQContextConfigurationName_ <<
00065 ". The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
00066 throw;
00067 }
00068 try
00069 {
00070 supervisorApplicationUID_ = theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getApplicationUID
00071 (
00072 getApplicationContext()->getContextDescriptor()->getURL(),
00073 getApplicationDescriptor()->getLocalId()
00074 );
00075 }
00076 catch(...)
00077 {
00078 __MOUT_ERR__ << "XDAQ Supervisor could not access it's configuration through the Configuration Application Group."
00079 << " The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
00080 throw;
00081 }
00082 supervisorConfigurationPath_ = "/" + supervisorContextUID_ + "/LinkToApplicationConfiguration/" + supervisorApplicationUID_ + "/LinkToSupervisorConfiguration";
00083
00084 setStateMachineName(supervisorApplicationUID_);
00085 init();
00086 }
00087
00088
00089 EventBuilderApp::~EventBuilderApp(void)
00090 {
00091 destroy();
00092 }
00093
00094 void EventBuilderApp::init(void)
00095 {
00096 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR INIT START!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00097 theSupervisorDescriptorInfo_.init(getApplicationContext());
00098 artdaq::configureMessageFacility("eventbuilder");
00099
00100
00101
00102 int const wanted_threading_level { MPI_THREAD_MULTIPLE };
00103
00104
00105 MPI_Comm local_group_comm;
00106 try
00107 {
00108 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR TRYING MPISENTRY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00109 mpiSentry_.reset( new artdaq::MPISentry(0, 0, wanted_threading_level, artdaq::TaskType::EventBuilderTask, local_group_comm) );
00110 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR DONE MPISENTRY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00111 }
00112 catch (cet::exception& errormsg)
00113 {
00114 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR INIT ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00115 mf::LogError("EventBuilderMain") << errormsg ;
00116 mf::LogError("EventBuilderMain") << "MPISentry error encountered in EventBuilderMain; exiting...";
00117 throw errormsg;
00118 }
00119
00120 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR NO ERRORS MAKING MSG FACILITY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00121 std::string name = "Builder";
00122 unsigned short port = 5200;
00123
00124 artdaq::setMsgFacAppName(name, port);
00125 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR MSG FACILITY DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00126 mf::LogDebug(name + "Supervisor") << "artdaq version " <<
00127
00128 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getPackageVersion()
00129 << ", built " <<
00130 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getBuildTimestamp();
00131
00132
00133
00134 theARTDAQEventBuilderInterfaces_[0] = new EventBuilderInterface(mpiSentry_->rank(), name );
00135
00136 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR INIT DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00137 }
00138
00139
00140 void EventBuilderApp::destroy(void)
00141 {
00142
00143 mpiSentry_.reset();
00144 }
00145
00146
00147 void EventBuilderApp::Default(xgi::Input * in, xgi::Output * out )
00148 throw (xgi::exception::Exception)
00149 {
00150
00151 *out << "<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/EventBuilderApp.html?urn=" <<
00152 this->getApplicationDescriptor()->getLocalId() <<"'></frameset></html>";
00153 }
00154
00155
00156 void EventBuilderApp::stateMachineXgiHandler(xgi::Input * in, xgi::Output * out )
00157 throw (xgi::exception::Exception)
00158 {}
00159
00160
00161 void EventBuilderApp::stateMachineResultXgiHandler(xgi::Input* in, xgi::Output* out )
00162 throw (xgi::exception::Exception)
00163 {}
00164
00165
00166 xoap::MessageReference EventBuilderApp::stateMachineXoapHandler(xoap::MessageReference message )
00167 throw (xoap::exception::Exception)
00168 {
00169 std::cout << __COUT_HDR_FL__ << "Soap Handler!" << std::endl;
00170 stateMachineWorkLoopManager_.removeProcessedRequests();
00171 stateMachineWorkLoopManager_.processRequest(message);
00172 std::cout << __COUT_HDR_FL__ << "Done - Soap Handler!" << std::endl;
00173 return message;
00174 }
00175
00176
00177 xoap::MessageReference EventBuilderApp::stateMachineResultXoapHandler(xoap::MessageReference message )
00178 throw (xoap::exception::Exception)
00179 {
00180 std::cout << __COUT_HDR_FL__ << "Soap Handler!" << std::endl;
00181
00182
00183 std::cout << __COUT_HDR_FL__ << "Done - Soap Handler!" << std::endl;
00184 return message;
00185 }
00186
00187
00188 bool EventBuilderApp::stateMachineThread(toolbox::task::WorkLoop* workLoop)
00189 {
00190 stateMachineSemaphore_.take();
00191 std::cout << __COUT_HDR_FL__ << "Re-sending message..." << SOAPUtilities::translate(stateMachineWorkLoopManager_.getMessage(workLoop)).getCommand() << std::endl;
00192 std::string reply = send(this->getApplicationDescriptor(),stateMachineWorkLoopManager_.getMessage(workLoop));
00193 stateMachineWorkLoopManager_.report(workLoop, reply, 100, true);
00194 std::cout << __COUT_HDR_FL__ << "Done with message" << std::endl;
00195 stateMachineSemaphore_.give();
00196 return false;
00197
00198 }
00199
00200
00201 xoap::MessageReference EventBuilderApp::stateMachineStateRequest(xoap::MessageReference message)
00202 throw (xoap::exception::Exception)
00203 {
00204 std::cout << __COUT_HDR_FL__ << theStateMachine_.getCurrentStateName() << std::endl;
00205 return SOAPUtilities::makeSOAPMessageReference(theStateMachine_.getCurrentStateName());
00206 }
00207
00208
00209 xoap::MessageReference EventBuilderApp::stateMachineErrorMessageRequest(xoap::MessageReference message)
00210 throw (xoap::exception::Exception)
00211 {
00212 __MOUT__<< "theStateMachine_.getErrorMessage() = " << theStateMachine_.getErrorMessage() << std::endl;
00213
00214 SOAPParameters retParameters;
00215 retParameters.addParameter("ErrorMessage",theStateMachine_.getErrorMessage());
00216 return SOAPUtilities::makeSOAPMessageReference("stateMachineErrorMessageRequestReply",retParameters);
00217 }
00218
00219
00220 void EventBuilderApp::stateInitial(toolbox::fsm::FiniteStateMachine& fsm)
00221 throw (toolbox::fsm::exception::Exception)
00222 {
00223
00224 }
00225
00226
00227 void EventBuilderApp::stateHalted(toolbox::fsm::FiniteStateMachine& fsm)
00228 throw (toolbox::fsm::exception::Exception)
00229 {
00230
00231 }
00232
00233
00234 void EventBuilderApp::stateRunning(toolbox::fsm::FiniteStateMachine& fsm)
00235 throw (toolbox::fsm::exception::Exception)
00236 {
00237
00238 }
00239
00240
00241 void EventBuilderApp::stateConfigured(toolbox::fsm::FiniteStateMachine& fsm)
00242 throw (toolbox::fsm::exception::Exception)
00243 {
00244
00245 }
00246
00247
00248 void EventBuilderApp::statePaused(toolbox::fsm::FiniteStateMachine& fsm)
00249 throw (toolbox::fsm::exception::Exception)
00250 {
00251
00252 }
00253
00254
00255 void EventBuilderApp::inError (toolbox::fsm::FiniteStateMachine & fsm)
00256 throw (toolbox::fsm::exception::Exception)
00257 {
00258 std::cout << __COUT_HDR_FL__ << "Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
00259
00260 }
00261
00262
00263 void EventBuilderApp::enteringError (toolbox::Event::Reference e)
00264 throw (toolbox::fsm::exception::Exception)
00265 {
00266 std::cout << __COUT_HDR_FL__ << "Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
00267 toolbox::fsm::FailedEvent& failedEvent = dynamic_cast<toolbox::fsm::FailedEvent&>(*e);
00268 std::ostringstream error;
00269 error << "Failure performing transition from "
00270 << failedEvent.getFromState()
00271 << " to "
00272 << failedEvent.getToState()
00273 << " exception: " << failedEvent.getException().what();
00274 std::cout << __COUT_HDR_FL__ << error.str() << std::endl;
00275
00276
00277 }
00278
00279 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
00280 #define ARTDAQ_FILE_PREAMBLE "builder"
00281
00282
00283 void EventBuilderApp::transitionConfiguring(toolbox::Event::Reference e)
00284 throw (toolbox::fsm::exception::Exception)
00285 {
00286
00287 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00288 std::cout << __COUT_HDR_FL__ << SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) << std::endl;
00289
00290
00291 std::pair<std::string , ConfigurationGroupKey> theGroup(
00292 SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
00293 getParameters().getValue("ConfigurationGroupName"),
00294 ConfigurationGroupKey(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
00295 getParameters().getValue("ConfigurationGroupKey")));
00296
00297 __MOUT__ << "Configuration group name: " << theGroup.first << " key: " <<
00298 theGroup.second << std::endl;
00299
00300 theConfigurationManager_->loadConfigurationGroup(
00301 theGroup.first,
00302 theGroup.second, true);
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316 __MOUT__ << std::endl;
00317 __MOUT__ << std::endl;
00318
00319 fhicl::ParameterSet pset;
00320
00321
00322
00323 std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE + "-";
00324 std::string uid = theConfigurationManager_->getNode(XDAQContextConfigurationName_).getNode(supervisorConfigurationPath_).getValue();
00325
00326 __MOUT__ << "uid: " << uid << std::endl;
00327 for(unsigned int i=0;i<uid.size();++i)
00328 if((uid[i] >= 'a' && uid[i] <= 'z') ||
00329 (uid[i] >= 'A' && uid[i] <= 'Z') ||
00330 (uid[i] >= '0' && uid[i] <= '9'))
00331 filename += uid[i];
00332 filename += ".fcl";
00333
00334 __MOUT__ << "filename: " << filename << std::endl;
00335
00336 std::string fileFclString;
00337 {
00338 std::ifstream in(filename, std::ios::in | std::ios::binary);
00339 if (in)
00340 {
00341 std::string contents;
00342 in.seekg(0, std::ios::end);
00343 fileFclString.resize(in.tellg());
00344 in.seekg(0, std::ios::beg);
00345 in.read(&fileFclString[0], fileFclString.size());
00346 in.close();
00347 }
00348 }
00349 __MOUT__ << fileFclString << std::endl;
00350
00351 try
00352 {
00353 fhicl::make_ParameterSet(fileFclString, pset);
00354
00355
00356
00357 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00358 it->second->configure(pset);
00359 }
00360 catch(const cet::coded_exception<fhicl::error, &fhicl::detail::translate>& e)
00361 {
00362 __SS__ << "Error was caught while configuring: " << e.what() << std::endl;
00363 __MOUT_ERR__ << "\n" << ss.str();
00364 theStateMachine_.setErrorMessage(ss.str());
00365 throw toolbox::fsm::exception::Exception(
00366 "Transition Error" ,
00367 ss.str() ,
00368 "EventBuilderApp::transitionConfiguring" ,
00369 __LINE__ ,
00370 __FUNCTION__
00371 );
00372 }
00373
00374 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR DONE CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00375
00376 }
00377
00378
00379 void EventBuilderApp::transitionHalting(toolbox::Event::Reference e)
00380 throw (toolbox::fsm::exception::Exception)
00381 {
00382
00383 for(auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00384 it->second->halt();
00385 }
00386
00387
00388 void EventBuilderApp::transitionInitializing(toolbox::Event::Reference e)
00389 throw (toolbox::fsm::exception::Exception)
00390 {
00391
00392 }
00393
00394
00395 void EventBuilderApp::transitionPausing(toolbox::Event::Reference e)
00396 throw (toolbox::fsm::exception::Exception)
00397 {
00398
00399 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00400 it->second->pause();
00401 }
00402
00403
00404 void EventBuilderApp::transitionResuming(toolbox::Event::Reference e)
00405 throw (toolbox::fsm::exception::Exception)
00406 {
00407
00408 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00409 it->second->resume();
00410 }
00411
00412
00413 void EventBuilderApp::transitionStarting(toolbox::Event::Reference e)
00414 throw (toolbox::fsm::exception::Exception)
00415 {
00416
00417 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00418 it->second->start(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue("RunNumber"));
00419 }
00420
00421
00422 void EventBuilderApp::transitionStopping(toolbox::Event::Reference e)
00423 throw (toolbox::fsm::exception::Exception)
00424 {
00425
00426 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00427 it->second->stop();
00428
00429 for(std::map<int,EventBuilderInterface*>::iterator it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00430 it->second->halt();
00431 }