otsdaq  v2_05_02_indev
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/MessageFacility/ITRACEController.h"
17 #include "otsdaq/WebUsersUtilities/RemoteWebUsers.h"
18 
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"
24 #include "xgi/Method.h"
25 
26 #include <toolbox/fsm/FailedEvent.h>
27 
28 #include <xdaq/NamespaceURI.h>
29 #include <xoap/Method.h>
30 
31 #include <map>
32 #include <memory>
33 #include <string> /*string and to_string*/
34 #include <vector>
35 
36 namespace ots
37 {
38 // clang-format off
39 
40 // CoreSupervisorBase
41 // This class should be the base class for all client otsdaq, XDAQ-based, supervisors.
42 // That is, all supervisors that need web requests through the ots desktop
43 // with access verified by the Gateway Supervisor,
44 // or that need a state machines driven by the Gateway Supervisor.
45 class CoreSupervisorBase : public xdaq::Application,
46  public SOAPMessenger,
49 {
50  friend class MacroMakerSupervisor; // to allow MacroMakerSupervisor to call
51  // requestWrapper in Macro Maker mode
52 
53  public:
54  CoreSupervisorBase(xdaq::ApplicationStub* stub);
55  virtual ~CoreSupervisorBase(void);
56 
57  void destroy(void);
58 
59  unsigned int getSupervisorLID (void) const { return getApplicationDescriptor()->getLocalId(); }
60 
61  // Here are the common web request handlers:
62  // defaultPage returns the public html page
63  // request checks the login before proceeding to virtual request
64  // - All Supervisors should implement request for their actions (and they will
65  // get the security wrapper for free)
66  // - The security setting defaults can be setup or forced by overriding
67  // setSupervisorPropertyDefaults and forceSupervisorProperties
68  virtual void defaultPage (xgi::Input* in, xgi::Output* out);
69  virtual void request (const std::string& requestType,
70  cgicc::Cgicc& cgiIn,
71  HttpXmlDocument& xmlOut,
72  const WebUsers::RequestUserInfo& userInfo);
73  virtual void nonXmlRequest (const std::string& requestType,
74  cgicc::Cgicc& cgiIn,
75  std::ostream& out,
76  const WebUsers::RequestUserInfo& userInfo);
77  virtual std::string getStatusProgressDetail (void);
78 
79  private:
80  xoap::MessageReference workLoopStatusRequestWrapper (xoap::MessageReference message);
81  void defaultPageWrapper (xgi::Input* in, xgi::Output* out);
82  void requestWrapper (xgi::Input* in, xgi::Output* out);
83 
84  public:
85  // State Machine request handlers
86  void stateMachineXgiHandler (xgi::Input* in, xgi::Output* out);
87  xoap::MessageReference stateMachineXoapHandler (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  protected:
108  void transitionConfiguringFSMs(void);
109  public:
110  virtual void transitionHalting (toolbox::Event::Reference event);
111  virtual void transitionInitializing (toolbox::Event::Reference event);
112  virtual void transitionPausing (toolbox::Event::Reference event);
113  virtual void transitionResuming (toolbox::Event::Reference event);
114  virtual void transitionStarting (toolbox::Event::Reference event);
115  virtual void transitionStopping (toolbox::Event::Reference event);
116  virtual void enteringError (toolbox::Event::Reference event);
117 
118  static const std::string WORK_LOOP_DONE, WORK_LOOP_WORKING;
119 
120  void SetTraceController(ITRACEController* tc) { theTRACEController_ = tc;}
121 
122  protected:
123  WorkLoopManager stateMachineWorkLoopManager_;
124  toolbox::BSem stateMachineSemaphore_;
125  std::vector<VStateMachine*> theStateMachineImplementation_;
126 
127  // for managing transition iterations
128  std::vector<bool> stateMachinesIterationDone_;
129  unsigned int stateMachinesIterationWorkCount_;
130  unsigned int subIterationWorkStateMachineIndex_;
131  void preStateMachineExecution(unsigned int i);
132  void postStateMachineExecution(unsigned int i);
133  void preStateMachineExecutionLoop(void);
134  void postStateMachineExecutionLoop(void);
135 
136  RemoteWebUsers theRemoteWebUsers_;
137 
138  ITRACEController* theTRACEController_;
139 };
140 // clang-format on
141 } // namespace ots
142 
143 #endif