otsdaq  v2_04_01
RunControlStateMachine.h
1 #ifndef _ots_RunControlStateMachine_h_
2 #define _ots_RunControlStateMachine_h_
3 
4 #include "otsdaq-core/FiniteStateMachine/FiniteStateMachine.h"
5 #include "otsdaq-core/ProgressBar/ProgressBar.h"
6 
7 #include <string>
8 #include "toolbox/lang/Class.h"
9 
10 namespace ots
11 {
12 // CoreSupervisorBase
13 // This class provides finite state machine functionality for otsdaq supervisors.
14 class RunControlStateMachine : public virtual toolbox::lang::Class
15 {
16  public:
17  RunControlStateMachine(const std::string& name = "Undefined Name");
18  virtual ~RunControlStateMachine(void);
19 
20  void reset(void);
21  void setStateMachineName(const std::string& name)
22  {
23  theStateMachine_.setStateMachineName(name);
24  }
25  const std::string& getErrorMessage(void) const
26  {
27  return theStateMachine_.getErrorMessage();
28  }
29 
30  template<class OBJECT>
31  void addStateTransition(toolbox::fsm::State from,
32  toolbox::fsm::State to,
33  const std::string& input,
34  const std::string& transitionName,
35  OBJECT* obj,
36  void (OBJECT::*func)(toolbox::Event::Reference))
37 
38  {
39  stateTransitionFunctionTable_[from][input] = func;
40  theStateMachine_.addStateTransition(from, to, input, transitionName, obj, func);
41  }
42 
43  template<class OBJECT>
44  void addStateTransition(toolbox::fsm::State from,
45  toolbox::fsm::State to,
46  const std::string& input,
47  const std::string& transitionName,
48  const std::string& transitionParameter,
49  OBJECT* obj,
50  void (OBJECT::*func)(toolbox::Event::Reference))
51 
52  {
53  stateTransitionFunctionTable_[from][input] = func;
54  theStateMachine_.addStateTransition(
55  from, to, input, transitionName, transitionParameter, obj, func);
56  }
57 
58  // using stateMachineFunction_t = void (ots::RunControlStateMachine::*
59  // )(toolbox::Event::Reference); stateMachineFunction_t getTransitionFunction (const
60  // toolbox::fsm::State from, const std::string &transition);
61 
62  // Finite State Machine States
63  // 1. Control Configuration and Function Manager are loaded.
64  virtual void stateInitial(toolbox::fsm::FiniteStateMachine& fsm) { ; }
65 
66  // 1. XDAQ applications are running.
67  // 2. Hardware is running.
68  // 3. Triggers are accepted.
69  // 4. Triggers are not sent.
70  // 5. Data is sent / read out.
71  virtual void statePaused(toolbox::fsm::FiniteStateMachine& fsm) { ; }
72 
73  // 1. XDAQ applications are running.
74  // 2. Hardware is running.
75  // 3. Triggers are accepted.
76  // 4. Triggers are sent.
77  // 5. Data is sent / read out.
78  virtual void stateRunning(toolbox::fsm::FiniteStateMachine& fsm) { ; }
79 
80  // 1. Control hierarchy is instantiated.
81  // 2. XDAQ executives are running and configured.
82  // 3. XDAQ applications are loaded and instantiated.
83  // 4. DCS nodes are allocated.
84  virtual void stateHalted(toolbox::fsm::FiniteStateMachine& fsm) { ; }
85 
86  // 1. Power supplies are turned off.
87  virtual void stateShutdown(toolbox::fsm::FiniteStateMachine& fsm) { ; }
88 
89  // 1. XDAQ applications are configured.
90  // 2. Run parameters have been distributed.
91  // 3. Hardware is configured.
92  // 4. I2O connections are established, no data is sent or read out.
93  // 5. Triggers are not sent.
94  virtual void stateConfigured(toolbox::fsm::FiniteStateMachine& fsm) { ; }
95 
96  virtual void inError(toolbox::fsm::FiniteStateMachine& fsm) { ; }
97 
98  virtual void transitionConfiguring(toolbox::Event::Reference e) { ; }
99  virtual void transitionHalting(toolbox::Event::Reference e) { ; }
100  virtual void transitionShuttingDown(toolbox::Event::Reference e) { ; }
101  virtual void transitionStartingUp(toolbox::Event::Reference e) { ; }
102  virtual void transitionInitializing(toolbox::Event::Reference e) { ; }
103  virtual void transitionPausing(toolbox::Event::Reference e) { ; }
104  virtual void transitionResuming(toolbox::Event::Reference e) { ; }
105  virtual void transitionStarting(toolbox::Event::Reference e) { ; }
106  virtual void transitionStopping(toolbox::Event::Reference e) { ; }
107  virtual void enteringError(toolbox::Event::Reference e) { ; }
108 
109  // Run Control Messages
110  xoap::MessageReference runControlMessageHandler(xoap::MessageReference message);
111 
112  static const std::string FAILED_STATE_NAME;
113 
114  unsigned int getIterationIndex(void) { return iterationIndex_; }
115  unsigned int getSubIterationIndex(void) { return subIterationIndex_; }
116  void indicateIterationWork(void) { iterationWorkFlag_ = true; }
117  void clearIterationWork(void) { iterationWorkFlag_ = false; }
118  bool getIterationWork(void) { return iterationWorkFlag_; }
119  void indicateSubIterationWork(void) { subIterationWorkFlag_ = true; }
120  void clearSubIterationWork(void) { subIterationWorkFlag_ = false; }
121  bool getSubIterationWork(void) { return subIterationWorkFlag_; }
122 
123  protected:
124  FiniteStateMachine theStateMachine_;
125  ProgressBar theProgressBar_;
126 
127  volatile bool asyncFailureReceived_, asyncSoftFailureReceived_;
128 
129  unsigned int iterationIndex_, subIterationIndex_;
130  bool iterationWorkFlag_, subIterationWorkFlag_;
131 
132  toolbox::fsm::State lastIterationState_;
133  std::string lastIterationCommand_;
134  std::string lastIterationResult_;
135  unsigned int lastIterationIndex_, lastSubIterationIndex_;
136 
137  std::map<toolbox::fsm::State,
138  std::map<std::string,
139  void (RunControlStateMachine::*)(toolbox::Event::Reference),
140  std::less<std::string> > >
141  stateTransitionFunctionTable_;
142 };
143 
144 } // namespace ots
145 
146 #endif