otsdaq  v2_04_02
CoreSupervisorBase.h
1 #ifndef _ots_CoreSupervisorBase_h_
2 #define _ots_CoreSupervisorBase_h_
3 
4 #include "otsdaq/FiniteStateMachine/RunControlStateMachine.h"
5 #include "otsdaq/SOAPUtilities/SOAPMessenger.h"
6 #include "otsdaq/WorkLoopManager/WorkLoopManager.h"
7 
8 #include "otsdaq/CoreSupervisors/CorePropertySupervisorBase.h"
9 
10 #include "otsdaq/CgiDataUtilities/CgiDataUtilities.h"
11 #include "otsdaq/SOAPUtilities/SOAPUtilities.h"
12 #include "otsdaq/XmlUtilities/HttpXmlDocument.h"
13 
14 #include "otsdaq/FiniteStateMachine/VStateMachine.h"
15 
16 #include "otsdaq/WebUsersUtilities/RemoteWebUsers.h"
17 
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"
23 #include "xgi/Method.h"
24 
25 #include <toolbox/fsm/FailedEvent.h>
26 
27 #include <xdaq/NamespaceURI.h>
28 #include <xoap/Method.h>
29 
30 #include <map>
31 #include <memory>
32 #include <string> /*string and to_string*/
33 #include <vector>
34 
35 namespace ots
36 {
37 // clang-format off
38 
39 // CoreSupervisorBase
40 // This class should be the base class for all client otsdaq, XDAQ-based, supervisors.
41 // That is, all supervisors that need web requests through the ots desktop
42 // with access verified by the Gateway Supervisor,
43 // or that need a state machines driven by the Gateway Supervisor.
44 class CoreSupervisorBase : public xdaq::Application,
45  public SOAPMessenger,
48 {
49  friend class MacroMakerSupervisor; // to allow MacroMakerSupervisor to call
50  // requestWrapper in Macro Maker mode
51 
52  public:
53  CoreSupervisorBase(xdaq::ApplicationStub* stub);
54  virtual ~CoreSupervisorBase(void);
55 
56  void destroy(void);
57 
58  const unsigned int getSupervisorLID(void) const { return getApplicationDescriptor()->getLocalId(); }
59 
60  // Here are the common web request handlers:
61  // defaultPage returns the public html page
62  // request checks the login before proceeding to virtual request
63  // - All Supervisors should implement request for their actions (and they will
64  // get the security wrapper for free)
65  // - The security setting defaults can be setup or forced by overriding
66  // setSupervisorPropertyDefaults and forceSupervisorProperties
67  virtual void defaultPage(xgi::Input* in, xgi::Output* out);
68  virtual void request(const std::string& requestType,
69  cgicc::Cgicc& cgiIn,
70  HttpXmlDocument& xmlOut,
71  const WebUsers::RequestUserInfo& userInfo);
72  virtual void nonXmlRequest(const std::string& requestType,
73  cgicc::Cgicc& cgiIn,
74  std::ostream& out,
75  const WebUsers::RequestUserInfo& userInfo);
76 
77  private:
78  xoap::MessageReference workLoopStatusRequestWrapper (xoap::MessageReference message);
79  void defaultPageWrapper (xgi::Input* in, xgi::Output* out);
80  void requestWrapper (xgi::Input* in, xgi::Output* out);
81 
82  public:
83  // State Machine request handlers
84  void stateMachineXgiHandler (xgi::Input* in, xgi::Output* out);
85  void stateMachineResultXgiHandler (xgi::Input* in, xgi::Output* out);
86  xoap::MessageReference stateMachineXoapHandler (xoap::MessageReference message);
87  xoap::MessageReference stateMachineResultXoapHandler (xoap::MessageReference message);
88 
89  xoap::MessageReference stateMachineStateRequest (xoap::MessageReference message);
90  xoap::MessageReference stateMachineErrorMessageRequest (xoap::MessageReference message);
91 
92  void sendAsyncErrorToGateway (const std::string& errMsg, bool isSoftError);
93 
94  virtual xoap::MessageReference workLoopStatusRequest (xoap::MessageReference message);
95  virtual xoap::MessageReference applicationStatusRequest (xoap::MessageReference message);
96 
97  bool stateMachineThread (toolbox::task::WorkLoop* workLoop);
98 
99  virtual void stateInitial (toolbox::fsm::FiniteStateMachine& fsm);
100  virtual void statePaused (toolbox::fsm::FiniteStateMachine& fsm);
101  virtual void stateRunning (toolbox::fsm::FiniteStateMachine& fsm);
102  virtual void stateHalted (toolbox::fsm::FiniteStateMachine& fsm);
103  virtual void stateConfigured (toolbox::fsm::FiniteStateMachine& fsm);
104  virtual void inError (toolbox::fsm::FiniteStateMachine& fsm);
105 
106  virtual void transitionConfiguring (toolbox::Event::Reference event);
107  virtual void transitionHalting (toolbox::Event::Reference event);
108  virtual void transitionInitializing (toolbox::Event::Reference event);
109  virtual void transitionPausing (toolbox::Event::Reference event);
110  virtual void transitionResuming (toolbox::Event::Reference event);
111  virtual void transitionStarting (toolbox::Event::Reference event);
112  virtual void transitionStopping (toolbox::Event::Reference event);
113  virtual void enteringError (toolbox::Event::Reference event);
114 
115  static const std::string WORK_LOOP_DONE, WORK_LOOP_WORKING;
116 
117  protected:
118  WorkLoopManager stateMachineWorkLoopManager_;
119  toolbox::BSem stateMachineSemaphore_;
120  std::vector<VStateMachine*> theStateMachineImplementation_;
121 
122  // for managing transition iterations
123  std::vector<bool> stateMachinesIterationDone_;
124  unsigned int stateMachinesIterationWorkCount_;
125  unsigned int subIterationWorkStateMachineIndex_;
126  void preStateMachineExecution(unsigned int i);
127  void postStateMachineExecution(unsigned int i);
128  void preStateMachineExecutionLoop(void);
129  void postStateMachineExecutionLoop(void);
130 
131  RemoteWebUsers theRemoteWebUsers_;
132 };
133 // clang-format on
134 } // namespace ots
135 
136 #endif