CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Attributes
evf::StateMachine Class Reference

#include <StateMachine.h>

Inheritance diagram for evf::StateMachine:

Public Member Functions

bool checkIfEnabled ()
 
xoap::MessageReference commandCallback (xoap::MessageReference msg) throw (xoap::exception::Exception)
 
void disableRcmsStateNotification ()
 
void failed (toolbox::Event::Reference e) throw (toolbox::fsm::exception::Exception)
 
void findRcmsStateListener ()
 
void fireEvent (const std::string &evtType, void *originator)
 
void fireFailed (const std::string &errorMsg, void *originator)
 
xdata::Boolean * foundRcmsStateListener ()
 
template<class T >
void initialize (T *app)
 
xdata::Bag
< xdaq2rc::ClassnameAndInstance > * 
rcmsStateListener ()
 
void stateChanged (toolbox::fsm::FiniteStateMachine &fsm) throw (toolbox::fsm::exception::Exception)
 
 StateMachine (xdaq::Application *app)
 
xdata::String * stateName ()
 
virtual ~StateMachine ()
 

Private Attributes

xdata::InfoSpace * appInfoSpace_
 
std::string appNameAndInstance_
 
toolbox::task::ActionSignature * asConfiguring_
 
toolbox::task::ActionSignature * asEnabling_
 
toolbox::task::ActionSignature * asHalting_
 
toolbox::task::ActionSignature * asStopping_
 
bool doStateNotification_
 
toolbox::fsm::FiniteStateMachine fsm_
 
log4cplus::Logger logger_
 
xdaq2rc::RcmsStateNotifier rcmsStateNotifier_
 
xdata::String stateName_
 
toolbox::task::WorkLoop * workLoopConfiguring_
 
toolbox::task::WorkLoop * workLoopEnabling_
 
toolbox::task::WorkLoop * workLoopHalting_
 
toolbox::task::WorkLoop * workLoopStopping_
 

Detailed Description

Definition at line 28 of file StateMachine.h.

Constructor & Destructor Documentation

StateMachine::StateMachine ( xdaq::Application *  app)

Definition at line 36 of file StateMachine.cc.

References appNameAndInstance_.

37  : logger_(app->getApplicationLogger())
38  , appInfoSpace_(app->getApplicationInfoSpace())
39  , doStateNotification_(true)
43  , workLoopHalting_(0)
44  , asConfiguring_(0)
45  , asEnabling_(0)
46  , asStopping_(0)
47  , asHalting_(0)
48  , rcmsStateNotifier_(app->getApplicationLogger(),
49  app->getApplicationDescriptor(),
50  app->getApplicationContext())
51 {
52  ostringstream oss;
53  oss<<app->getApplicationDescriptor()->getClassName()
54  <<app->getApplicationDescriptor()->getInstance();
55  appNameAndInstance_ = oss.str();
56 }
toolbox::task::WorkLoop * workLoopEnabling_
Definition: StateMachine.h:175
xdata::InfoSpace * appInfoSpace_
Definition: StateMachine.h:165
toolbox::task::ActionSignature * asConfiguring_
Definition: StateMachine.h:180
toolbox::task::ActionSignature * asHalting_
Definition: StateMachine.h:183
toolbox::task::ActionSignature * asEnabling_
Definition: StateMachine.h:181
toolbox::task::WorkLoop * workLoopConfiguring_
Definition: StateMachine.h:174
xdaq2rc::RcmsStateNotifier rcmsStateNotifier_
Definition: StateMachine.h:186
toolbox::task::WorkLoop * workLoopStopping_
Definition: StateMachine.h:176
log4cplus::Logger logger_
Definition: StateMachine.h:164
toolbox::task::WorkLoop * workLoopHalting_
Definition: StateMachine.h:177
std::string appNameAndInstance_
Definition: StateMachine.h:166
toolbox::task::ActionSignature * asStopping_
Definition: StateMachine.h:182
StateMachine::~StateMachine ( )
virtual

Definition at line 60 of file StateMachine.cc.

61 {
62 
63 }

Member Function Documentation

bool evf::StateMachine::checkIfEnabled ( )
inline

Definition at line 66 of file StateMachine.h.

References fsm_.

