CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/EventFilter/ResourceBroker/src/RBStateMachine.cc

Go to the documentation of this file.
00001 
00002 //
00003 // RBStateMachine.cc
00004 // -------
00005 //
00006 // Finite state machine for the Resource Broker.
00007 //
00008 //  Created on: Dec 9, 2011
00009 //                                                                              Andrei Spataru : aspataru@cern.ch
00011 
00012 #include "EventFilter/ResourceBroker/interface/RBStateMachine.h"
00013 #include "EventFilter/ResourceBroker/interface/SharedResources.h"
00014 
00015 #include <typeinfo>
00016 #include <fstream>
00017 
00018 using std::cout;
00019 using std::endl;
00020 using std::string;
00021 using namespace evf::rb_statemachine;
00022 
00023 RBStateMachine::RBStateMachine(xdaq::Application* app, SharedResourcesPtr_t sr) :
00024                         app_(app),
00025                         sharedResources_(sr),
00026                         rcmsStateNotifier_(app_->getApplicationLogger(),
00027                                         app_->getApplicationDescriptor(),
00028                                         app_->getApplicationContext()), visibleStateName_("N/A"),
00029                         internalStateName_("N/A"), firstTimeInHalted_(true) {
00030 
00031         // pass pointer of FSM to shared resources
00032         sharedResources_->setFsmPointer(this);
00033 
00034         // make the RW Lock writer-biased
00035         pthread_rwlockattr_t attr;
00036         pthread_rwlockattr_init(&attr);
00037 
00038 #ifndef __APPLE__
00039         // unsupported on Mac OS
00040         pthread_rwlockattr_setkind_np(&attr,
00041                         PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
00042 #endif
00043 
00044         pthread_rwlock_init(&transitionLock_, &attr);
00045 
00046 }
00047 
00048 RBStateMachine::~RBStateMachine() {
00049         pthread_rwlock_destroy(&transitionLock_);
00050         cout << "State machine DESTROYED!" << endl;
00051 }
00052 
00053 BaseState const& RBStateMachine::getCurrentState() const throw (std::bad_cast) {
00054         return state_cast<BaseState const&> ();
00055 }
00056 
00057 BaseState & RBStateMachine::getCurrentStateNC() const throw (std::bad_cast) {
00058         return const_cast<BaseState &> (state_cast<BaseState const&> ());
00059 }
00060 
00061 void RBStateMachine::setExternallyVisibleState(const std::string& s) {
00062         visibleStateName_ = s;
00063         updateWebGUIExternalState(visibleStateName_);
00064 }
00065 
00066 void RBStateMachine::setInternalStateName(const std::string& s) {
00067         internalStateName_ = s;
00068         updateWebGUIInternalState(internalStateName_);
00069 }
00070 
00071 // get the RCMS StateListener parameter (classname/instance)
00072 xdata::Bag<xdaq2rc::ClassnameAndInstance>* RBStateMachine::rcmsStateListener() {
00073         return rcmsStateNotifier_.getRcmsStateListenerParameter();
00074 }
00075 
00076 // report if RCMS StateListener was found
00077 xdata::Boolean* RBStateMachine::foundRcmsStateListener() {
00078         return rcmsStateNotifier_.getFoundRcmsStateListenerParameter();
00079 }
00080 
00081 void RBStateMachine::findRcmsStateListener(xdaq::Application* app) {
00082         rcmsStateNotifier_.findRcmsStateListener(); //might not be needed
00083         rcmsStateNotifier_.subscribeToChangesInRcmsStateListener(
00084                         app->getApplicationInfoSpace());
00085 }
00086 
00087 void RBStateMachine::rcmsStateChangeNotify() {
00088         string state = getExternallyVisibleState();
00089         try {
00090                 cout << "-->RCMS state change notify: " << state << endl;
00091                 rcmsStateNotifier_.stateChanged(state,
00092                                 "ResourceBroker has reached target state " + state);
00093         } catch (...) {
00094                 cout << "Failed to notify state change: " << state << endl;
00095         }
00096 }
00097 
00098 void RBStateMachine::updateWebGUIExternalState(string newStateName) const {
00099         sharedResources_->updateGUIExternalState(newStateName);
00100 }
00101 
00102 void RBStateMachine::updateWebGUIInternalState(string newStateName) const {
00103         sharedResources_->updateGUIInternalState(newStateName);
00104 }