/search.css" rel="stylesheet" type="text/css"/> /search.js">
00001 #include "DybOutputHeaderSvc.h" 00002 #include "Event/IHeader.h" 00003 #include "Event/HeaderObject.h" 00004 00005 #include "DybKernel/EventStoreIncident.h" 00006 00007 #include "GaudiKernel/IRegistry.h" 00008 00009 using namespace DayaBay; 00010 00011 DybOutputHeaderSvc::DybOutputHeaderSvc(const std::string& name, ISvcLocator *svc) 00012 : Service(name,svc) 00013 , m_incsvc(0) 00014 { 00015 } 00016 00017 DybOutputHeaderSvc::~DybOutputHeaderSvc() 00018 { 00019 this->clear(); 00020 } 00021 00022 StatusCode DybOutputHeaderSvc::queryInterface(const InterfaceID& riid, void** ppvInterface) 00023 { 00024 if ( riid == IID_IOutputHeaderSvc ) { 00025 *ppvInterface = (IOutputHeaderSvc*)this; 00026 } 00027 else { 00028 return Service::queryInterface(riid, ppvInterface); 00029 } 00030 addRef(); 00031 return StatusCode::SUCCESS; 00032 } 00033 00034 StatusCode DybOutputHeaderSvc::initialize() 00035 { 00036 MsgStream log(msgSvc(), name()); 00037 00038 StatusCode sc = Service::initialize(); 00039 if (sc.isFailure()) return sc; 00040 00041 sc = this->service("IncidentSvc",m_incsvc,true); 00042 if (sc.isFailure()) { 00043 log << MSG::ERROR 00044 << "Failed to get IncidentSvc" << endreq; 00045 return sc; 00046 } 00047 m_incsvc->addListener(this,"EventStoreIncident"); 00048 00049 return sc; 00050 } 00051 00052 StatusCode DybOutputHeaderSvc::finalize() 00053 { 00054 m_incsvc->removeListener(this,"EventStoreIncident"); 00055 m_incsvc->release(); 00056 m_incsvc = 0; 00057 00058 return Service::finalize(); 00059 } 00060 00061 // StatusCode DybOutputHeaderSvc::reinitialize() 00062 // { 00063 // } 00064 00065 00066 std::string DybOutputHeaderSvc::hostr(const HeaderObject& ho) 00067 { 00068 std::stringstream ss; 00069 ss << "HeaderObject: @" << (void*)&ho 00070 << " name=\"" << ho.name() << "\"" 00071 << " path=\"" << path(ho) << "\"" 00072 << " execNum=" << ho.execNumber() 00073 << " refCount=" << ho.refCount() 00074 << " #inputHeaders=" << ho.inputHeaders().size() 00075 << " context=\"" << ho.context().AsString() << "\""; 00076 return ss.str(); 00077 } 00078 00079 void DybOutputHeaderSvc::handle(const Incident& incident) 00080 { 00081 // Through this incident handler the service is notified of 00082 // new header objects to add and obsolete ones to remove. 00083 00084 MsgStream log(msgSvc(), name()); 00085 00086 if (incident.type() != "EventStoreIncident") { 00087 log << MSG::DEBUG 00088 << "Got unknown incident: \"" << incident.type() << "\"" 00089 << endreq; 00090 return; 00091 } 00092 00093 const EventStoreIncident* esi = dynamic_cast<const EventStoreIncident*>(&incident); 00094 if (!esi) { 00095 log << MSG::DEBUG 00096 << "Got right incident (" << incident.type() << ") but does not dynamic cast" 00097 << endreq; 00098 return; 00099 } 00100 log << MSG::DEBUG 00101 << "Handling incident of type " << incident.type() 00102 << " stored=" << esi->stored() 00103 << endreq; 00104 00105 00106 const DataObject* dobj = esi->object(); 00107 if (!dobj) { 00108 log << MSG::DEBUG << incident.type() << ": object is null" << endreq; 00109 return; 00110 } 00111 00112 const HeaderObject* ho = dynamic_cast<const HeaderObject*>(dobj); 00113 if (!ho) { 00114 log << MSG::DEBUG 00115 << incident.type() << ": object is not HeaderObject" << endreq; 00116 return; 00117 } 00118 00119 if (esi->stored()) { 00120 log << MSG::DEBUG 00121 << incident.type() << ": adding: " << hostr(*ho) << endreq; 00122 size_t siz = ho->inputHeaders().size(); 00123 for (size_t ind = 0; ind < siz; ++ind) { 00124 const HeaderObject* other_ho = dynamic_cast<const HeaderObject*>(ho->inputHeaders()[ind]); 00125 log << MSG::DEBUG << "\t" << hostr(*other_ho) << endreq; 00126 } 00127 this->add(*ho); 00128 } 00129 else { 00130 log << MSG::DEBUG 00131 << incident.type() << ": erasing: " << hostr(*ho) << endreq; 00132 this->erase(*ho); 00133 } 00134 } 00135 00136 00137 IOutputHeaderSvc::OutputHeaders DybOutputHeaderSvc::get(const IHeader& header) 00138 { 00139 MsgStream log(msgSvc(), name()); 00140 00141 OutputHeaderMap::iterator it = find(header); 00142 return it->second; // find can not fail 00143 } 00144 00145 DybOutputHeaderSvc::OutputHeaderMap::iterator DybOutputHeaderSvc::find(const IHeader& header) 00146 { 00147 OutputHeaderMap::iterator it = m_ohmap.find(&header); 00148 if (it != m_ohmap.end()) { 00149 return it; 00150 } 00151 00152 m_ohmap[&header] = IOutputHeaderSvc::OutputHeaders(); 00153 return m_ohmap.find(&header); // what we just added 00154 } 00155 00156 bool DybOutputHeaderSvc::in(OutputHeaders& oh, const DayaBay::IHeader& header) 00157 { 00158 size_t size = oh.size(); 00159 for (size_t ind=0; ind<size; ++ind) { 00160 if (oh[ind] == &header) return true; 00161 } 00162 return false; 00163 } 00164 00165 void DybOutputHeaderSvc::removeHeaderName(const HeaderObject& ho) 00166 { 00167 OutputHeaderNameMap::iterator nit = m_names.find(&ho); 00168 if (nit != m_names.end()) { 00169 m_names.erase(nit); 00170 } 00171 } 00172 00173 void DybOutputHeaderSvc::addHeaderName(const HeaderObject& ho) 00174 { 00175 IRegistry* registry = ho.registry(); 00176 if (!registry) { 00177 return; 00178 } 00179 std::string hname = registry->identifier(); 00180 m_names[&ho] = hname; 00181 } 00182 00183 void DybOutputHeaderSvc::add(const IHeader& header) 00184 { 00185 MsgStream log(msgSvc(), name()); 00186 HeaderObject* ho = dynamic_cast<HeaderObject*>(const_cast<IHeader*>(&header)); 00187 00188 if (!ho) { 00189 log << MSG::ERROR 00190 << "Failed to cast IHeader @ " << (void*)&header << endreq; 00191 return; 00192 } 00193 00194 addHeaderName(*ho); 00195 00196 const std::vector<const IHeader*>& inputHeaders = header.inputHeaders(); 00197 if (!inputHeaders.size()) { 00198 log << MSG::DEBUG << "No input headers: " << hostr(*ho) << endreq; 00199 return; // origin header 00200 } 00201 00202 // if (m_ohmap.find(&header) != m_ohmap.end()) { 00203 // return; // already mapped 00204 // } 00205 00206 00207 for (size_t ind=0; ind<inputHeaders.size(); ++ind) { 00208 const IHeader* inhead = inputHeaders[ind]; 00209 OutputHeaderMap::iterator it = find(*inhead); 00210 00211 if (!in(it->second,header)) { 00212 it->second.push_back(&header); 00213 00214 HeaderObject* other_ho = 00215 dynamic_cast<HeaderObject*>(const_cast<IHeader*>(it->first)); 00216 addHeaderName(*other_ho); 00217 00218 log << MSG::DEBUG << "Setting as OutputHeader for: " << hostr(*other_ho) << endreq; 00219 ho->addRef(); 00220 } 00221 add(*inhead); // recurse 00222 } 00223 } 00224 00225 std::string DybOutputHeaderSvc::path(const DayaBay::IHeader& header) 00226 { 00227 OutputHeaderNameMap::iterator it = m_names.find(&header); 00228 if (it == m_names.end()) { 00229 return "UnknownHeader"; 00230 } 00231 return it->second; 00232 } 00233 00234 void DybOutputHeaderSvc::erase(const IHeader& header) 00235 { 00236 MsgStream log(msgSvc(), name()); 00237 HeaderObject* ho = dynamic_cast<HeaderObject*>(const_cast<IHeader*>(&header)); 00238 00239 OutputHeaderMap::iterator it = m_ohmap.find(&header), done = m_ohmap.end(); 00240 if (it == done) { 00241 log << MSG::DEBUG 00242 << "Given an IHeader to erase which is not in my map: " << hostr(*ho) 00243 << endreq; 00244 return; 00245 } 00246 00247 log << MSG::DEBUG 00248 << "Erasing from output headers map: " << hostr(*ho) 00249 << " " << path(header) 00250 << endreq; 00251 m_ohmap.erase(it); 00252 00253 done = m_ohmap.end(); 00254 for (it = m_ohmap.begin(); it != done; ++it) { 00255 IOutputHeaderSvc::OutputHeaders newone, &ohs = it->second; 00256 size_t nohs = ohs.size(); 00257 for (size_t ind = 0; ind < nohs; ++ind) { 00258 const IHeader* thisone = ohs[ind]; 00259 if (thisone == &header) { 00260 log << MSG::DEBUG 00261 << "Found as output header: " << hostr(*ho) 00262 << endreq; 00263 continue; 00264 } 00265 newone.push_back(thisone); 00266 } 00267 if (newone.size() < nohs) { 00268 it->second = newone; 00269 } 00270 } 00271 00272 00273 log << MSG::DEBUG << "Releasing: " << hostr(*ho) << endreq; 00274 ho->release(); 00275 removeHeaderName(*ho); 00276 } 00277 00278 void DybOutputHeaderSvc::clear() 00279 { 00280 MsgStream log(msgSvc(), name()); 00281 m_ohmap.clear(); 00282 } 00283