Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

IncidentSvc.cpp

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/GaudiSvc/src/IncidentSvc/IncidentSvc.cpp,v 1.1.1.2 2001/04/18 18:32:50 tlindner Exp $
00002 
00003 // Include Files
00004 #include "GaudiKernel/MsgStream.h"
00005 #include "GaudiKernel/ISvcLocator.h"
00006 #include "GaudiKernel/IIncidentListener.h"
00007 #include "GaudiKernel/Incident.h"
00008 #include "GaudiKernel/GaudiException.h"
00009 #include "IncidentSvc.h"
00010 
00011 // Instantiation of a static factory class used by clients to create
00012 //  instances of this service
00013 static SvcFactory<IncidentSvc> s_factory;
00014 const ISvcFactory& IncidentSvcFactory = s_factory;
00015 
00016 //============================================================================================
00017 // Constructors and Desctructors
00018 //============================================================================================
00019 IncidentSvc::IncidentSvc( const std::string& name, ISvcLocator* svc )
00020 : Service(name, svc) {
00021 }
00022 
00023 IncidentSvc::~IncidentSvc() { 
00024 }
00025 
00026 //============================================================================================
00027 // Inherited Service overrides:
00028 //============================================================================================
00029 //
00030 StatusCode IncidentSvc::initialize() {
00031   // initialize the Service Base class
00032   StatusCode sc = Service::initialize();
00033   if ( sc.isFailure() ) {
00034     return sc;
00035   }
00036 
00037   MsgStream log( msgSvc(), name() ); 
00038 
00039   // set my own (IncidentSvc) properties via the jobOptionService 
00040   setProperties();
00041 
00042   return StatusCode::SUCCESS;
00043 }
00044   
00045 StatusCode IncidentSvc::finalize() {
00046   // Finalize this specific service
00047   StatusCode sc = Service::finalize();
00048   if ( sc.isFailure() ) {
00049     return sc;
00050   }
00051 
00052   return StatusCode::SUCCESS;
00053 }
00054   
00055 StatusCode IncidentSvc::queryInterface( const IID& riid, void** ppvInterface ) {
00056   if ( IID_IIncidentSvc == riid )    {
00057     *ppvInterface = (IIncidentSvc*)this;
00058   }
00059   else  {
00060     // Interface is not directly available: try out a base class
00061     return Service::queryInterface(riid, ppvInterface);
00062   }
00063   addRef();
00064   return StatusCode::SUCCESS;
00065 }
00066   
00067 //============================================================================================
00068 // Inherited IIncidentSvc overrides:
00069 //============================================================================================
00070 //
00071 void IncidentSvc::addListener(IIncidentListener* lis, const std::string& type, long prio) {
00072   std::string ltype;
00073   if( type == "" ) ltype = "ALL";
00074   else             ltype = type;
00075   // find if the type already exists
00076   ListenerMap::iterator itMap = m_listenerMap.find( ltype );
00077   if( itMap == m_listenerMap.end() ) {
00078     // if not found, create and insert now a list of listeners
00079     ListenerList* newlist = new ListenerList();
00080     std::pair<ListenerMap::iterator, bool> p;
00081     p = m_listenerMap.insert(ListenerMap::value_type(ltype, newlist));
00082     if( p.second ) itMap = p.first;
00083   }
00084   ListenerList* llist = (*itMap).second;
00085   // add Listener in the ListenerList according to the priority
00086   ListenerList::iterator itlist;
00087   for( itlist = llist->begin(); itlist != llist->end(); itlist++ ) {
00088     if( (*itlist).second < prio ) {
00089       // We insert before the current position
00090       break;
00091     }
00092   }
00093   llist->insert(itlist, Listener(lis, prio));
00094   return;
00095 }
00096 void IncidentSvc::removeListener(IIncidentListener* lis, const std::string& type) {
00097   if( type == "") {
00098     // remove Listener from all the lists
00099     ListenerMap::iterator itmap;
00100     for ( itmap = m_listenerMap.begin(); itmap != m_listenerMap.end();) {
00101       // since the current entry may be eventually deleted 
00102       // we need to keep a memory of the next index before calling recursivelly this method
00103       ListenerMap::iterator itmap_old = itmap;
00104       itmap++;
00105       removeListener( lis, (*itmap_old).first );
00106     }
00107   }
00108   else {
00109     ListenerMap::iterator itmap = m_listenerMap.find( type );
00110     if( itmap == m_listenerMap.end() ) {
00111       // if not found the incident type then return
00112       return;
00113     }
00114     else {
00115       ListenerList* llist = (*itmap).second;
00116       ListenerList::iterator itlist;
00117       // loop over all the entries in the Listener list to remove all of them than matches 
00118       // the listener address. Remember the next index before erasing the current one
00119       for( itlist = llist->begin(); itlist != llist->end(); ) {
00120         if( (*itlist).first == lis || lis == 0) {
00121           itlist = llist->erase(itlist);    // remove from the list now
00122         }
00123         else {
00124           itlist++;
00125         }
00126       }
00127       if( llist->size() == 0) {
00128         delete llist;
00129         m_listenerMap.erase(itmap);
00130       }
00131     }
00132   }
00133   return;
00134 }
00135 void IncidentSvc::fireIncident( const Incident& incident ) {
00136   MsgStream log ( msgSvc() , name());
00137   ListenerMap::iterator itmap = m_listenerMap.find( incident.type() );
00138   if( itmap != m_listenerMap.end() ) {
00139     ListenerList* llist = (*itmap).second;
00140     ListenerList::iterator itlist;
00141     // loop over all registered Listeners
00142     for( itlist = llist->begin(); itlist != llist->end(); itlist++ ) {
00143       // handle exceptions if they occur
00144       try {
00145         (*itlist).first->handle(incident);
00146       }
00147       catch( const GaudiException& exc ) {
00148        log << MSG::ERROR << "Exception with tag=" << exc.tag() << " is caught " << endreq;
00149        log << MSG::ERROR <<  exc  << endreq;
00150       }
00151       catch( const std::exception& exc ) {
00152        log << MSG::ERROR << "Standard std::exception is caught " << endreq;
00153        log << MSG::ERROR << exc.what()  << endreq;
00154       }
00155       catch(...) {
00156        log << MSG::ERROR << "UNKNOWN Exception is caught " << endreq;
00157       }
00158     }
00159   } 
00160   return;
00161 }
00162 
00163 

Generated at Wed Nov 21 12:22:30 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000