otsdaq  v2_04_01
FEDataManagerSupervisor.cc
1 #include "otsdaq-core/CoreSupervisors/FEDataManagerSupervisor.h"
2 
3 #include "../ARTDAQDataManager/ARTDAQDataManager.h"
4 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
5 #include "otsdaq-core/DataManager/DataManager.h"
6 #include "otsdaq-core/DataManager/DataManagerSingleton.h"
7 
8 //#include "otsdaq-core/FECore/FEProducerVInterface.h"
9 
10 using namespace ots;
11 
12 XDAQ_INSTANTIATOR_IMPL(FEDataManagerSupervisor)
13 
14 //========================================================================================================================
15 FEDataManagerSupervisor::FEDataManagerSupervisor(xdaq::ApplicationStub* s,
16  bool artdaqDataManager)
17  : FESupervisor(s)
18 {
19  __SUP_COUT__ << "Constructor." << __E__;
20 
21  // WARNING THE ORDER IS IMPORTANT SINCE THE FIRST STATE MACHINE ELEMENT
22  // WILL BE CALLED FIRST DURING TRANSITION!!!!!
23 
24  // the FE Interfaces Manager is added first, and then the Data Manager
25  // So on FSM transitions, front-ends will transition first.
26 
27  // FEVInterfacesManager gets added in FESupervisor constructor
28  __SUP_COUTV__(CoreSupervisorBase::theStateMachineImplementation_.size());
29 
30  //
31  // CoreSupervisorBase::theStateMachineImplementation_.push_back(
32  // new FEVInterfacesManager(
33  // CorePropertySupervisorBase::getContextTreeNode(),
34  // CorePropertySupervisorBase::supervisorConfigurationPath_
35  // )
36  // );
37 
38  if(artdaqDataManager)
39  {
40  __SUP_COUT__ << "Adding ARTDAQ Data Manager now...!" << __E__;
41  CoreSupervisorBase::theStateMachineImplementation_.push_back(
42  DataManagerSingleton::getInstance<ARTDAQDataManager>(
43  CorePropertySupervisorBase::getContextTreeNode(),
44  CorePropertySupervisorBase::getSupervisorConfigurationPath(),
45  CorePropertySupervisorBase::getSupervisorUID()));
46  }
47  else
48  {
49  __SUP_COUT__ << "Adding Data Manager now...!" << __E__;
50  CoreSupervisorBase::theStateMachineImplementation_.push_back(
51  DataManagerSingleton::getInstance<DataManager>(
52  CorePropertySupervisorBase::getContextTreeNode(),
53  CorePropertySupervisorBase::getSupervisorConfigurationPath(),
54  CorePropertySupervisorBase::getSupervisorUID()));
55  }
56 
57  extractDataManager();
58 
59  __SUP_COUT__ << "Constructed." << __E__;
60 } // end constructor()
61 
62 //========================================================================================================================
63 FEDataManagerSupervisor::~FEDataManagerSupervisor(void)
64 {
65  __SUP_COUT__ << "Destroying..." << __E__;
66 
67  // theStateMachineImplementation_ is reset and the object it points to deleted in
68  // ~CoreSupervisorBase() This destructor must happen before the CoreSupervisor
69  // destructor
70 
71  DataManagerSingleton::deleteInstance(CoreSupervisorBase::getSupervisorUID());
72  theStateMachineImplementation_.pop_back();
73 
74  __SUP_COUT__ << "Destructed." << __E__;
75 } // end destructor()
76 
77 //========================================================================================================================
78 // transitionConfiguring
79 // swap order of state machine vector for configuring
80 // so that DataManager gets configured first.
81 void FEDataManagerSupervisor::transitionConfiguring(toolbox::Event::Reference e)
82 {
83  __SUP_COUT__ << "transitionConfiguring" << __E__;
84 
85  // Data Manager needs to be configured (instantiate buffers)
86  // before FEVinterfaceManager configures (creates interfaces)
87 
88  if(theStateMachineImplementation_.size() != 2)
89  {
90  __SUP_SS__ << "State machine size is not 2! It is "
91  << theStateMachineImplementation_.size() << ". What happened??"
92  << __E__;
93  __SUP_SS_THROW__;
94  }
95 
96  VStateMachine* p = theStateMachineImplementation_[0];
97  theStateMachineImplementation_[0] = theStateMachineImplementation_[1];
98  theStateMachineImplementation_[1] = p;
99 
100  CoreSupervisorBase::transitionConfiguring(e);
101 
102  { // print buffer status
103  __SUP_SS__;
104  // theDataManager_->dumpStatus((std::ostream*)&ss);
105  std::cout << ss.str() << __E__;
106  }
107 
108  // At this point the buffers have been made with producers and consumers
109  // then the FEs (including FEProducers) were made.
110  // Producers (including FEProducers) and Consumers attach to their DataManager
111  // Buffer during their construction.
112 
113  // revert state machine order back
114  theStateMachineImplementation_[1] = theStateMachineImplementation_[0];
115  theStateMachineImplementation_[0] = p;
116 
117 } // end transitionConfiguring()
118 
119 //========================================================================================================================
120 // transitionStarting
121 // swap order of state machine vector for starting
122 // so that DataManager gets configured first.
123 // buffers need to be ready before FEs start writing to them
124 void FEDataManagerSupervisor::transitionStarting(toolbox::Event::Reference e)
125 {
126  __SUP_COUT__ << "transitionStarting" << __E__;
127 
128  // Data Manager needs to be started (start buffers)
129  // before FEVinterfaceManager starts (interfaces write)
130 
131  if(theStateMachineImplementation_.size() != 2)
132  {
133  __SUP_SS__ << "State machine size is not 2! It is "
134  << theStateMachineImplementation_.size() << ". What happened??"
135  << __E__;
136  }
137 
138  VStateMachine* p = theStateMachineImplementation_[0];
139  theStateMachineImplementation_[0] = theStateMachineImplementation_[1];
140  theStateMachineImplementation_[1] = p;
141 
142  CoreSupervisorBase::transitionStarting(e);
143 
144  // revert state machine order back
145  theStateMachineImplementation_[1] = theStateMachineImplementation_[0];
146  theStateMachineImplementation_[0] = p;
147 
148 } // end transitionStarting()
149 
150 //========================================================================================================================
151 // transitionResuming
152 // swap order of state machine vector for resuming
153 // so that DataManager gets configured first.
154 // buffers need to be ready before FEs start writing to them
155 void FEDataManagerSupervisor::transitionResuming(toolbox::Event::Reference e)
156 {
157  __SUP_COUT__ << "transitionStarting" << __E__;
158 
159  // Data Manager needs to be resumed (resume buffers)
160  // before FEVinterfaceManager resumes (interfaces write)
161 
162  if(theStateMachineImplementation_.size() != 2)
163  {
164  __SUP_SS__ << "State machine size is not 2! It is "
165  << theStateMachineImplementation_.size() << ". What happened??"
166  << __E__;
167  }
168 
169  VStateMachine* p = theStateMachineImplementation_[0];
170  theStateMachineImplementation_[0] = theStateMachineImplementation_[1];
171  theStateMachineImplementation_[1] = p;
172 
173  CoreSupervisorBase::transitionResuming(e);
174 
175  // revert state machine order back
176  theStateMachineImplementation_[1] = theStateMachineImplementation_[0];
177  theStateMachineImplementation_[0] = p;
178 
179 } // end transitionResuming()
180 
181 //========================================================================================================================
182 // extractDataManager
183 //
184 // locates theDataManager in state machines vector and
185 // returns 0 if not found.
186 //
187 // Note: this code assumes a CoreSupervisorBase has only one
188 // DataManager in its vector of state machines
189 DataManager* FEDataManagerSupervisor::extractDataManager()
190 {
191  theDataManager_ = 0;
192 
193  for(unsigned int i = 0; i < theStateMachineImplementation_.size(); ++i)
194  {
195  try
196  {
197  theDataManager_ =
198  dynamic_cast<DataManager*>(theStateMachineImplementation_[i]);
199  if(!theDataManager_)
200  {
201  // dynamic_cast returns null pointer on failure
202  __SUP_SS__ << "Dynamic cast failure!" << __E__;
203  __SUP_COUT_ERR__ << ss.str();
204  __SUP_SS_THROW__;
205  }
206  __SUP_COUT__ << "State Machine " << i << " WAS of type DataManager" << __E__;
207 
208  break;
209  }
210  catch(...)
211  {
212  __SUP_COUT__ << "State Machine " << i << " was NOT of type DataManager"
213  << __E__;
214  }
215  }
216 
217  __SUP_COUT__ << "theDataManager pointer = " << theDataManager_ << __E__;
218 
219  return theDataManager_;
220 } // end extractDataManager()