MASA-Core
Buffer2.hpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #ifndef BUFFER2_H
00023 #define BUFFER2_H
00024 
00025 #include <pthread.h>
00026 #include <string>
00027 using namespace std;
00028 
00029 
00030 #include "CellsReader.hpp"
00031 #include "CellsWriter.hpp"
00032 
00033 struct buffer2_statistics_t {
00034         float time;
00035         int bufferUsage;
00036         int totalReadBytes;
00037         float blockingReadTime;
00038         int totalWriteBytes;
00039         float blockingWriteTime;
00040 
00041         const buffer2_statistics_t operator-(const buffer2_statistics_t &other) const {
00042                 buffer2_statistics_t ret;
00043                 ret.time = this->time - other.time;
00044                 ret.bufferUsage = this->bufferUsage - other.bufferUsage;
00045                 ret.totalReadBytes = this->totalReadBytes - other.totalReadBytes;
00046                 ret.blockingReadTime = this->blockingReadTime - other.blockingReadTime;
00047                 ret.totalWriteBytes = this->totalWriteBytes - other.totalWriteBytes;
00048                 ret.blockingWriteTime = this->blockingWriteTime - other.blockingWriteTime;
00049                 return ret;
00050         }
00051 };
00052 
00053 
00054 
00055 class Buffer2 {
00056 public:
00057         Buffer2(int bufferMax);
00058         virtual ~Buffer2();
00059         
00060         int readBuffer(cell_t* data, int nmemb);
00061         int writeBuffer(const cell_t* data, int nmemb);
00062         void waitEmptyBuffer();
00063         
00064         void destroy();
00065         bool isDestroyed();
00066 
00067         /**
00068          * Returns the maximum capacity in bytes of the buffer.
00069          * @return The size of the buffer in bytes.
00070          */
00071         int getCapacity();
00072 
00073         /**
00074          * Returns a struct containing statistics of the buffer.
00075          * @return statistics of the buffer.
00076          */
00077         buffer2_statistics_t getStatistics();
00078 
00079         /**
00080          * Defines a logfile to receive the buffer statistics in each
00081          * interval period.
00082          *
00083          * @param logFile the filename of the log file.
00084          * @param interval The period in seconds to log the statistics.
00085          */
00086     void setLogFile(string logFile, float interval);
00087 
00088         private:
00089                 buffer2_statistics_t stats;
00090 
00091         float tempBlockingWriteTime;
00092         float tempBlockingReadTime;
00093 
00094 
00095                 pthread_mutex_t mutex;
00096                 pthread_cond_t notFullCond;
00097                 pthread_cond_t notEmptyCond;
00098                 pthread_cond_t emptyCond;
00099                 pthread_cond_t loggerCond;
00100                 
00101                 bool destroyed;
00102                 
00103                 cell_t* buffer;
00104                 int buffer_size;
00105                 int buffer_start;
00106                 int buffer_end;
00107                 int buffer_max;
00108 
00109         pthread_t loggerThread;
00110         string logFile;
00111         bool isLogging;
00112         float logInterval; // in seconds
00113 
00114                 int circularSkip(int len);
00115                 int circularLoad(cell_t* dst, int len);
00116                 int circularStore(const cell_t* src, int len);
00117                 int sizeUsed();
00118                 int sizeAvailable();
00119 
00120         static void* staticLogThread(void *arg);
00121 
00122             void logThread();
00123 };
00124 
00125 #endif // BUFFER2_H