00001 #include "otsdaq-core/SupervisorInfo/AllSupervisorInfo.h"
00002
00003 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00004 #include "otsdaq-core/Macros/CoutMacros.h"
00005
00006 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
00007 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
00008
00009
00010 #include <iostream>
00011
00012
00013
00014 using namespace ots;
00015
00016
00017
00018
00019 AllSupervisorInfo::AllSupervisorInfo(void)
00020 : theSupervisorInfo_ (0)
00021 , theWizardInfo_ (0)
00022 {
00023 }
00024
00025
00026 AllSupervisorInfo::AllSupervisorInfo(xdaq::ApplicationContext* applicationContext)
00027 : AllSupervisorInfo()
00028 {
00029 init(applicationContext);
00030 }
00031
00032
00033 AllSupervisorInfo::~AllSupervisorInfo(void)
00034 {
00035 destroy();
00036 }
00037
00038
00039 void AllSupervisorInfo::destroy(void)
00040 {
00041 allSupervisorInfo_.clear();
00042 allFETypeSupervisorInfo_.clear();
00043 allDMTypeSupervisorInfo_.clear();
00044
00045 theSupervisorInfo_ = 0;
00046 theWizardInfo_ = 0;
00047
00048 SupervisorDescriptorInfoBase::destroy();
00049 }
00050
00051
00052 void AllSupervisorInfo::init(xdaq::ApplicationContext* applicationContext)
00053 {
00054 __COUT__ << "Initializing info based on XDAQ context..." << __E__;
00055
00056 AllSupervisorInfo::destroy();
00057 SupervisorDescriptorInfoBase::init(applicationContext);
00058
00059 auto allDescriptors = SupervisorDescriptorInfoBase::getAllDescriptors();
00060
00061
00062
00063
00064
00065
00066 bool isWizardMode = false;
00067
00068
00069
00070 for(const auto& descriptor:allDescriptors)
00071 {
00072 SupervisorInfo tempSupervisorInfo(
00073 descriptor.second ,
00074 "" ,""
00075 );
00076
00077
00078
00079 if(tempSupervisorInfo.isGatewaySupervisor())
00080 {
00081
00082 isWizardMode = false;
00083 break;
00084 }
00085 else if(tempSupervisorInfo.isWizardSupervisor())
00086 {
00087
00088 isWizardMode = true;
00089 break;
00090 }
00091 }
00092
00093
00094
00095 if(isWizardMode)
00096 {
00097 __COUT__ << "Initializing info for Wiz mode XDAQ context..." << __E__;
00098
00099
00100
00101
00102 for(const auto& descriptor:allDescriptors)
00103 {
00104 auto emplacePair = allSupervisorInfo_.emplace(std::pair<unsigned int, SupervisorInfo>(
00105 descriptor.second->getLocalId(),
00106 SupervisorInfo(
00107 descriptor.second ,
00108 "" ,""
00109 )));
00110 if(!emplacePair.second)
00111 {
00112 __SS__ << "Error! Duplicate Application IDs are not allowed. ID =" <<
00113 descriptor.second->getLocalId() << __E__;
00114 __SS_THROW__;
00115 }
00116
00118
00119
00120
00121
00122 if(emplacePair.first->second.isGatewaySupervisor())
00123 {
00124 if(theSupervisorInfo_)
00125 {
00126 __SS__ << "Error! Multiple Gateway Supervisors of class " << XDAQContextConfiguration::GATEWAY_SUPERVISOR_CLASS <<
00127 " found. There can only be one. ID =" <<
00128 descriptor.second->getLocalId() << __E__;
00129 __SS_THROW__;
00130 }
00131
00132 theSupervisorInfo_ = &(emplacePair.first->second);
00133 continue;
00134 }
00135
00136
00137
00138 if(emplacePair.first->second.isWizardSupervisor())
00139 {
00140 if(theWizardInfo_)
00141 {
00142 __SS__ << "Error! Multiple Wizard Supervisors of class " << XDAQContextConfiguration::WIZARD_SUPERVISOR_CLASS <<
00143 " found. There can only be one. ID =" <<
00144 descriptor.second->getLocalId() << __E__;
00145 __SS_THROW__;
00146 }
00147
00148 theWizardInfo_ = &(emplacePair.first->second);
00149 continue;
00150 }
00151
00152
00153
00154
00155 if(emplacePair.first->second.isTypeFESupervisor())
00156 {
00157 allFETypeSupervisorInfo_.emplace(std::pair<unsigned int, const SupervisorInfo&>(
00158 emplacePair.first->second.getId(),
00159 emplacePair.first->second));
00160 }
00161
00162
00163
00164 if(emplacePair.first->second.isTypeDMSupervisor())
00165 {
00166 allDMTypeSupervisorInfo_.emplace(std::pair<unsigned int, const SupervisorInfo&>(
00167 emplacePair.first->second.getId(),
00168 emplacePair.first->second));
00169 }
00170
00171
00172
00173 if(emplacePair.first->second.isTypeLogbookSupervisor())
00174 {
00175 allLogbookTypeSupervisorInfo_.emplace(std::pair<unsigned int, const SupervisorInfo&>(
00176 emplacePair.first->second.getId(),
00177 emplacePair.first->second));
00178 }
00179
00180 }
00181
00182 }
00183 else
00184 {
00185 __COUT__ << "Initializing info for Normal mode XDAQ context..." << __E__;
00186
00187
00188 ConfigurationManager cfgMgr;
00189 const XDAQContextConfiguration* contextConfig =
00190 cfgMgr.__GET_CONFIG__(XDAQContextConfiguration);
00191
00192
00193
00194 auto allDescriptors = SupervisorDescriptorInfoBase::getAllDescriptors();
00195 for(const auto& descriptor:allDescriptors)
00196 {
00197 auto emplacePair = allSupervisorInfo_.emplace(std::pair<unsigned int, SupervisorInfo>(
00198 descriptor.second->getLocalId(),
00199 SupervisorInfo(
00200 descriptor.second ,
00201 contextConfig->getApplicationUID
00202 (
00203 descriptor.second->getContextDescriptor()->getURL(),
00204 descriptor.second->getLocalId()
00205 ) ,
00206 contextConfig->getContextUID(
00207 descriptor.second->getContextDescriptor()->getURL())
00208 )));
00209 if(!emplacePair.second)
00210 {
00211 __SS__ << "Error! Duplicate Application IDs are not allowed. ID =" <<
00212 descriptor.second->getLocalId() << __E__;
00213 __SS_THROW__;
00214 }
00215
00217
00218
00219
00220
00221 if(emplacePair.first->second.isGatewaySupervisor())
00222 {
00223 if(theSupervisorInfo_)
00224 {
00225 __SS__ << "Error! Multiple Gateway Supervisors of class " << XDAQContextConfiguration::GATEWAY_SUPERVISOR_CLASS <<
00226 " found. There can only be one. ID =" <<
00227 descriptor.second->getLocalId() << __E__;
00228 __SS_THROW__;
00229 }
00230
00231 theSupervisorInfo_ = &(emplacePair.first->second);
00232 continue;
00233 }
00234
00235
00236
00237 if(emplacePair.first->second.isWizardSupervisor())
00238 {
00239 if(theWizardInfo_)
00240 {
00241 __SS__ << "Error! Multiple Wizard Supervisors of class " << XDAQContextConfiguration::WIZARD_SUPERVISOR_CLASS <<
00242 " found. There can only be one. ID =" <<
00243 descriptor.second->getLocalId() << __E__;
00244 __SS_THROW__;
00245 }
00246
00247 theWizardInfo_ = &(emplacePair.first->second);
00248 continue;
00249 }
00250
00251
00252
00253
00254 if(emplacePair.first->second.isTypeFESupervisor())
00255 {
00256 allFETypeSupervisorInfo_.emplace(std::pair<unsigned int, const SupervisorInfo&>(
00257 emplacePair.first->second.getId(),
00258 emplacePair.first->second));
00259 }
00260
00261
00262
00263 if(emplacePair.first->second.isTypeDMSupervisor())
00264 {
00265 allDMTypeSupervisorInfo_.emplace(std::pair<unsigned int, const SupervisorInfo&>(
00266 emplacePair.first->second.getId(),
00267 emplacePair.first->second));
00268 }
00269
00270
00271
00272 if(emplacePair.first->second.isTypeLogbookSupervisor())
00273 {
00274 allLogbookTypeSupervisorInfo_.emplace(std::pair<unsigned int, const SupervisorInfo&>(
00275 emplacePair.first->second.getId(),
00276 emplacePair.first->second));
00277 }
00278
00279 }
00280 }
00281
00282
00283 if((!theWizardInfo_ && !theSupervisorInfo_) ||
00284 (theWizardInfo_ && theSupervisorInfo_))
00285 {
00286 __SS__ << "Error! Must have one " << XDAQContextConfiguration::GATEWAY_SUPERVISOR_CLASS <<
00287 " OR one " << XDAQContextConfiguration::WIZARD_SUPERVISOR_CLASS <<
00288 " as part of the context configuration! " <<
00289 "Neither were found." << __E__;
00290 __SS_THROW__;
00291 }
00292
00293
00294 SupervisorDescriptorInfoBase::destroy();
00295
00296 __COUT__ << "Init complete" << __E__;
00297
00298
00299
00300 }
00301
00302
00303 const SupervisorInfo& AllSupervisorInfo::getSupervisorInfo(xdaq::Application* app) const
00304 {
00305 auto it = allSupervisorInfo_.find(app->getApplicationDescriptor()->getLocalId());
00306 if(it == allSupervisorInfo_.end())
00307 {
00308 __SS__ << "Could not find: " << app->getApplicationDescriptor()->getLocalId() << std::endl;
00309 __SS_THROW__;
00310 }
00311 return it->second;
00312 }
00313
00314
00315 void AllSupervisorInfo::setSupervisorStatus(xdaq::Application* app,
00316 const std::string& status)
00317 {
00318 setSupervisorStatus(app->getApplicationDescriptor()->getLocalId(), status);
00319 }
00320
00321 void AllSupervisorInfo::setSupervisorStatus(const SupervisorInfo& appInfo,
00322 const std::string& status)
00323 {
00324 setSupervisorStatus(appInfo.getId(), status);
00325 }
00326
00327 void AllSupervisorInfo::setSupervisorStatus(const unsigned int& id,
00328 const std::string& status)
00329 {
00330 auto it = allSupervisorInfo_.find(id);
00331 if(it == allSupervisorInfo_.end())
00332 {
00333 __SS__ << "Could not find: " << id << std::endl;
00334 __SS_THROW__;
00335 }
00336 it->second.setStatus(status);
00337 }
00338
00339
00340 const SupervisorInfo& AllSupervisorInfo::getGatewayInfo(void) const
00341 {
00342 if(!theSupervisorInfo_)
00343 {
00344 __SS__ << "AllSupervisorInfo was not initialized or no Application of type " <<
00345 XDAQContextConfiguration::GATEWAY_SUPERVISOR_CLASS << " found!" << __E__;
00346 __SS_THROW__;
00347 }
00348 return *theSupervisorInfo_;
00349 }
00350
00351 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* AllSupervisorInfo::getGatewayDescriptor(void) const
00352 {
00353 return getGatewayInfo().getDescriptor();
00354 }
00355
00356
00357 const SupervisorInfo& AllSupervisorInfo::getWizardInfo(void) const
00358 {
00359 if(!theWizardInfo_)
00360 {
00361 __SS__ << "AllSupervisorInfo was not initialized or no Application of type " <<
00362 XDAQContextConfiguration::WIZARD_SUPERVISOR_CLASS << " found!" << __E__;
00363 __SS_THROW__;
00364 }
00365 return *theWizardInfo_;
00366 }
00367
00368 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* AllSupervisorInfo::getWizardDescriptor(void) const
00369 {
00370 return getWizardInfo().getDescriptor();
00371 }
00372
00373
00374
00375 std::vector<const SupervisorInfo*> AllSupervisorInfo::getOrderedSupervisorDescriptors(
00376 const std::string& stateMachineCommand) const
00377 {
00378 __COUT__ << "getOrderedSupervisorDescriptors" << __E__;
00379
00380 std::map<uint64_t , std::vector< unsigned int > > orderedByPriority;
00381
00382 try
00383 {
00384 ConfigurationManager cfgMgr;
00385 const std::vector<XDAQContextConfiguration::XDAQContext>& contexts =
00386 cfgMgr.__GET_CONFIG__(XDAQContextConfiguration)->getContexts();
00387
00388 for (const auto& context : contexts)
00389 if(context.status_)
00390 for (const auto& app : context.applications_)
00391 {
00392 if(!app.status_) continue;
00393
00394 auto it = app.stateMachineCommandPriority_.find(stateMachineCommand);
00395 if(it == app.stateMachineCommandPriority_.end())
00396 orderedByPriority[100].push_back(app.id_);
00397 else
00398 orderedByPriority[it->second?it->second:100].push_back(app.id_);
00399
00400
00401 }
00402 }
00403 catch(...)
00404 {
00405 __COUT_ERR__ << "SupervisorDescriptorInfoBase could not access the XDAQ Context and Application configuration through the Configuration Context Group." << __E__;
00406 throw;
00407 }
00408
00409
00410 __COUT__ << "Here is the order supervisors will be " << stateMachineCommand << "'d:" << __E__;
00411
00412
00413
00414
00415 std::vector<const SupervisorInfo*> retVec;
00416 for (const auto& priorityAppVector : orderedByPriority)
00417 for (const auto& priorityApp : priorityAppVector.second)
00418 {
00419 auto it = allSupervisorInfo_.find(priorityApp);
00420 if(it == allSupervisorInfo_.end())
00421 {
00422 __SS__ << "Error! Was AllSupervisorInfo properly initialized? The app.id_ " << priorityApp << " priority " <<
00423 (unsigned int)priorityAppVector.first << " could not be found in AllSupervisorInfo." << __E__;
00424 __SS_THROW__;
00425 }
00426
00427
00428
00429
00430 if(it->second.isGatewaySupervisor()) continue;
00431 if(it->second.isTypeLogbookSupervisor()) continue;
00432 if(it->second.isTypeMacroMakerSupervisor()) continue;
00433 if(it->second.isTypeConfigurationGUISupervisor()) continue;
00434 if(it->second.isTypeChatSupervisor()) continue;
00435 if(it->second.isTypeConsoleSupervisor()) continue;
00436
00437 retVec.push_back(&(it->second));
00438 __COUT__ << it->second.getName() << " [LID=" << it->second.getId() << "]: " << " priority " <<
00439 (unsigned int)priorityAppVector.first << __E__;
00440 }
00441 return retVec;
00442 }
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452