66 {return fsm_.getCurrentState()=='E';}
toolbox::fsm::FiniteStateMachine fsm_
Definition: StateMachine.h:171
xoap::MessageReference StateMachine::commandCallback ( xoap::MessageReference  msg)
throw (xoap::exception::Exception
)

Definition at line 71 of file StateMachine.cc.

References edmPickEvents::command, alignCSCRings::e, web.browse_db::env, edm::hlt::Exception, i, lumiQueryAPI::msg, python.Node::node, and evf::utils::state.

Referenced by evf::FUEventProcessor::fsmCallback().

73 {
74  xoap::SOAPPart part =msg->getSOAPPart();
75  xoap::SOAPEnvelope env =part.getEnvelope();
76  xoap::SOAPBody body =env.getBody();
77  DOMNode *node =body.getDOMNode();
78  DOMNodeList *bodyList=node->getChildNodes();
79  DOMNode *command =0;
80  string commandName;
81 
82  for (unsigned int i=0;i<bodyList->getLength();i++) {
83  command = bodyList->item(i);
84  if(command->getNodeType() == DOMNode::ELEMENT_NODE) {
85  commandName = xoap::XMLCh2String(command->getLocalName());
86  break;
87  }
88  }
89 
90  if (commandName.empty()) {
91  XCEPT_RAISE(xoap::exception::Exception,"Command not found.");
92  }
93 
94  // fire appropriate event and create according response message
95  try {
96  toolbox::Event::Reference e(new toolbox::Event(commandName,this));
97  fsm_.fireEvent(e);
98 
99  // response string
100  xoap::MessageReference reply = xoap::createMessage();
101  xoap::SOAPEnvelope envelope = reply->getSOAPPart().getEnvelope();
102  xoap::SOAPName responseName = envelope.createName(commandName+"Response",
103  "xdaq",XDAQ_NS_URI);
104  xoap::SOAPBodyElement responseElem =
105  envelope.getBody().addBodyElement(responseName);
106 
107  // state string
108  int iState = fsm_.getCurrentState();
109  string state = fsm_.getStateName(iState);
110  xoap::SOAPName stateName = envelope.createName("state",
111  "xdaq",XDAQ_NS_URI);
112  xoap::SOAPElement stateElem = responseElem.addChildElement(stateName);
113  xoap::SOAPName attributeName = envelope.createName("stateName",
114  "xdaq",XDAQ_NS_URI);
115  stateElem.addAttribute(attributeName,state);
116 
117  return reply;
118  }
120  XCEPT_RETHROW(xoap::exception::Exception,"invalid command.",e);
121  }
122 }
int i
Definition: DBlmapReader.cc:9
tuple node
Definition: Node.py:50
xdata::String * stateName()
Definition: StateMachine.h:69
part
Definition: HCALResponse.h:21
char state
Definition: procUtils.cc:75
toolbox::fsm::FiniteStateMachine fsm_
Definition: StateMachine.h:171
void evf::StateMachine::disableRcmsStateNotification ( )
inline
void StateMachine::failed ( toolbox::Event::Reference  e)
throw (toolbox::fsm::exception::Exception
)

Definition at line 184 of file StateMachine.cc.

References alignCSCRings::e, and evf::FsmFailedEvent::errorMessage().

Referenced by initialize().

186 {
187  if (typeid(*e) == typeid(toolbox::fsm::FailedEvent)) {
188  toolbox::fsm::FailedEvent &fe=dynamic_cast<toolbox::fsm::FailedEvent&>(*e);
189  LOG4CPLUS_FATAL(logger_,"Failure occurred in transition from '"
190  <<fe.getFromState()<<"' to '"<<fe.getToState()
191  <<"', exception history: "
192  <<xcept::stdformat_exception_history(fe.getException()));
193  }
194  else if (typeid(*e) == typeid(evf::FsmFailedEvent)) {
195  evf::FsmFailedEvent &fe=dynamic_cast<evf::FsmFailedEvent&>(*e);
196  LOG4CPLUS_FATAL(logger_,"fsm failure occured: "<<fe.errorMessage());
197  }
198 }
std::string errorMessage()
log4cplus::Logger logger_
Definition: StateMachine.h:164
void StateMachine::findRcmsStateListener ( )

Definition at line 218 of file StateMachine.cc.

