1 #ifndef _ots_Iterator_h
2 #define _ots_Iterator_h
5 #include "otsdaq-core/ConfigurationPluginDataFormats/IterateConfiguration.h"
6 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h"
10 #include "otsdaq-core/ConfigurationInterface/ConfigurationManagerRW.h"
15 class GatewaySupervisor;
16 class ConfigurationManagerRW;
26 void playIterationPlan (
HttpXmlDocument& xmldoc,
const std::string& planName);
31 bool handleCommandRequest (
HttpXmlDocument& xmldoc,
const std::string& command,
const std::string& parameter);
36 struct IteratorWorkLoopStruct
39 :theIterator_ (iterator)
41 ,originalTrackChanges_ (
false)
44 ,doPauseAction_ (
false)
45 ,doHaltAction_ (
false)
46 ,doResumeAction_ (
false)
47 ,commandIndex_ ((
unsigned int)-1)
52 bool originalTrackChanges_;
53 std::string originalConfigGroup_;
56 bool running_, commandBusy_;
57 bool doPauseAction_, doHaltAction_, doResumeAction_;
59 std::string activePlan_;
60 std::vector<IterateConfiguration::Command> commands_;
61 std::vector<unsigned int> commandIterations_;
62 unsigned int commandIndex_;
63 std::vector<unsigned int> stepIndexStack_;
66 std::string fsmName_, fsmRunAlias_;
67 unsigned int fsmNextRunNumber_;
70 std::vector<std::string> fsmCommandParameters_;
75 static void IteratorWorkLoop (
Iterator *iterator);
76 static void startCommand (IteratorWorkLoopStruct *iteratorStruct);
77 static bool checkCommand (IteratorWorkLoopStruct *iteratorStruct);
79 static void startCommandChooseFSM (IteratorWorkLoopStruct *iteratorStruct,
const std::string& fsmName);
81 static void startCommandConfigureActive (IteratorWorkLoopStruct *iteratorStruct);
82 static void startCommandConfigureAlias (IteratorWorkLoopStruct *iteratorStruct,
const std::string& systemAlias);
83 static void startCommandConfigureGroup (IteratorWorkLoopStruct *iteratorStruct);
84 static bool checkCommandConfigure (IteratorWorkLoopStruct *iteratorStruct);
86 static void startCommandModifyActive (IteratorWorkLoopStruct *iteratorStruct);
89 static void startCommandBeginLabel (IteratorWorkLoopStruct *iteratorStruct);
90 static void startCommandRepeatLabel (IteratorWorkLoopStruct *iteratorStruct);
92 static void startCommandRun (IteratorWorkLoopStruct *iteratorStruct);
93 static bool checkCommandRun (IteratorWorkLoopStruct *iteratorStruct);
95 static bool haltIterator (
Iterator *iterator, IteratorWorkLoopStruct *iteratorStruct = 0);
97 std::mutex accessMutex_;
98 volatile bool workloopRunning_;
99 volatile bool activePlanIsRunning_;
100 volatile bool iteratorBusy_;
101 volatile bool commandPlay_, commandPause_, commandHalt_;
102 std::string activePlanName_, lastStartedPlanName_, lastFinishedPlanName_;
103 volatile unsigned int activeCommandIndex_, activeCommandIteration_;
104 std::vector<unsigned int> depthIterationStack_;
105 volatile time_t activeCommandStartTime_;
106 std::string lastFsmName_;
107 std::string errorMessage_;
115 static void helpCommandModifyActive(
116 IteratorWorkLoopStruct *iteratorStruct,
118 bool doTrackGroupChanges)
121 __COUT__ <<
"Set value " << setValue << __E__;
122 __COUT__ <<
"doTrackGroupChanges " << doTrackGroupChanges << __E__;
125 const std::string& pathToField =
126 iteratorStruct->commands_[iteratorStruct->commandIndex_].params_
127 [IterateConfiguration::commandModifyActiveParams_.RelativePathToField_];
129 __COUT__ <<
"pathToField " << pathToField << std::endl;
133 std::stringstream valSS;
136 std::string groupName = cfgMgr->getActiveGroupName();
139 __COUT__ <<
"Active group is '" << groupName <<
" (" <<
140 originalKey <<
")'" << __E__;
148 cfgMgr->loadConfigurationGroup(
155 catch(
const std::runtime_error& e)
157 __SS__ <<
"Failed to load the active configuration group. Is there a valid one activated? " <<
159 throw std::runtime_error(ss.str());
162 for(
const auto& member: memberMap)
163 __COUT__ <<
"newGroup " << member.first <<
"-v" << member.second << __E__;
165 for(
const auto& target:
166 iteratorStruct->commands_[iteratorStruct->commandIndex_].targets_)
168 __COUT__ <<
"target " << target.table_ <<
":" << target.UID_ << __E__;
170 target.table_).getNode(
171 target.UID_).getNode(
174 if(!node.isValueNode())
176 __SS__ <<
"Invalid target node. The target path '" << pathToField <<
177 "' must be a value node." << __E__;
178 throw std::runtime_error(ss.str());
181 __COUT__ <<
"value node table: " << node.getConfigurationName() << __E__;
182 __COUT__ <<
"value node UID: " << node.getUIDAsString() << __E__;
184 __COUT__ <<
"value node row: " << node.getFieldRow() << __E__;
185 __COUT__ <<
"value node column: " << node.getFieldColumn() << __E__;
193 valueTable.cfgView_->setValueAsString(
196 node.getFieldColumn()
200 valueTable.cfgView_->print();
201 valueTable.cfgView_->init();
205 __COUT__ <<
"Handling command table errors while saving. Erasing all newly created versions." << std::endl;
207 if(valueTable.createdTemporaryVersion_)
209 __COUT__ <<
"Erasing temporary version " << valueTable.configName_ <<
210 "-v" << valueTable.temporaryVersion_ << std::endl;
212 cfgMgr->eraseTemporaryVersion(valueTable.configName_,valueTable.temporaryVersion_);
225 const std::string& configName = valueTable.configName_;
231 if(doTrackGroupChanges)
233 __COUT__ <<
"Checking for duplicate tables..." << std::endl;
239 const std::map<std::string, ConfigurationInfo>& allCfgInfo =
240 cfgMgr->getAllConfigurationInfo();
242 auto versionReverseIterator = allCfgInfo.at(configName).versions_.rbegin();
243 __COUT__ <<
"Filling up cached from " <<
244 config->getNumberOfStoredViews() <<
245 " to max count of " << config->MAX_VIEWS_IN_CACHE << std::endl;
246 for(;config->getNumberOfStoredViews() < config->MAX_VIEWS_IN_CACHE &&
247 versionReverseIterator != allCfgInfo.at(configName).versions_.rend();++versionReverseIterator)
249 __COUT__ <<
"Versions in reverse order " << *versionReverseIterator << std::endl;
250 cfgMgr->getVersionedConfigurationByName(configName,*versionReverseIterator);
255 newAssignedVersion = config->checkForDuplicate(temporaryModifiedVersion);
257 __COUT__ <<
"Check for duplicate tables complete." << std::endl;
261 if(!newAssignedVersion.isInvalid())
264 __COUT__ <<
"Equivalent table found in version v" << newAssignedVersion << std::endl;
267 cfgMgr->eraseTemporaryVersion(configName,temporaryModifiedVersion);
273 __COUT__ <<
"\t\t**************************** Save v" <<
274 temporaryModifiedVersion <<
" as new table version" << std::endl;
277 cfgMgr->saveNewConfiguration(configName,temporaryModifiedVersion,
282 __COUT__ <<
"\t\t newAssignedVersion: " << newAssignedVersion << std::endl;
288 __COUT__ <<
"Final plan version is " << valueTable.configName_ <<
"-v" <<
289 newAssignedVersion << std::endl;
292 memberMap[valueTable.configName_] = newAssignedVersion;
297 for(
const auto& member: memberMap)
298 __COUT__ <<
"newGroup " << member.first <<
"-v" << member.second << __E__;
300 __COUT__ <<
"Checking for duplicate groups..." << std::endl;
302 cfgMgr->findConfigurationGroup(groupName,memberMap);
304 if(!theKey.isInvalid())
306 __COUT__ <<
"Found equivalent group key (" << theKey <<
") for " <<
307 groupName <<
"." << std::endl;
311 __COUT__ <<
"Saving new group..." << __E__;
314 theKey = cfgMgr->saveNewConfigurationGroup(
317 "Created by Iterator modify active configuration command.");
320 __COUT__ <<
"Final group key is " << groupName <<
"(" <<
321 theKey <<
")" << std::endl;
324 __COUT__ <<
"Activating new group..." << __E__;
325 cfgMgr->activateConfigurationGroup(groupName,theKey);
328 catch(
const std::runtime_error& e)
330 __SS__ <<
"Modify command failed! " << e.what() << __E__;
333 throw std::runtime_error(ss.str());