Gaudi Framework, version v24r2

Home   Generated: Wed Dec 4 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
IoComponentMgr.cpp
Go to the documentation of this file.
1 
2 // IoComponentMgr.cxx
3 // Implementation file for class IoComponentMgr
4 // Author: S.Binet<binet@cern.ch>
6 
7 // GaudiMP includes
8 #include "IoComponentMgr.h"
11 
12 #include "GaudiKernel/IFileMgr.h"
14 #include <boost/filesystem.hpp>
15 
16 // STL includes
17 
18 // FrameWork includes
19 #include "GaudiKernel/SvcFactory.h"
20 #include "GaudiKernel/Property.h"
21 
22 #define ON_DEBUG if (UNLIKELY(outputLevel() <= MSG::DEBUG))
23 #define ON_VERBOSE if (UNLIKELY(outputLevel() <= MSG::VERBOSE))
24 
25 #define DEBMSG ON_DEBUG debug()
26 #define VERMSG ON_VERBOSE verbose()
27 
29 
30 using namespace std;
31 
32 
33 std::ostream&
34 operator<< ( std::ostream& os, const IIoComponentMgr::IoMode::Type& m) {
35  switch (m) {
36  case IIoComponentMgr::IoMode::READ : os << "READ"; break;
37  case IIoComponentMgr::IoMode::WRITE : os << "WRITE"; break;
38  case IIoComponentMgr::IoMode::RW : os << "RW"; break;
39  default:
40  os << "INVALID"; break;
41  }
42 
43  return os;
44 
45 }
46 
47 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
48 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
49 
51  ISvcLocator* svc )
52 : base_class(name,svc), m_log(msgSvc(), name )
53 {
54  //
55  // Property declaration
56  //
57  //declareProperty( "Property", m_nProperty );
58 
59 // declareProperty ("Registry",
60 // m_dict_location = "GaudiMP.IoRegistry.registry",
61 // "Location of the python dictionary holding the "
62 // "associations: \n"
63 // " {component-name:{ 'old-fname' : ['io','new-fname'] }}\n"
64 // "\nSyntax: <python-module>.<python-module>.<fct-name> \n"
65 // " where fct-name is a function returning the wanted "
66 // " dictionary.");
67 }
68 
69 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
70 
72 {
73 }
74 
75 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
76 
79  m_log << MSG::DEBUG << "--> initialize()" << endmsg;
80 
81  if ( Service::initialize().isFailure() ) {
82  m_log << MSG::ERROR << "Unable to initialize Service base class" << endmsg;
83  return StatusCode::FAILURE;
84  }
86 
87  IIncidentSvc* p_incSvc(0);
88 
89  if (service("IncidentSvc", p_incSvc, true).isFailure()) {
90  m_log << MSG::ERROR << "unable to get the IncidentSvc" << endmsg;
91  return StatusCode::FAILURE;
92  } else {
93  p_incSvc->addListener( this, IncidentType::BeginOutputFile, 100, true);
94  p_incSvc->addListener( this, IncidentType::BeginInputFile, 100, true);
95  }
96 
97  return StatusCode::SUCCESS;
98 }
99 
100 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
101 
102 StatusCode
104  m_log << MSG::DEBUG << "--> finalize()" << endmsg;
105 
106  return StatusCode::SUCCESS;
107 }
108 
109 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
110 
113 bool
115  DEBMSG << "--> io_hasitem()" << endmsg;
116  if ( 0 == iocomponent ) {
117  return false;
118  }
119  const std::string& ioname = iocomponent->name();
121  return io != m_ioregistry.end();
122 }
123 
124 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
125 
129 bool
131  const std::string& fname) const
132 {
133  m_log << MSG::DEBUG << "--> io_contains()" << endmsg;
134  if ( 0 == iocomponent ) {
135  return false;
136  }
137  const std::string& ioname = iocomponent->name();
138 
139  DEBMSG << "io_contains: c: " << ioname << " f: " << fname << endmsg;
140 
141  pair<iodITR,iodITR> fit = m_cdict.equal_range(iocomponent);
142  if (fit.first == fit.second) {
143  return false;
144  } else {
145  iodITR it;
146  for (it=fit.first; it != fit.second; ++it) {
147  IoComponentEntry ioe = it->second;
148  DEBMSG << " " << ioe << endmsg;
149  if (ioe.m_oldfname == "") {
150  m_log << MSG::ERROR << "IIoComponent " << ioname
151  << " has empty old filename" << endmsg;
152  return false;
153  } else if (ioe.m_oldfname == fname) {
154  return true;
155  }
156  }
157  }
158 
159  return false;
160 }
161 
162 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
163 
172 {
173  if ( 0 == iocomponent ) {
174  m_log << MSG::ERROR
175  << "io_register (component) received a NULL pointer !" << endmsg;
176  return StatusCode::FAILURE;
177  }
178  const std::string& ioname = iocomponent->name();
179  DEBMSG << "--> io_register(" << ioname << ")" << endmsg;
181  if ( itr == m_ioregistry.end() ) {
182  DEBMSG << " registering IoComponent \"" << ioname << "\"" << endmsg;
183  iocomponent->addRef(); // ownership...
184  m_ioregistry[ioname] = iocomponent;
185  m_iostack.push_back (iocomponent);
186  } else {
187  m_log << MSG::INFO << "IoComponent[" << iocomponent->name()
188  <<"] already registered @" << (void*)itr->second << endmsg;
189  }
190  return StatusCode::SUCCESS;
191 }
192 
193 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
194 
204  const std::string& fname,
205  const std::string& pfn)
206 {
207  if ( 0 == iocomponent ) {
208  return StatusCode::FAILURE;
209  }
210  const std::string& ioname = iocomponent->name();
211 
212  DEBMSG << "--> io_register(" << ioname << ","
213  << ( (iomode== IIoComponentMgr::IoMode::READ) ? "R" : "W" )
214  << "," << fname << ")" << endmsg;
215 
216  if ( !io_hasitem (iocomponent) ) {
217  if ( !io_register (iocomponent).isSuccess() ) {
218  m_log << MSG::ERROR
219  << "could not register component [" << iocomponent->name() << "] "
220  << "with the I/O component manager !"
221  << endmsg;
222  return StatusCode::FAILURE;
223  }
224  }
225 
226  pair<iodITR,iodITR> fit = m_cdict.equal_range(iocomponent);
227  if (fit.first != fit.second) {
228  for (iodITR it=fit.first; it != fit.second; ++it) {
229  IoComponentEntry ioe = it->second;
230  if (ioe.m_oldfname == fname) {
231  if (ioe.m_iomode == iomode) {
232  m_log << MSG::INFO << "IoComponent " << ioname
233  << " has already had file " << fname
234  << " registered with i/o mode " << iomode << endmsg;
235  return StatusCode::SUCCESS;
236  } else {
237  m_log << MSG::WARNING << "IoComponent " << ioname
238  << " has already had file " << fname
239  << " registered with a different i/o mode " << ioe.m_iomode
240  << " - now trying " << iomode << endmsg;
241  }
242  }
243  }
244  }
245 
246  // We need to take into account that boost::filesystem::absolute() does not
247  // work for files read from eos, i.e. starting with "root:"
248  std::string tmp_name = pfn.empty()?fname:pfn;
249  bool from_eos = tmp_name.find("root:")==0;
250  IoComponentEntry ioc(fname,(from_eos?tmp_name:boost::filesystem::absolute(tmp_name).string()),iomode);
252 
253  return StatusCode::SUCCESS;
254 }
255 
256 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
257 
263  std::string& fname)
264 {
265  if ( 0 == iocomponent ) {
266  return StatusCode::FAILURE;
267  }
268 
269  std::string ofname = fname;
270  const std::string& ioname = iocomponent->name();
271 
272  m_log << MSG::DEBUG << "--> io_retrieve(" << ioname << "," << fname << ")"
273  << endmsg;
274 
275  iodITR it;
276  if (!findComp(iocomponent,ofname,it)) {
277  DEBMSG << "could not find c: " << ioname << " old_f: " << ofname << endmsg;
278  return StatusCode::FAILURE;
279  } else {
280 
282  for (it = m_cdict.equal_range(iocomponent).first;
283  it != m_cdict.equal_range(iocomponent).second;
284  ++it) {
285 
286  if (it->second.m_oldfname == ofname) {
287  DEBMSG << "retrieving new name for the component " << iocomponent->name()
288  << " old name: " << ofname
289  << ", new name: " << it->second.m_newfname << endmsg;
290  fname = it->second.m_newfname;
291  return StatusCode::SUCCESS;
292  }
293  }
294  }
295 
296  DEBMSG << "Unexpected error! Unable to find entry in the dictionary corresponding to old filename: " << ofname << endmsg;
297  return StatusCode::FAILURE;
298 }
299 
300 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
301 
308 {
309  m_log << MSG::DEBUG << "--> io_reinitialize()" << endmsg;
310  m_log << MSG::DEBUG << "reinitializing I/O subsystem..." << endmsg;
311 
312  if (m_log.level() <= MSG::DEBUG) {
313  m_log << MSG::DEBUG << "Listing all monitored entries: " << std::endl;
314  DEBMSG << list() << endmsg;
315  }
316 
317  bool allgood = true;
318  for ( IoStack_t::iterator io = m_iostack.begin(), ioEnd = m_iostack.end();
319  io != ioEnd;
320  ++io ) {
321  m_log << MSG::DEBUG << " [" << (*io)->name() << "]->io_reinit()..."
322  << endmsg;
323  if ( !(*io)->io_reinit().isSuccess() ) {
324  allgood = false;
325  m_log << MSG::ERROR << "problem in [" << (*io)->name()
326  << "]->io_reinit() !" << endmsg;
327  }
328  // we are done with this guy... release it
329  (*io)->release();
330  }
331 
332  // we are done.
333  // FIXME: shall we allow for multiple io_reinitialize ?
334  // m_iostack.clear();
335  // m_ioregistry.clear();
336  // m_cdict.clear();
337 
338  return allgood
341 }
342 
343 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
344 
347  const std::string& new_fname) {
348 
349  DEBMSG << "--> io_update(" << ioc->name() << ","
350  << old_fname << "," << new_fname << ")" << endmsg;
351 
353  for (it = m_cdict.equal_range(ioc).first;
354  it != m_cdict.equal_range(ioc).second;
355  ++it) {
356 
357  if (it->second.m_oldfname == old_fname) {
358  DEBMSG << "updating " << ioc->name() << " f: " << old_fname << " -> "
359  << new_fname << endmsg;
360  it->second.m_newfname = new_fname;
361  return StatusCode::SUCCESS;
362  }
363  }
364 
365  return StatusCode::FAILURE;
366 
367 }
368 
369 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
370 
373 
374  DEBMSG << "--> io_update(" << ioc->name() << ","
375  << work_dir << ")" << endmsg;
376 
378  for (it = m_cdict.equal_range(ioc).first;
379  it != m_cdict.equal_range(ioc).second;
380  ++it) {
381 
382  switch(it->second.m_iomode) {
384  {
385  it->second.m_newfname = it->second.m_oldabspath;
386  break;
387  }
389  {
390  boost::filesystem::path oldPath(it->second.m_oldfname);
391  if(oldPath.is_relative() &&
392  oldPath.filename()==oldPath.relative_path()) {
393 
394  // Only file name was provided for writing. This is the usual mode of operation
395  // ***
396  // NB. In such cases it would make sense to set newfname=oldfname, however this will break
397  // existing client codes, which assume that newfname contains "/"
398  // Thus we set newfname=workdir/oldfname
399  // ***
400 
401  boost::filesystem::path newfname(work_dir);
402  newfname /= oldPath;
403  it->second.m_newfname = newfname.string();
404  }
405  else {
406  // New name should be the old absolute path
407  it->second.m_newfname = it->second.m_oldabspath;
408  }
409 
410  break;
411  }
412  default:
413  {
414  // Don't know how to deal with either RW or INVALID
415  m_log << MSG::ERROR << "Unable to update IoComponent for the mode " << it->second.m_iomode << endmsg;
416  return StatusCode::FAILURE;
417  }
418  }
419 
420  } // for
421 
422  return StatusCode::SUCCESS;
423 
424 }
425 
426 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
427 
428 StatusCode
430 {
431  DEBMSG << "-->io_update_all for the directory " << work_dir << endmsg;
432  bool allgood = true;
433  for ( IoStack_t::iterator io = m_iostack.begin(), ioEnd = m_iostack.end();
434  io != ioEnd;
435  ++io ) {
436  if ( !io_update(*io,work_dir).isSuccess() ) {
437  allgood = false;
438  m_log << MSG::ERROR << "problem in [" << (*io)->name()
439  << "]->io_update() !" << endmsg;
440  }
441  }
442  return allgood ? StatusCode::SUCCESS : StatusCode::FAILURE;
443 }
444 
445 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
446 
453 {
454 
455  m_log << MSG::DEBUG << "--> io_finalize()" << endmsg;
456  m_log << MSG::DEBUG << "finalizing I/O subsystem..." << endmsg;
457 
458  if (m_log.level() <= MSG::DEBUG) {
459  m_log << MSG::DEBUG << "Listing all monitored entries: " << std::endl;
460  DEBMSG << list() << endmsg;
461  }
462 
463  bool allgood = true;
464  for ( IoStack_t::iterator io = m_iostack.begin(), ioEnd = m_iostack.end();
465  io != ioEnd;
466  ++io ) {
467  m_log << MSG::DEBUG << " [" << (*io)->name() << "]->io_finalize()..."
468  << endmsg;
469  if ( !(*io)->io_finalize().isSuccess() ) {
470  allgood = false;
471  m_log << MSG::ERROR << "problem in [" << (*io)->name()
472  << "]->io_finalize() !" << endmsg;
473  }
474  }
475 
476  return allgood
479 }
480 
481 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
482 
483 bool
485  const {
486 
488  if (!findComp(c,pit)) {
489  itr = pit.first;
490  return false;
491  }
492 
493  for (itr = pit.first; itr != pit.second; ++itr) {
494  if (itr->second.m_oldfname == f) {
495  return true;
496  }
497  }
498 
499  return false;
500 
501 }
502 
503 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
504 
505 bool
507 
508  pit = m_cdict.equal_range(c);
509 
510  if (pit.first == pit.second) {
511  return false;
512  } else {
513  return true;
514  }
515 
516 }
517 
518 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
519 
520 bool
522  std::pair<iodITR,iodITR>& pit) const {
523 
524  pit.first = m_cdict.end();
525  pit.second = m_cdict.end();
526 
528  if (itr == m_ioregistry.end()) {
529  return false;
530  }
531 
532  return findComp(itr->second, pit);
533 }
534 
535 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
536 
539 
540  ostringstream ost;
541 
542  ost << "Listing all IoComponents (" << m_cdict.size() << "): " << endl;
543  for (iodITR it = m_cdict.begin(); it != m_cdict.end(); ++it) {
544  ost << " " << it->first->name() << " " << it->second
545  << endl;
546  }
547 
548  return ost.str();
549 }
550 
551 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
552 
553 void
555 
557 
558 
559  if ( i.type() == IncidentType::BeginInputFile ) {
560 
561  const FileIncident *fi = dynamic_cast<const FileIncident*> ( &i );
562  DEBMSG << "BeginInputFile: s: " << fi->source() << " t: " << fi->type()
563  << " n: " << fi->fileName() << " g: " << fi->fileGuid()
564  << endmsg;
565 
566  if (findComp(fi->source(),pit)) {
567  DEBMSG << " found component: " << endmsg;
568  while (pit.first != pit.second) {
569  IIoComponent* c = pit.first->first;
570  IoComponentEntry e = pit.first->second;
571  DEBMSG << " c: " << c->name() << " " << e << endmsg;
572 
573  ++pit.first;
574  }
575  } else {
576  DEBMSG << " could not find component \"" << fi->source()
577  << "\"!" << endmsg;
578  }
579 
580 
581 
582  } else if ( i.type() == IncidentType::BeginOutputFile ) {
583 
584  const FileIncident *fi = dynamic_cast<const FileIncident*> ( &i );
585  DEBMSG << "BeginOutputFile: s: " << fi->source() << " t: " << fi->type()
586  << " n: " << fi->fileName() << " g: " << fi->fileGuid()
587  << endmsg;
588 
589  if (findComp(fi->source(),pit)) {
590  DEBMSG << " found component: " << endmsg;
591  while (pit.first != pit.second) {
592  IIoComponent* c = pit.first->first;
593  IoComponentEntry e = pit.first->second;
594  DEBMSG << " c: " << c->name() << " " << e << endmsg;
595 
596  ++pit.first;
597  }
598  } else {
599  DEBMSG << " could not find component \"" << fi->source()
600  << "\"!" << endmsg;
601  }
602 
603  }
604 
605 }

Generated at Wed Dec 4 2013 14:33:10 for Gaudi Framework, version v24r2 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004