otsdaq  v1_01_02
 All Classes Namespaces Functions
AggregatorApp.cc
1 #include "otsdaq/AggregatorApp/AggregatorApp.h"
2 #include "otsdaq/AggregatorApp/AggregatorInterface.h"
3 #include "otsdaq-core/MessageFacility/MessageFacility.h"
4 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
5 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h"
6 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
7 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
8 
9 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationGroupKey.h"
10 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
11 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
12 
13 #include <toolbox/fsm/FailedEvent.h>
14 
15 #include <xdaq/NamespaceURI.h>
16 #include <xoap/Method.h>
17 
18 #include <memory>
19 #include "messagefacility/MessageLogger/MessageLogger.h"
20 #include "artdaq/DAQdata/configureMessageFacility.hh"
21 #include "artdaq/DAQrate/quiet_mpi.hh"
22 #include "cetlib/exception.h"
23 #include "artdaq/BuildInfo/GetPackageBuildInfo.hh"
24 #include "fhiclcpp/make_ParameterSet.h"
25 
26 #include <fstream>
27 #include <iostream>
28 #include <cassert>
29 
30 using namespace ots;
31 
32 XDAQ_INSTANTIATOR_IMPL(AggregatorApp)
33 
34 //========================================================================================================================
35 AggregatorApp::AggregatorApp(xdaq::ApplicationStub * s) throw (xdaq::exception::Exception)
36 : xdaq::Application (s)
37 , SOAPMessenger (this)
38 , stateMachineWorkLoopManager_(toolbox::task::bind(this, &AggregatorApp::stateMachineThread, "StateMachine"))
39 , stateMachineSemaphore_ (toolbox::BSem::FULL)
40 , theConfigurationManager_ (new ConfigurationManager)//(Singleton<ConfigurationManager>::getInstance()) //I always load the full config but if I want to load a partial configuration (new ConfigurationManager)
41 , XDAQContextConfigurationName_(theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getConfigurationName())
42 , supervisorConfigurationPath_ ("INITIALIZED INSIDE THE CONTRUCTOR BECAUSE IT NEEDS supervisorContextUID_ and supervisorApplicationUID_")
43 , supervisorContextUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
44 , supervisorApplicationUID_ ("INITIALIZED INSIDE THE CONTRUCTOR TO LAUNCH AN EXCEPTION")
45 {
46  INIT_MF("AggregatorApp");
47  xgi::bind (this, &AggregatorApp::Default, "Default" );
48  xgi::bind (this, &AggregatorApp::stateMachineXgiHandler, "StateMachineXgiHandler");
49 
50  xoap::bind(this, &AggregatorApp::stateMachineStateRequest, "StateMachineStateRequest", XDAQ_NS_URI );
51  try
52  {
53  supervisorContextUID_ = theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getContextUID(getApplicationContext()->getContextDescriptor()->getURL());
54  }
55  catch(...)
56  {
57  __MOUT_ERR__ << "XDAQ Supervisor could not access it's configuration through the Configuration Context Group." <<
58  " The XDAQContextConfigurationName = " << XDAQContextConfigurationName_ <<
59  ". The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
60  throw;
61  }
62  try
63  {
64  supervisorApplicationUID_ = theConfigurationManager_->__GET_CONFIG__(XDAQContextConfiguration)->getApplicationUID
65  (
66  getApplicationContext()->getContextDescriptor()->getURL(),
67  getApplicationDescriptor()->getLocalId()
68  );
69  }
70  catch(...)
71  {
72  __MOUT_ERR__ << "XDAQ Supervisor could not access it's configuration through the Configuration Application Group."
73  << " The supervisorApplicationUID = " << supervisorApplicationUID_ << std::endl;
74  throw;
75  }
76  supervisorConfigurationPath_ = "/" + supervisorContextUID_ + "/LinkToApplicationConfiguration/" + supervisorApplicationUID_ + "/LinkToSupervisorConfiguration";
77 
78  setStateMachineName(supervisorApplicationUID_);
79  init();
80 }
81 
82 //========================================================================================================================
83 AggregatorApp::~AggregatorApp(void)
84 {
85  destroy();
86 }
87 //========================================================================================================================
88 void AggregatorApp::init(void)
89 {
90  std::cout << __COUT_HDR_FL__ << "ARTDAQAGGREGATOR SUPERVISOR INIT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
91  theSupervisorDescriptorInfo_.init(getApplicationContext());
92  artdaq::configureMessageFacility("aggregator");
93 
94  // initialization
95 
96  int const wanted_threading_level { MPI_THREAD_MULTIPLE };
97  //int const wanted_threading_level { MPI_THREAD_FUNNELED };
98 
99  MPI_Comm local_group_comm;
100 
101  try
102  {
103  mpiSentry_.reset( new artdaq::MPISentry(0, 0, wanted_threading_level, artdaq::TaskType::AggregatorTask, local_group_comm) );
104  }
105  catch (cet::exception& errormsg)
106  {
107  mf::LogError("AggregatorMain") << errormsg ;
108  mf::LogError("AggregatorMain") << "MPISentry error encountered in AggregatorMain; exiting...";
109  throw errormsg;
110  }
111 
112  std::cout << __COUT_HDR_FL__ << "ARTDAQAGGREGATOR SUPERVISOR INIT4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
113  std::string name = "Aggregator";
114  unsigned short port = 5300;
115 // artdaq::setMsgFacAppName(supervisorApplicationUID_, port);
116  artdaq::setMsgFacAppName(name, port);
117 // mf::LogDebug(supervisorApplicationUID_) << "artdaq version " <<
118  mf::LogDebug(name + "Supervisor") << "artdaq version " <<
119  artdaq::GetPackageBuildInfo::getPackageBuildInfo().getPackageVersion()
120  << ", built " <<
121  artdaq::GetPackageBuildInfo::getPackageBuildInfo().getBuildTimestamp();
122 
123  // create the AggregatorInterface
124  theAggregatorInterface_ = new AggregatorInterface(mpiSentry_->rank(), name );
125  //theAggregatorInterface_ = new AggregatorInterface(mpiSentry_->rank(), local_group_comm, supervisorApplicationUID_ );
126 }
127 
128 //========================================================================================================================
129 void AggregatorApp::destroy(void)
130 {
131  delete theAggregatorInterface_;
132  //called by destructor
133  mpiSentry_.reset();
134 }
135 
136 //========================================================================================================================
137 void AggregatorApp::Default(xgi::Input * in, xgi::Output * out ) throw (xgi::exception::Exception)
138 {
139 
140  *out << "<!DOCTYPE HTML><html lang='en'><frameset col='100%' row='100%'><frame src='/WebPath/html/AggregatorApp.html?urn=" <<
141  this->getApplicationDescriptor()->getLocalId() << "'></frameset></html>";
142 }
143 
144 //========================================================================================================================
145 void AggregatorApp::stateMachineXgiHandler(xgi::Input * in, xgi::Output * out ) throw (xgi::exception::Exception)
146 {}
147 
148 //========================================================================================================================
149 void AggregatorApp::stateMachineResultXgiHandler(xgi::Input* in, xgi::Output* out ) throw (xgi::exception::Exception)
150 {}
151 
152 //========================================================================================================================
153 xoap::MessageReference AggregatorApp::stateMachineXoapHandler(xoap::MessageReference message ) throw (xoap::exception::Exception)
154 {
155  std::cout << __COUT_HDR_FL__ << "Soap Handler!" << std::endl;
156  stateMachineWorkLoopManager_.removeProcessedRequests();
157  stateMachineWorkLoopManager_.processRequest(message);
158  std::cout << __COUT_HDR_FL__ << "Done - Soap Handler!" << std::endl;
159  return message;
160 }
161 
162 //========================================================================================================================
163 xoap::MessageReference AggregatorApp::stateMachineResultXoapHandler(xoap::MessageReference message ) throw (xoap::exception::Exception)
164 {
165  std::cout << __COUT_HDR_FL__ << "Soap Handler!" << std::endl;
166  //stateMachineWorkLoopManager_.removeProcessedRequests();
167  //stateMachineWorkLoopManager_.processRequest(message);
168  std::cout << __COUT_HDR_FL__ << "Done - Soap Handler!" << std::endl;
169  return message;
170 }
171 
172 //========================================================================================================================
173 bool AggregatorApp::stateMachineThread(toolbox::task::WorkLoop* workLoop)
174 {
175  stateMachineSemaphore_.take();
176  std::cout << __COUT_HDR_FL__ << "Re-sending message..." << SOAPUtilities::translate(stateMachineWorkLoopManager_.getMessage(workLoop)).getCommand() << std::endl;
177  std::string reply = send(this->getApplicationDescriptor(),stateMachineWorkLoopManager_.getMessage(workLoop));
178  stateMachineWorkLoopManager_.report(workLoop, reply, 100, true);
179  std::cout << __COUT_HDR_FL__ << "Done with message" << std::endl;
180  stateMachineSemaphore_.give();
181  return false;//execute once and automatically remove the workloop so in WorkLoopManager the try workLoop->remove(job_) could be commented out
182  //return true;//go on and then you must do the workLoop->remove(job_) in WorkLoopManager
183 }
184 
185 //========================================================================================================================
186 xoap::MessageReference AggregatorApp::stateMachineStateRequest(xoap::MessageReference message) throw (xoap::exception::Exception)
187 {
188  std::cout << __COUT_HDR_FL__ << theStateMachine_.getCurrentStateName() << std::endl;
189  return SOAPUtilities::makeSOAPMessageReference(theStateMachine_.getCurrentStateName());
190 }
191 
192 //========================================================================================================================
193 void AggregatorApp::stateInitial(toolbox::fsm::FiniteStateMachine& fsm) throw (toolbox::fsm::exception::Exception)
194 {
195 
196 }
197 
198 //========================================================================================================================
199 void AggregatorApp::stateHalted(toolbox::fsm::FiniteStateMachine& fsm) throw (toolbox::fsm::exception::Exception)
200 {
201 
202 }
203 
204 //========================================================================================================================
205 void AggregatorApp::stateRunning(toolbox::fsm::FiniteStateMachine& fsm) throw (toolbox::fsm::exception::Exception)
206 {
207 
208 }
209 
210 //========================================================================================================================
211 void AggregatorApp::stateConfigured(toolbox::fsm::FiniteStateMachine& fsm) throw (toolbox::fsm::exception::Exception)
212 {
213 
214 }
215 
216 //========================================================================================================================
217 void AggregatorApp::statePaused(toolbox::fsm::FiniteStateMachine& fsm) throw (toolbox::fsm::exception::Exception)
218 {
219 
220 }
221 
222 //========================================================================================================================
223 void AggregatorApp::inError (toolbox::fsm::FiniteStateMachine & fsm) throw (toolbox::fsm::exception::Exception)
224 {
225  std::cout << __COUT_HDR_FL__ << "Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
226  //rcmsStateNotifier_.stateChanged("Error", "");
227 }
228 
229 //========================================================================================================================
230 void AggregatorApp::enteringError (toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
231 {
232  std::cout << __COUT_HDR_FL__ << "Fsm current state: " << theStateMachine_.getCurrentStateName()<< std::endl;
233  toolbox::fsm::FailedEvent& failedEvent = dynamic_cast<toolbox::fsm::FailedEvent&>(*e);
234  std::ostringstream error;
235  error << "Failure performing transition from "
236  << failedEvent.getFromState()
237  << " to "
238  << failedEvent.getToState()
239  << " exception: " << failedEvent.getException().what();
240  std::cout << __COUT_HDR_FL__ << error.str() << std::endl;
241  //diagService_->reportError(errstr.str(),DIAGERROR);
242 
243 }
244 
245 
246 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
247 #define ARTDAQ_FILE_PREAMBLE "aggregator"
248 //========================================================================================================================
249 void AggregatorApp::transitionConfiguring(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
250 {
251 
252  std::cout << __COUT_HDR_FL__ << "ARTDAQAGGREGATOR SUPERVISOR CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
253  std::cout << __COUT_HDR_FL__ << SOAPUtilities::translate(theStateMachine_.getCurrentMessage()) << std::endl;
254 
255  std::pair<std::string /*group name*/, ConfigurationGroupKey> theGroup(
256  SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
257  getParameters().getValue("ConfigurationGroupName"),
258  ConfigurationGroupKey(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).
259  getParameters().getValue("ConfigurationGroupKey")));
260 
261  __MOUT__ << "Configuration group name: " << theGroup.first << " key: " <<
262  theGroup.second << std::endl;
263 
264  theConfigurationManager_->loadConfigurationGroup(
265  theGroup.first,
266  theGroup.second, true);
267 
268 
269  std::string path = "";
270  char* dirMRB = getenv("MRB_BUILDDIR");
271  char* dirP = getenv("OTSDAQ_DIR");
272 
273  if(dirMRB) { path = std::string(dirMRB) + "/otsdaq_demo/"; }
274  else if(dirP) { path = std::string(dirP) + "/"; }
275 
276  //Now that the configuration manager has all the necessary configurations I can create all objects dependent of the configuration
277  //std::string configString = "daq:{aggregator:{event_builder_count:2 event_queue_depth:20 event_queue_wait_time:5 expected_events_per_bunch:1 file_duration:0 file_event_count:0 file_size_MB:0 first_event_builder_rank:2 mpi_buffer_count:8 print_event_store_stats:true xmlrpc_client_list:\";http://localhost:5603/RPC2,3;http://localhost:5604/RPC2,3;http://localhost:5605/RPC2,4;http://localhost:5606/RPC2,4;http://localhost:5601/RPC2,5;http://localhost:5602/RPC2,5\"} max_fragment_size_words:2.097152e6} outputs:{normalOutput:{fileName:\"/data/otsdata/data/artdaqots_r%06r_sr%02s_%to.root\" module_type:\"RootOutput\"}} physics:{my_output_modules:[\"normalOutput\"] p2:[\"BuildInfo\"] producers:{BuildInfo:{instance_name:\"ArtdaqOts\" module_type:\"ArtdaqOtsBuildInfo\"}}} process_name:\"DAQAG\" services:{Timing:{summaryOnly:true} scheduler:{fileMode:\"NOMERGE\"} user:{NetMonTransportServiceInterface:{max_fragment_size_words:2.097152e6 service_provider:\"NetMonTransportService\"}}} source:{module_type:\"NetMonInput\"}";
278  //ONLY 1 BOARD READER
279  // std::string configString = "daq:{aggregator:{event_builder_count:1 event_queue_depth:20 event_queue_wait_time:5 expected_events_per_bunch:1 file_duration:0 file_event_count:0 file_size_MB:0 first_event_builder_rank:1 mpi_buffer_count:8 print_event_store_stats:true xmlrpc_client_list:\";http://localhost:5100/RPC2,3;http://localhost:5101/RPC2,3;http://localhost:5200/RPC2,4;http://localhost:5201/RPC2,4;http://localhost:5300/RPC2,5;http://localhost:5301/RPC2,5\"} max_fragment_size_words:2.097152e6} outputs:{normalOutput:{fileName:\""+path+"artdaqots_r%06r_sr%02s_%to.root\" module_type:\"RootOutput\"}} physics:{my_output_modules:[\"normalOutput\"] p2:[\"BuildInfo\"] a1:[\"wf\"] producers:{BuildInfo:{instance_name:\"ArtdaqOts\" module_type:\"ArtdaqOtsBuildInfo\"}} analyzers:{wf: {module_type:\"WFViewer\" fragment_ids:[0] fragment_type_labels:[ \"DataGen\" ] prescale:60 write_to_file:true fileName:\""+path+"otsdaqdemo_onmon.root\"}}} process_name:\"DAQAG\" services:{Timing:{summaryOnly:true} scheduler:{fileMode:\"NOMERGE\" errorOnFailureToPut: false} NetMonTransportServiceInterface:{max_fragment_size_words:2097152 service_provider:\"NetMonTransportService\"}} source:{module_type:\"NetMonInput\"}";
280  //2 BOARD READERS
281  //std::string configString = "daq:{aggregator:{event_builder_count:1 event_queue_depth:20 event_queue_wait_time:5 expected_events_per_bunch:1 file_duration:0 file_event_count:0 file_size_MB:0 first_event_builder_rank:1 mpi_buffer_count:8 print_event_store_stats:true xmlrpc_client_list:\";http://localhost:5100/RPC2,3;http://localhost:5101/RPC2,3;http://localhost:5200/RPC2,4;http://localhost:5201/RPC2,4;http://localhost:5300/RPC2,5;http://localhost:5301/RPC2,5\"} max_fragment_size_words:2.097152e6} outputs:{normalOutput:{fileName:\""+path+"artdaqots_r%06r_sr%02s_%to.root\" module_type:\"RootOutput\"}} physics:{my_output_modules:[\"normalOutput\"] p2:[\"BuildInfo\"] a1:[\"wf\"] producers:{BuildInfo:{instance_name:\"ArtdaqOts\" module_type:\"ArtdaqOtsBuildInfo\"}} analyzers:{wf: {module_type:\"WFViewer\" fragment_ids:[0] fragment_type_labels:[ \"DataGen\" ] prescale:60 write_to_file:true fileName:\""+path+"otsdaqdemo_onmon.root\"}}} process_name:\"DAQAG\" services:{Timing:{summaryOnly:true} scheduler:{fileMode:\"NOMERGE\" errorOnFailureToPut: false} NetMonTransportServiceInterface:{max_fragment_size_words:2097152 service_provider:\"NetMonTransportService\"}} source:{module_type:\"NetMonInput\"}";
282 
283  fhicl::ParameterSet pset;
284  //fhicl::make_ParameterSet(configString, pset);
285 
286  std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE + "-";
287  std::string uid = theConfigurationManager_->getNode(XDAQContextConfigurationName_).getNode(supervisorConfigurationPath_).getValue();
288 
289  __MOUT__ << "uid: " << uid << std::endl;
290  for(unsigned int i=0;i<uid.size();++i)
291  if((uid[i] >= 'a' && uid[i] <= 'z') ||
292  (uid[i] >= 'A' && uid[i] <= 'Z') ||
293  (uid[i] >= '0' && uid[i] <= '9')) //only allow alpha numeric in file name
294  filename += uid[i];
295  filename += ".fcl";
296 
297 
298  __MOUT__ << std::endl;
299  __MOUT__ << std::endl;
300  __MOUT__ << "filename: " << filename << std::endl;
301 
302  std::string fileFclString;
303  {
304  std::ifstream in(filename, std::ios::in | std::ios::binary);
305  if (in)
306  {
307  std::string contents;
308  in.seekg(0, std::ios::end);
309  fileFclString.resize(in.tellg());
310  in.seekg(0, std::ios::beg);
311  in.read(&fileFclString[0], fileFclString.size());
312  in.close();
313  }
314  }
315 
316  __MOUT__ << fileFclString << std::endl;
317  fhicl::make_ParameterSet(fileFclString, pset);
318  //fhicl::make_ParameterSet(theConfigurationManager_->getNode(XDAQContextConfigurationName_).getNode(supervisorConfigurationPath_).getNode("ConfigurationString").getValue<std::string>(), pset);
319 
320 
321  theAggregatorInterface_->configure(pset);
322  mf::LogInfo("AggregatorInterface") << "ARTDAQAGGREGATOR SUPERVISOR DONE CONFIGURING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
323 
324 }
325 
326 //========================================================================================================================
327 void AggregatorApp::transitionHalting(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
328 {
329  theAggregatorInterface_->halt();
330 }
331 
332 //========================================================================================================================
333 void AggregatorApp::transitionInitializing(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
334 {
335 
336 }
337 
338 //========================================================================================================================
339 void AggregatorApp::transitionPausing(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
340 {
341  theAggregatorInterface_->pause();
342 }
343 
344 //========================================================================================================================
345 void AggregatorApp::transitionResuming(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
346 {
347  theAggregatorInterface_->resume();
348 }
349 
350 //========================================================================================================================
351 void AggregatorApp::transitionStarting(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
352 {
353  theAggregatorInterface_->start(SOAPUtilities::translate(theStateMachine_.getCurrentMessage()).getParameters().getValue("RunNumber"));
354 }
355 
356 //========================================================================================================================
357 void AggregatorApp::transitionStopping(toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
358 {
359  theAggregatorInterface_->stop();
360  theAggregatorInterface_->halt();
361 }