1 #ifndef _ots_GatewaySupervisor_h
2 #define _ots_GatewaySupervisor_h
5 #include "otsdaq/CoreSupervisors/ConfigurationSupervisorBase.h"
6 #include "otsdaq/CoreSupervisors/CorePropertySupervisorBase.h"
7 #include "otsdaq/FiniteStateMachine/RunControlStateMachine.h"
8 #include "otsdaq/GatewaySupervisor/Iterator.h"
9 #include "otsdaq/SOAPUtilities/SOAPMessenger.h"
10 #include "otsdaq/SupervisorInfo/AllSupervisorInfo.h"
11 #include "otsdaq/WebUsersUtilities/WebUsers.h"
12 #include "otsdaq/WorkLoopManager/WorkLoopManager.h"
14 #include "otsdaq/CodeEditor/CodeEditor.h"
15 #include "otsdaq/TablePlugins/DesktopIconTable.h"
17 #include "otsdaq/NetworkUtilities/TransceiverSocket.h"
19 #pragma GCC diagnostic push
20 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
21 #include <xdaq/Application.h>
22 #pragma GCC diagnostic pop
23 #include "otsdaq/Macros/XDAQApplicationMacros.h"
25 #include <toolbox/task/WorkLoop.h>
26 #include <xdata/String.h>
27 #include <xgi/Method.h>
28 #include "otsdaq/GatewaySupervisor/PixelHistoPicGen.h"
61 class ConfigurationManager;
63 class WorkLoopManager;
79 static const std::string COMMAND_PARAM_LOG_ENTRY_PREAMBLE;
80 static const std::string COMMAND_PARAM_SUBSYSTEM_COMMON_PREAMBLE;
81 static const std::string COMMAND_PARAM_SUBSYSTEM_COMMON_OVERRIDE_PREAMBLE;
91 void Default (xgi::Input* in, xgi::Output* out);
93 void loginRequest (xgi::Input* in, xgi::Output* out);
94 void request (xgi::Input* in, xgi::Output* out);
95 void tooltipRequest (xgi::Input* in, xgi::Output* out);
96 void XGI_Turtle (xgi::Input* in, xgi::Output* out);
98 void addStateMachineStatusToXML (
HttpXmlDocument& xmlOut,
const std::string& fsmName,
bool getRunNumber =
true);
99 void addFilteredConfigAliasesToXML (
HttpXmlDocument& xmlOut,
const std::string& fsmName);
100 void addRequiredFsmLogInputToXML (
HttpXmlDocument& xmlOut,
const std::string& fsmName);
103 void stateMachineXgiHandler(xgi::Input* in, xgi::Output* out);
104 void stateMachineIterationBreakpoint(xgi::Input* in, xgi::Output* out);
106 static std::string getIconHeaderString(
void);
107 static bool handleAddDesktopIconRequest(
const std::string& author, cgicc::Cgicc& cgiIn,
HttpXmlDocument& xmlOut, std::vector<DesktopIconTable::DesktopIcon>* newIcons =
nullptr);
108 static void handleGetApplicationIdRequest(
AllSupervisorInfo* applicationInfo, cgicc::Cgicc& cgiIn,
HttpXmlDocument& xmlOut, std::map<std::string , std::map<std::string /* requestUrlHostPort */, std::string /* translatedHostPort */>>* portTranslationMap =
nullptr);
110 xoap::MessageReference stateMachineXoapHandler(xoap::MessageReference msg);
112 bool stateMachineThread(toolbox::task::WorkLoop* workLoop);
115 void statusRequest(xgi::Input* in, xgi::Output* out);
116 void infoRequestResultHandler(xgi::Input* in, xgi::Output* out);
117 bool infoRequestThread(toolbox::task::WorkLoop* workLoop);
120 xoap::MessageReference supervisorCookieCheck(xoap::MessageReference msg);
121 xoap::MessageReference supervisorGetActiveUsers(xoap::MessageReference msg);
122 xoap::MessageReference supervisorSystemMessage(xoap::MessageReference msg);
123 xoap::MessageReference supervisorGetUserInfo(xoap::MessageReference msg);
124 xoap::MessageReference supervisorSystemLogbookEntry(xoap::MessageReference msg);
125 xoap::MessageReference supervisorLastTableGroupRequest(xoap::MessageReference msg);
133 void inError(toolbox::fsm::FiniteStateMachine& fsm)
override;
135 void transitionConfiguring(toolbox::Event::Reference e)
override;
136 void transitionHalting(toolbox::Event::Reference e)
override;
137 void transitionInitializing(toolbox::Event::Reference e)
override;
138 void transitionPausing(toolbox::Event::Reference e)
override;
139 void transitionResuming(toolbox::Event::Reference e)
override;
140 void transitionStarting(toolbox::Event::Reference e)
override;
141 void transitionStopping(toolbox::Event::Reference e)
override;
142 void transitionShuttingDown(toolbox::Event::Reference e)
override;
143 void transitionStartingUp(toolbox::Event::Reference e)
override;
144 void enteringError(toolbox::Event::Reference e)
override;
146 void makeSystemLogEntry(
const std::string& entryText,
const std::string& subjectText =
"");
147 static void addSystemMessage(std::string toUserCSV, std::string message);
149 void checkForAsyncError(
void);
157 unsigned int getNextRunNumber (
const std::string& fsmName =
"");
158 void setNextRunNumber (
unsigned int runNumber,
const std::string& fsmName =
"");
159 std::string getLastLogEntry (
const std::string& logType,
const std::string& fsmName =
"");
160 void setLastLogEntry (
const std::string& logType,
const std::string& logEntry,
const std::string& fsmName =
"");
163 static xoap::MessageReference lastTableGroupRequestHandler (
const SOAPParameters& parameters);
165 static void launchStartOneServerCommand (
const std::string& command,
ConfigurationManager* cfgMgr,
const std::string& contextName);
168 xoap::MessageReference TRACESupervisorRequest (xoap::MessageReference message);
171 static void AppStatusWorkLoop (
GatewaySupervisor* supervisorPtr,
const bool doDisconnected =
false);
174 std::ostringstream* out,
175 const std::string& command,
176 const std::string& fsmName,
177 const std::string& fsmWindowName,
178 const std::string& username,
179 const std::vector<std::string>& parameters,
180 std::string logEntry =
"");
181 void broadcastMessage (xoap::MessageReference msg);
182 void broadcastMessageToRemoteGateways (
const xoap::MessageReference msg);
183 void broadcastMessageToRemoteGatewaysComplete (
const xoap::MessageReference msg);
184 void signalAndWaitForBroadcastThreads (
unsigned int numberOfThreads);
186 struct BroadcastMessageIterationsDoneStruct
192 ~BroadcastMessageIterationsDoneStruct()
194 for (
auto& arr : iterationsDone_)
196 iterationsDone_.clear();
200 void push(
const unsigned int& size)
202 iterationsDone_.push_back(
new bool[size]);
203 arraySizes_.push_back(size);
206 for (
unsigned int i = 0; i < size; ++i)
207 iterationsDone_[iterationsDone_.size() - 1][i] =
false;
210 bool* operator[](
unsigned int i) {
return iterationsDone_[i]; }
211 const bool* operator[](
unsigned int i)
const {
return iterationsDone_[i]; }
212 unsigned int size(
unsigned int i = -1)
214 if (i == (
unsigned int)-1)
215 return iterationsDone_.size();
216 return arraySizes_[i];
220 std::vector<bool*> iterationsDone_;
221 std::vector<unsigned int> arraySizes_;
224 struct BroadcastThreadStruct
227 BroadcastThreadStruct()
233 , hardCancelRequested_(false)
234 , hasPthreadId_(false)
239 BroadcastThreadStruct(BroadcastThreadStruct &&b)
240 : threadIndex_(b.threadIndex_)
241 , exitThread_(b.exitThread_.load())
242 , working_(b.working_.load())
243 , workToDo_(b.workToDo_.load())
244 , error_(b.error_.load())
245 , hardCancelRequested_(b.hardCancelRequested_.load())
246 , pthreadId_(b.pthreadId_)
247 , hasPthreadId_(b.hasPthreadId_.load())
256 xoap::MessageReference message,
257 const std::string& command,
258 const unsigned int& iteration,
259 bool& iterationsDone,
260 std::shared_ptr<BroadcastMessageIterationsDoneStruct> iterationsDoneOwner)
264 , iteration_(iteration)
265 , iterationsDone_(iterationsDone)
266 , iterationsDoneOwner_(iterationsDoneOwner)
271 xoap::MessageReference message_;
272 const std::string command_;
273 const unsigned int iteration_;
274 bool& iterationsDone_;
278 std::shared_ptr<BroadcastMessageIterationsDoneStruct> iterationsDoneOwner_;
285 xoap::MessageReference message,
286 const std::string& command,
287 const unsigned int& iteration,
288 bool& iterationsDone,
289 std::shared_ptr<BroadcastMessageIterationsDoneStruct> iterationsDoneOwner)
293 appInfo, message, command, iteration, iterationsDone, iterationsDoneOwner));
297 const SupervisorInfo& getAppInfo() {
return messages_[0].appInfo_; }
298 xoap::MessageReference getMessage() {
return messages_[0].message_; }
299 const std::string& getCommand() {
return messages_[0].command_; }
300 const unsigned int& getIteration() {
return messages_[0].iteration_; }
301 std::string& getReply() {
return messages_[0].reply_; }
302 bool& getIterationsDone() {
return messages_[0].iterationsDone_; }
305 std::mutex threadMutex_;
306 unsigned int threadIndex_;
307 std::atomic<bool> exitThread_, working_, workToDo_, error_;
308 std::atomic<bool> hardCancelRequested_;
309 pthread_t pthreadId_;
310 std::atomic<bool> hasPthreadId_;
312 std::vector<BroadcastThreadStruct::BroadcastMessageStruct> messages_;
315 static void broadcastMessageThread(
317 std::shared_ptr<GatewaySupervisor::BroadcastThreadStruct> threadStruct);
318 bool handleBroadcastMessageTarget(
const SupervisorInfo& appInfo,
319 xoap::MessageReference message,
320 const std::string& command,
321 const unsigned int& iteration,
323 unsigned int threadIndex = 0);
328 bool supervisorGuiHasBeenLoaded_;
329 static WebUsers theWebUsers_;
330 std::map<std::string , std::map<std::string ,
334 WorkLoopManager stateMachineWorkLoopManager_;
335 toolbox::BSem stateMachineSemaphore_;
337 std::string activeStateMachineName_;
338 std::string activeStateMachineWindowName_;
339 std::string activeStateMachineDumpFormatOnRun_, activeStateMachineDumpFormatOnConfigure_;
340 std::string activeStateMachineSystemDumpOnRun_, activeStateMachineSystemDumpOnConfigure_;
341 bool activeStateMachineSystemDumpOnRunEnable_, activeStateMachineSystemDumpOnConfigureEnable_;
342 std::string activeStateMachineSystemDumpOnRunFilename_, activeStateMachineSystemDumpOnConfigureFilename_;
343 bool activeStateMachineRequireUserLogOnRun_, activeStateMachineRequireUserLogOnConfigure_;
344 std::string activeStateMachineRunInfoPluginType_;
345 std::map<std::string , std::string >
346 stateMachineConfigureLogEntry_, stateMachineStartLogEntry_, stateMachineStopLogEntry_;
347 std::string activeStateMachineRunNumber_, activeStateMachineRunAlias_, activeStateMachineConfigurationAlias_;
348 bool activeStateMachineRollOverLogOnConfigure_, activeStateMachineRollOverLogOnStart_;
349 std::chrono::steady_clock::time_point
350 activeStateMachineRunStartTime;
351 int activeStateMachineRunDuration_ms;
352 unsigned int activeStateMachineConfigureConditionID_, activeStateMachineRunConditionID_;
353 std::string activeStateMachineSubsystemCommonList_, activeStateMachineSubsystemCommonOverrideList_;
355 std::mutex systemStatusMutex_;
356 std::string lastLogbookEntry_;
357 time_t lastLogbookEntryTime_ = 0;
359 std::string lastConsoleErr_, lastConsoleWarn_, lastConsoleInfo_, lastConsoleErrTime_, lastConsoleWarnTime_, lastConsoleInfoTime_;
360 std::string firstConsoleErr_, firstConsoleWarn_, firstConsoleInfo_, firstConsoleErrTime_, firstConsoleWarnTime_, firstConsoleInfoTime_;
361 size_t systemConsoleErrCount_ = 0, systemConsoleWarnCount_ = 0, systemConsoleInfoCount_ = 0;
363 std::pair<std::string , TableGroupKey>
364 theConfigurationTableGroup_;
365 std::string stateMachineTransitionUsername_;
367 Iterator theIterator_;
368 std::mutex stateMachineAccessMutex_;
370 std::string stateMachineLastCommandInput_;
376 CodeEditor codeEditor_;
378 std::mutex broadcastCommandMessageIndexMutex_, broadcastIterationsDoneMutex_;
379 unsigned int broadcastCommandMessageIndex_;
380 bool broadcastIterationsDone_;
381 std::mutex broadcastIterationBreakpointMutex_;
382 unsigned int broadcastIterationBreakpoint_;
384 std::mutex broadcastCommandStatusUpdateMutex_;
385 std::string broadcastCommandStatus_;
386 static std::vector<std::shared_ptr<GatewaySupervisor::BroadcastThreadStruct>> broadcastThreadStructs_;
388 std::string securityType_;
392 unsigned int conditionID_;
409 time_t errorTimestamp = 0;
411 void setError(
const std::string& err) { error = err; errorTimestamp = time(0); }
412 void clearError() { error =
""; errorTimestamp = 0; }
413 void copyError(
const RemoteGatewayInfo& r) { error = r.error; errorTimestamp = r.errorTimestamp; }
414 const std::string& getError()
const {
return error; }
417 std::string config_dump;
422 size_t consoleErrCount = 0, consoleWarnCount = 0;
424 std::string fullName;
432 std::string usernameWithLock;
441 bool fsm_included =
true;
443 std::string getFsmMode()
const {
446 case FSM_ModeTypes::Follow_FSM:
return "Follow FSM";
449 default:
return "Impossible";
453 std::string getConfigDumpType()
const {
454 switch(config_dump_type)
456 case ConfigDumpTypes::Text:
return "Text";
457 case ConfigDumpTypes::JSON_all:
return "JSON all";
458 default:
return "Unknown";
462 std::map<std::string, SupervisorInfo::SubappInfo>
subapps;
465 std::vector<GatewaySupervisor::RemoteGatewayInfo> remoteGatewayApps_;
466 std::mutex remoteGatewayAppsMutex_;
467 std::map<std::string ,
468 bool > appLastStatusGood_;
469 std::mutex dualStatusThreadMutex_;
474 std::mutex latestGatewayIconsMutex_;
483 static void CheckRemoteGatewayStatus (
GatewaySupervisor::RemoteGatewayInfo& remoteGatewayApp,
const std::unique_ptr<TransceiverSocket>& remoteGatewaySocket,
const std::string& ipForReverseLoginOverUDP,
int portForReverseLoginOverUDP);
486 void loadRemoteGatewaySettings (std::vector<GatewaySupervisor::RemoteGatewayInfo>& remoteGateways,
bool onlyNotFound =
false)
const;
487 void saveRemoteGatewaySettings (
void)
const;
488 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);
489 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
void stateRunning(toolbox::fsm::FiniteStateMachine &fsm) override
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
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)