otsdaq  v2_00_00
AllSupervisorInfo.cc
1 #include "otsdaq-core/SupervisorInfo/AllSupervisorInfo.h"
2 
3 #include "otsdaq-core/MessageFacility/MessageFacility.h"
4 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
5 
6 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
7 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
8 
9 
10 #include <iostream>
11 
12 
13 
14 using namespace ots;
15 
16 
17 
18 //========================================================================================================================
19 AllSupervisorInfo::AllSupervisorInfo(void)
20 : theSupervisorInfo_ (0)
21 , theWizardInfo_ (0)
22 {
23 }
24 
25 //========================================================================================================================
26 AllSupervisorInfo::AllSupervisorInfo(xdaq::ApplicationContext* applicationContext)
28 {
29  init(applicationContext);
30 }
31 
32 //========================================================================================================================
33 AllSupervisorInfo::~AllSupervisorInfo(void)
34 {
35  destroy();
36 }
37 
38 //========================================================================================================================
39 void AllSupervisorInfo::destroy(void)
40 {
41  allSupervisorInfo_.clear();
42  allFETypeSupervisorInfo_.clear();
43  allDMTypeSupervisorInfo_.clear();
44 
45  theSupervisorInfo_ = 0;
46  theWizardInfo_ = 0;
47 
48  SupervisorDescriptorInfoBase::destroy();
49 }
50 
51 //========================================================================================================================
52 void AllSupervisorInfo::init(xdaq::ApplicationContext* applicationContext)
53 {
54  AllSupervisorInfo::destroy();
55  SupervisorDescriptorInfoBase::init(applicationContext);
56 
57  //ready.. loop through all descriptors, and organize
58 
59  ConfigurationManager cfgMgr;
60  const XDAQContextConfiguration* contextConfig =
61  cfgMgr.__GET_CONFIG__(XDAQContextConfiguration);
62 
63  auto allDescriptors = SupervisorDescriptorInfoBase::getAllDescriptors();
64  for(const auto& descriptor:allDescriptors)
65  {
66  auto /*<iterator,bool>*/ emplacePair = allSupervisorInfo_.emplace(std::pair<unsigned int, SupervisorInfo>(
67  descriptor.second->getLocalId(),//descriptor.first,
69  descriptor.second /* descriptor */,
70  contextConfig->getApplicationUID
71  (
72  descriptor.second->getContextDescriptor()->getURL(),
73  descriptor.second->getLocalId()
74  ) /* name */,
75  contextConfig->getContextUID(
76  descriptor.second->getContextDescriptor()->getURL()) /* xdaq parent context */
77  )));
78  if(!emplacePair.second)
79  {
80  __SS__ << "Error! Duplicate Application IDs are not allowed. ID =" <<
81  descriptor.second->getLocalId() << __E__;
82  __SS_THROW__;
83  }
84 
86  // now organize new descriptor by class...
87 
88  //check for gateway supervisor
89  // note: necessarily exclusive to other Supervisor types
90  if(emplacePair.first->second.isGatewaySupervisor())
91  {
92  if(theSupervisorInfo_)
93  {
94  __SS__ << "Error! Multiple Gateway Supervisors of class " << XDAQContextConfiguration::GATEWAY_SUPERVISOR_CLASS <<
95  " found. There can only be one. ID =" <<
96  descriptor.second->getLocalId() << __E__;
97  __SS_THROW__;
98  }
99  //copy and erase from map
100  theSupervisorInfo_ = &(emplacePair.first->second);
101  continue;
102  }
103 
104  //check for wizard supervisor
105  // note: necessarily exclusive to other Supervisor types
106  if(emplacePair.first->second.isWizardSupervisor())
107  {
108  if(theWizardInfo_)
109  {
110  __SS__ << "Error! Multiple Wizard Supervisors of class " << XDAQContextConfiguration::WIZARD_SUPERVISOR_CLASS <<
111  " found. There can only be one. ID =" <<
112  descriptor.second->getLocalId() << __E__;
113  __SS_THROW__;
114  }
115  //copy and erase from map
116  theWizardInfo_ = &(emplacePair.first->second);
117  continue;
118  }
119 
120 
121  //check for FE type, then add to FE group
122  // note: not necessarily exclusive to other Supervisor types
123  if(emplacePair.first->second.isTypeFESupervisor())
124  {
125  allFETypeSupervisorInfo_.emplace(std::pair<unsigned int, const SupervisorInfo&>(
126  emplacePair.first->second.getId(),
127  emplacePair.first->second));
128  }
129 
130  //check for DM type, then add to DM group
131  // note: not necessarily exclusive to other Supervisor types
132  if(emplacePair.first->second.isTypeDMSupervisor())
133  {
134  allDMTypeSupervisorInfo_.emplace(std::pair<unsigned int, const SupervisorInfo&>(
135  emplacePair.first->second.getId(),
136  emplacePair.first->second));
137  }
138 
139  //check for Logbook type, then add to Logbook group
140  // note: not necessarily exclusive to other Supervisor types
141  if(emplacePair.first->second.isTypeLogbookSupervisor())
142  {
143  allLogbookTypeSupervisorInfo_.emplace(std::pair<unsigned int, const SupervisorInfo&>(
144  emplacePair.first->second.getId(),
145  emplacePair.first->second));
146  }
147 
148  } //end main extraction loop
149 
150 
151  if((!theWizardInfo_ && !theSupervisorInfo_) ||
152  (theWizardInfo_ && theSupervisorInfo_))
153  {
154  __SS__ << "Error! Must have one " << XDAQContextConfiguration::GATEWAY_SUPERVISOR_CLASS <<
155  " OR one " << XDAQContextConfiguration::WIZARD_SUPERVISOR_CLASS <<
156  " as part of the context configuration! " <<
157  "Neither were found." << __E__;
158  __SS_THROW__;
159  }
160 
161 
162  SupervisorDescriptorInfoBase::destroy();
163 
164  __COUT__ << "Init" << __E__;
165 
166  //for debugging
167  //getOrderedSupervisorDescriptors("Configure");
168 }
169 
170 //========================================================================================================================
171 const SupervisorInfo& AllSupervisorInfo::getSupervisorInfo(xdaq::Application* app) const
172 {
173  auto it = allSupervisorInfo_.find(app->getApplicationDescriptor()->getLocalId());
174  if(it == allSupervisorInfo_.end())
175  {
176  __SS__ << "Could not find: " << app->getApplicationDescriptor()->getLocalId() << std::endl;
177  __SS_THROW__;
178  }
179  return it->second;
180 }
181 
182 //========================================================================================================================
183 void AllSupervisorInfo::setSupervisorStatus(xdaq::Application* app,
184  const std::string& status)
185 {
186  setSupervisorStatus(app->getApplicationDescriptor()->getLocalId(), status);
187 }
188 //========================================================================================================================
189 void AllSupervisorInfo::setSupervisorStatus(const SupervisorInfo& appInfo,
190  const std::string& status)
191 {
192  setSupervisorStatus(appInfo.getId(), status);
193 }
194 //========================================================================================================================
195 void AllSupervisorInfo::setSupervisorStatus(const unsigned int& id,
196  const std::string& status)
197 {
198  auto it = allSupervisorInfo_.find(id);
199  if(it == allSupervisorInfo_.end())
200  {
201  __SS__ << "Could not find: " << id << std::endl;
202  __SS_THROW__;
203  }
204  it->second.setStatus(status);
205 }
206 
207 //========================================================================================================================
208 const SupervisorInfo& AllSupervisorInfo::getGatewayInfo(void) const
209 {
210  if(!theSupervisorInfo_)
211  {
212  __SS__ << "AllSupervisorInfo was not initialized or no Application of type " <<
213  XDAQContextConfiguration::GATEWAY_SUPERVISOR_CLASS << " found!" << __E__;
214  __SS_THROW__;
215  }
216  return *theSupervisorInfo_;
217 }
218 //========================================================================================================================
219 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* AllSupervisorInfo::getGatewayDescriptor(void) const
220 {
221  return getGatewayInfo().getDescriptor();
222 }
223 
224 //========================================================================================================================
225 const SupervisorInfo& AllSupervisorInfo::getWizardInfo(void) const
226 {
227  if(!theWizardInfo_)
228  {
229  __SS__ << "AllSupervisorInfo was not initialized or no Application of type " <<
230  XDAQContextConfiguration::WIZARD_SUPERVISOR_CLASS << " found!" << __E__;
231  __SS_THROW__;
232  }
233  return *theWizardInfo_;
234 }
235 //========================================================================================================================
236 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* AllSupervisorInfo::getWizardDescriptor(void) const
237 {
238  return getWizardInfo().getDescriptor();
239 }
240 
241 
242 //========================================================================================================================
243 std::vector<const SupervisorInfo*> AllSupervisorInfo::getOrderedSupervisorDescriptors(
244  const std::string& stateMachineCommand) const
245 {
246  __COUT__ << "getOrderedSupervisorDescriptors" << __E__;
247 
248  std::map<uint8_t /*priority*/, std::vector< unsigned int /*appId*/> > orderedByPriority;
249 
250  try
251  {
252  ConfigurationManager cfgMgr;
253  const std::vector<XDAQContextConfiguration::XDAQContext>& contexts =
254  cfgMgr.__GET_CONFIG__(XDAQContextConfiguration)->getContexts();
255 
256  for (const auto& context : contexts)
257  if(context.status_)
258  for (const auto& app : context.applications_)
259  {
260  if(!app.status_) continue; //skip disabled apps
261 
262  auto it = app.stateMachineCommandPriority_.find(stateMachineCommand);
263  if(it == app.stateMachineCommandPriority_.end())
264  orderedByPriority[100].push_back(app.id_);
265  else
266  orderedByPriority[it->second?it->second:100].push_back(app.id_);
267 
268  //__COUT__ << "app.id_ " << app.id_ << __E__;
269  }
270  }
271  catch(...)
272  {
273  __COUT_ERR__ << "SupervisorDescriptorInfoBase could not access the XDAQ Context and Application configuration through the Configuration Context Group." << __E__;
274  throw;
275  }
276 
277 
278  __COUT__ << "Here is the order supervisors will be " << stateMachineCommand << "'d:" << __E__;
279 
280 
281  //return ordered set of supervisor infos
282  // skip over Gateway Supervisor
283  std::vector<const SupervisorInfo*> retVec;
284  for (const auto& priorityAppVector : orderedByPriority)
285  for (const auto& priorityApp : priorityAppVector.second)
286  {
287  auto it = allSupervisorInfo_.find(priorityApp);
288  if(it == allSupervisorInfo_.end())
289  {
290  __SS__ << "Error! Was AllSupervisorInfo properly initialized? The app.id_ " << priorityApp << " priority " <<
291  (unsigned int)priorityAppVector.first << " could not be found in AllSupervisorInfo." << __E__;
292  __SS_THROW__;
293  }
294 
295  //__COUT__ << it->second.getName() << " [" << it->second.getId() << "]: " << " priority? " <<
296  // (unsigned int)priorityAppVector.first << __E__;
297 
298  if(it->second.isGatewaySupervisor()) continue; //skip gateway supervisor
299  if(it->second.isTypeLogbookSupervisor()) continue; //skip logbook supervisor(s)
300  if(it->second.isTypeMacroMakerSupervisor()) continue; //skip macromaker supervisor(s)
301  if(it->second.isTypeConfigurationGUISupervisor()) continue; //skip configurationGUI supervisor(s)
302  if(it->second.isTypeChatSupervisor()) continue; //skip chat supervisor(s)
303  if(it->second.isTypeConsoleSupervisor()) continue; //skip console supervisor(s)
304 
305  retVec.push_back(&(it->second));
306  __COUT__ << it->second.getName() << " [LID=" << it->second.getId() << "]: " << " priority " <<
307  (unsigned int)priorityAppVector.first << __E__;
308  }
309  return retVec;
310 }
311 
312 
313 
314 
315 
316 
317 
318 
319 
320