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