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