1 #ifndef _ots_GatewaySupervisor_h
2 #define _ots_GatewaySupervisor_h
4 #include "otsdaq/CoreSupervisors/ConfigurationSupervisorBase.h"
5 #include "otsdaq/CoreSupervisors/CorePropertySupervisorBase.h"
6 #include "otsdaq/FiniteStateMachine/RunControlStateMachine.h"
7 #include "otsdaq/GatewaySupervisor/Iterator.h"
8 #include "otsdaq/SOAPUtilities/SOAPMessenger.h"
9 #include "otsdaq/SupervisorInfo/AllSupervisorInfo.h"
10 #include "otsdaq/WebUsersUtilities/WebUsers.h"
11 #include "otsdaq/WorkLoopManager/WorkLoopManager.h"
13 #include "otsdaq/CodeEditor/CodeEditor.h"
14 #include "otsdaq/TablePlugins/DesktopIconTable.h"
16 #include "otsdaq/NetworkUtilities/TransceiverSocket.h"
18 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
20 #include <xdaq/Application.h>
21 #pragma GCC diagnostic pop
22 #include "otsdaq/Macros/XDAQApplicationMacros.h"
24 #include <toolbox/task/WorkLoop.h>
25 #include <xdata/String.h>
26 #include <xgi/Method.h>
27 #include "otsdaq/GatewaySupervisor/PixelHistoPicGen.h"
59 class ConfigurationManager;
61 class WorkLoopManager;
85 void Default (xgi::Input* in, xgi::Output* out);
87 void loginRequest (xgi::Input* in, xgi::Output* out);
88 void request (xgi::Input* in, xgi::Output* out);
89 void tooltipRequest (xgi::Input* in, xgi::Output* out);
90 void XGI_Turtle (xgi::Input* in, xgi::Output* out);
92 void addStateMachineStatusToXML (
HttpXmlDocument& xmlOut,
const std::string& fsmName,
bool getRunNumber =
true);
93 void addFilteredConfigAliasesToXML (
HttpXmlDocument& xmlOut,
const std::string& fsmName);
94 void addRequiredFsmLogInputToXML (
HttpXmlDocument& xmlOut,
const std::string& fsmName);
97 void stateMachineXgiHandler(xgi::Input* in, xgi::Output* out);
98 void stateMachineIterationBreakpoint(xgi::Input* in, xgi::Output* out);
100 static std::string getIconHeaderString(
void);
101 static bool handleAddDesktopIconRequest(
const std::string& author, cgicc::Cgicc& cgiIn,
HttpXmlDocument& xmlOut, std::vector<DesktopIconTable::DesktopIcon>* newIcons =
nullptr);
102 static void handleGetApplicationIdRequest(
AllSupervisorInfo* applicationInfo, cgicc::Cgicc& cgiIn,
HttpXmlDocument& xmlOut, std::map<std::string , std::map<std::string /* requestUrlHostPort */, std::string /* translatedHostPort */>>* portTranslationMap =
nullptr);
104 xoap::MessageReference stateMachineXoapHandler(xoap::MessageReference msg);
106 bool stateMachineThread(toolbox::task::WorkLoop* workLoop);
109 void statusRequest(xgi::Input* in, xgi::Output* out);
110 void infoRequestResultHandler(xgi::Input* in, xgi::Output* out);
111 bool infoRequestThread(toolbox::task::WorkLoop* workLoop);
114 xoap::MessageReference supervisorCookieCheck(xoap::MessageReference msg);
115 xoap::MessageReference supervisorGetActiveUsers(xoap::MessageReference msg);
116 xoap::MessageReference supervisorSystemMessage(xoap::MessageReference msg);
117 xoap::MessageReference supervisorGetUserInfo(xoap::MessageReference msg);
118 xoap::MessageReference supervisorSystemLogbookEntry(xoap::MessageReference msg);
119 xoap::MessageReference supervisorLastTableGroupRequest(xoap::MessageReference msg);
127 void inError(toolbox::fsm::FiniteStateMachine& fsm)
override;
129 void transitionConfiguring(toolbox::Event::Reference e)
override;
130 void transitionHalting(toolbox::Event::Reference e)
override;
131 void transitionInitializing(toolbox::Event::Reference e)
override;
132 void transitionPausing(toolbox::Event::Reference e)
override;
133 void transitionResuming(toolbox::Event::Reference e)
override;
134 void transitionStarting(toolbox::Event::Reference e)
override;
135 void transitionStopping(toolbox::Event::Reference e)
override;
136 void transitionShuttingDown(toolbox::Event::Reference e)
override;
137 void transitionStartingUp(toolbox::Event::Reference e)
override;
138 void enteringError(toolbox::Event::Reference e)
override;
140 void makeSystemLogEntry(
const std::string& entryText,
const std::string& subjectText =
"");
141 static void addSystemMessage(std::string toUserCSV, std::string message);
143 void checkForAsyncError(
void);
151 unsigned int getNextRunNumber (
const std::string& fsmName =
"");
152 void setNextRunNumber (
unsigned int runNumber,
const std::string& fsmName =
"");
153 std::string getLastLogEntry (
const std::string& logType,
const std::string& fsmName =
"");
154 void setLastLogEntry (
const std::string& logType,
const std::string& logEntry,
const std::string& fsmName =
"");
157 static xoap::MessageReference lastTableGroupRequestHandler (
const SOAPParameters& parameters);
159 static void launchStartOneServerCommand (
const std::string& command,
ConfigurationManager* cfgMgr,
const std::string& contextName);
162 xoap::MessageReference TRACESupervisorRequest (xoap::MessageReference message);
165 static void AppStatusWorkLoop (
GatewaySupervisor* supervisorPtr,
const bool doDisconnected =
false);
168 std::ostringstream* out,
169 const std::string& command,
170 const std::string& fsmName,
171 const std::string& fsmWindowName,
172 const std::string& username,
173 const std::vector<std::string>& parameters,
174 std::string logEntry =
"");
175 void broadcastMessage (xoap::MessageReference msg);
176 void broadcastMessageToRemoteGateways (
const xoap::MessageReference msg);
177 bool broadcastMessageToRemoteGatewaysComplete (
const xoap::MessageReference msg);
179 struct BroadcastMessageIterationsDoneStruct
185 ~BroadcastMessageIterationsDoneStruct()
187 for (
auto& arr : iterationsDone_)
189 iterationsDone_.clear();
193 void push(
const unsigned int& size)
195 iterationsDone_.push_back(
new bool[size]);
196 arraySizes_.push_back(size);
199 for (
unsigned int i = 0; i < size; ++i)
200 iterationsDone_[iterationsDone_.size() - 1][i] =
false;
203 bool* operator[](
unsigned int i) {
return iterationsDone_[i]; }
204 const bool* operator[](
unsigned int i)
const {
return iterationsDone_[i]; }
205 unsigned int size(
unsigned int i = -1)
207 if (i == (
unsigned int)-1)
208 return iterationsDone_.size();
209 return arraySizes_[i];
213 std::vector<bool*> iterationsDone_;
214 std::vector<unsigned int> arraySizes_;
217 struct BroadcastThreadStruct
220 BroadcastThreadStruct()
230 BroadcastThreadStruct(BroadcastThreadStruct &&b)
231 : threadIndex_(b.threadIndex_)
232 , exitThread_(b.exitThread_)
233 , working_(b.working_)
234 , workToDo_(b.workToDo_)
244 xoap::MessageReference message,
245 const std::string& command,
246 const unsigned int& iteration,
247 bool& iterationsDone)
251 , iteration_(iteration)
252 , iterationsDone_(iterationsDone)
257 xoap::MessageReference message_;
258 const std::string command_;
259 const unsigned int iteration_;
260 bool& iterationsDone_;
267 xoap::MessageReference message,
268 const std::string& command,
269 const unsigned int& iteration,
270 bool& iterationsDone)
274 appInfo, message, command, iteration, iterationsDone));
278 const SupervisorInfo& getAppInfo() {
return messages_[0].appInfo_; }
279 xoap::MessageReference getMessage() {
return messages_[0].message_; }
280 const std::string& getCommand() {
return messages_[0].command_; }
281 const unsigned int& getIteration() {
return messages_[0].iteration_; }
282 std::string& getReply() {
return messages_[0].reply_; }
283 bool& getIterationsDone() {
return messages_[0].iterationsDone_; }
286 std::mutex threadMutex_;
287 unsigned int threadIndex_;
288 volatile bool exitThread_, working_, workToDo_, error_;
290 std::vector<BroadcastThreadStruct::BroadcastMessageStruct> messages_;
293 static void broadcastMessageThread(
295 std::shared_ptr<GatewaySupervisor::BroadcastThreadStruct> threadStruct);
296 bool handleBroadcastMessageTarget(
const SupervisorInfo& appInfo,
297 xoap::MessageReference message,
298 const std::string& command,
299 const unsigned int& iteration,
301 unsigned int threadIndex = 0);
306 bool supervisorGuiHasBeenLoaded_;
307 static WebUsers theWebUsers_;
308 std::map<std::string , std::map<std::string ,
312 WorkLoopManager stateMachineWorkLoopManager_;
313 toolbox::BSem stateMachineSemaphore_;
315 std::string activeStateMachineName_;
316 std::string activeStateMachineWindowName_;
317 std::string activeStateMachineDumpFormatOnRun_, activeStateMachineDumpFormatOnConfigure_;
318 std::string activeStateMachineConfigurationDumpOnRun_, activeStateMachineConfigurationDumpOnConfigure_;
319 bool activeStateMachineConfigurationDumpOnRunEnable_, activeStateMachineConfigurationDumpOnConfigureEnable_;
320 std::string activeStateMachineConfigurationDumpOnRunFilename_, activeStateMachineConfigurationDumpOnConfigureFilename_;
321 bool activeStateMachineRequireUserLogOnRun_, activeStateMachineRequireUserLogOnConfigure_;
322 std::string activeStateMachineRunInfoPluginType_;
323 std::map<std::string , std::string >
324 stateMachineConfigureLogEntry_, stateMachineStartLogEntry_, stateMachineStopLogEntry_;
325 std::string activeStateMachineRunNumber_, activeStateMachineRunAlias_, activeStateMachineConfigurationAlias_;
326 bool activeStateMachineRollOverLogOnConfigure_, activeStateMachineRollOverLogOnStart_;
327 std::chrono::steady_clock::time_point
328 activeStateMachineRunStartTime;
329 int activeStateMachineRunDuration_ms;
330 unsigned int activeStateMachineConfigureConditionID_, activeStateMachineRunConditionID_;
332 std::mutex systemStatusMutex_;
333 std::string lastLogbookEntry_;
334 time_t lastLogbookEntryTime_ = 0;
336 std::string lastConsoleErr_, lastConsoleWarn_, lastConsoleInfo_, lastConsoleErrTime_, lastConsoleWarnTime_, lastConsoleInfoTime_;
337 std::string firstConsoleErr_, firstConsoleWarn_, firstConsoleInfo_, firstConsoleErrTime_, firstConsoleWarnTime_, firstConsoleInfoTime_;
338 size_t systemConsoleErrCount_ = 0, systemConsoleWarnCount_ = 0, systemConsoleInfoCount_ = 0;
340 std::pair<std::string , TableGroupKey>
341 theConfigurationTableGroup_;
342 std::string stateMachineTransitionUsername_;
344 Iterator theIterator_;
345 std::mutex stateMachineAccessMutex_;
347 std::string stateMachineLastCommandInput_;
353 CodeEditor codeEditor_;
355 std::mutex broadcastCommandMessageIndexMutex_, broadcastIterationsDoneMutex_;
356 unsigned int broadcastCommandMessageIndex_;
357 bool broadcastIterationsDone_;
358 std::mutex broadcastIterationBreakpointMutex_;
359 unsigned int broadcastIterationBreakpoint_;
361 std::mutex broadcastCommandStatusUpdateMutex_;
362 std::string broadcastCommandStatus_;
363 static std::vector<std::shared_ptr<GatewaySupervisor::BroadcastThreadStruct>> broadcastThreadStructs_;
365 std::string securityType_;
369 unsigned int conditionID_;
384 std::string error, config_dump;
389 size_t consoleErrCount = 0, consoleWarnCount = 0;
391 std::string fullName;
399 std::string usernameWithLock;
408 bool fsm_included =
true;
410 std::string getFsmMode()
const {
413 case FSM_ModeTypes::Follow_FSM:
return "Follow FSM";
416 default:
return "Impossible";
420 std::string getConfigDumpType()
const {
421 switch(config_dump_type)
423 case ConfigDumpTypes::Text:
return "Text";
424 case ConfigDumpTypes::JSON_all:
return "JSON all";
425 default:
return "Unknown";
429 std::map<std::string, SupervisorInfo::SubappInfo>
subapps;
432 std::vector<GatewaySupervisor::RemoteGatewayInfo> remoteGatewayApps_;
433 std::mutex remoteGatewayAppsMutex_;
434 std::map<std::string ,
435 bool > appLastStatusGood_;
436 std::mutex dualStatusThreadMutex_;
441 std::mutex latestGatewayIconsMutex_;
450 static void CheckRemoteGatewayStatus (
GatewaySupervisor::RemoteGatewayInfo& remoteGatewayApp,
const std::unique_ptr<TransceiverSocket>& remoteGatewaySocket,
const std::string& ipForReverseLoginOverUDP,
int portForReverseLoginOverUDP);
453 void loadRemoteGatewaySettings (std::vector<GatewaySupervisor::RemoteGatewayInfo>& remoteGateways,
bool onlyNotFound =
false)
const;
454 void saveRemoteGatewaySettings (
void)
const;
455 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);
456 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