00001 #include "otsdaq/EventBuilderApp/EventBuilderApp.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutMacros.h"
00004 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h"
00005 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
00006 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
00007
00008 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationGroupKey.h"
00009 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
00010 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
00011
00012 #include <toolbox/fsm/FailedEvent.h>
00013
00014 #include <xdaq/NamespaceURI.h>
00015 #include <xoap/Method.h>
00016
00017 #include <memory>
00018 #include "messagefacility/MessageLogger/MessageLogger.h"
00019 #include "artdaq-core/Utilities/configureMessageFacility.hh"
00020 #include "cetlib/exception.h"
00021 #include "artdaq/BuildInfo/GetPackageBuildInfo.hh"
00022 #include "fhiclcpp/make_ParameterSet.h"
00023
00024 #include <fstream>
00025 #include <iostream>
00026 #include <cassert>
00027
00028 using namespace ots;
00029
00030 XDAQ_INSTANTIATOR_IMPL(EventBuilderApp)
00031
00032
00033 EventBuilderApp::EventBuilderApp(xdaq::ApplicationStub* stub)
00034
00035 : xdaq::Application (stub)
00036 , SOAPMessenger (this)
00037 , stateMachineWorkLoopManager_ (toolbox::task::bind(this, &EventBuilderApp::stateMachineThread, "StateMachine"))
00038 , stateMachineSemaphore_ (toolbox::BSem::FULL)
00039 , theConfigurationManager_ (new ConfigurationManager)
00040 , XDAQContextConfigurationName_(theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getConfigurationName())
00041 , supervisorConfigurationPath_ ("INITIALIZED INSIDE THE CONTRUCTOR BECAUSE IT NEEDS supervisorContextUID_ and supervisorApplicationUID_")
00042 , supervisorContextUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
00043 , supervisorApplicationUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
00044
00045
00046
00047 {
00048 INIT_MF("EventBuilderApp");
00049 xgi::bind (this, &EventBuilderApp::Default, "Default" );
00050 xgi::bind (this, &EventBuilderApp::stateMachineXgiHandler, "StateMachineXgiHandler");
00051
00052 xoap::bind(this, &EventBuilderApp::stateMachineStateRequest, "StateMachineStateRequest", XDAQ_NS_URI );
00053 xoap::bind(this, &EventBuilderApp::stateMachineErrorMessageRequest, "StateMachineErrorMessageRequest", XDAQ_NS_URI );
00054
00055 try
00056 {
00057 supervisorContextUID_ = theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL());
00058 }
00059 catch(...)
00060 {
00061 __COUT_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 __COUT_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 allSupervisorInfo_.init(getApplicationContext());
00096
00097 artdaq::configureMessageFacility("eventbuilder");
00098
00099
00100
00101 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR NO ERRORS MAKING MSG FACILITY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00102 std::string name = "Builder";
00103 unsigned short port = 5200;
00104
00105 artdaq::setMsgFacAppName(name, port);
00106 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR MSG FACILITY DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00107 mf::LogDebug(name + "Supervisor") << "artdaq version " <<
00108
00109 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getPackageVersion()
00110 << ", built " <<
00111 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getBuildTimestamp();
00112
00113
00114 app_name = name;
00115 my_rank = this->getApplicationDescriptor()->getLocalId();
00116 theARTDAQEventBuilderInterfaces_[0] = new artdaq::EventBuilderApp( );
00117 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR INIT DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00118 }
00119
00120
00121 void EventBuilderApp::destroy(void)
00122 {
00123 }
00124
00125
00126 void EventBuilderApp::Default(xgi::Input * in, xgi::Output * out )
00127
00128 {
00129
00130 *out << "<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/EventBuilderApp.html?urn=" <<
00131 this->getApplicationDescriptor()->getLocalId() <<"'></frameset></html>";
00132 }
00133
00134
00135 void EventBuilderApp::stateMachineXgiHandler(xgi::Input * in, xgi::Output * out )
00136
00137 {}
00138
00139
00140 void EventBuilderApp::stateMachineResultXgiHandler(xgi::Input* in, xgi::Output* out )
00141
00142 {}
00143
00144
00145 xoap::MessageReference EventBuilderApp::stateMachineXoapHandler(xoap::MessageReference message )
00146
00147 {
00148 std::cout << __COUT_HDR_FL__ << "Soap Handler!" << std::endl;
00149 stateMachineWorkLoopManager_.removeProcessedRequests();
00150 stateMachineWorkLoopManager_.processRequest(message);
00151 std::cout << __COUT_HDR_FL__ << "Done - Soap Handler!" << std::endl;
00152 return message;
00153 }
00154
00155
00156 xoap::MessageReference EventBuilderApp::stateMachineResultXoapHandler(xoap::MessageReference message )
00157
00158 {
00159 std::cout << __COUT_HDR_FL__ << "Soap Handler!" << std::endl;
00160
00161
00162 std::cout << __COUT_HDR_FL__ << "Done - Soap Handler!" << std::endl;
00163 return message;
00164 }
00165
00166
00167 bool EventBuilderApp::stateMachineThread(toolbox::task::WorkLoop* workLoop)
00168 {
00169 stateMachineSemaphore_.take();
00170 std::cout << __COUT_HDR_FL__ << "Re-sending message..." << SOAPUtilities::translate(stateMachineWorkLoopManager_.getMessage(workLoop)).getCommand() << std::endl;
00171 std::string reply = send(this->getApplicationDescriptor(),stateMachineWorkLoopManager_.getMessage(workLoop));
00172 stateMachineWorkLoopManager_.report(workLoop, reply, 100, true);
00173 std::cout << __COUT_HDR_FL__ << "Done with message" << std::endl;
00174 stateMachineSemaphore_.give();
00175 return false;
00176
00177 }
00178
00179
00180 xoap::MessageReference EventBuilderApp::stateMachineStateRequest(xoap::MessageReference message)
00181
00182 {
00183 std::cout << __COUT_HDR_FL__ << theStateMachine_.getCurrentStateName() << std::endl;
00184 return SOAPUtilities::makeSOAPMessageReference(theStateMachine_.getCurrentStateName());
00185 }
00186
00187
00188 xoap::MessageReference EventBuilderApp::stateMachineErrorMessageRequest(xoap::MessageReference message)
00189
00190 {
00191 __COUT__<< "theStateMachine_.getErrorMessage() = " << theStateMachine_.getErrorMessage() << std::endl;
00192
00193 SOAPParameters retParameters;
00194 retParameters.addParameter("ErrorMessage",theStateMachine_.getErrorMessage());
00195 return SOAPUtilities::makeSOAPMessageReference("stateMachineErrorMessageRequestReply",retParameters);
00196 }
00197
00198
00199 void EventBuilderApp::stateInitial(toolbox::fsm::FiniteStateMachine& fsm)
00200
00201 {
00202
00203 }
00204
00205
00206 void EventBuilderApp::stateHalted(toolbox::fsm::FiniteStateMachine& fsm)
00207
00208 {
00209
00210 }
00211
00212
00213 void EventBuilderApp::stateRunning(toolbox::fsm::FiniteStateMachine& fsm)
00214
00215 {
00216
00217 }
00218
00219
00220 void EventBuilderApp::stateConfigured(toolbox::fsm::FiniteStateMachine& fsm)
00221
00222 {
00223
00224 }
00225
00226
00227 void EventBuilderApp::statePaused(toolbox::fsm::FiniteStateMachine& fsm)
00228
00229 {
00230
00231 }
00232
00233
00234 void EventBuilderApp::inError (toolbox::fsm::FiniteStateMachine & fsm)
00235
00236 {
00237 std::cout << __COUT_HDR_FL__ << "Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
00238
00239 }
00240
00241
00242 void EventBuilderApp::enteringError (toolbox::Event::Reference e)
00243
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 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
00259 #define ARTDAQ_FILE_PREAMBLE "builder"
00260
00261
00262 void EventBuilderApp::transitionConfiguring(toolbox::Event::Reference e)
00263
00264 {
00265
00266 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00267 std::cout << __COUT_HDR_FL__ << SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) << std::endl;
00268
00269
00270 std::pair<std::string , ConfigurationGroupKey> theGroup(
00271 SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
00272 getParameters().getValue("ConfigurationGroupName"),
00273 ConfigurationGroupKey(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
00274 getParameters().getValue("ConfigurationGroupKey")));
00275
00276 __COUT__ << "Configuration group name: " << theGroup.first << " key: " <<
00277 theGroup.second << std::endl;
00278
00279 theConfigurationManager_->loadConfigurationGroup(
00280 theGroup.first,
00281 theGroup.second, true);
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 __COUT__ << std::endl;
00296 __COUT__ << std::endl;
00297
00298 fhicl::ParameterSet pset;
00299
00300
00301
00302 std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE + "-";
00303 std::string uid = theConfigurationManager_->getNode(XDAQContextConfigurationName_).getNode(supervisorConfigurationPath_).getValue();
00304
00305 __COUT__ << "uid: " << uid << std::endl;
00306 for(unsigned int i=0;i<uid.size();++i)
00307 if((uid[i] >= 'a' && uid[i] <= 'z') ||
00308 (uid[i] >= 'A' && uid[i] <= 'Z') ||
00309 (uid[i] >= '0' && uid[i] <= '9'))
00310 filename += uid[i];
00311 filename += ".fcl";
00312
00313 __COUT__ << "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 __COUT__ << fileFclString << std::endl;
00329
00330 try
00331 {
00332 fhicl::make_ParameterSet(fileFclString, pset);
00333
00334
00335
00336 for(auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00337 it->second->initialize(pset,0,0);
00338 }
00339 catch(const cet::coded_exception<fhicl::error, &fhicl::detail::translate>& e)
00340 {
00341 __SS__ << "Error was caught while configuring: " << e.what() << std::endl;
00342 __COUT_ERR__ << "\n" << ss.str();
00343 theStateMachine_.setErrorMessage(ss.str());
00344 throw toolbox::fsm::exception::Exception(
00345 "Transition Error" ,
00346 ss.str() ,
00347 "EventBuilderApp::transitionConfiguring" ,
00348 __LINE__ ,
00349 __FUNCTION__
00350 );
00351 }
00352
00353 std::cout << __COUT_HDR_FL__ << "ARTDAQBUILDER SUPERVISOR DONE CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00354
00355 }
00356
00357
00358 void EventBuilderApp::transitionHalting(toolbox::Event::Reference e)
00359
00360 {
00361
00362
00363
00364 }
00365
00366
00367 void EventBuilderApp::transitionInitializing(toolbox::Event::Reference e)
00368
00369 {
00370
00371 }
00372
00373
00374 void EventBuilderApp::transitionPausing(toolbox::Event::Reference e)
00375
00376 {
00377
00378 for(auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00379 it->second->pause(0, 0);
00380 }
00381
00382
00383 void EventBuilderApp::transitionResuming(toolbox::Event::Reference e)
00384
00385 {
00386
00387 for(auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00388 it->second->resume(0, 0);
00389 }
00390
00391
00392 void EventBuilderApp::transitionStarting(toolbox::Event::Reference e)
00393
00394 {
00395
00396 art::RunID runId((art::RunNumber_t)boost::lexical_cast<art::RunNumber_t>(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue("RunNumber")));
00397 for(auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00398 it->second->start(runId, 0, 0);
00399 }
00400
00401
00402 void EventBuilderApp::transitionStopping(toolbox::Event::Reference e)
00403
00404 {
00405
00406 for(auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00407 it->second->stop(45, 0);
00408
00409 for(auto it=theARTDAQEventBuilderInterfaces_.begin(); it!=theARTDAQEventBuilderInterfaces_.end(); it++)
00410 it->second->shutdown(45);
00411 }