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