1 #ifndef _ots_GatewaySupervisor_h
2 #define _ots_GatewaySupervisor_h
4 #include <condition_variable>
7 #include "otsdaq/CoreSupervisors/ConfigurationSupervisorBase.h"
8 #include "otsdaq/CoreSupervisors/CorePropertySupervisorBase.h"
9 #include "otsdaq/FiniteStateMachine/RunControlStateMachine.h"
10 #include "otsdaq/GatewaySupervisor/Iterator.h"
11 #include "otsdaq/SOAPUtilities/SOAPMessenger.h"
12 #include "otsdaq/SupervisorInfo/AllSupervisorInfo.h"
13 #include "otsdaq/WebUsersUtilities/WebUsers.h"
14 #include "otsdaq/WorkLoopManager/WorkLoopManager.h"
16 #include "otsdaq/CodeEditor/CodeEditor.h"
17 #include "otsdaq/TablePlugins/DesktopIconTable.h"
19 #include "otsdaq/NetworkUtilities/TransceiverSocket.h"
21 #pragma GCC diagnostic push
22 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
23 #include <xdaq/Application.h>
24 #pragma GCC diagnostic pop
25 #include "otsdaq/Macros/XDAQApplicationMacros.h"
27 #include <toolbox/task/WorkLoop.h>
28 #include <xdata/String.h>
29 #include <xgi/Method.h>
30 #include "otsdaq/GatewaySupervisor/PixelHistoPicGen.h"
63 class ConfigurationManager;
65 class WorkLoopManager;
81 static const std::string COMMAND_PARAM_LOG_ENTRY_PREAMBLE;
82 static const std::string COMMAND_PARAM_SUBSYSTEM_COMMON_PREAMBLE;
83 static const std::string COMMAND_PARAM_SUBSYSTEM_COMMON_OVERRIDE_PREAMBLE;
84 static const std::string COMMAND_PARAM_ITERATION_INDEX_PREAMBLE;
94 void Default (xgi::Input* in, xgi::Output* out);
96 void loginRequest (xgi::Input* in, xgi::Output* out);
97 void request (xgi::Input* in, xgi::Output* out);
98 void tooltipRequest (xgi::Input* in, xgi::Output* out);
99 void XGI_Turtle (xgi::Input* in, xgi::Output* out);
101 void addStateMachineStatusToXML (
HttpXmlDocument& xmlOut,
const std::string& fsmName,
bool getRunNumber =
true);
102 void addFilteredConfigAliasesToXML (
HttpXmlDocument& xmlOut,
const std::string& fsmName);
103 void addRequiredFsmLogInputToXML (
HttpXmlDocument& xmlOut,
const std::string& fsmName);
106 void stateMachineXgiHandler(xgi::Input* in, xgi::Output* out);
107 void stateMachineIterationBreakpoint(xgi::Input* in, xgi::Output* out);
109 static std::string getIconHeaderString(
void);
110 static bool handleAddDesktopIconRequest(
const std::string& author, cgicc::Cgicc& cgiIn,
HttpXmlDocument& xmlOut, std::vector<DesktopIconTable::DesktopIcon>* newIcons =
nullptr);
111 static void handleGetApplicationIdRequest(
AllSupervisorInfo* applicationInfo, cgicc::Cgicc& cgiIn,
HttpXmlDocument& xmlOut, std::map<std::string , std::map<std::string /* requestUrlHostPort */, std::string /* translatedHostPort */>>* portTranslationMap =
nullptr);
113 xoap::MessageReference stateMachineXoapHandler(xoap::MessageReference msg);
115 bool stateMachineThread(toolbox::task::WorkLoop* workLoop);
118 void statusRequest(xgi::Input* in, xgi::Output* out);
119 void infoRequestResultHandler(xgi::Input* in, xgi::Output* out);
120 bool infoRequestThread(toolbox::task::WorkLoop* workLoop);
123 xoap::MessageReference supervisorCookieCheck(xoap::MessageReference msg);
124 xoap::MessageReference supervisorGetActiveUsers(xoap::MessageReference msg);
125 xoap::MessageReference supervisorSystemMessage(xoap::MessageReference msg);
126 xoap::MessageReference supervisorGetUserInfo(xoap::MessageReference msg);
127 xoap::MessageReference supervisorSystemLogbookEntry(xoap::MessageReference msg);
128 xoap::MessageReference supervisorLastTableGroupRequest(xoap::MessageReference msg);
136 void inError(toolbox::fsm::FiniteStateMachine& fsm)
override;
138 void transitionConfiguring(toolbox::Event::Reference e)
override;
139 void transitionHalting(toolbox::Event::Reference e)
override;
140 void transitionInitializing(toolbox::Event::Reference e)
override;
141 void transitionPausing(toolbox::Event::Reference e)
override;
142 void transitionResuming(toolbox::Event::Reference e)
override;
143 void transitionStarting(toolbox::Event::Reference e)
override;
144 void transitionStopping(toolbox::Event::Reference e)
override;
145 void transitionShuttingDown(toolbox::Event::Reference e)
override;
146 void transitionStartingUp(toolbox::Event::Reference e)
override;
147 void enteringError(toolbox::Event::Reference e)
override;
149 void makeSystemLogEntry(
const std::string& entryText,
const std::string& subjectText =
"");
150 static void addSystemMessage(std::string toUserCSV, std::string message);
152 void checkForAsyncError(
void);
160 unsigned int getNextRunNumber (
const std::string& fsmName =
"");
161 void setNextRunNumber (
unsigned int runNumber,
const std::string& fsmName =
"");
162 std::string getLastLogEntry (
const std::string& logType,
const std::string& fsmName =
"");
163 void setLastLogEntry (
const std::string& logType,
const std::string& logEntry,
const std::string& fsmName =
"");
166 static xoap::MessageReference lastTableGroupRequestHandler (
const SOAPParameters& parameters);
168 static void launchStartOneServerCommand (
const std::string& command,
ConfigurationManager* cfgMgr,
const std::string& contextName);
171 xoap::MessageReference TRACESupervisorRequest (xoap::MessageReference message);
174 static void AppStatusWorkLoop (
GatewaySupervisor* supervisorPtr,
const bool doDisconnected =
false);
177 std::ostringstream* out,
178 const std::string& command,
179 const std::string& fsmName,
180 const std::string& fsmWindowName,
181 const std::string& username,
182 const std::vector<std::string>& parameters,
183 std::string logEntry =
"");
184 void broadcastMessage (xoap::MessageReference msg);
185 void broadcastMessageToRemoteGateways (
const xoap::MessageReference msg,
unsigned int iteration = 0);
186 void broadcastMessageToRemoteGatewaysComplete (
const xoap::MessageReference msg,
unsigned int iterationIndex = 0);
187 void signalAndWaitForBroadcastThreads (
unsigned int numberOfThreads);
189 struct BroadcastMessageIterationsDoneStruct
195 ~BroadcastMessageIterationsDoneStruct()
197 for (
auto& arr : iterationsDone_)
199 iterationsDone_.clear();
203 void push(
const unsigned int& size)
205 iterationsDone_.push_back(
new bool[size]);
206 arraySizes_.push_back(size);
209 for (
unsigned int i = 0; i < size; ++i)
210 iterationsDone_[iterationsDone_.size() - 1][i] =
false;
213 bool* operator[](
unsigned int i) {
return iterationsDone_[i]; }
214 const bool* operator[](
unsigned int i)
const {
return iterationsDone_[i]; }
215 unsigned int size(
unsigned int i = -1)
217 if (i == (
unsigned int)-1)
218 return iterationsDone_.size();
219 return arraySizes_[i];
223 std::vector<bool*> iterationsDone_;
224 std::vector<unsigned int> arraySizes_;
227 struct BroadcastThreadStruct
230 BroadcastThreadStruct()
240 BroadcastThreadStruct(BroadcastThreadStruct &&b)
241 : threadIndex_(b.threadIndex_)
242 , exitThread_(b.exitThread_.load())
243 , working_(b.working_.load())
244 , workToDo_(b.workToDo_.load())
245 , error_(b.error_.load())
254 xoap::MessageReference message,
255 const std::string& command,
256 const unsigned int& iteration,
257 bool& iterationsDone,
258 std::shared_ptr<BroadcastMessageIterationsDoneStruct> iterationsDoneOwner)
262 , iteration_(iteration)
263 , iterationsDone_(iterationsDone)
264 , iterationsDoneOwner_(iterationsDoneOwner)
269 xoap::MessageReference message_;
270 const std::string command_;
271 const unsigned int iteration_;
272 bool& iterationsDone_;
276 std::shared_ptr<BroadcastMessageIterationsDoneStruct> iterationsDoneOwner_;
283 xoap::MessageReference message,
284 const std::string& command,
285 const unsigned int& iteration,
286 bool& iterationsDone,
287 std::shared_ptr<BroadcastMessageIterationsDoneStruct> iterationsDoneOwner)
291 appInfo, message, command, iteration, iterationsDone, iterationsDoneOwner));
295 const SupervisorInfo& getAppInfo() {
return messages_[0].appInfo_; }
296 xoap::MessageReference getMessage() {
return messages_[0].message_; }
297 const std::string& getCommand() {
return messages_[0].command_; }
298 const unsigned int& getIteration() {
return messages_[0].iteration_; }
299 std::string& getReply() {
return messages_[0].reply_; }
300 bool& getIterationsDone() {
return messages_[0].iterationsDone_; }
303 std::mutex threadMutex_;
304 unsigned int threadIndex_;
305 std::atomic<bool> exitThread_, working_, workToDo_, error_;
307 std::vector<BroadcastThreadStruct::BroadcastMessageStruct> messages_;
310 static void broadcastMessageThread(
312 std::shared_ptr<GatewaySupervisor::BroadcastThreadStruct> threadStruct);
313 bool handleBroadcastMessageTarget(
const SupervisorInfo& appInfo,
314 xoap::MessageReference message,
315 const std::string& command,
316 const unsigned int& iteration,
318 unsigned int threadIndex = 0,
319 const std::atomic<bool>* exitFlag =
nullptr);
324 bool supervisorGuiHasBeenLoaded_;
325 static WebUsers theWebUsers_;
326 std::map<std::string , std::map<std::string ,
330 WorkLoopManager stateMachineWorkLoopManager_;
331 toolbox::BSem stateMachineSemaphore_;
333 std::string activeStateMachineName_;
334 std::string activeStateMachineWindowName_;
335 std::string activeStateMachineDumpFormatOnRun_, activeStateMachineDumpFormatOnConfigure_;
336 std::string activeStateMachineSystemDumpOnRun_, activeStateMachineSystemDumpOnConfigure_;
337 bool activeStateMachineSystemDumpOnRunEnable_, activeStateMachineSystemDumpOnConfigureEnable_;
338 std::string activeStateMachineSystemDumpOnRunFilename_, activeStateMachineSystemDumpOnConfigureFilename_;
339 bool activeStateMachineRequireUserLogOnRun_, activeStateMachineRequireUserLogOnConfigure_;
340 std::string activeStateMachineRunInfoPluginType_;
341 std::map<std::string , std::string >
342 stateMachineConfigureLogEntry_, stateMachineStartLogEntry_, stateMachineStopLogEntry_;
343 std::string activeStateMachineRunNumber_, activeStateMachineRunAlias_, activeStateMachineConfigurationAlias_;
344 bool activeStateMachineRollOverLogOnConfigure_, activeStateMachineRollOverLogOnStart_;
345 std::chrono::steady_clock::time_point
346 activeStateMachineRunStartTime;
347 int activeStateMachineRunDuration_ms;
348 unsigned int activeStateMachineConfigureConditionID_, activeStateMachineRunConditionID_;
349 std::string activeStateMachineSubsystemCommonList_, activeStateMachineSubsystemCommonOverrideList_;
351 std::mutex systemStatusMutex_;
352 std::string lastLogbookEntry_;
353 time_t lastLogbookEntryTime_ = 0;
355 std::string lastConsoleErr_, lastConsoleWarn_, lastConsoleInfo_, lastConsoleErrTime_, lastConsoleWarnTime_, lastConsoleInfoTime_;
356 std::string firstConsoleErr_, firstConsoleWarn_, firstConsoleInfo_, firstConsoleErrTime_, firstConsoleWarnTime_, firstConsoleInfoTime_;
357 size_t systemConsoleErrCount_ = 0, systemConsoleWarnCount_ = 0, systemConsoleInfoCount_ = 0;
359 std::pair<std::string , TableGroupKey>
360 theConfigurationTableGroup_;
361 std::string stateMachineTransitionUsername_;
363 Iterator theIterator_;
364 std::mutex stateMachineAccessMutex_;
366 std::string stateMachineLastCommandInput_;
372 CodeEditor codeEditor_;
374 std::mutex broadcastCommandMessageIndexMutex_;
375 unsigned int broadcastCommandMessageIndex_;
376 std::atomic<bool> broadcastIterationsDone_{
true};
377 std::mutex broadcastIterationBreakpointMutex_;
378 unsigned int broadcastIterationBreakpoint_;
380 std::mutex broadcastCommandStatusUpdateMutex_;
381 std::string broadcastCommandStatus_;
383 std::mutex remoteIterationMutex_;
384 std::condition_variable remoteIterationCV_;
385 unsigned int remoteIterationIndex_ = 0;
386 std::atomic<bool> isRemoteSubsystemIteration_{
false};
388 static std::vector<std::shared_ptr<GatewaySupervisor::BroadcastThreadStruct>> broadcastThreadStructs_;
390 std::string securityType_;
394 unsigned int conditionID_;
411 time_t errorTimestamp = 0;
413 void setError(
const std::string& err) { error = err; errorTimestamp = time(0); }
414 void clearError() { error =
""; errorTimestamp = 0; }
415 void copyError(
const RemoteGatewayInfo& r) { error = r.error; errorTimestamp = r.errorTimestamp; }
416 const std::string& getError()
const {
return error; }
419 std::string config_dump;
424 size_t consoleErrCount = 0, consoleWarnCount = 0;
426 std::string fullName;
434 std::string usernameWithLock;
443 bool fsm_included =
true;
445 std::string getFsmMode()
const {
448 case FSM_ModeTypes::Follow_FSM:
return "Follow FSM";
451 default:
return "Impossible";
455 std::string getConfigDumpType()
const {
456 switch(config_dump_type)
458 case ConfigDumpTypes::Text:
return "Text";
459 case ConfigDumpTypes::JSON_all:
return "JSON all";
460 default:
return "Unknown";
464 std::map<std::string, SupervisorInfo::SubappInfo>
subapps;
468 std::vector<GatewaySupervisor::RemoteGatewayInfo> remoteGatewayApps_;
469 std::mutex remoteGatewayAppsMutex_;
470 std::map<std::string ,
471 bool > appLastStatusGood_;
472 std::mutex dualStatusThreadMutex_;
480 std::mutex latestGatewayIconsMutex_;
489 static void CheckRemoteGatewayStatus (
GatewaySupervisor::RemoteGatewayInfo& remoteGatewayApp,
const std::unique_ptr<TransceiverSocket>& remoteGatewaySocket,
const std::string& ipForReverseLoginOverUDP,
int portForReverseLoginOverUDP);
492 void loadRemoteGatewaySettings (std::vector<GatewaySupervisor::RemoteGatewayInfo>& remoteGateways,
bool onlyNotFound =
false)
const;
493 void saveRemoteGatewaySettings (
void)
const;
494 static std::string translateURLForRequestOrigin (
const std::string& url,
const std::string& requestOrigin, std::map<std::string , std::map<std::string /* requestUrlHostPort */, std::string /* translatedHostPort */>>& portTranslationMap);
495 static std::string translateRemoteIconStringForRequestOrigin (
const std::string& iconString,
const std::string& requestOrigin, std::map<std::string , std::map<std::string /* requestUrlHostPort */, std::string /* translatedHostPort */>>& portTranslationMap);
friend class GatewaySupervisor
for access to indicateOtsAlive()
std::string latestGatewayRemoteIconsString_
cached string of remote gateway icons for quick access
virtual void setSupervisorPropertyDefaults(void) override
override to control supervisor specific defaults
void stateHalted(toolbox::fsm::FiniteStateMachine &fsm) override
virtual void forceSupervisorPropertyValues(void) override
override to force supervisor property values (and ignore user settings)
std::vector< DesktopIconTable::DesktopIcon > latestGatewayIcons_
used to track the latest desktop icons (which are defined by the active context but allowed to change...
void statePaused(toolbox::fsm::FiniteStateMachine &fsm) override
std::string ipAddressForStateChangesOverUDP_
IP used for UDP reverse-login propagation to remote gateways.
void stateRunning(toolbox::fsm::FiniteStateMachine &fsm) override
int portForReverseLoginOverUDP_
UDP port for reverse-login; 0 = disabled.
std::map< unsigned int, SupervisorInfo > localAllSupervisorInfo_
only use in main thread, stable copy of app status
std::pair< std::string, TableGroupKey > latestGatewayIconsContextGroup_
used to track the table group key for the latest desktop icons
void stateConfigured(toolbox::fsm::FiniteStateMachine &fsm) override
void stateInitial(toolbox::fsm::FiniteStateMachine &fsm) override
std::pair< std::string, TableGroupKey > latestGatewayRemoteIconsContextGroup_
used to track the table group key for the latest remote desktop icons
defines used also by OtsConfigurationWizardSupervisor
ConfigDumpTypes
<FSM Modes: 'Follow FSM,' 'Do not Halt' (artdaq), or 'Only Configure' (DCS/DQM)
FSM_ModeTypes
<FSM Modes: 'Follow FSM,' 'Do not Halt' (artdaq), or 'Only Configure' (DCS/DQM)
@ OnlyConfigure
(e.g. for DCS/DQM)
@ DoNotHalt
(e.g. for artdaq)
std::string permissionThresholdString
used for desktop icons
std::set< std::string > config_aliases
used for remote gateway subapp control
std::string selected_config_alias
used for remote gateway subapp control
FSM_ModeTypes fsm_mode
used for remote gateway subapp control
std::string fsmName
when not "", need to send
bool iterationsDone
tracks per-gateway iteration completion during FSM transitions
std::map< std::string, SupervisorInfo::SubappInfo > subapps
remote gateways can have subapps
size_t ignoreStatusCount
if non-zero, do not ask for status
std::string instanceUser
used for remote ots instance ssh launch
std::string user_data_path_record
used for remote gateway subapp control
static std::string getTimestampString(const std::string &linuxTimeInSeconds)