References appInfoSpace_, and rcmsStateNotifier_.

Referenced by evf::BU::BU(), and evf::FUEventProcessor::FUEventProcessor().

219 {
220  rcmsStateNotifier_.findRcmsStateListener(); //might not be needed
221  rcmsStateNotifier_.subscribeToChangesInRcmsStateListener(appInfoSpace_);
222 }
xdata::InfoSpace * appInfoSpace_
Definition: StateMachine.h:165
xdaq2rc::RcmsStateNotifier rcmsStateNotifier_
Definition: StateMachine.h:186
void evf::StateMachine::fireEvent ( const std::string &  evtType,
void *  originator 
)
void StateMachine::fireFailed ( const std::string &  errorMsg,
void *  originator 
)
xdata::Boolean* evf::StateMachine::foundRcmsStateListener ( )
inline

Definition at line 78 of file StateMachine.h.

References rcmsStateNotifier_.

Referenced by evf::BU::exportParameters(), and evf::FUEventProcessor::FUEventProcessor().

79  {
80  return rcmsStateNotifier_.getFoundRcmsStateListenerParameter();
81  }
xdaq2rc::RcmsStateNotifier rcmsStateNotifier_
Definition: StateMachine.h:186
template<class T >
void evf::StateMachine::initialize ( T app)
inline

Definition at line 84 of file StateMachine.h.

References appNameAndInstance_, asConfiguring_, asEnabling_, asHalting_, asStopping_, failed(), fsm_, stateChanged(), stateName_, workLoopConfiguring_, workLoopEnabling_, workLoopHalting_, and workLoopStopping_.

Referenced by evf::BU::BU(), and evf::FUEventProcessor::FUEventProcessor().

