| # Gaudi Framework, version v20r4
Generated: 8 Jan 2009

# AlgorithmManager.cpp

Go to the documentation of this file.

    00001 // $Id: AlgorithmManager.cpp,v 1.11 2008/10/20 20:58:10 marcocle Exp $
    00002
    00003 // Include files
    00004 #include "AlgorithmManager.h"
    00005 #include "GaudiKernel/IAlgorithm.h"
    00006 #include "GaudiKernel/ISvcLocator.h"
    00007 #include "GaudiKernel/AlgFactory.h"
    00008 #include "GaudiKernel/System.h"
    00009 #include "GaudiKernel/MsgStream.h"
    00010 #include <iostream>
    00011 #ifndef _WIN32
    00012 #include <errno.h>
    00013 #endif
    00014
    00015 using ROOT::Reflex::PluginService;
    00016
    00017 // constructor
    00018 AlgorithmManager::AlgorithmManager(IInterface* iface):
    00019   m_statemgr(iface)
    00020 {
    00021   m_pOuter = iface;
    00022   m_pOuter->queryInterface(IID_ISvcLocator, pp_cast<void>(&m_svclocator)).ignore();
    00023   m_msgsvc     = 0;
    00024   m_refcount   = 1;
    00025   m_listalg    = new ListAlg();
    00026   m_listmgralg = new ListAlg();
    00027 }
    00028
    00029 // destructor
    00030 AlgorithmManager::~AlgorithmManager() {
    00031   delete m_listalg;
    00032   delete m_listmgralg;
    00033   if( m_msgsvc ) m_msgsvc->release();
    00034   if( m_svclocator ) m_svclocator->release();
    00035 }
    00036
    00037 // addRef
    00038 unsigned long AlgorithmManager::addRef() {
    00039   m_refcount++;
    00040   return m_refcount;
    00041 }
    00042
    00043 // release
    00044 unsigned long AlgorithmManager::release() {
    00045   unsigned long count = --m_refcount;
    00046   if( count <= 0) {
    00047     delete this;
    00048   }
    00049   return count;
    00050 }
    00051
    00052 // queryInterface
    00053 StatusCode AlgorithmManager::queryInterface(const InterfaceID& iid, void** pinterface) {
    00054   if( iid == IID_IInterface ) {
    00055     *pinterface = (IInterface*)this;
    00056     addRef();
    00057     return StatusCode::SUCCESS;
    00058   }
    00059   else if ( iid == IID_IAlgManager ) {
    00060     *pinterface = (IAlgManager*)this;
    00061     addRef();
    00062     return StatusCode::SUCCESS;
    00063   }
    00064   else {
    00065     return m_pOuter->queryInterface(iid, pinterface);
    00066   }
    00067   return StatusCode::SUCCESS;
    00068 }
    00069
    00070 // addAlgorithm
    00071 StatusCode AlgorithmManager::addAlgorithm( IAlgorithm* alg ) {
    00072   m_listalg->push_back( alg );
    00073   return StatusCode::SUCCESS;
    00074 }
    00075
    00076 // removeAlgorithm
    00077 StatusCode AlgorithmManager::removeAlgorithm( IAlgorithm* alg ) {
    00078   ListAlg::iterator it;
    00079   for (it = m_listalg->begin(); it != m_listalg->end(); it++ ) {
    00080     if( *it == alg ) {
    00081       m_listalg->erase(it);
    00082       break;
    00083     }
    00084   }
    00085   return StatusCode::SUCCESS;
    00086 }
    00087
    00088 // createService
    00089 StatusCode AlgorithmManager::createAlgorithm( const std::string& algtype,
    00090                                               const std::string& algname,
    00091                                               IAlgorithm*& algorithm,
    00092                                               bool managed)
    00093 {
    00094   MsgStream log(msgSvc(), "AlgorithmManager");
    00095   // Check is the algorithm is already existing
    00096   if( existsAlgorithm( algname ) ) {
    00097     // return an error because an algorithm with that name already exists
    00098     return StatusCode::FAILURE;
    00099   }
    00100   algorithm = PluginService::Create<IAlgorithm*>(algtype, algname, m_svclocator);
    00101   if ( !algorithm ) {
    00102     algorithm = PluginService::CreateWithId<IAlgorithm*>(algtype, algname, m_svclocator);
    00103   }
    00104   if ( algorithm ) {
    00105     // Check the compatibility of the version of the interface obtained
    00106     if( !isValidInterface(algorithm) ) {
    00107       log << MSG::FATAL << "Incompatible interface IAlgorithm version for " << algtype << endreq;
    00108       return StatusCode::FAILURE;
    00109     }
    00110     StatusCode rc;
    00111     m_listalg->push_back( algorithm );
    00112     if ( managed ) {
    00113       algorithm->addRef();
    00114       m_listmgralg->push_back( algorithm );
    00115
    00116       // Bring the created service to the same state of the ApplicationMgr
    00117       if (m_statemgr->FSMState() >= Gaudi::StateMachine::INITIALIZED) {
    00118         rc = algorithm->sysInitialize();
    00119         if (rc.isSuccess() && m_statemgr->FSMState() >= Gaudi::StateMachine::RUNNING) {
    00120           rc = algorithm->sysStart();
    00121         }
    00122       }
    00123       if ( !rc.isSuccess() )  {
    00124         log << MSG::ERROR << "Failed to initialize algorithm: "
    00125             << "[" << algname << "]" << endmsg;
    00126       }
    00127     }
    00128     return rc;
    00129   }
    00130   log << MSG::ERROR << "Algorithm of type " << algtype
    00131       << " is unknown (No factory available)." << endmsg;
    00132 #ifndef _WIN32
    00133   errno = 0xAFFEDEAD; // code used by Gaudi for library load errors: forces getLastErrorString do use dlerror (on Linux)
    00134 #endif
    00135   std::string err = System::getLastErrorString();
    00136   if (! err.empty()) {
    00137     log << MSG::ERROR << err << endmsg;
    00138   }
    00139   log << MSG::ERROR << "More information may be available by setting the global jobOpt \"ReflexPluginDebugLevel\" to 1" << endmsg;
    00140
    00141   return StatusCode::FAILURE;
    00142 }
    00143
    00144 // getAlgorithm
    00145 StatusCode AlgorithmManager::getAlgorithm( const std::string& name, IAlgorithm*& alg) const {
    00146   ListAlg::const_iterator it;
    00147   for (it = m_listalg->begin(); it != m_listalg->end(); it++ ) {
    00148     if( (*it)->name() == name ) {
    00149       alg = *it;
    00150       return StatusCode::SUCCESS;
    00151     }
    00152   }
    00153   return StatusCode::FAILURE;
    00154 }
    00155
    00156 // existsAlgorithm
    00157 bool AlgorithmManager::existsAlgorithm( const std::string& name ) const {
    00158   ListAlg::const_iterator it;
    00159   for (it = m_listalg->begin(); it != m_listalg->end(); it++ ) {
    00160     if( (*it)->name() == name ) {
    00161       return true;
    00162     }
    00163   }
    00164   return false;
    00165 }
    00166
    00167   // Return the list of Algorithms
    00168 std::list<IAlgorithm*>& AlgorithmManager::getAlgorithms( ) const
    00169 {
    00170   return *m_listalg;
    00171 }
    00172
    00173 StatusCode AlgorithmManager::initializeAlgorithms() {
    00174   StatusCode rc;
    00175   ListAlg::const_iterator it;
    00176   for (it = m_listmgralg->begin(); it != m_listmgralg->end(); it++ ) {
    00177     rc = (*it)->sysInitialize();
    00178     if ( rc.isFailure() ) return rc;
    00179   }
    00180   return rc;
    00181 }
    00182
    00183 StatusCode AlgorithmManager::startAlgorithms() {
    00184   StatusCode rc;
    00185   ListAlg::const_iterator it;
    00186   for (it = m_listmgralg->begin(); it != m_listmgralg->end(); it++ ) {
    00187     rc = (*it)->sysStart();
    00188     if ( rc.isFailure() ) return rc;
    00189   }
    00190   return rc;
    00191 }
    00192
    00193 StatusCode AlgorithmManager::stopAlgorithms() {
    00194   StatusCode rc;
    00195   ListAlg::const_iterator it;
    00196   for (it = m_listmgralg->begin(); it != m_listmgralg->end(); it++ ) {
    00197     rc = (*it)->sysStop();
    00198     if ( rc.isFailure() ) return rc;
    00199   }
    00200   return rc;
    00201 }
    00202
    00203 StatusCode AlgorithmManager::finalizeAlgorithms() {
    00204   StatusCode rc;
    00205   ListAlg::const_iterator it;
    00206   for (it = m_listmgralg->begin(); it != m_listmgralg->end(); it++ ) {
    00207     rc = (*it)->sysFinalize();
    00208     if( rc.isFailure() ) return rc;
    00209     (*it)->release();
    00210   }
    00211   m_listmgralg->clear();
    00212   return rc;
    00213 }
    00214
    00215 StatusCode AlgorithmManager::reinitializeAlgorithms() {
    00216   StatusCode rc;
    00217   ListAlg::const_iterator it;
    00218   for (it = m_listmgralg->begin(); it != m_listmgralg->end(); it++ ) {
    00219     rc = (*it)->sysReinitialize();
    00220     if( rc.isFailure() ){
    00221       MsgStream log(msgSvc(), "AlgorithmManager");
    00222       log << MSG::ERROR << "Unable to re-initialize Service: " << (*it)->name() << endreq;
    00223       return rc;
    00224     }
    00225   }
    00226   return rc;
    00227 }
    00228
    00229 StatusCode AlgorithmManager::restartAlgorithms() {
    00230   StatusCode rc;
    00231   ListAlg::const_iterator it;
    00232   for (it = m_listmgralg->begin(); it != m_listmgralg->end(); it++ ) {
    00233     rc = (*it)->sysRestart();
    00234     if( rc.isFailure() ){
    00235       MsgStream log(msgSvc(), "AlgorithmManager");
    00236       log << MSG::ERROR << "Unable to re-initialize Service: " << (*it)->name() << endreq;
    00237       return rc;
    00238     }
    00239   }
    00240   return rc;
    00241 }
    00242
    00243 IMessageSvc *AlgorithmManager::msgSvc(){
    00244   // Access the message service if not yet done already
    00245   if( m_msgsvc == 0 ) {
    00246     m_svclocator->getService( "MessageSvc", IID_IMessageSvc,
    00247                               *pp_cast<IInterface>(&m_msgsvc) ).ignore();
    00248   }
    00249   return m_msgsvc;
    00250 }