1 #include "otsdaq/DispatcherApp/DispatcherApp.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutMacros.h"
4 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h"
5 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
6 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
8 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationGroupKey.h"
9 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
10 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
12 #include <toolbox/fsm/FailedEvent.h>
14 #include <xdaq/NamespaceURI.h>
15 #include <xoap/Method.h>
17 #include "artdaq/DAQdata/Globals.hh"
19 #include "messagefacility/MessageLogger/MessageLogger.h"
20 #include "artdaq-core/Utilities/configureMessageFacility.hh"
21 #include "cetlib/exception.h"
22 #include "artdaq/BuildInfo/GetPackageBuildInfo.hh"
23 #include "fhiclcpp/make_ParameterSet.h"
35 : xdaq::Application(s)
37 , stateMachineWorkLoopManager_(toolbox::task::bind(this, &
DispatcherApp::stateMachineThread, "StateMachine"))
38 , stateMachineSemaphore_(toolbox::BSem::FULL)
40 , XDAQContextConfigurationName_(theConfigurationManager_->__GET_CONFIG__(
XDAQContextConfiguration)->getConfigurationName())
41 , supervisorConfigurationPath_("INITIALIZED INSIDE THE CONTRUCTOR BECAUSE IT NEEDS supervisorContextUID_ and supervisorApplicationUID_")
42 , supervisorContextUID_("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
43 , supervisorApplicationUID_("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
45 INIT_MF(
"DispatcherApp");
46 xgi::bind(
this, &DispatcherApp::Default,
"Default");
47 xgi::bind(
this, &DispatcherApp::stateMachineXgiHandler,
"StateMachineXgiHandler");
49 xoap::bind(
this, &DispatcherApp::stateMachineStateRequest,
"StateMachineStateRequest", XDAQ_NS_URI);
50 xoap::bind(
this, &DispatcherApp::stateMachineErrorMessageRequest,
"StateMachineErrorMessageRequest", XDAQ_NS_URI);
54 supervisorContextUID_ = theConfigurationManager_->__GET_CONFIG__(
XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL());
58 __COUT_ERR__ <<
"XDAQ Supervisor could not access it's configuration through the Configuration Context Group." <<
59 " The XDAQContextConfigurationName = " << XDAQContextConfigurationName_ <<
60 ". The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
67 getApplicationContext()->getContextDescriptor()->getURL(),
68 getApplicationDescriptor()->getLocalId()
73 __COUT_ERR__ <<
"XDAQ Supervisor could not access it's configuration through the Configuration Application Group."
74 <<
" The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
77 supervisorConfigurationPath_ =
"/" + supervisorContextUID_ +
"/LinkToApplicationConfiguration/" + supervisorApplicationUID_ +
"/LinkToSupervisorConfiguration";
79 setStateMachineName(supervisorApplicationUID_);
84 DispatcherApp::~DispatcherApp(
void)
89 void DispatcherApp::init(
void)
91 std::cout << __COUT_HDR_FL__ <<
"ARTDAQDispatcher SUPERVISOR INIT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
92 allSupervisorInfo_.init(getApplicationContext());
93 artdaq::configureMessageFacility(
"Dispatcher");
97 std::cout << __COUT_HDR_FL__ <<
"ARTDAQDispatcher SUPERVISOR INIT4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
98 std::string name =
"Dispatcher";
99 unsigned short port = 5300;
101 artdaq::setMsgFacAppName(name, port);
103 mf::LogDebug(name +
"Supervisor") <<
"artdaq version " <<
104 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getPackageVersion()
106 artdaq::GetPackageBuildInfo::getPackageBuildInfo().getBuildTimestamp();
110 my_rank = this->getApplicationDescriptor()->getLocalId();
111 theDispatcherInterface_ =
new artdaq::DispatcherApp();
116 void DispatcherApp::destroy(
void)
118 delete theDispatcherInterface_;
122 void DispatcherApp::Default(xgi::Input * in, xgi::Output * out)
125 *out <<
"<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/DispatcherApp.html?urn=" <<
126 this->getApplicationDescriptor()->getLocalId() <<
"'></frameset></html>";
130 void DispatcherApp::stateMachineXgiHandler(xgi::Input * in, xgi::Output * out)
134 void DispatcherApp::stateMachineResultXgiHandler(xgi::Input* in, xgi::Output* out)
138 xoap::MessageReference DispatcherApp::stateMachineXoapHandler(xoap::MessageReference message)
140 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
141 stateMachineWorkLoopManager_.removeProcessedRequests();
142 stateMachineWorkLoopManager_.processRequest(message);
143 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
148 xoap::MessageReference DispatcherApp::stateMachineResultXoapHandler(xoap::MessageReference message)
150 std::cout << __COUT_HDR_FL__ <<
"Soap Handler!" << std::endl;
153 std::cout << __COUT_HDR_FL__ <<
"Done - Soap Handler!" << std::endl;
158 bool DispatcherApp::stateMachineThread(toolbox::task::WorkLoop* workLoop)
160 stateMachineSemaphore_.take();
161 std::cout << __COUT_HDR_FL__ <<
"Re-sending message..." << SOAPUtilities::translate(stateMachineWorkLoopManager_.getMessage(workLoop)).getCommand() << std::endl;
162 std::string reply = send(this->getApplicationDescriptor(), stateMachineWorkLoopManager_.getMessage(workLoop));
163 stateMachineWorkLoopManager_.report(workLoop, reply, 100,
true);
164 std::cout << __COUT_HDR_FL__ <<
"Done with message" << std::endl;
165 stateMachineSemaphore_.give();
171 xoap::MessageReference DispatcherApp::stateMachineStateRequest(xoap::MessageReference message)
173 std::cout << __COUT_HDR_FL__ << theStateMachine_.getCurrentStateName() << std::endl;
174 return SOAPUtilities::makeSOAPMessageReference(theStateMachine_.getCurrentStateName());
178 xoap::MessageReference DispatcherApp::stateMachineErrorMessageRequest(xoap::MessageReference message)
181 __COUT__ <<
"theStateMachine_.getErrorMessage() = " << theStateMachine_.getErrorMessage() << std::endl;
184 retParameters.addParameter(
"ErrorMessage", theStateMachine_.getErrorMessage());
185 return SOAPUtilities::makeSOAPMessageReference(
"stateMachineErrorMessageRequestReply", retParameters);
189 void DispatcherApp::stateInitial(toolbox::fsm::FiniteStateMachine& fsm)
195 void DispatcherApp::stateHalted(toolbox::fsm::FiniteStateMachine& fsm)
201 void DispatcherApp::stateRunning(toolbox::fsm::FiniteStateMachine& fsm)
207 void DispatcherApp::stateConfigured(toolbox::fsm::FiniteStateMachine& fsm)
213 void DispatcherApp::statePaused(toolbox::fsm::FiniteStateMachine& fsm)
219 void DispatcherApp::inError(toolbox::fsm::FiniteStateMachine & fsm)
221 std::cout << __COUT_HDR_FL__ <<
"Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
226 void DispatcherApp::enteringError(toolbox::Event::Reference e)
228 std::cout << __COUT_HDR_FL__ <<
"Fsm current state: " << theStateMachine_.getCurrentStateName() << std::endl;
229 toolbox::fsm::FailedEvent& failedEvent =
dynamic_cast<toolbox::fsm::FailedEvent&
>(*e);
230 std::ostringstream error;
231 error <<
"Failure performing transition from "
232 << failedEvent.getFromState()
234 << failedEvent.getToState()
235 <<
" exception: " << failedEvent.getException().what();
236 std::cout << __COUT_HDR_FL__ << error.str() << std::endl;
242 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
243 #define ARTDAQ_FILE_PREAMBLE "aggregator"
245 void DispatcherApp::transitionConfiguring(toolbox::Event::Reference e)
248 std::cout << __COUT_HDR_FL__ <<
"ARTDAQDispatcher SUPERVISOR CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
249 std::cout << __COUT_HDR_FL__ << SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) << std::endl;
252 SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
253 getParameters().getValue(
"ConfigurationGroupName"),
255 getParameters().getValue(
"ConfigurationGroupKey")));
257 __COUT__ <<
"Configuration group name: " << theGroup.first <<
" key: " <<
258 theGroup.second << std::endl;
260 theConfigurationManager_->loadConfigurationGroup(
262 theGroup.second,
true);
265 std::string path =
"";
266 char* dirMRB = getenv(
"MRB_BUILDDIR");
267 char* dirP = getenv(
"OTSDAQ_DIR");
269 if (dirMRB) { path = std::string(dirMRB) +
"/otsdaq_demo/"; }
270 else if (dirP) { path = std::string(dirP) +
"/"; }
279 fhicl::ParameterSet pset;
282 std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE +
"-";
283 std::string uid = theConfigurationManager_->getNode(XDAQContextConfigurationName_).getNode(supervisorConfigurationPath_).getValue();
285 __COUT__ <<
"uid: " << uid << std::endl;
286 for (
unsigned int i = 0; i < uid.size(); ++i)
287 if ((uid[i] >=
'a' && uid[i] <=
'z') ||
288 (uid[i] >=
'A' && uid[i] <=
'Z') ||
289 (uid[i] >=
'0' && uid[i] <=
'9'))
294 __COUT__ << std::endl;
295 __COUT__ << std::endl;
296 __COUT__ <<
"filename: " << filename << std::endl;
298 std::string fileFclString;
300 std::ifstream in(filename, std::ios::in | std::ios::binary);
303 std::string contents;
304 in.seekg(0, std::ios::end);
305 fileFclString.resize(in.tellg());
306 in.seekg(0, std::ios::beg);
307 in.read(&fileFclString[0], fileFclString.size());
312 __COUT__ << fileFclString << std::endl;
313 fhicl::make_ParameterSet(fileFclString, pset);
317 theDispatcherInterface_->initialize(pset, 0, 0);
318 mf::LogInfo(
"DispatcherInterface") <<
"ARTDAQDispatcher SUPERVISOR DONE CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
323 void DispatcherApp::transitionHalting(toolbox::Event::Reference e)
329 void DispatcherApp::transitionInitializing(toolbox::Event::Reference e)
335 void DispatcherApp::transitionPausing(toolbox::Event::Reference e)
337 theDispatcherInterface_->pause(0, 0);
341 void DispatcherApp::transitionResuming(toolbox::Event::Reference e)
343 theDispatcherInterface_->resume(0, 0);
347 void DispatcherApp::transitionStarting(toolbox::Event::Reference e)
349 art::RunID runId((art::RunNumber_t)boost::lexical_cast<art::RunNumber_t>(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue(
"RunNumber")));
350 theDispatcherInterface_->start(runId, 0, 0);
354 void DispatcherApp::transitionStopping(toolbox::Event::Reference e)
356 theDispatcherInterface_->stop(45, 0);
357 theDispatcherInterface_->shutdown(45);