otsdaq  v1_01_03
 All Classes Namespaces Functions
RunControlStateMachine.cc
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"
6 
7 #include <toolbox/fsm/FailedEvent.h>
8 #include <xoap/Method.h>
9 #include <xdaq/NamespaceURI.h>
10 
11 #include <iostream>
12 
13 using namespace ots;
14 
15 const std::string RunControlStateMachine::FAILED_STATE_NAME = "Failed";
16 
17 //========================================================================================================================
18 RunControlStateMachine::RunControlStateMachine(std::string name)
19 : stateMachineName_(name)
20 {
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);
26  //theStateMachine_.addState('v', "Recovering", this, &RunControlStateMachine::stateRecovering);
27  //theStateMachine_.addState('T', "TTSTestMode", this, &RunControlStateMachine::stateTTSTestMode);
28 
29  //RAR added back in on 11/20/2016.. why was it removed..
30  //exceptions like..
31  // XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());)
32  // take state machine to "failed" otherwise
33  theStateMachine_.setStateName('F',RunControlStateMachine::FAILED_STATE_NAME);//x
34  theStateMachine_.setFailedStateTransitionAction (this, &RunControlStateMachine::enteringError);
35  theStateMachine_.setFailedStateTransitionChanged(this, &RunControlStateMachine::inError);
36 
37  //this line was added to get out of Failed state
38  theStateMachine_.addStateTransition('F', 'H', "Halt" , "Halting" , this, &RunControlStateMachine::transitionHalting);
39  //this attempt to get out of fail state makes things crash FIXME
40  //end RAR added back in on 11/20/2016.. why was it removed..
41 
42  theStateMachine_.addStateTransition('H', 'C', "Configure" , "Configuring" , "ConfigurationAlias", this, &RunControlStateMachine::transitionConfiguring);
43 
44  //Every state can transition to halted
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);
50 
51 
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);
57 
58 
59  // NOTE!! There must be a defined message handler for each transition name created above
60  // Also Note: The definition of theStateMachine above will get
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); //added for "Abort" transition name
69 
70 
71  reset();
72 }
73 
74 //========================================================================================================================
75 RunControlStateMachine::~RunControlStateMachine(void)
76 {
77 }
78 
79 //========================================================================================================================
80 void RunControlStateMachine::reset(void)
81 {
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;
86 }
87 
88 //========================================================================================================================
89 xoap::MessageReference RunControlStateMachine::runControlMessageHandler(
90  xoap::MessageReference message)
91 throw (xoap::exception::Exception)
92 {
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();
96  //__MOUT__ << "Command:-" << command << "-" << std::endl;
97  theProgressBar_.reset(command,stateMachineName_);
98  std::string result = command + "Done";
99 
100  //if error is received, immediately go to fail state
101  // likely error was sent by central FSM or external xoap
102  if(command == "Error" || command == "Fail")
103  {
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);
108  }
109 
110  //handle normal transitions here
111  try
112  {
113  theStateMachine_.execTransition(command,message);
114  //__MOUT__ << "I don't know what is going on!" << std::endl;
115 
116  if(theStateMachine_.getCurrentStateName() == RunControlStateMachine::FAILED_STATE_NAME)
117  {
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;
121  }
122  }
123  catch (toolbox::fsm::exception::Exception& e)
124  {
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;
129  }
130 
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);
135 }
136