otsdaq  v2_04_01
FiniteStateMachine.h
1 #ifndef _ots_FiniteStateMachine_h
2 #define _ots_FiniteStateMachine_h
3 
4 #include <toolbox/fsm/FiniteStateMachine.h>
5 #include <xoap/MessageReference.h>
6 
7 namespace ots
8 {
9 class FiniteStateMachine : public toolbox::fsm::FiniteStateMachine
10 {
11  public:
12  FiniteStateMachine(const std::string& stateMachineName);
13  ~FiniteStateMachine(void);
14 
15  using toolbox::fsm::FiniteStateMachine::addStateTransition;
16 
17  template<class OBJECT>
18  void addStateTransition(toolbox::fsm::State from,
19  toolbox::fsm::State to,
20  const std::string& input,
21  const std::string& transitionName,
22  OBJECT* obj,
23  void (OBJECT::*func)(toolbox::Event::Reference))
24 
25  {
26  stateTransitionNameTable_[from][input] = transitionName;
27  toolbox::fsm::FiniteStateMachine::addStateTransition(from, to, input, obj, func);
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  const std::string& transitionParameter,
36  OBJECT* obj,
37  void (OBJECT::*func)(toolbox::Event::Reference))
38 
39  {
40  stateTransitionParameterTable_[from][input] = transitionParameter;
41  addStateTransition(from, to, input, transitionName, obj, func);
42  }
43 
44  toolbox::fsm::State getProvenanceState(void);
45  toolbox::fsm::State getTransitionFinalState(const std::string& transition);
46 
47  std::string getProvenanceStateName(void);
48  std::string getCurrentStateName(void);
49  time_t getTimeInState(void);
50  std::string getCurrentTransitionName(const std::string& transition);
51  std::string getTransitionName(const toolbox::fsm::State from,
52  const std::string& transition);
53  std::string getTransitionParameter(const toolbox::fsm::State from,
54  const std::string& transition);
55  std::string getTransitionFinalStateName(const std::string& transition);
56  const std::string& getErrorMessage() const;
57  const std::string& getStateMachineName(void) const { return stateMachineName_; }
58  void setStateMachineName(const std::string& name) { stateMachineName_ = name; }
59 
60  const xoap::MessageReference& getCurrentMessage(void);
61 
62  bool execTransition(const std::string& transition);
63  bool execTransition(const std::string& transition,
64  const xoap::MessageReference& message);
65  bool isInTransition(void);
66  void setInitialState(toolbox::fsm::State state);
67  void setErrorMessage(const std::string& errMessage, bool append = true);
68 
69  protected:
70  time_t stateEntranceTime_;
71 
72  // The volatile keyword indicates that a field might be modified by multiple
73  // concurrently executing threads. Fields that are declared volatile are not subject
74  // to compiler optimizations that assume access by a single thread. This ensures that
75  // the most up-to-date value is present in the field at all times. If you don't mark
76  // it volatile, the generated code might optimize the value into a registry and your
77  // thread will never see the change The atomicity has nothing to do with the
78  // visibility between threads... just because an operation is executed in one CPU
79  // cycle (atomic) it doesn't mean that the result will be visible to the other threads
80  // unless the value is marked volatile
81  volatile bool inTransition_;
82  toolbox::fsm::State provenanceState_;
83  std::map<toolbox::fsm::State,
84  std::map<std::string, std::string, std::less<std::string> > >
85  stateTransitionNameTable_;
86  std::map<toolbox::fsm::State,
87  std::map<std::string, std::string, std::less<std::string> > >
88  stateTransitionParameterTable_;
89 
90  xoap::MessageReference theMessage_;
91  std::string theErrorMessage_;
92  std::string stateMachineName_;
93 
94  private:
95 };
96 
97 } // namespace ots
98 
99 #endif