1 #include "otsdaq-core/FiniteStateMachine/RunControlStateMachine.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
4 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
5 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
7 #include <toolbox/fsm/FailedEvent.h>
8 #include <xoap/Method.h>
9 #include <xdaq/NamespaceURI.h>
15 const std::string RunControlStateMachine::FAILED_STATE_NAME =
"Failed";
18 RunControlStateMachine::RunControlStateMachine(std::string name)
19 : stateMachineName_(name)
21 theStateMachine_.addState(
'I',
"Initial",
this, &RunControlStateMachine::stateInitial);
22 theStateMachine_.addState(
'H',
"Halted",
this, &RunControlStateMachine::stateHalted);
23 theStateMachine_.addState(
'C',
"Configured",
this, &RunControlStateMachine::stateConfigured);
24 theStateMachine_.addState(
'R',
"Running",
this, &RunControlStateMachine::stateRunning);
25 theStateMachine_.addState(
'P',
"Paused",
this, &RunControlStateMachine::statePaused);
33 theStateMachine_.setStateName(
'F',RunControlStateMachine::FAILED_STATE_NAME);
34 theStateMachine_.setFailedStateTransitionAction (
this, &RunControlStateMachine::enteringError);
35 theStateMachine_.setFailedStateTransitionChanged(
this, &RunControlStateMachine::inError);
38 theStateMachine_.addStateTransition(
'F',
'H',
"Halt" ,
"Halting" ,
this, &RunControlStateMachine::transitionHalting);
42 theStateMachine_.addStateTransition(
'H',
'C',
"Configure" ,
"Configuring" ,
"ConfigurationAlias",
this, &RunControlStateMachine::transitionConfiguring);
45 theStateMachine_.addStateTransition(
'I',
'H',
"Initialize",
"Initializing",
this, &RunControlStateMachine::transitionInitializing);
46 theStateMachine_.addStateTransition(
'H',
'H',
"Halt" ,
"Halting" ,
this, &RunControlStateMachine::transitionHalting);
47 theStateMachine_.addStateTransition(
'C',
'H',
"Halt" ,
"Halting" ,
this, &RunControlStateMachine::transitionHalting);
48 theStateMachine_.addStateTransition(
'R',
'H',
"Abort" ,
"Aborting" ,
this, &RunControlStateMachine::transitionHalting);
49 theStateMachine_.addStateTransition(
'P',
'H',
"Abort" ,
"Aborting" ,
this, &RunControlStateMachine::transitionHalting);
52 theStateMachine_.addStateTransition(
'R',
'P',
"Pause" ,
"Pausing" ,
this, &RunControlStateMachine::transitionPausing);
53 theStateMachine_.addStateTransition(
'P',
'R',
"Resume" ,
"Resuming" ,
this, &RunControlStateMachine::transitionResuming);
54 theStateMachine_.addStateTransition(
'C',
'R',
"Start" ,
"Starting" ,
this, &RunControlStateMachine::transitionStarting);
55 theStateMachine_.addStateTransition(
'R',
'C',
"Stop" ,
"Stopping" ,
this, &RunControlStateMachine::transitionStopping);
56 theStateMachine_.addStateTransition(
'P',
'C',
"Stop" ,
"Stopping" ,
this, &RunControlStateMachine::transitionStopping);
61 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Initialize", XDAQ_NS_URI);
62 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Configure" , XDAQ_NS_URI);
63 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Start" , XDAQ_NS_URI);
64 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Stop" , XDAQ_NS_URI);
65 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Pause" , XDAQ_NS_URI);
66 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Resume" , XDAQ_NS_URI);
67 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Halt" , XDAQ_NS_URI);
68 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Abort" , XDAQ_NS_URI);
75 RunControlStateMachine::~RunControlStateMachine(
void)
80 void RunControlStateMachine::reset(
void)
82 __MOUT__ << stateMachineName_ <<
" is in transition?" << theStateMachine_.isInTransition() << std::endl;
83 theStateMachine_.setInitialState(
'I');
84 theStateMachine_.reset();
85 __MOUT__ << stateMachineName_ <<
" is in transition?" << theStateMachine_.isInTransition() << std::endl;
89 xoap::MessageReference RunControlStateMachine::runControlMessageHandler(
90 xoap::MessageReference message)
91 throw (xoap::exception::Exception)
93 __MOUT__ <<
"Starting state for " << stateMachineName_ <<
" is " << theStateMachine_.getCurrentStateName() << std::endl;
94 __MOUT__ << SOAPUtilities::translate(message) << std::endl;
95 std::string command = SOAPUtilities::translate(message).getCommand();
97 theProgressBar_.reset(command,stateMachineName_);
98 std::string result = command +
"Done";
102 if(command ==
"Error" || command ==
"Fail")
104 __SS__ << command <<
" was received! Immediately throwing FSM exception." << std::endl;
105 __MOUT_ERR__ <<
"\n" << ss.str();
106 XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());
107 return SOAPUtilities::makeSOAPMessageReference(result);
113 theStateMachine_.execTransition(command,message);
116 if(theStateMachine_.getCurrentStateName() == RunControlStateMachine::FAILED_STATE_NAME)
118 result = command +
" " + RunControlStateMachine::FAILED_STATE_NAME +
": " + theStateMachine_.getErrorMessage();
119 __MOUT_ERR__ <<
"Unexpected Failure state for " << stateMachineName_ <<
" is " << theStateMachine_.getCurrentStateName() << std::endl;
120 __MOUT_ERR__ <<
"Error message was as follows: " << theStateMachine_.getErrorMessage() << std::endl;
123 catch (toolbox::fsm::exception::Exception& e)
125 result = command +
" " + RunControlStateMachine::FAILED_STATE_NAME +
": " + theStateMachine_.getErrorMessage();
126 __SS__ <<
"Run Control Message Handling Failed: " << e.what() << std::endl;
127 __MOUT_ERR__ <<
"\n" << ss.str();
128 __MOUT_ERR__ <<
"Error message was as follows: " << theStateMachine_.getErrorMessage() << std::endl;
131 theProgressBar_.complete();
132 __MOUT__ <<
"Ending state for " << stateMachineName_ <<
" is " << theStateMachine_.getCurrentStateName() << std::endl;
133 __MOUT__ <<
"result = " << result << std::endl;
134 return SOAPUtilities::makeSOAPMessageReference(result);