00001 #include "otsdaq-core/FiniteStateMachine/RunControlStateMachine.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00004 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
00005 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
00006
00007 #include <toolbox/fsm/FailedEvent.h>
00008 #include <xoap/Method.h>
00009 #include <xdaq/NamespaceURI.h>
00010
00011 #include <iostream>
00012
00013 using namespace ots;
00014
00015
00016
00017 RunControlStateMachine::RunControlStateMachine(std::string name)
00018 : stateMachineName_(name)
00019 {
00020 theStateMachine_.addState('I', "Initial", this, &RunControlStateMachine::stateInitial);
00021 theStateMachine_.addState('H', "Halted", this, &RunControlStateMachine::stateHalted);
00022 theStateMachine_.addState('C', "Configured", this, &RunControlStateMachine::stateConfigured);
00023 theStateMachine_.addState('R', "Running", this, &RunControlStateMachine::stateRunning);
00024 theStateMachine_.addState('P', "Paused", this, &RunControlStateMachine::statePaused);
00025
00026
00027
00028
00029
00030
00031
00032 theStateMachine_.setStateName('F',"Failed");
00033 theStateMachine_.setFailedStateTransitionAction (this, &RunControlStateMachine::enteringError);
00034 theStateMachine_.setFailedStateTransitionChanged(this, &RunControlStateMachine::inError);
00035
00036
00037 theStateMachine_.addStateTransition('F', 'H', "Halt" , "Halting" , this, &RunControlStateMachine::transitionHalting);
00038
00039
00040
00041 theStateMachine_.addStateTransition('H', 'C', "Configure" , "Configuring" , "ConfigurationAlias", this, &RunControlStateMachine::transitionConfiguring);
00042
00043
00044 theStateMachine_.addStateTransition('I', 'H', "Initialize", "Initializing", this, &RunControlStateMachine::transitionInitializing);
00045 theStateMachine_.addStateTransition('H', 'H', "Halt" , "Halting" , this, &RunControlStateMachine::transitionHalting);
00046 theStateMachine_.addStateTransition('C', 'H', "Halt" , "Halting" , this, &RunControlStateMachine::transitionHalting);
00047 theStateMachine_.addStateTransition('R', 'H', "Abort" , "Aborting" , this, &RunControlStateMachine::transitionHalting);
00048 theStateMachine_.addStateTransition('P', 'H', "Abort" , "Aborting" , this, &RunControlStateMachine::transitionHalting);
00049
00050
00051 theStateMachine_.addStateTransition('R', 'P', "Pause" , "Pausing" , this, &RunControlStateMachine::transitionPausing);
00052 theStateMachine_.addStateTransition('P', 'R', "Resume" , "Resuming" , this, &RunControlStateMachine::transitionResuming);
00053 theStateMachine_.addStateTransition('C', 'R', "Start" , "Starting" , this, &RunControlStateMachine::transitionStarting);
00054 theStateMachine_.addStateTransition('R', 'C', "Stop" , "Stopping" , this, &RunControlStateMachine::transitionStopping);
00055 theStateMachine_.addStateTransition('P', 'C', "Stop" , "Stopping" , this, &RunControlStateMachine::transitionStopping);
00056
00057
00058
00059
00060 xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Initialize", XDAQ_NS_URI);
00061 xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Configure" , XDAQ_NS_URI);
00062 xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Start" , XDAQ_NS_URI);
00063 xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Stop" , XDAQ_NS_URI);
00064 xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Pause" , XDAQ_NS_URI);
00065 xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Resume" , XDAQ_NS_URI);
00066 xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Halt" , XDAQ_NS_URI);
00067 xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Abort" , XDAQ_NS_URI);
00068
00069
00070 reset();
00071 }
00072
00073
00074 RunControlStateMachine::~RunControlStateMachine(void)
00075 {
00076 }
00077
00078
00079 void RunControlStateMachine::reset(void)
00080 {
00081 __MOUT__ << stateMachineName_ << " is in transition?" << theStateMachine_.isInTransition() << std::endl;
00082 theStateMachine_.setInitialState('I');
00083 theStateMachine_.reset();
00084 __MOUT__ << stateMachineName_ << " is in transition?" << theStateMachine_.isInTransition() << std::endl;
00085 }
00086
00087
00088 xoap::MessageReference RunControlStateMachine::runControlMessageHandler(
00089 xoap::MessageReference message)
00090 throw (xoap::exception::Exception)
00091 {
00092 __MOUT__ << "Starting state for " << stateMachineName_ << " is " << theStateMachine_.getCurrentStateName() << std::endl;
00093 __MOUT__ << SOAPUtilities::translate(message) << std::endl;
00094 std::string command = SOAPUtilities::translate(message).getCommand();
00095
00096 theProgressBar_.reset(command,stateMachineName_);
00097 std::string result = command + "Done";
00098
00099
00100
00101 if(command == "Error" || command == "Fail")
00102 {
00103 __SS__ << command << " was received! Immediately throwing FSM exception." << std::endl;
00104 __MOUT_ERR__ << "\n" << ss.str();
00105 XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());
00106 return SOAPUtilities::makeSOAPMessageReference(result);
00107 }
00108
00109
00110 try
00111 {
00112 theStateMachine_.execTransition(command,message);
00113
00114 }
00115 catch (toolbox::fsm::exception::Exception& e)
00116 {
00117 result = command + "Failed";
00118 __MOUT__ << e.what() << std::endl;
00119 }
00120
00121 theProgressBar_.complete();
00122 __MOUT__ << "Ending state for " << stateMachineName_ << " is " << theStateMachine_.getCurrentStateName() << std::endl;
00123 return SOAPUtilities::makeSOAPMessageReference(result);
00124 }
00125