00001 /* 00002 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva. 00003 * 00004 * This file may be distributed under the terms of the Q Public License 00005 * as defined by Trolltech AS of Norway and appearing in the file 00006 * LICENSE.QPL included in the packaging of this file. 00007 * 00008 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING 00009 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00010 * PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00011 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00012 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00013 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00014 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00015 * 00016 */ 00017 00018 #ifndef __BUFFERSTREAM_H__ 00019 #define __BUFFERSTREAM_H__ 00020 00021 #define MAXLEN_FILE 1024 00022 00023 typedef struct 00024 { 00025 unsigned int writePoint; //indica a posi��o de arquivo em que o bloco se 00026 //encontra (para uso do fseek) 00027 bool unicastRequested; //indica se a requisi��o do bloco foi feita unicast 00028 } StreamControl; 00029 00030 class BufferStream 00031 { 00032 protected: 00033 unsigned int blockSize; 00034 StreamControl *streamControl; 00035 unsigned int streamSize; 00036 FILE *streamFile; 00037 char fileName[ MAXLEN_FILE + MAXLEN_FILE + 1 ]; 00038 int offset; 00039 unsigned int writePoint; 00040 00041 public: 00042 BufferStream( char *fileName, unsigned int streamSize, 00043 unsigned int blockSize ); 00044 ~BufferStream(); 00045 00046 int getNextBlock( unsigned int *block ); 00047 unsigned int getBlockSize(); 00048 void setFileName( const char *fileName ); 00049 void setFileName( const char *fileName, const char *localPath ); 00050 char *getFileName(); 00051 00052 int writeBlock( unsigned int n, unsigned char *block ); 00053 int readBlock( unsigned int n, unsigned char *block ); 00054 int exist( unsigned int n ); 00055 00056 /*** 00057 * isUnicast retorna true se o bloco n tiver sido requisitado unicast, e false 00058 * caso contr�rio. Este valor � preenchido no momento da requisi��o do bloco. 00059 */ 00060 int isUnicast( unsigned int n ); 00061 00062 /*** 00063 * setUnicast seta o valor de unicastRequested para true para o bloco n 00064 * informando que este bloco � fruto de uma requisi��o unicast. 00065 * @param n informa o bloco que deve ter a informa��o unicastRequested setada 00066 * para true. 00067 */ 00068 void setUnicast( unsigned int n ); 00069 00070 #ifdef RIO_EMUL 00071 /*** 00072 * Este metodo soh eh usado no caso em que se estah emulando um cliente 00073 * multicast sem cache. Como os blocos nao sao gravados na cache, podemos 00074 * simplesmente setar a posicaoo no vetor para 0 para dizer que o bloco foi 00075 * liberado. Se o bloco realmnte fosse gravado no disco teriamos que encontrar 00076 * o bloco no arquivo da cache e apaga-lo de lah, mas o arquivo ficaria com um 00077 * buraco e isto teria que ser consertado, bem como todos os writepoints dos 00078 * demais blocos deveria ser redefinido. 00079 */ 00080 bool dropBlock( unsigned int n ); 00081 #endif 00082 }; 00083 #endif