85  {
86  // action signatures
87  asConfiguring_ = toolbox::task::bind(app,&T::configuring,"configuring");
88  asEnabling_ = toolbox::task::bind(app,&T::enabling, "enabling");
89  asStopping_ = toolbox::task::bind(app,&T::stopping, "stopping");
90  asHalting_ = toolbox::task::bind(app,&T::halting, "halting");
91 
92  // work loops
94  toolbox::task::getWorkLoopFactory()->getWorkLoop(appNameAndInstance_+
95  "_Configuring",
96  "waiting");
98  toolbox::task::getWorkLoopFactory()->getWorkLoop(appNameAndInstance_+
99  "_Enabling",
100  "waiting");
102  toolbox::task::getWorkLoopFactory()->getWorkLoop(appNameAndInstance_+
103  "_Stopping",
104  "waiting");
106  toolbox::task::getWorkLoopFactory()->getWorkLoop(appNameAndInstance_+
107  "_Halting",
108  "waiting");
109 
110 
111  // bind SOAP callbacks
112  xoap::bind(app,&T::fsmCallback,"Configure",XDAQ_NS_URI);
113  xoap::bind(app,&T::fsmCallback,"Enable", XDAQ_NS_URI);
114  xoap::bind(app,&T::fsmCallback,"Stop", XDAQ_NS_URI);
115  xoap::bind(app,&T::fsmCallback,"Halt", XDAQ_NS_URI);
116 
117  // define finite state machine, states&transitions
118  fsm_.addState('h', "halting" ,this,&evf::StateMachine::stateChanged);
119  fsm_.addState('H', "Halted" ,this,&evf::StateMachine::stateChanged);
120  fsm_.addState('c', "configuring",this,&evf::StateMachine::stateChanged);
121  fsm_.addState('R', "Ready" ,this,&evf::StateMachine::stateChanged);
122  fsm_.addState('e', "enabling" ,this,&evf::StateMachine::stateChanged);
123  fsm_.addState('E', "Enabled" ,this,&evf::StateMachine::stateChanged);
124  fsm_.addState('s', "stopping" ,this,&evf::StateMachine::stateChanged);
125 
126  fsm_.addStateTransition('H','c',"Configure");
127  fsm_.addStateTransition('c','R',"ConfigureDone");
128  fsm_.addStateTransition('R','e',"Enable");
129  fsm_.addStateTransition('e','E',"EnableDone");
130  fsm_.addStateTransition('E','s',"Stop");
131  fsm_.addStateTransition('s','R',"StopDone");
132  fsm_.addStateTransition('E','h',"Halt");
133  fsm_.addStateTransition('R','h',"Halt");
134  fsm_.addStateTransition('h','H',"HaltDone");
135 
136  fsm_.addStateTransition('c','F',"Fail",this,&evf::StateMachine::failed);
137  fsm_.addStateTransition('e','F',"Fail",this,&evf::StateMachine::failed);
138  fsm_.addStateTransition('s','F',"Fail",this,&evf::StateMachine::failed);
139  fsm_.addStateTransition('h','F',"Fail",this,&evf::StateMachine::failed);
140 
141  fsm_.addStateTransition('E','F',"Fail",this,&evf::StateMachine::failed);
142 
143  fsm_.setFailedStateTransitionAction(this,&evf::StateMachine::failed);
144  fsm_.setFailedStateTransitionChanged(this,&evf::StateMachine::stateChanged);
145  fsm_.setStateName('F',"Failed");
146 
147  fsm_.setInitialState('H');
148  fsm_.reset();
149  stateName_ = fsm_.getStateName(fsm_.getCurrentState());
150 
151  if (!workLoopConfiguring_->isActive()) workLoopConfiguring_->activate();
152  if (!workLoopEnabling_->isActive()) workLoopEnabling_ ->activate();
153  if (!workLoopStopping_->isActive()) workLoopStopping_ ->activate();
154  if (!workLoopHalting_->isActive()) workLoopHalting_ ->activate();
155  }
toolbox::task::WorkLoop * workLoopEnabling_
Definition: StateMachine.h:175
toolbox::task::ActionSignature * asConfiguring_
Definition: StateMachine.h:180
toolbox::task::ActionSignature * asHalting_
Definition: StateMachine.h:183
void failed(toolbox::Event::Reference e)
toolbox::task::ActionSignature * asEnabling_
Definition: StateMachine.h:181
toolbox::task::WorkLoop * workLoopConfiguring_
Definition: StateMachine.h:174
xdata::String stateName_
Definition: StateMachine.h:167
toolbox::task::WorkLoop * workLoopStopping_
Definition: StateMachine.h:176
toolbox::task::WorkLoop * workLoopHalting_
Definition: StateMachine.h:177
std::string appNameAndInstance_
Definition: StateMachine.h:166
void stateChanged(toolbox::fsm::FiniteStateMachine &fsm)
toolbox::task::ActionSignature * asStopping_
Definition: StateMachine.h:182
toolbox::fsm::FiniteStateMachine fsm_
Definition: StateMachine.h:171
xdata::Bag<xdaq2rc::ClassnameAndInstance>* evf::StateMachine::rcmsStateListener ( )
inline

Definition at line 72 of file StateMachine.h.

References rcmsStateNotifier_.

Referenced by evf::BU::exportParameters(), and evf::FUEventProcessor::FUEventProcessor().

73  {
74  return rcmsStateNotifier_.getRcmsStateListenerParameter();
75  }
xdaq2rc::RcmsStateNotifier rcmsStateNotifier_
Definition: StateMachine.h:186
void StateMachine::stateChanged ( toolbox::fsm::FiniteStateMachine &  fsm)
throw (toolbox::fsm::exception::Exception
)

Definition at line 126 of file StateMachine.cc.

References alignCSCRings::e, edm::hlt::Exception, and evf::utils::state.

Referenced by initialize().

