otsdaq  v1_01_02
 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 
16 //========================================================================================================================
17 RunControlStateMachine::RunControlStateMachine(std::string name)
18 : stateMachineName_(name)
19 {
20  theStateMachine_.addState('I', "Initial", this, &RunControlStateMachine::stateInitial);
21  theStateMachine_.addState('H', "Halted", this, &RunControlStateMachine::stateHalted);
22  theStateMachine_.addState('C', "Configured", this, &RunControlStateMachine::stateConfigured);
23  theStateMachine_.addState('R', "Running", this, &RunControlStateMachine::stateRunning);
24  theStateMachine_.addState('P', "Paused", this, &RunControlStateMachine::statePaused);
25  //theStateMachine_.addState('v', "Recovering", this, &RunControlStateMachine::stateRecovering);
26  //theStateMachine_.addState('T', "TTSTestMode", this, &RunControlStateMachine::stateTTSTestMode);
27 
28  //RAR added back in on 11/20/2016.. why was it removed..
29  //exceptions like..
30  // XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());)
31  // take state machine to "failed" otherwise
32  theStateMachine_.setStateName('F',"Failed");//x
33  theStateMachine_.setFailedStateTransitionAction (this, &RunControlStateMachine::enteringError);
34  theStateMachine_.setFailedStateTransitionChanged(this, &RunControlStateMachine::inError);
35 
36  //this line was added to get out of Failed state
37  theStateMachine_.addStateTransition('F', 'H', "Halt" , "Halting" , this, &RunControlStateMachine::transitionHalting);
38  //this attempt to get out of fail state makes things crash FIXME
39  //end RAR added back in on 11/20/2016.. why was it removed..
40 
41  theStateMachine_.addStateTransition('H', 'C', "Configure" , "Configuring" , "ConfigurationAlias", this, &RunControlStateMachine::transitionConfiguring);
42 
43  //Every state can transition to halted
44  theStateMachine_.addStateTransition('I', 'H', "Initialize", "Initializing", this, &RunControlStateMachine::transitionInitializing);
45  theStateMachine_.addStateTransition('H', 'H', "Halt" , "Halting" , this, &RunControlStateMachine::transitionHalting);
46  theStateMachine_.addStateTransition('C', 'H', "Halt" , "Halting" , this, &RunControlStateMachine::transitionHalting);
47  theStateMachine_.addStateTransition('R', 'H', "Abort" , "Aborting" , this, &RunControlStateMachine::transitionHalting);
48  theStateMachine_.addStateTransition('P', 'H', "Abort" , "Aborting" , this, &RunControlStateMachine::transitionHalting);
49 
50 
51  theStateMachine_.addStateTransition('R', 'P', "Pause" , "Pausing" , this, &RunControlStateMachine::transitionPausing);
52  theStateMachine_.addStateTransition('P', 'R', "Resume" , "Resuming" , this, &RunControlStateMachine::transitionResuming);
53  theStateMachine_.addStateTransition('C', 'R', "Start" , "Starting" , this, &RunControlStateMachine::transitionStarting);
54  theStateMachine_.addStateTransition('R', 'C', "Stop" , "Stopping" , this, &RunControlStateMachine::transitionStopping);
55  theStateMachine_.addStateTransition('P', 'C', "Stop" , "Stopping" , this, &RunControlStateMachine::transitionStopping);
56 
57 
58  // NOTE!! There must be a defined message handler for each transition name created above
59  // Also Note: The definition of theStateMachine above will get
60  xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Initialize", XDAQ_NS_URI);
61  xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Configure" , XDAQ_NS_URI);
62  xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Start" , XDAQ_NS_URI);
63  xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Stop" , XDAQ_NS_URI);
64  xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Pause" , XDAQ_NS_URI);
65  xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Resume" , XDAQ_NS_URI);
66  xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Halt" , XDAQ_NS_URI);
67  xoap::bind(this, &RunControlStateMachine::runControlMessageHandler, "Abort" , XDAQ_NS_URI); //added for "Abort" transition name
68 
69 
70  reset();
71 }
72 
73 //========================================================================================================================
74 RunControlStateMachine::~RunControlStateMachine(void)
75 {
76 }
77 
78 //========================================================================================================================
79 void RunControlStateMachine::reset(void)
80 {
81  __MOUT__ << stateMachineName_ << " is in transition?" << theStateMachine_.isInTransition() << std::endl;
82  theStateMachine_.setInitialState('I');
83  theStateMachine_.reset();
84  __MOUT__ << stateMachineName_ << " is in transition?" << theStateMachine_.isInTransition() << std::endl;
85 }
86 
87 //========================================================================================================================
88 xoap::MessageReference RunControlStateMachine::runControlMessageHandler(
89  xoap::MessageReference message)
90 throw (xoap::exception::Exception)
91 {
92  __MOUT__ << "Starting state for " << stateMachineName_ << " is " << theStateMachine_.getCurrentStateName() << std::endl;
93  __MOUT__ << SOAPUtilities::translate(message) << std::endl;
94  std::string command = SOAPUtilities::translate(message).getCommand();
95  //__MOUT__ << "Command:-" << command << "-" << std::endl;
96  theProgressBar_.reset(command,stateMachineName_);
97  std::string result = command + "Done";
98 
99  //if error is received, immediately go to fail state
100  // likely error was sent by central FSM or external xoap
101  if(command == "Error" || command == "Fail")
102  {
103  __SS__ << command << " was received! Immediately throwing FSM exception." << std::endl;
104  __MOUT_ERR__ << "\n" << ss.str();
105  XCEPT_RAISE (toolbox::fsm::exception::Exception, ss.str());
106  return SOAPUtilities::makeSOAPMessageReference(result);
107  }
108 
109  //handle normal transitions here
110  try
111  {
112  theStateMachine_.execTransition(command,message);
113  //__MOUT__ << "I don't know what is going on!" << std::endl;
114  }
115  catch (toolbox::fsm::exception::Exception& e)
116  {
117  result = command + "Failed";
118  __MOUT__ << e.what() << std::endl;
119  }
120 
121  theProgressBar_.complete();
122  __MOUT__ << "Ending state for " << stateMachineName_ << " is " << theStateMachine_.getCurrentStateName() << std::endl;
123  return SOAPUtilities::makeSOAPMessageReference(result);
124 }
125