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 INIT_MF(
"RunControlStateMachine");
23 theStateMachine_.addState(
'I',
"Initial",
this, &RunControlStateMachine::stateInitial);
24 theStateMachine_.addState(
'H',
"Halted",
this, &RunControlStateMachine::stateHalted);
25 theStateMachine_.addState(
'C',
"Configured",
this, &RunControlStateMachine::stateConfigured);
26 theStateMachine_.addState(
'R',
"Running",
this, &RunControlStateMachine::stateRunning);
27 theStateMachine_.addState(
'P',
"Paused",
this, &RunControlStateMachine::statePaused);
28 theStateMachine_.addState(
'S',
"Shutdown",
this, &RunControlStateMachine::stateShutdown);
36 theStateMachine_.setStateName(
'F',RunControlStateMachine::FAILED_STATE_NAME);
37 theStateMachine_.setFailedStateTransitionAction (
this, &RunControlStateMachine::enteringError);
38 theStateMachine_.setFailedStateTransitionChanged(
this, &RunControlStateMachine::inError);
41 theStateMachine_.addStateTransition(
'F',
'H',
"Halt" ,
"Halting" ,
this, &RunControlStateMachine::transitionHalting);
45 theStateMachine_.addStateTransition(
'H',
'C',
"Configure" ,
"Configuring" ,
"ConfigurationAlias",
this, &RunControlStateMachine::transitionConfiguring);
46 theStateMachine_.addStateTransition(
'H',
'S',
"Shutdown" ,
"Shutting Down",
this, &RunControlStateMachine::transitionShuttingDown);
47 theStateMachine_.addStateTransition(
'S',
'I',
"Startup" ,
"Starting Up" ,
this, &RunControlStateMachine::transitionStartingUp);
50 theStateMachine_.addStateTransition(
'I',
'H',
"Initialize",
"Initializing" ,
this, &RunControlStateMachine::transitionInitializing);
51 theStateMachine_.addStateTransition(
'H',
'H',
"Halt" ,
"Halting" ,
this, &RunControlStateMachine::transitionHalting);
52 theStateMachine_.addStateTransition(
'C',
'H',
"Halt" ,
"Halting" ,
this, &RunControlStateMachine::transitionHalting);
53 theStateMachine_.addStateTransition(
'R',
'H',
"Abort" ,
"Aborting" ,
this, &RunControlStateMachine::transitionHalting);
54 theStateMachine_.addStateTransition(
'P',
'H',
"Abort" ,
"Aborting" ,
this, &RunControlStateMachine::transitionHalting);
57 theStateMachine_.addStateTransition(
'R',
'P',
"Pause" ,
"Pausing" ,
this, &RunControlStateMachine::transitionPausing);
58 theStateMachine_.addStateTransition(
'P',
'R',
"Resume" ,
"Resuming" ,
this, &RunControlStateMachine::transitionResuming);
59 theStateMachine_.addStateTransition(
'C',
'R',
"Start" ,
"Starting" ,
this, &RunControlStateMachine::transitionStarting);
60 theStateMachine_.addStateTransition(
'R',
'C',
"Stop" ,
"Stopping" ,
this, &RunControlStateMachine::transitionStopping);
61 theStateMachine_.addStateTransition(
'P',
'C',
"Stop" ,
"Stopping" ,
this, &RunControlStateMachine::transitionStopping);
65 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Initialize", XDAQ_NS_URI);
66 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Configure" , XDAQ_NS_URI);
67 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Start" , XDAQ_NS_URI);
68 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Stop" , XDAQ_NS_URI);
69 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Pause" , XDAQ_NS_URI);
70 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Resume" , XDAQ_NS_URI);
71 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Halt" , XDAQ_NS_URI);
72 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Abort" , XDAQ_NS_URI);
73 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Shutdown" , XDAQ_NS_URI);
74 xoap::bind(
this, &RunControlStateMachine::runControlMessageHandler,
"Startup" , XDAQ_NS_URI);
81 RunControlStateMachine::~RunControlStateMachine(
void)
86 void RunControlStateMachine::reset(
void)
88 __COUT__ << stateMachineName_ <<
" is in transition?" << theStateMachine_.isInTransition() << std::endl;
89 theStateMachine_.setInitialState(
'I');
90 theStateMachine_.reset();
91 __COUT__ << stateMachineName_ <<
" is in transition?" << theStateMachine_.isInTransition() << std::endl;
95 xoap::MessageReference RunControlStateMachine::runControlMessageHandler(
96 xoap::MessageReference message)
97 throw (xoap::exception::Exception)
99 __COUT__ <<
"Starting state for " << stateMachineName_ <<
" is " <<
100 theStateMachine_.getCurrentStateName() << std::endl;
101 __COUT__ << SOAPUtilities::translate(message) << std::endl;
102 std::string command = SOAPUtilities::translate(message).getCommand();
104 theProgressBar_.reset(command,stateMachineName_);
105 std::string result = command +
"Done";
109 if(command ==
"Error" || command ==
"Fail")
111 __SS__ << command <<
" was received! Immediately throwing FSM exception." << std::endl;
112 __COUT_ERR__ <<
"\n" << ss.str();
113 XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());
114 return SOAPUtilities::makeSOAPMessageReference(result);
120 if(command ==
"Initialize" &&
121 theStateMachine_.getCurrentStateName() ==
"Halted")
123 __COUT__ <<
"Already Initialized.. ignoring Initialize command." << std::endl;
124 return SOAPUtilities::makeSOAPMessageReference(result);
130 theStateMachine_.execTransition(command,message);
133 if(theStateMachine_.getCurrentStateName() == RunControlStateMachine::FAILED_STATE_NAME)
135 result = command +
" " + RunControlStateMachine::FAILED_STATE_NAME +
": " + theStateMachine_.getErrorMessage();
136 __COUT_ERR__ <<
"Unexpected Failure state for " << stateMachineName_ <<
" is " << theStateMachine_.getCurrentStateName() << std::endl;
137 __COUT_ERR__ <<
"Error message was as follows: " << theStateMachine_.getErrorMessage() << std::endl;
140 catch (toolbox::fsm::exception::Exception& e)
142 result = command +
" " + RunControlStateMachine::FAILED_STATE_NAME +
": " + theStateMachine_.getErrorMessage();
143 __SS__ <<
"Run Control Message Handling Failed: " << e.what() << std::endl;
144 __COUT_ERR__ <<
"\n" << ss.str();
145 __COUT_ERR__ <<
"Error message was as follows: " << theStateMachine_.getErrorMessage() << std::endl;
148 theProgressBar_.complete();
149 __COUT__ <<
"Ending state for " << stateMachineName_ <<
" is " << theStateMachine_.getCurrentStateName() << std::endl;
150 __COUT__ <<
"result = " << result << std::endl;
151 return SOAPUtilities::makeSOAPMessageReference(result);