128 {
129  stateName_ = fsm_.getStateName(fsm_.getCurrentState());
130  string state = stateName_.toString();
131 
132  LOG4CPLUS_INFO(logger_,"New state is: "<<state);
133 
134  if (state=="configuring") {
135  try {
137  }
138  catch (xdaq::exception::Exception& e) {
139  LOG4CPLUS_ERROR(logger_,xcept::stdformat_exception_history(e));
140  }
141  }
142  else if (state=="enabling") {
143  try {
145  }
146  catch (xdaq::exception::Exception& e) {
147  LOG4CPLUS_ERROR(logger_,xcept::stdformat_exception_history(e));
148  }
149  }
150  else if (state=="stopping") {
151  try {
153  }
154  catch (xdaq::exception::Exception& e) {
155  LOG4CPLUS_ERROR(logger_,xcept::stdformat_exception_history(e));
156  }
157  }
158  else if (state=="halting") {
159  try {
160  workLoopHalting_->submit(asHalting_);
161  }
162  catch (xdaq::exception::Exception& e) {
163  LOG4CPLUS_ERROR(logger_,xcept::stdformat_exception_history(e));
164  }
165  }
166  else if (state=="Halted"||state=="Ready"||state=="Enabled"||state=="Failed") {
168  {
169  try {
170  rcmsStateNotifier_.stateChanged(state,appNameAndInstance_+
171  " has reached target state " +
172  state);
173  }
174  catch (xcept::Exception& e) {
175  LOG4CPLUS_ERROR(logger_,"Failed to notify state change: "
176  <<xcept::stdformat_exception_history(e));
177  }
178  }
179  }
180 }
toolbox::task::WorkLoop * workLoopEnabling_
Definition: StateMachine.h:175
toolbox::task::ActionSignature * asConfiguring_
Definition: StateMachine.h:180
toolbox::task::ActionSignature * asHalting_
Definition: StateMachine.h:183
toolbox::task::ActionSignature * asEnabling_
Definition: StateMachine.h:181
toolbox::task::WorkLoop * workLoopConfiguring_
Definition: StateMachine.h:174
xdaq2rc::RcmsStateNotifier rcmsStateNotifier_
Definition: StateMachine.h:186
xdata::String stateName_
Definition: StateMachine.h:167
toolbox::task::WorkLoop * workLoopStopping_
Definition: StateMachine.h:176
log4cplus::Logger logger_
Definition: StateMachine.h:164
toolbox::task::WorkLoop * workLoopHalting_
Definition: StateMachine.h:177
char state
Definition: procUtils.cc:75
std::string appNameAndInstance_
Definition: StateMachine.h:166
toolbox::task::ActionSignature * asStopping_
Definition: StateMachine.h:182
toolbox::fsm::FiniteStateMachine fsm_
Definition: StateMachine.h:171
xdata::String* evf::StateMachine::stateName ( )
inline

Member Data Documentation

xdata::InfoSpace* evf::StateMachine::appInfoSpace_
private

Definition at line 165 of file StateMachine.h.

Referenced by findRcmsStateListener().

std::string evf::StateMachine::appNameAndInstance_
private

Definition at line 166 of file StateMachine.h.

Referenced by initialize(), and StateMachine().

toolbox::task::ActionSignature* evf::StateMachine::asConfiguring_
private

Definition at line 180 of file StateMachine.h.

Referenced by initialize().

toolbox::task::ActionSignature* evf::StateMachine::asEnabling_
private

Definition at line 181 of file StateMachine.h.

Referenced by initialize().

toolbox::task::ActionSignature* evf::StateMachine::asHalting_
private

Definition at line 183 of file StateMachine.h.

Referenced by initialize().

toolbox::task::ActionSignature* evf::StateMachine::asStopping_
private

Definition at line 182 of file StateMachine.h.

Referenced by initialize().

bool evf::StateMachine::doStateNotification_
private

Definition at line 168 of file StateMachine.h.

Referenced by disableRcmsStateNotification().

toolbox::fsm::FiniteStateMachine evf::StateMachine::fsm_
private

Definition at line 171 of file StateMachine.h.

Referenced by checkIfEnabled(), fireFailed(), and initialize().

log4cplus::Logger evf::StateMachine::logger_
private

Definition at line 164 of file StateMachine.h.

xdaq2rc::RcmsStateNotifier evf::StateMachine::rcmsStateNotifier_
private
xdata::String evf::StateMachine::stateName_
private

Definition at line 167 of file StateMachine.h.

Referenced by initialize(), and stateName().

toolbox::task::WorkLoop* evf::StateMachine::workLoopConfiguring_
private

Definition at line 174 of file StateMachine.h.

Referenced by initialize().

toolbox::task::WorkLoop* evf::StateMachine::workLoopEnabling_
private

Definition at line 175 of file StateMachine.h.

Referenced by initialize().

toolbox::task::WorkLoop* evf::StateMachine::workLoopHalting_
private

Definition at line 177 of file StateMachine.h.

Referenced by initialize().

toolbox::task::WorkLoop* evf::StateMachine::workLoopStopping_
private

Definition at line 176 of file StateMachine.h.

Referenced by initialize().