CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EPStateMachine.h
Go to the documentation of this file.
1 #ifndef EPSTATEMACHINE_H
2 #define EPSTATEMACHINE_H
3 
4 #include "toolbox/fsm/FiniteStateMachine.h"
5 #include "toolbox/fsm/FailedEvent.h"
6 #include "xcept/tools.h"
7 #include "xoap/MessageFactory.h"
8 #include "xoap/Method.h"
9 #include "log4cplus/logger.h"
10 #include "xdata/String.h"
11 #include "xdaq/NamespaceURI.h"
12 
13 namespace evf
14 {
15  class EPStateMachine : public toolbox::fsm::FiniteStateMachine
16  {
17  public:
18  EPStateMachine(log4cplus::Logger &logger);
23 
24 
29  xdata::String stateName_;
30 
31 
32  template<class T> void init(T*me)
33  {
34  // Define FSM states
35  addState('H', "Halted" , this, &EPStateMachine::stateChanged);
36  addState('R', "Ready" , this, &EPStateMachine::stateChanged);
37  addState('E', "Enabled" , this, &EPStateMachine::stateChanged);
38  addState('S', "Suspended", this, &EPStateMachine::stateChanged);
39 
40  // Define FSM transitions
41  addStateTransition('H', 'R', "Configure", me, &T::configureAction);
42  addStateTransition('R', 'E', "Enable", me, &T::enableAction);
43  addStateTransition('E', 'R', "Stop", me, &T::stopAction);
44  addStateTransition('E', 'S', "Suspend", me, &T::suspendAction);
45  addStateTransition('S', 'E', "Resume", me, &T::resumeAction);
46  addStateTransition('H', 'H', "Halt", me, &T::nullAction);
47  addStateTransition('R', 'H', "Halt", me, &T::haltAction);
48  addStateTransition('E', 'H', "Halt", me, &T::haltAction);
49  addStateTransition('S', 'H', "Halt", me, &T::haltAction);
50 
51  setFailedStateTransitionAction(this,&EPStateMachine::failedTransition);
52  setFailedStateTransitionChanged(this,&EPStateMachine::stateChanged);
53 
54  setInitialState('H');
55  reset();
56 
57  xoap::bind(me,&T::fireEvent,"Configure", XDAQ_NS_URI);
58  xoap::bind(me,&T::fireEvent,"Stop" , XDAQ_NS_URI);
59  xoap::bind(me,&T::fireEvent,"Enable" , XDAQ_NS_URI);
60  xoap::bind(me,&T::fireEvent,"Suspend" , XDAQ_NS_URI);
61  xoap::bind(me,&T::fireEvent,"Resume" , XDAQ_NS_URI);
62  xoap::bind(me,&T::fireEvent,"Halt" , XDAQ_NS_URI);
63  xoap::bind(me,&T::fireEvent,"Disable" , XDAQ_NS_URI);
64  xoap::bind(me,&T::fireEvent,"Fail" , XDAQ_NS_URI);
65  }
66 
67 
68  void failedTransition(toolbox::Event::Reference e)
70  {
71  toolbox::fsm::FailedEvent &fe =
72  dynamic_cast<toolbox::fsm::FailedEvent&>(*e);
73 
74  LOG4CPLUS_FATAL(logger_,
75  "Failure occurred when performing transition from: "
76  << fe.getFromState() << " to: " << fe.getToState()
77  << " exception: " << fe.getException().what());
78  }
79 
80  void stateChanged(toolbox::fsm::FiniteStateMachine & fsm)
82  {
83  LOG4CPLUS_INFO(logger_,
84  "Changed to state: "
85  << getStateName(getCurrentState()));
86  }
87 
88 
93  void reset() throw (toolbox::fsm::exception::Exception)
94  {
96 
97  state_ = FiniteStateMachine::getCurrentState();
98  stateName_ = FiniteStateMachine::getStateName(state_);
99  }
100 
101 
106  void fireEvent(toolbox::Event::Reference e)
108  {
109  try{
110  FiniteStateMachine::fireEvent(e);
111  }
113  {
114  LOG4CPLUS_ERROR(logger_,"EPStateMachine fireEvent failed "
115  << ex.what());
116  }
117  catch(...)
118  {
119  LOG4CPLUS_ERROR(logger_,"EPStateMachine fireEvent failed "
120  << " Unknown Exception " << " state is "
121  << FiniteStateMachine::getCurrentState());
122  }
123 
124 
125  state_ = FiniteStateMachine::getCurrentState();
126  stateName_ = FiniteStateMachine::getStateName(state_);
127  }
128 
129  xoap::MessageReference processFSMCommand(const std::string cmdName)
131  xoap::MessageReference createFSMReplyMsg(const std::string cmd,
132  const std::string state);
133 
134  private:
135  log4cplus::Logger &logger_;
136 
137  };
138 }
139 #endif
std::ostream & logger()
Definition: fwLog.cc:41
void fireEvent(toolbox::Event::Reference e)
xoap::MessageReference createFSMReplyMsg(const std::string cmd, const std::string state)
toolbox::fsm::State state_
xdata::String stateName_
void failedTransition(toolbox::Event::Reference e)
string cmd
Definition: asciidump.py:19
xoap::MessageReference processFSMCommand(const std::string cmdName)
char state
Definition: procUtils.cc:75
void stateChanged(toolbox::fsm::FiniteStateMachine &fsm)
EPStateMachine(log4cplus::Logger &logger)
long double T
void reset(double vett[256])
Definition: TPedValues.cc:11
log4cplus::Logger & logger_