00001 #ifndef _ots_BufferImplementation_h_
00002 #define _ots_BufferImplementation_h_
00003
00004 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00005 #include "otsdaq-core/Macros/CoutMacros.h"
00006 #include "otsdaq-core/DataManager/CircularBufferBase.h"
00007
00008 #include <iostream>
00009 #include <string>
00010 #include <vector>
00011 #include <map>
00012 #include <atomic>
00013
00014 namespace ots
00015 {
00016
00017 template <class D, class H>
00018 class BufferImplementation
00019 {
00020 struct ConsumerStruct
00021 {
00022 CircularBufferBase::ConsumerPriority priority_;
00023 int readPointer_;
00024 std::atomic_bool* subBuffersStatus_;
00025 };
00026
00027 public:
00028 BufferImplementation(std::string producerName="", unsigned int numberOfSubBuffers=10);
00029 BufferImplementation(const BufferImplementation<D,H>& toCopy);
00030 BufferImplementation<D,H>& operator=(const BufferImplementation<D,H>& toCopy);
00031 virtual ~BufferImplementation(void);
00032
00033 void init (void);
00034 void reset (void);
00035 void resetConsumerList (void);
00036 void registerConsumer (std::string name, CircularBufferBase::ConsumerPriority priority);
00037 void unregisterConsumer (std::string name);
00038 int attachToEmptySubBuffer (D*& data,H*& header);
00039 int setWrittenSubBuffer (void);
00040 int write (const D& buffer, const H& header=H());
00041 int read (D& buffer, const std::string& consumer);
00042 int read (D& buffer, H& header, const std::string& consumer);
00043 int read (D*& buffer, H*& header, const std::string& consumer);
00044 int setReadSubBuffer (const std::string& consumer);
00045
00046 bool isEmpty (void);
00047 unsigned int bufferSize (void){return numberOfSubBuffers_;}
00048 unsigned int numberOfWrittenBuffers(void);
00049
00050 private:
00051 enum
00052 {
00053 ErrorBufferFull = -1,
00054 ErrorBufferLocked = -2,
00055 ErrorBufferNotAvailable = -3,
00056 ErrorReadBufferOutOfSync = -4
00057 };
00058 std::string producerName_;
00059 unsigned int numberOfSubBuffers_;
00060 std::map<std::string, ConsumerStruct> consumers_;
00061 int writePointer_;
00062 std::atomic_bool* subBuffersStatus_;
00063 std::vector<H> headers_ ;
00064 std::vector<D> subBuffers_ ;
00065 const bool bufferFree_;
00066
00067 unsigned int nextWritePointer (void);
00068 unsigned int nextReadPointer (const std::string& consumer);
00069 int getFreeBufferIndex(void);
00070 unsigned int getReadPointer (const std::string& consumer);
00071 void setWritten (unsigned int subBuffer);
00072 void setFree (unsigned int subBuffer, const std::string& consumer);
00073 std::atomic_bool& isFree (unsigned int subBuffer);
00074 std::atomic_bool& isFree (unsigned int subBuffer, const std::string& consumer);
00075 void dumpStatus (void);
00076 H& getHeader (unsigned int subBuffer);
00077 D& getSubBuffer (unsigned int subBuffer);
00078 void writeSubBuffer (unsigned int subBuffer, const D& buffer, const H& header);
00079
00080 };
00081 #include "otsdaq-core/DataManager/BufferImplementation.icc"
00082
00083 }
00084 #endif
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097