MASA-Core
BufferedStream.cpp
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 #include "BufferedStream.hpp"
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <pthread.h>
00027 
00028 #include "BufferLogger.hpp"
00029 
00030 BufferedStream::BufferedStream() {
00031         this->buffer = NULL;
00032 }
00033 
00034 BufferedStream::~BufferedStream() {
00035         destroyBuffer();
00036 }
00037 
00038 void BufferedStream::destroyBuffer() {
00039         if (buffer != NULL) {
00040                 buffer->waitEmptyBuffer();
00041                 buffer->destroy();
00042                 pthread_join(threadId, NULL);
00043                 delete buffer;
00044                 buffer = NULL;
00045         }
00046 }
00047 
00048 bool BufferedStream::isBufferDestroyed() {
00049         return buffer->isDestroyed();
00050 }
00051 
00052 int BufferedStream::readBuffer(cell_t* buf, int len) {
00053         return buffer->readBuffer(buf, len);
00054 }
00055 
00056 int BufferedStream::writeBuffer(const cell_t* buf, int len) {
00057         return buffer->writeBuffer(buf, len);
00058 }
00059 
00060 void BufferedStream::initBuffer(int bufferLimit) {
00061         this->buffer = new Buffer2(bufferLimit);
00062 
00063     int rc = pthread_create(&threadId, NULL, staticThreadFunction, (void *)this);
00064     if (rc){
00065         printf("ERROR; return code from pthread_create() is %d\n", rc);
00066         exit(-1);
00067     }
00068 }
00069 
00070 void* BufferedStream::staticThreadFunction(void* arg) {
00071         BufferedStream* _this = (BufferedStream*)arg;
00072     _this->bufferLoop();
00073     return NULL;
00074 }
00075 
00076 void BufferedStream::setLogFile(string logFile, float interval) {
00077         buffer->setLogFile(logFile, interval);
00078 }