otsdaq  v1_01_04
 All Classes Namespaces Functions
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(toolbox::fsm::State from,
20  toolbox::fsm::State to,
21  const std::string& input,
22  const std::string& transitionName,
23  OBJECT * obj,
24  void (OBJECT::*func)(toolbox::Event::Reference) )
25  throw (toolbox::fsm::exception::Exception)
26  {
27  stateTransitionNameTable_[from][input] = transitionName;
28  toolbox::fsm::FiniteStateMachine::addStateTransition(from, to, input, obj, func);
29  }
30 
31  template <class OBJECT>
32  void addStateTransition(toolbox::fsm::State from,
33  toolbox::fsm::State to,
34  const std::string &input,
35  const std::string &transitionName,
36  const std::string &transitionParameter,
37  OBJECT * obj,
38  void (OBJECT::*func)(toolbox::Event::Reference) )
39  throw (toolbox::fsm::exception::Exception)
40  {
41  stateTransitionParameterTable_[from][input] = transitionParameter;
42  addStateTransition(from, to, input, transitionName, obj, func);
43  }
44 
45  toolbox::fsm::State getProvenanceState (void);
46  toolbox::fsm::State getTransitionFinalState(const std::string &transition) throw (toolbox::fsm::exception::Exception);
47 
48  std::string getProvenanceStateName (void);
49  std::string getCurrentStateName (void);
50  time_t getTimeInState (void);
51  std::string getCurrentTransitionName (const std::string &transition) throw (toolbox::fsm::exception::Exception);
52  std::string getTransitionName (const toolbox::fsm::State from, const std::string &transition) throw (toolbox::fsm::exception::Exception);
53  std::string getTransitionParameter (const toolbox::fsm::State from, const std::string &transition) throw (toolbox::fsm::exception::Exception);
54  std::string getTransitionFinalStateName(const std::string &transition) throw (toolbox::fsm::exception::Exception);
55  const std::string &getErrorMessage() const;
56 
57  const xoap::MessageReference& getCurrentMessage(void);
58 
59  bool execTransition (const std::string &transition) throw (toolbox::fsm::exception::Exception);
60  bool execTransition (const std::string &transition, const xoap::MessageReference& message) throw (toolbox::fsm::exception::Exception);
61  bool isInTransition (void);
62  void setInitialState(toolbox::fsm::State state);
63  void setErrorMessage(const std::string &errMessage);
64 
65 protected:
66 
67  time_t stateEntranceTime_;
68 
69  //The volatile keyword indicates that a field might be modified by multiple concurrently executing threads.
70  //Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread.
71  //This ensures that the most up-to-date value is present in the field at all times.
72  //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
73  //The atomicity has nothing to do with the visibility between threads...
74  //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
75  //unless the value is marked volatile
76  volatile bool inTransition_;
77  toolbox::fsm::State provenanceState_;
78  std::map<toolbox::fsm::State, std::map<std::string, std::string, std::less<std::string> > > stateTransitionNameTable_;
79  std::map<toolbox::fsm::State, std::map<std::string, std::string, std::less<std::string> > > stateTransitionParameterTable_;
80 
81  xoap::MessageReference theMessage_;
82  std::string theErrorMessage_;
83 
84 private:
85 };
86 
87 }
88 
89 #endif