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