LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
TrivialFileDelivery_service.cc
Go to the documentation of this file.
1 // vim: set sw=2 expandtab :
2 
10 #include "fhiclcpp/fwd.h"
11 
12 #include <fstream>
13 #include <mutex>
14 #include <string>
15 #include <vector>
16 
17 using namespace std;
19 
20 namespace art {
21 
22  namespace {
23 
24  void
25  throwIfFileNotExist(char const* fname) noexcept(false)
26  {
27  ifstream f(fname);
28  if (!f) {
30  << "Input file not found: " << fname << ".\n";
31  }
32  }
33 
34  } // unnamed namespace
35 
37  public:
38  struct Config {};
40 
41  TrivialFileDelivery(Parameters const& config);
42 
43  void doConfigure(std::vector<std::string> const& items) override;
44  int doGetNextFileURI(std::string& uri, double& waitTime) override;
45  void doUpdateStatus(std::string const& uri,
46  FileDisposition status) override;
47  void doOutputFileOpened(std::string const& module_label) override;
48  void doOutputModuleInitiated(std::string const& module_label,
49  fhicl::ParameterSet const& pset) override;
50  void doOutputFileClosed(std::string const& module_label,
51  std::string const& file) override;
52  void doEventSelected(std::string const& module_label,
53  EventID const& event_id,
54  HLTGlobalStatus const& acceptance_info) override;
55  bool doIsSearchable() override;
56  void doRewind() override;
57 
58  private:
59  std::string prependFileDesignation(std::string const& name) const;
60 
61  // Protects all data members.
62  mutable std::recursive_mutex mutex_{};
63  std::vector<std::string> fileList_{};
64  std::vector<std::string>::const_iterator nextFile_{fileList_.cbegin()};
65  std::vector<std::string>::const_iterator endOfFiles_{fileList_.cend()};
66  };
67 
68  TrivialFileDelivery::TrivialFileDelivery(
70  {}
71 
72  void
73  TrivialFileDelivery::doConfigure(vector<string> const& items)
74  {
75  std::lock_guard sentry{mutex_};
76  fileList_ = items;
77  nextFile_ = fileList_.begin();
78  endOfFiles_ = fileList_.end();
79  }
80 
81  int
82  TrivialFileDelivery::doGetNextFileURI(string& uri, double& waitTime)
83  {
84  std::lock_guard sentry{mutex_};
85  FileDeliveryStatus stat;
86  if (nextFile_ == endOfFiles_) {
88  return stat;
89  }
90  // Look for protocol.
91  auto pos = nextFile_->find("://");
92  if (pos == string::npos) {
93  // Bare filename.
94  throwIfFileNotExist(nextFile_->c_str());
95  uri = prependFileDesignation(*nextFile_);
96  } else if (nextFile_->substr(0, pos) == "file") {
97  // file://
98  throwIfFileNotExist(nextFile_->c_str() + pos + 3);
99  uri = *nextFile_;
100  } else {
101  // Unknown URI.
102  uri = *nextFile_;
103  }
104  waitTime = 0.0;
106  ++nextFile_;
107  return stat;
108  }
109 
110  void
111  TrivialFileDelivery::doUpdateStatus(string const&, FileDisposition)
112  {}
113 
114  void
115  TrivialFileDelivery::doOutputFileOpened(string const&)
116  {}
117 
118  void
119  TrivialFileDelivery::doOutputModuleInitiated(string const&,
120  ParameterSet const&)
121  {}
122 
123  void
124  TrivialFileDelivery::doOutputFileClosed(string const&, string const&)
125  {}
126 
127  void
128  TrivialFileDelivery::doEventSelected(string const&,
129  EventID const&,
130  HLTGlobalStatus const&)
131  {}
132 
133  bool
134  TrivialFileDelivery::doIsSearchable()
135  {
136  return true;
137  }
138 
139  void
140  TrivialFileDelivery::doRewind()
141  {
142  std::lock_guard sentry{mutex_};
143  nextFile_ = fileList_.begin();
144  }
145 
146  string
147  TrivialFileDelivery::prependFileDesignation(string const& name) const
148  {
149  std::lock_guard sentry{mutex_};
150  string ret{"file://"};
151  ret += name;
152  return ret;
153  }
154 
155 } // namespace art
156 
159  SHARED)
160 
FileDisposition
STL namespace.
intermediate_table::const_iterator const_iterator
TFile f
Definition: plotHisto.C:6
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
TFile * file
Definition: MVAAlg.h:12
#define DECLARE_ART_SERVICE_INTERFACE_IMPL(svc, iface, scope)
#define SUCCESS
Definition: DBScan3DAlg.h:39
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)