otsdaq  v2_01_00
GatewaySupervisor.h
1 #ifndef _ots_GatewaySupervisor_h
2 #define _ots_GatewaySupervisor_h
3 
4 #include "otsdaq-core/SupervisorInfo/AllSupervisorInfo.h"
5 #include "otsdaq-core/SOAPUtilities/SOAPMessenger.h"
6 #include "otsdaq-core/WebUsersUtilities/WebUsers.h"
7 #include "otsdaq-core/SystemMessenger/SystemMessenger.h"
8 #include "otsdaq-core/WorkLoopManager/WorkLoopManager.h"
9 #include "otsdaq-core/FiniteStateMachine/RunControlStateMachine.h"
10 #include "otsdaq-core/SupervisorInfo/AllSupervisorInfo.h"
11 #include "otsdaq-core/GatewaySupervisor/Iterator.h"
12 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationGroupKey.h"
13 #include "otsdaq-core/CoreSupervisors/CorePropertySupervisorBase.h"
14 #include "otsdaq-core/GatewaySupervisor/ARTDAQCommandable.h"
15 
16 #pragma GCC diagnostic push
17 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
18 #include <xdaq/Application.h>
19 #pragma GCC diagnostic pop
20 #include "otsdaq-core/Macros/XDAQApplicationMacros.h"
21 //#include <toolbox/fsm/FiniteStateMachine.h>
22 #include <toolbox/task/WorkLoop.h>
23 #include <xgi/Method.h>
24 #include <xdata/String.h>
25 
26 #include <string>
27 #include <set>
28 
29 //defines used also by OtsConfigurationWizardSupervisor
30 #define FSM_LAST_CONFIGURED_GROUP_ALIAS_FILE std::string("FSMLastConfiguredGroupAlias.hist")
31 #define FSM_LAST_STARTED_GROUP_ALIAS_FILE std::string("FSMLastStartedGroupAlias.hist")
32 
33 
34 namespace ots
35 {
36 
37 class ConfigurationManager;
38 class ConfigurationGroupKey;
39 class WorkLoopManager;
40 
41 //GatewaySupervisor
42 // This class is the gateway server for all otsdaq requests in "Normal Mode." It validates user access
43 // for every request. It synchronizes
44 // the state machines of all other supervisors.
45 class GatewaySupervisor: public xdaq::Application, public SOAPMessenger,
47 {
48  friend class WizardSupervisor;
49  friend class Iterator;
50  friend class ARTDAQCommandable;
51 
52 public:
53 
54  XDAQ_INSTANTIATOR();
55 
56  GatewaySupervisor (xdaq::ApplicationStub * s);
57  virtual ~GatewaySupervisor(void);
58 
59  void init (void);
60 
61  void Default (xgi::Input* in, xgi::Output* out ) ;
62 // void TmpTest (xgi::Input* in, xgi::Output* out ) ;
63 
64  void loginRequest (xgi::Input* in, xgi::Output* out ) ;
65  void request (xgi::Input* in, xgi::Output* out ) ;
66  void tooltipRequest (xgi::Input* in, xgi::Output* out ) ;
67 
68  //State Machine requests handlers
69 
70  void stateMachineXgiHandler (xgi::Input* in, xgi::Output* out ) ;
71  //void stateMachineResultXgiHandler (xgi::Input* in, xgi::Output* out ) ;
72 
73  xoap::MessageReference stateMachineXoapHandler (xoap::MessageReference msg ) ;
74  xoap::MessageReference stateMachineResultXoapHandler (xoap::MessageReference msg ) ;
75 
76  bool stateMachineThread (toolbox::task::WorkLoop* workLoop);
77 
78  //Status requests handlers
79  void infoRequestHandler (xgi::Input* in, xgi::Output* out ) ;
80  void infoRequestResultHandler (xgi::Input* in, xgi::Output* out ) ;
81  bool infoRequestThread (toolbox::task::WorkLoop* workLoop);
82 
83  //External GatewaySupervisor XOAP handlers
84  xoap::MessageReference supervisorCookieCheck (xoap::MessageReference msg) ;
85  xoap::MessageReference supervisorGetActiveUsers (xoap::MessageReference msg) ;
86  xoap::MessageReference supervisorSystemMessage (xoap::MessageReference msg) ;
87  xoap::MessageReference supervisorGetUserInfo (xoap::MessageReference msg) ;
88  xoap::MessageReference supervisorSystemLogbookEntry (xoap::MessageReference msg) ;
89  xoap::MessageReference supervisorLastConfigGroupRequest(xoap::MessageReference msg) ;
90 
91  //Finite State Machine States
92  void stateInitial (toolbox::fsm::FiniteStateMachine& fsm) ;
93  void statePaused (toolbox::fsm::FiniteStateMachine& fsm) ;
94  void stateRunning (toolbox::fsm::FiniteStateMachine& fsm) ;
95  void stateHalted (toolbox::fsm::FiniteStateMachine& fsm) ;
96  void stateConfigured (toolbox::fsm::FiniteStateMachine& fsm) ;
97  void inError (toolbox::fsm::FiniteStateMachine& fsm) ;
98 
99  void transitionConfiguring (toolbox::Event::Reference e) ;
100  void transitionHalting (toolbox::Event::Reference e) ;
101  void transitionInitializing (toolbox::Event::Reference e) ;
102  void transitionPausing (toolbox::Event::Reference e) ;
103  void transitionResuming (toolbox::Event::Reference e) ;
104  void transitionStarting (toolbox::Event::Reference e) ;
105  void transitionStopping (toolbox::Event::Reference e) ;
106  void transitionShuttingDown (toolbox::Event::Reference e) ;
107  void transitionStartingUp (toolbox::Event::Reference e) ;
108  void enteringError (toolbox::Event::Reference e) ;
109 
110  void makeSystemLogbookEntry (std::string entryText);
111 
112  //CorePropertySupervisorBase override functions
113  virtual void setSupervisorPropertyDefaults (void) override; //override to control supervisor specific defaults
114  virtual void forceSupervisorPropertyValues (void) override; //override to force supervisor property values (and ignore user settings)
115 
116 
117 private:
118  unsigned int getNextRunNumber (const std::string &fsmName = "");
119  bool setNextRunNumber (unsigned int runNumber, const std::string &fsmName = "");
120  static std::pair<std::string /*group name*/, ConfigurationGroupKey> loadGroupNameAndKey (const std::string &fileName, std::string &returnedTimeString);
121  void saveGroupNameAndKey (const std::pair<std::string /*group name*/, ConfigurationGroupKey> &theGroup, const std::string &fileName);
122  static xoap::MessageReference lastConfigGroupRequestHandler (const SOAPParameters &parameters);
123  void launchStartOTSCommand (const std::string& command);
124 
125  static void StateChangerWorkLoop (GatewaySupervisor *supervisorPtr);
126  std::string attemptStateMachineTransition (HttpXmlDocument* xmldoc, std::ostringstream* out, const std::string& command, const std::string& fsmName, const std::string& fsmWindowName, const std::string& username, const std::vector<std::string>& parameters);
127  bool broadcastMessage (xoap::MessageReference msg) ;
128 
129  bool supervisorGuiHasBeenLoaded_ ; //use to indicate first access by user of ots since execution
130 
131  //Member Variables
132 
133  //AllSupervisorInfo allSupervisorInfo_ ;
134  // ConfigurationManager* theConfigurationManager_ ;
135  WebUsers theWebUsers_ ;
136  SystemMessenger theSystemMessenger_ ;
137  ARTDAQCommandable theArtdaqCommandable_;
138 
139  WorkLoopManager stateMachineWorkLoopManager_;
140  toolbox::BSem stateMachineSemaphore_ ;
141  WorkLoopManager infoRequestWorkLoopManager_ ;
142  toolbox::BSem infoRequestSemaphore_ ;
143 //
144 // std::string supervisorContextUID_ ; //now comes from CorePropertySup
145 // std::string supervisorApplicationUID_ ;
146 
147  std::string activeStateMachineName_ ; //when multiple state machines, this is the name of the state machine which executed the configure transition
148  std::string activeStateMachineWindowName_ ;
149  std::pair<std::string /*group name*/, ConfigurationGroupKey> theConfigurationGroup_; //used to track the active configuration group at states after the configure state
150 
151  Iterator theIterator_ ;
152  std::mutex stateMachineAccessMutex_ ; //for sharing state machine access with iterator thread
153  std::string stateMachineLastCommandInput_ ;
154 
155  enum {
156  VERBOSE_MUTEX = 0
157  };
158 
159  //temporary member variable to avoid redeclaration in repetitive functions
160  char tmpStringForConversions_[100];
161 
162  //Trash tests
163  void wait(int milliseconds, std::string who="") const;
164  unsigned long long counterTest_ ;
165  std::vector<int> vectorTest_ ;
166  std::string securityType_;
167 
168 };
169 
170 }
171 
172 #endif
173