otsdaq  v2_01_00
BufferImplementation.h
1 #ifndef _ots_BufferImplementation_h_
2 #define _ots_BufferImplementation_h_
3 
4 #include "otsdaq-core/MessageFacility/MessageFacility.h"
5 #include "otsdaq-core/Macros/CoutMacros.h"
6 #include "otsdaq-core/DataManager/CircularBufferBase.h"
7 
8 #include <iostream>
9 #include <string>
10 #include <vector>
11 #include <map>
12 #include <atomic>
13 
14 namespace ots
15 {
16 
17 template <class D, class H>
19 {
20  struct ConsumerStruct
21  {
22  CircularBufferBase::ConsumerPriority priority_;
23  int readPointer_;
24  std::atomic_bool* subBuffersStatus_;// Status of the Circular Buffer:
25  };
26 
27 public:
28  BufferImplementation(std::string producerName="", unsigned int numberOfSubBuffers=10);
30  BufferImplementation<D,H>& operator=(const BufferImplementation<D,H>& toCopy);
31  virtual ~BufferImplementation(void);
32 
33  void init (void);
34  void reset (void);
35  void resetConsumerList (void);
36  void registerConsumer (std::string name, CircularBufferBase::ConsumerPriority priority);
37  void unregisterConsumer (std::string name);
38  int attachToEmptySubBuffer (D*& data,H*& header);
39  int setWrittenSubBuffer (void);
40  int write (const D& buffer, const H& header=H());
41  int read (D& buffer, const std::string& consumer);
42  int read (D& buffer, H& header, const std::string& consumer);
43  int read (D*& buffer, H*& header, const std::string& consumer);
44  int setReadSubBuffer (const std::string& consumer);//Must be used in conjunction with attachToEmptySubBuffer because it attach to the nextWritePointer buffer
45 
46  bool isEmpty (void);
47  unsigned int bufferSize (void){return numberOfSubBuffers_;}
48  unsigned int numberOfWrittenBuffers(void);
49 
50 private:
51  enum
52  {
53  ErrorBufferFull = -1,
54  ErrorBufferLocked = -2,
55  ErrorBufferNotAvailable = -3,
56  ErrorReadBufferOutOfSync = -4
57  };
58  std::string producerName_;
59  unsigned int numberOfSubBuffers_;
60  std::map<std::string, ConsumerStruct> consumers_; // Pointers to the blocks which the consumers are reading
61  int writePointer_; //Pointer to the available free buffer, -1 means no free buffers!
62  std::atomic_bool* subBuffersStatus_;// Status of the Circular Buffer:
63  std::vector<H> headers_ ;// Buffer Header
64  std::vector<D> subBuffers_ ;// Buffers filled with data
65  const bool bufferFree_;
66 
67  unsigned int nextWritePointer (void);
68  unsigned int nextReadPointer (const std::string& consumer);
69  int getFreeBufferIndex(void);//can return -1 if there are no free buffers!
70  unsigned int getReadPointer (const std::string& consumer);
71  void setWritten (unsigned int subBuffer);
72  void setFree (unsigned int subBuffer, const std::string& consumer);
73  std::atomic_bool& isFree (unsigned int subBuffer);
74  std::atomic_bool& isFree (unsigned int subBuffer, const std::string& consumer);
75  void dumpStatus (void);
76  H& getHeader (unsigned int subBuffer);
77  D& getSubBuffer (unsigned int subBuffer);
78  void writeSubBuffer (unsigned int subBuffer, const D& buffer, const H& header);
79 
80 };
81 #include "otsdaq-core/DataManager/BufferImplementation.icc"
82 
83 }
84 #endif
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97