artdaq_core  v1_07_08
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
SimpleQueueReader.cc
1 #include "artdaq-core/Core/SimpleQueueReader.hh"
2 
3 #include <chrono> // for milliseconds
4 #include <cstddef> // for std::size_t
5 #include <iostream>
6 #include <string>
7 #include <thread> // for sleep_for
8 #include "trace.h" // TRACE
9 
10 namespace artdaq
11 {
12  int simpleQueueReaderApp(int argc, char** argv)
13  {
14  try
15  {
16  size_t eec(0);
17  if (argc == 2)
18  {
19  std::istringstream ins(argv[1]);
20  ins >> eec;
21  }
22  SimpleQueueReader reader(eec);
23  reader.run();
24  return 0;
25  }
26  catch (std::string const& msg)
27  {
28  std::cerr << "simpleQueueReaderApp failed: "
29  << msg;
30  return 1;
31  }
32  catch (...)
33  {
34  return 1;
35  }
36  }
37 
39  SimpleQueueReader(std::size_t eec) :
40  queue_(getGlobalQueue())
41  , expectedEventCount_(eec)
42  {
43  queue_.setReaderIsReady();
44  TRACE( 50, "SimpleQueueReader ctor done (after queue_.setReaderIsReady())" );
45  }
46 
48  {
49  std::size_t eventsSeen = 0;
50  auto doPrint = getenv("VERBOSE_QUEUE_READING");
51  while (true)
52  {
53  RawEvent_ptr rawEventPtr;
54  if (queue_.deqNowait(rawEventPtr))
55  {
56  // If we got a null pointer, we're done...
57  if (!rawEventPtr) { break; }
58  ++eventsSeen;
59  // Otherwise, do our work ...
60  if (doPrint) { std::cout << *rawEventPtr << std::endl; }
61  }
62  else
63  {
64  std::this_thread::sleep_for(std::chrono::milliseconds(250));
65  }
66  }
67  if (expectedEventCount_ && eventsSeen != expectedEventCount_)
68  {
69  std::ostringstream os;
70  os << "Wrong number of events in SimpleQueueReader ("
71  << eventsSeen << " != " << expectedEventCount_ << ").\n";
72  throw os.str();
73  }
74  }
75 }
SimpleQueueReader(std::size_t expectedEventCount=0)
Constructs a SimpleQueueReader.
RawEventQueue & getGlobalQueue(RawEventQueue::SizeType maxSize)
The first thread to call getGlobalQueue() causes the creation of the queue. The queue will be destroy...
Definition: GlobalQueue.cc:10
int simpleQueueReaderApp(int argc, char **argv)
An application which pops items off a RawEventQueue using the SimpleQueueReader.
std::shared_ptr< RawEvent > RawEvent_ptr
A smart pointer to a RawEvent object.
Definition: GlobalQueue.hh:13
bool deqNowait(ValueType &item)
Assign the value at the head of the queue to item and then remove the head of the queue...
SimpleQueueReader will continue to read RawEvent objects off the queue until it encounters a null poi...
void run()
Run until a null pointer is popped off of the RawEventQueue. Throws an excpetion if expectedEventCoun...
void setReaderIsReady(bool rdy=true)
Set the ready flag for the reader.