Corrected formatting

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@627 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
cppenthu 2008-07-02 04:05:58 +00:00
parent 1db82dee51
commit eeb13e7b18
6 changed files with 305 additions and 250 deletions

View File

@ -1,14 +1,24 @@
#include "ftfilecreator.h" #include "ftfilecreator.h"
/***********************************************************
*
* ftFileCreator methods
*
***********************************************************/
ftFileCreator::ftFileCreator(std::string path, uint64_t size, std::string ftFileCreator::ftFileCreator(std::string path, uint64_t size, std::string
hash, std::string chunker="default"): ftFileProvider(path,size,hash) { hash, std::string chunker="default"): ftFileProvider(path,size,hash)
/* any inits to do?*/ {
/*
* FIXME any inits to do?
*/
initialize(chunker); initialize(chunker);
} }
void ftFileCreator::initialize(std::string chunker) { void ftFileCreator::initialize(std::string chunker)
{
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/ RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
if (chunker == "default") if (chunker == "default")
fileChunker = new ftFileChunker(total_size); fileChunker = new ftFileChunker(total_size);
else else
@ -17,45 +27,40 @@ void ftFileCreator::initialize(std::string chunker) {
fileChunker->splitFile(); fileChunker->splitFile();
} }
int ftFileCreator::initializeFileAttrs() //not override? int ftFileCreator::initializeFileAttrs()
{ {
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/ //RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
/* check if the file exists */
{ /*
std::cout << * check if the file exists
"ftFileProvider::initializeFileAttrs() Filename: " << file_name; */
} std::cout << "ftFileProvider::initializeFileAttrs() Filename: " << file_name;
/* attempt to open file in writing mode*/ /*
* attempt to open file in writing mode
*/
fd = fopen(file_name.c_str(), "w+b"); fd = fopen(file_name.c_str(), "w+b");
if (!fd) if (!fd)
{ {
std::cout << std::cout << "ftFileProvider::initializeFileAttrs() Failed to open (w+b): " << file_name << std::endl;
"ftFileProvider::initializeFileAttrs() Failed to open (w+b): "
<< file_name << std::endl;
return 0; return 0;
} }
/*
/* if it opened, find it's length */ * if it opened, find it's length
/* move to the end */ * move to the end
*/
if (0 != fseek(fd, 0L, SEEK_END)) if (0 != fseek(fd, 0L, SEEK_END))
{ {
std::cerr << "ftFileProvider::initializeFileAttrs() Seek Failed" << std::endl; std::cerr << "ftFileProvider::initializeFileAttrs() Seek Failed" << std::endl;
//s->status = (PQIFILE_FAIL | PQIFILE_FAIL_NOT_SEEK);*/
return 0; return 0;
} }
/*
/*s->recv_size = ftell(fd); /* get the file length */ * get the file length
//s->total_size = s->size; /* save the total length */ */
//s->fd = fd;*/ recv_size = ftell(fd);
recv_size = ftell(fd); /* get the file length */
/*total_size is unchanged its set at construction*/
return 1; return 1;
} }
@ -64,23 +69,28 @@ bool ftFileCreator::addFileData(uint64_t offset, uint32_t chunk_size, void *data
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/ RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
/* check the status */ /* check the status */
if (fd==NULL) { if (fd==NULL)
{
int init = initializeFileAttrs(); int init = initializeFileAttrs();
if (init ==0) { if (init ==0) {
std::cerr <<"Initialization Failed" << std::endl; std::cerr <<"Initialization Failed" << std::endl;
return 0; return 0;
} }
} }
/* check its at the correct location */
/*
* check its at the correct location
*/
if (offset + chunk_size > this->total_size) if (offset + chunk_size > this->total_size)
{ {
chunk_size = total_size - offset; chunk_size = total_size - offset;
std::cerr <<"Chunk Size greater than total file size, adjusting chunk " std::cerr <<"Chunk Size greater than total file size, adjusting chunk " << "size " << chunk_size << std::endl;
<< "size " << chunk_size << std::endl;
} }
/* go to the offset of the file */ /*
* go to the offset of the file
*/
if (0 != fseek(this->fd, offset, SEEK_SET)) if (0 != fseek(this->fd, offset, SEEK_SET))
{ {
std::cerr << "ftFileCreator::addFileData() Bad fseek" << std::endl; std::cerr << "ftFileCreator::addFileData() Bad fseek" << std::endl;
@ -91,72 +101,66 @@ bool ftFileCreator::addFileData(uint64_t offset, uint32_t chunk_size, void *data
pos = ftell(fd); pos = ftell(fd);
std::cerr << pos << " BEFORE RECV SIZE "<< recv_size << std::endl; std::cerr << pos << " BEFORE RECV SIZE "<< recv_size << std::endl;
/* add the data */ /*
* add the data
*/
if (1 != fwrite(data, chunk_size, 1, this->fd)) if (1 != fwrite(data, chunk_size, 1, this->fd))
{ {
std::cerr << "ftFileCreator::addFileData() Bad fwrite" << std::endl; std::cerr << "ftFileCreator::addFileData() Bad fwrite" << std::endl;
return 0; return 0;
} }
this->recv_size += chunk_size; this->recv_size += chunk_size;
pos = ftell(fd); pos = ftell(fd);
std::cerr << pos << " RECV SIZE "<< recv_size << std::endl;
//Notify ftFileChunker that all are received /*
* Notify ftFileChunker about chunks received
*/
fileChunker->notifyReceived(offset,chunk_size); fileChunker->notifyReceived(offset,chunk_size);
/* if we've completed the request this time */ /*
/*if (s->req_loc + s->req_size == s->recv_size) * FIXME HANDLE COMPLETION HERE - Any better way?
{ */
s->lastDelta = time(NULL) - s->lastTS;
}
if (s->recv_size == s->total_size)
{
pqioutput(PQL_DEBUG_BASIC, ftfilerzone,
"ftfiler::addFileData() File Complete!");
s->status = PQIFILE_COMPLETE;
///* HANDLE COMPLETION HERE
//completeFileTransfer(s); Notify ftFileChunker that all are received
}*/
return 1; return 1;
} }
ftFileCreator::~ftFileCreator()
{
/*
* FIXME Any cleanups specific to filecreator?
ftFileCreator::~ftFileCreator(){ */
} }
bool ftFileCreator::getMissingChunk(uint64_t &offset, uint32_t &chunk) { bool ftFileCreator::getMissingChunk(uint64_t &offset, uint32_t &chunk)
fileChunker->getMissingChunk(offset, chunk); {
return fileChunker->getMissingChunk(offset, chunk);
} }
/*********************************************************** /***********************************************************
* *
* FtFileChunker methods * ftFileChunker methods
* *
***********************************************************/ ***********************************************************/
ftFileChunker::ftFileChunker(uint64_t size): file_size(size), std_chunk_size(10000), monitorPeriod(30) { ftFileChunker::ftFileChunker(uint64_t size): file_size(size), std_chunk_size(10000), monitorPeriod(30)
/* any inits to do?*/ {
std::cout << "Constructor ftFileChunker\n"; /*
* srand for randomized version - move it to the sub-class?
*/
srand ( time(NULL) ); srand ( time(NULL) );
aggregate_status = 0; aggregate_status = 0;
} }
ftFileChunker::~ftFileChunker(){ ftFileChunker::~ftFileChunker()
std::cout << "Destructor of ftFileChunker\n"; {
std::vector<ftChunk>::iterator it; std::vector<ftChunk>::iterator it;
for(int i=0; i<allocationTable.size();i++) { for(unsigned int i=0; i<allocationTable.size();i++) {
delete allocationTable.at(i); /* Does this need a check? */ delete allocationTable.at(i); /* Does this need a check? */
} }
if(!allocationTable.empty()){ if(!allocationTable.empty()){
std::cerr << "Empty table\n";
allocationTable.clear(); allocationTable.clear();
} }
} }
@ -164,15 +168,20 @@ ftFileChunker::~ftFileChunker(){
int ftFileChunker::splitFile(){ int ftFileChunker::splitFile(){
RsStackMutex stack(chunkerMutex); /********** STACK LOCKED MTX ******/ RsStackMutex stack(chunkerMutex); /********** STACK LOCKED MTX ******/
num_chunks = file_size/std_chunk_size; /* all in bytes */
/*
* all in bytes
*/
num_chunks = file_size/std_chunk_size;
/*
* FIXME - Remainder should go into last chunk
*/
uint64_t rem = file_size % std_chunk_size; uint64_t rem = file_size % std_chunk_size;
unsigned int index=0;
int index=0;
std::cout << "splitFile\n";
uint64_t max_chunk_size = file_size - (index * std_chunk_size); uint64_t max_chunk_size = file_size - (index * std_chunk_size);
for(index=0;index<num_chunks;index++){
std::cout << "INDEX " << index << std::endl; for(index=0;index<num_chunks;index++)
{
uint64_t offset = index * std_chunk_size; uint64_t offset = index * std_chunk_size;
time_t now = time(NULL); time_t now = time(NULL);
max_chunk_size = file_size - (index * std_chunk_size); max_chunk_size = file_size - (index * std_chunk_size);
@ -180,33 +189,49 @@ int ftFileChunker::splitFile(){
allocationTable.push_back(f); allocationTable.push_back(f);
} }
for(int j=0;j<allocationTable.size();j++){ /*
std::cout << "SIZE " << allocationTable.at(j)->max_chunk_size << " " << allocationTable.at(j)->chunk_status << std::endl; * DEBUGGER
} * for(int j=0;j<allocationTable.size();j++)
* {
* std::cout << "SIZE " << allocationTable.at(j)->max_chunk_size << " " << allocationTable.at(j)->chunk_status << std::endl;
* }
*/
return 1; return 1;
} }
/*
* This method sets the offset, chunk may be reset if needed
*/
bool ftFileChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk) { bool ftFileChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk)
//This method sets the offset, chunk may be reset if needed {
RsStackMutex stack(chunkerMutex); /********** STACK LOCKED MTX ******/ RsStackMutex stack(chunkerMutex); /********** STACK LOCKED MTX ******/
std::cerr << "Calling getMissingChunk with chunk="<< chunk << std::endl;
int i =0; unsigned int i =0;
bool found = false; bool found = false;
int chunks_after = 0; int chunks_after = 0;
int chunks_rem = 0; int chunks_rem = 0;
/*
* This signals file completion
* FIXME Does it need to be more explicit
*/
if(aggregate_status == num_chunks * ftChunk::RECEIVED) if(aggregate_status == num_chunks * ftChunk::RECEIVED)
return found; return found;
while(i<allocationTable.size())
while(i<allocationTable.size()) { {
if(allocationTable.at(i)->max_chunk_size >=chunk){ if(allocationTable.at(i)->max_chunk_size >=chunk)
{
offset = allocationTable.at(i)->offset; offset = allocationTable.at(i)->offset;
chunks_after = chunk/std_chunk_size; //10KB chunks_after = chunk/std_chunk_size; //10KB
/*
* FIXME Handling remaining chunk < 10KB
*/
chunks_rem = chunk % std_chunk_size; chunks_rem = chunk % std_chunk_size;
chunk -= chunks_rem; chunk -= chunks_rem;
std::cout << "Found " << chunk << " at " << i << " "<< chunks_after << std::endl; /*std::cout << "Found " << chunk << " at " << i << " "<< chunks_after << std::endl;*/
allocationTable.at(i)->max_chunk_size=0; allocationTable.at(i)->max_chunk_size=0;
allocationTable.at(i)->timestamp = time(NULL); allocationTable.at(i)->timestamp = time(NULL);
allocationTable.at(i)->chunk_status = ftChunk::ALLOCATED; allocationTable.at(i)->chunk_status = ftChunk::ALLOCATED;
@ -216,20 +241,24 @@ bool ftFileChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk) {
i++; i++;
} }
if (!found) { if (!found)
{
i=0; i=0;
uint64_t min = allocationTable.at(i)->max_chunk_size - chunk; uint64_t min = allocationTable.at(i)->max_chunk_size - chunk;
uint64_t diff = min; uint64_t diff = min;
int mini = -1; int mini = -1;
while(i<allocationTable.size()) { while(i<allocationTable.size())
{
diff = allocationTable.at(i)->max_chunk_size-chunk; diff = allocationTable.at(i)->max_chunk_size-chunk;
if(diff <= min && diff >0){ if(diff <= min && diff >0)
{
min = allocationTable.at(i)->max_chunk_size - chunk; min = allocationTable.at(i)->max_chunk_size - chunk;
mini = i; mini = i;
} }
i++; i++;
} }
if (min > -1) { if (mini > -1) //mini or min
{
offset = allocationTable.at(mini)->offset; offset = allocationTable.at(mini)->offset;
chunk = allocationTable.at(mini)->max_chunk_size; chunk = allocationTable.at(mini)->max_chunk_size;
chunks_after = chunk/std_chunk_size; //10KB chunks_after = chunk/std_chunk_size; //10KB
@ -244,31 +273,44 @@ bool ftFileChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk) {
} //if not found } //if not found
if (found) { if (found) {
std::cout << "Chunks remaining " << chunks_rem << std::endl; std::cout << "Chunks remaining " << chunks_rem << std::endl;
// update all previous chunks max available size /*
for(int j=0;j<i;j++){ * update all previous chunks max available size
if (allocationTable.at(j)->max_chunk_size >0) * Expensive? Can it be smarter FIXME
allocationTable.at(j)->max_chunk_size -= chunk; */
}
for(unsigned int j=0;j<i;j++)
{
if (allocationTable.at(j)->max_chunk_size >0)
allocationTable.at(j)->max_chunk_size -= chunk;
}
for(int j=i;j<i+chunks_after;j++){ for(unsigned int j=i;j<i+chunks_after;j++)
allocationTable.at(j)->max_chunk_size = 0; {
allocationTable.at(j)->chunk_status = ftChunk::ALLOCATED; allocationTable.at(j)->max_chunk_size = 0;
allocationTable.at(j)->chunk_status = ftChunk::ALLOCATED;
} }
for(int j=0;j<allocationTable.size();j++){ // DEBUGGER - Uncomment
std::cout << "After allocation " << allocationTable.at(j)->max_chunk_size << " " << allocationTable.at(j)->chunk_status << std::endl; for(unsigned int j=0;j<allocationTable.size();j++)
} {
std::cout << "After allocation " << allocationTable.at(j)->max_chunk_size << " " << allocationTable.at(j)->chunk_status << std::endl;
}
} }
return found; return found;
} }
/* This should run on a separate thread when ftFileChunker is initialized*/ /*
int ftFileChunker::monitor() { * This should run on a separate thread when ftFileChunker is initialized
* FIXME Implemet DrBob's suggestion of request-fired check instead of dedicated
* thread
*/
int ftFileChunker::monitor()
{
int reset = 0; int reset = 0;
std::cout<<"Running monitor.."<<std::endl; std::cout<<"Running monitor.."<<std::endl;
for(int j=0;j<allocationTable.size();j++){ for(unsigned int j=0;j<allocationTable.size();j++){
if(allocationTable.at(j)->chunk_status == ftChunk::ALLOCATED && allocationTable.at(j)->timestamp - time(NULL) > 30){ if(allocationTable.at(j)->chunk_status == ftChunk::ALLOCATED && allocationTable.at(j)->timestamp - time(NULL) > 30){
allocationTable.at(j)->chunk_status = ftChunk::AVAIL; allocationTable.at(j)->chunk_status = ftChunk::AVAIL;
reset++; reset++;
@ -277,11 +319,13 @@ int ftFileChunker::monitor() {
return reset; return reset;
} }
void ftFileChunker::setMonitorPeriod(int period) { void ftFileChunker::setMonitorPeriod(int period)
{
monitorPeriod = period; monitorPeriod = period;
} }
void ftFileChunker::run(){ void ftFileChunker::run()
{
while(1) while(1)
{ {
@ -289,37 +333,54 @@ void ftFileChunker::run(){
for(int i = 0; i < monitorPeriod; i++) for(int i = 0; i < monitorPeriod; i++)
{ {
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ /******************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS #ifndef WINDOWS_SYS
sleep(1); sleep(1);
#else #else
Sleep(1000); Sleep(1000);
#endif #endif
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ /******************* WINDOWS/UNIX SPECIFIC PART ******************/
} }
monitor(); monitor();
} }
} }
int ftFileChunker::notifyReceived(uint64_t offset, uint32_t chunk_size)
{
RsStackMutex stack(chunkerMutex); /********** STACK LOCKED MTX ******/
int index = offset / std_chunk_size;
if(allocationTable.at(index)->chunk_status == ftChunk::ALLOCATED){
allocationTable.at(index)->chunk_status = ftChunk::RECEIVED;
aggregate_status += ftChunk::RECEIVED;
}
return aggregate_status;
}
/***********************************************************
*
* ftFileRandomizeChunker methods
*
***********************************************************/
ftFileRandomizeChunker::ftFileRandomizeChunker(uint64_t size): ftFileRandomizeChunker::ftFileRandomizeChunker(uint64_t size):
ftFileChunker(size) { ftFileChunker(size)
{
} }
ftFileRandomizeChunker::~ftFileRandomizeChunker(){ ftFileRandomizeChunker::~ftFileRandomizeChunker()
std::cout << "Destructor of ftFileRandomizeChunker\n"; {
} }
bool ftFileRandomizeChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk) { bool ftFileRandomizeChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk) {
//This method sets the offset, chunk may be reset if needed
RsStackMutex stack(chunkerMutex); /********** STACK LOCKED MTX ******/ RsStackMutex stack(chunkerMutex); /********** STACK LOCKED MTX ******/
std::cerr << "Calling getMissingChunk with chunk="<< chunk << std::endl; std::cerr << "Calling getMissingChunk with chunk="<< chunk << std::endl;
int i =0; unsigned int i =0;
bool found = false; bool found = false;
int chunks_after = 0; int chunks_after = 0;
int chunks_rem = 0; int chunks_rem = 0;
@ -328,15 +389,19 @@ bool ftFileRandomizeChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk)
return found; return found;
std::vector<int> randomIndex; std::vector<int> randomIndex;
while(i<allocationTable.size()) { while(i<allocationTable.size())
{
if(allocationTable.at(i)->max_chunk_size >=chunk){ if(allocationTable.at(i)->max_chunk_size >=chunk){
randomIndex.push_back(i); randomIndex.push_back(i);
} }
i++; i++;
} }
/* test to make sure its picking every index*/ /*
if (randomIndex.size()>0) { * FIXME test sufficiently to make sure its picking every index
*/
if (randomIndex.size()>0)
{
int rnum = rand() % randomIndex.size(); int rnum = rand() % randomIndex.size();
i = randomIndex.at(rnum); i = randomIndex.at(rnum);
std::cout << "i=" <<i << " rnum " << rnum << std::endl; std::cout << "i=" <<i << " rnum " << rnum << std::endl;
@ -351,20 +416,24 @@ bool ftFileRandomizeChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk)
found = true; found = true;
} }
if (!found) { if (!found)
{
i=0; i=0;
uint64_t min = allocationTable.at(i)->max_chunk_size - chunk; uint64_t min = allocationTable.at(i)->max_chunk_size - chunk;
uint64_t diff = min; uint64_t diff = min;
int mini = -1; int mini = -1;
while(i<allocationTable.size()) { while(i<allocationTable.size())
{
diff = allocationTable.at(i)->max_chunk_size-chunk; diff = allocationTable.at(i)->max_chunk_size-chunk;
if(diff <= min && diff >0){ if(diff <= min && diff >0)
{
min = allocationTable.at(i)->max_chunk_size - chunk; min = allocationTable.at(i)->max_chunk_size - chunk;
mini = i; mini = i;
} }
i++; i++;
} }
if (min > -1) { if (mini > -1)
{
offset = allocationTable.at(mini)->offset; offset = allocationTable.at(mini)->offset;
chunk = allocationTable.at(mini)->max_chunk_size; chunk = allocationTable.at(mini)->max_chunk_size;
chunks_after = chunk/std_chunk_size; //10KB chunks_after = chunk/std_chunk_size; //10KB
@ -378,39 +447,39 @@ bool ftFileRandomizeChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk)
} //if not found } //if not found
if (found) { if (found)
std::cout << "Chunks remaining " << chunks_rem << std::endl; {
// update all previous chunks max available size // update all previous chunks max available size
for(int j=0;j<i;j++){ for(unsigned int j=0;j<i;j++)
if (allocationTable.at(j)->max_chunk_size >0) {
allocationTable.at(j)->max_chunk_size -= chunk; if (allocationTable.at(j)->max_chunk_size >0)
} allocationTable.at(j)->max_chunk_size -= chunk;
}
for(int j=i;j<i+chunks_after;j++){ for(unsigned int j=i;j<i+chunks_after;j++)
allocationTable.at(j)->max_chunk_size = 0; {
allocationTable.at(j)->chunk_status = ftChunk::ALLOCATED; allocationTable.at(j)->max_chunk_size = 0;
allocationTable.at(j)->chunk_status = ftChunk::ALLOCATED;
} }
/* for(int j=0;j<allocationTable.size();j++){ /* for(int j=0;j<allocationTable.size();j++){
std::cout << "After allocation " << j << " " << allocationTable.at(j)->max_chunk_size << " " << allocationTable.at(j)->chunk_status << std::endl; std::cout << "After allocation " << j << " " << allocationTable.at(j)->max_chunk_size << " " << allocationTable.at(j)->chunk_status << std::endl;
}*/ }*/
} }
return found; return found;
} }
/***********************************************************
*
* ftChunk methods
*
***********************************************************/
ftChunk::ftChunk(uint64_t offset,uint64_t chunk_size,time_t time, ftChunk::Status s) : offset(offset), max_chunk_size(chunk_size), timestamp(time), chunk_status(s)
{
int ftFileChunker::notifyReceived(uint64_t offset, uint32_t chunk_size) {
RsStackMutex stack(chunkerMutex); /********** STACK LOCKED MTX ******/ }
int index = offset / std_chunk_size;
std::cout << "INDEX " << index << std::endl; ftChunk::~ftChunk()
if(allocationTable.at(index)->chunk_status == ftChunk::ALLOCATED){ {
allocationTable.at(index)->chunk_status = ftChunk::RECEIVED;
aggregate_status += ftChunk::RECEIVED;
}
}
ftChunk::ftChunk(uint64_t offset,uint64_t chunk_size,time_t time, ftChunk::Status s) : offset(offset), max_chunk_size(chunk_size), timestamp(time), chunk_status(s){
} }

View File

@ -46,21 +46,30 @@ public:
~ftFileCreator(); ~ftFileCreator();
/* overloaded from FileProvider */ /*
//virtual bool getFileData(uint64_t offset, uint32_t chunk_size, void *data); * overloaded from FileProvider FIXME
int initializeFileAttrs(); //not override? * virtual bool getFileData(uint64_t offset, uint32_t chunk_size, void *data);
*
*/
/*
* FIXME: initializeFileAttrs
* Should this be a over-ridden version of ftfile provider?
* What happens in the case of simultaneous upload and download?
*/
/* creation functions for FileCreator */ int initializeFileAttrs();
bool getMissingChunk(uint64_t &offset, uint32_t &chunk); /*
bool addFileData(uint64_t offset, uint32_t chunk_size, void *data); * creation functions for FileCreator
*/
bool getMissingChunk(uint64_t &offset, uint32_t &chunk);
bool addFileData(uint64_t offset, uint32_t chunk_size, void *data);
private: private:
/* structure to track missing chunks */ /*
* structure to track missing chunks
*/
/* structure to hold*/
// std::string save_path; use file_name from parent
// uint64_t total_size;
uint64_t recv_size; uint64_t recv_size;
std::string hash; std::string hash;
ftFileChunker *fileChunker; ftFileChunker *fileChunker;
@ -74,14 +83,18 @@ private:
*/ */
class ftFileChunker : public RsThread { class ftFileChunker : public RsThread {
public: public:
/* Does this require hash?? */ /*
* FIXME Does filechunker require hash??
*/
ftFileChunker(uint64_t size); ftFileChunker(uint64_t size);
virtual ~ftFileChunker(); virtual ~ftFileChunker();
/* Breaks up the file into evenly sized chunks
Initializes all chunks to never_requested /*
*/ * Breaks up the file into evenly sized chunks
int splitFile(); * Initializes all chunks to never_requested
virtual void run(); */
int splitFile();
virtual void run();
virtual bool getMissingChunk(uint64_t &offset, uint32_t &chunk); virtual bool getMissingChunk(uint64_t &offset, uint32_t &chunk);
bool getMissingChunkRandom(uint64_t &offset, uint32_t &chunk); bool getMissingChunkRandom(uint64_t &offset, uint32_t &chunk);
int notifyReceived(uint64_t offset, uint32_t chunk_size); int notifyReceived(uint64_t offset, uint32_t chunk_size);
@ -97,7 +110,6 @@ protected:
RsMutex chunkerMutex; /********** STACK LOCKED MTX ******/ RsMutex chunkerMutex; /********** STACK LOCKED MTX ******/
}; };
class ftFileRandomizeChunker : public ftFileChunker { class ftFileRandomizeChunker : public ftFileChunker {
public: public:
ftFileRandomizeChunker(uint64_t size); ftFileRandomizeChunker(uint64_t size);
@ -106,7 +118,6 @@ public:
}; };
class ftChunk { class ftChunk {
public: public:
enum Status {AVAIL, ALLOCATED, RECEIVED}; enum Status {AVAIL, ALLOCATED, RECEIVED};
@ -115,7 +126,7 @@ public:
uint64_t max_chunk_size; uint64_t max_chunk_size;
time_t timestamp; time_t timestamp;
Status chunk_status; Status chunk_status;
~ftChunk();
}; };
#endif // FT_FILE_PROVIDER_HEADER #endif // FT_FILE_PROVIDER_HEADER

View File

@ -1,33 +1,8 @@
#include "ftfilecreator.h" #include "ftfilecreator.h"
main(){ main(){
/*Testing default file chunker*/
uint32_t total_size = 1000000; /*100KB*/
ftFileChunker fc(total_size);
fc.setMonitorPeriod(10);
fc.splitFile();
std::cout << "Starting fc monitor thread\n";
fc.start();
/*Simulate calls from transfer module*/
uint64_t offset; uint64_t offset;
uint32_t csize = 40000; /* 40KB*/ uint32_t csize;
if (fc.getMissingChunk(offset, csize)){
std::cout << "Missing Chunk's offset=" << offset << " chunk_size=" << csize << std::endl;
}
sleep(5);
csize = 10000;
if (fc.getMissingChunk(offset, csize)){
std::cout << "Missing Chunk's offset=" << offset << " chunk_size=" << csize << std::endl;
}
csize = 15000; /* Ask more than the multiple of std_chunk_size*/
if (fc.getMissingChunk(offset, csize)){
std::cout << "Missing Chunk's offset=" << offset << " chunk_size=" << csize << std::endl;
}
sleep(10);
/*Test file creator*/ /*Test file creator*/
ftFileCreator fcreator("somefile",100000,"hash","default"); ftFileCreator fcreator("somefile",100000,"hash","default");
csize = 40000; csize = 40000;
@ -45,7 +20,8 @@ main(){
std::cout << "Missing Chunk's offset=" << offset << " chunk_size=" << csize << std::endl; std::cout << "Missing Chunk's offset=" << offset << " chunk_size=" << csize << std::endl;
} }
/* Test file creator adding data to file out-of-order*/
std::cout << "Test file creator adding data to file out-of-order\n";
char* alpha = "abcdefghij"; char* alpha = "abcdefghij";
std::cerr << "Call to addFileData =" << fcreator.addFileData(10,10,alpha ); std::cerr << "Call to addFileData =" << fcreator.addFileData(10,10,alpha );
char* numerical = "1234567890"; char* numerical = "1234567890";
@ -106,7 +82,6 @@ main(){
std::cout << "getMissing chunk returned nothing" << std::endl; std::cout << "getMissing chunk returned nothing" << std::endl;
} }
fc.join();
free(allA); free(allA);
free(allB); free(allB);
} }

View File

@ -2,32 +2,35 @@
ftFileProvider::ftFileProvider(std::string path, uint64_t size, std::string ftFileProvider::ftFileProvider(std::string path, uint64_t size, std::string
hash) : total_size(size), hash(hash), file_name(path), fd(NULL) { hash) : total_size(size), hash(hash), file_name(path), fd(NULL) {
//open a file and read it into a binary array!
} }
ftFileProvider::~ftFileProvider(){ ftFileProvider::~ftFileProvider(){
std::cout << "ftFileProvider d'tor" << std::endl;
if (fd!=NULL) { if (fd!=NULL) {
std::cout <<"fd is not null"<<std::endl;
fclose(fd); fclose(fd);
} }
else {
std::cout <<"fd is null"<<std::endl;
}
} }
bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *data) bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *data)
{ {
RsStackMutex stack(ftPMutex); /********** STACK LOCKED MTX ******/ RsStackMutex stack(ftPMutex); /********** STACK LOCKED MTX ******/
if (fd==NULL) { if (fd==NULL)
{
int init = initializeFileAttrs(); int init = initializeFileAttrs();
if (init ==0) { if (init ==0)
{
std::cerr <<"Initialization Failed" << std::endl; std::cerr <<"Initialization Failed" << std::endl;
return 0; return 0;
} }
} }
//Use private data, which has a pointer to file, total size etc
/*
* Use private data, which has a pointer to file, total size etc
*/
/*
* FIXME: Warning of comparison between unsigned and signed int?
*/
int data_size = chunk_size; int data_size = chunk_size;
long base_loc = offset; long base_loc = offset;
@ -38,13 +41,18 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *dat
} }
if (data_size > 0) if (data_size > 0)
{ {
/*
/* seek for base_loc */ * seek for base_loc
*/
fseek(fd, base_loc, SEEK_SET); fseek(fd, base_loc, SEEK_SET);
void *data = malloc(chunk_size); void *data = malloc(chunk_size);
/* read the data */
/*
* read the data
*/
if (1 != fread(data, data_size, 1, fd)) if (1 != fread(data, data_size, 1, fd))
{ {
std::cerr << "ftFileProvider::getFileData() Failed to get data!"; std::cerr << "ftFileProvider::getFileData() Failed to get data!";
@ -52,8 +60,8 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *dat
return 0; return 0;
} }
/*
/* Update status of ftFileStatus to reflect last usage (for GUI display) * Update status of ftFileStatus to reflect last usage (for GUI display)
* We need to store. * We need to store.
* (a) Id, * (a) Id,
* (b) Offset, * (b) Offset,
@ -61,8 +69,7 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *dat
* (d) timestamp * (d) timestamp
*/ */
time_t now = time(NULL); time_t now = time(NULL);
//s->id = id;
req_loc = offset; req_loc = offset;
req_size = data_size; req_size = data_size;
lastTS = now; lastTS = now;
@ -74,44 +81,42 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *dat
return 1; return 1;
} }
int ftFileProvider::initializeFileAttrs() int ftFileProvider::initializeFileAttrs()
{ {
/* check if the file exists */ /*
* check if the file exists
*/
{ {
std::cout << std::cout <<
"ftFileProvider::initializeFileAttrs() Filename: " << file_name; "ftFileProvider::initializeFileAttrs() Filename: " << file_name;
} }
/* attempt to open file */ /*
* attempt to open file
*/
fd = fopen(file_name.c_str(), "r+b"); fd = fopen(file_name.c_str(), "r+b");
if (!fd) if (!fd)
{ {
std::cout << std::cout <<
"ftFileProvider::initializeFileAttrs() Failed to open (r+b): " "ftFileProvider::initializeFileAttrs() Failed to open (r+b): "<< file_name << std::endl;
<< file_name << std::endl;
return 0; return 0;
} }
/* if it opened, find it's length */ /*
/* move to the end */ * if it opened, find it's length
* move to the end
*/
if (0 != fseek(fd, 0L, SEEK_END)) if (0 != fseek(fd, 0L, SEEK_END))
{ {
std::cerr << "ftFileProvider::initializeFileAttrs() Seek Failed" << std::endl; std::cerr << "ftFileProvider::initializeFileAttrs() Seek Failed" << std::endl;
//s->status = (PQIFILE_FAIL | PQIFILE_FAIL_NOT_SEEK);*/
return 0; return 0;
} }
/*s->recv_size = ftell(fd); /* get the file length */
//s->total_size = s->size; /* save the total length */
//s->fd = fd;*/
total_size = ftell(fd); total_size = ftell(fd);
std::cout <<"exit init\n";
return 1; return 1;
} }

View File

@ -36,37 +36,31 @@
class ftFileProvider class ftFileProvider
{ {
public: public:
ftFileProvider(std::string path, uint64_t size, std::string hash); ftFileProvider(std::string path, uint64_t size, std::string hash);
virtual ~ftFileProvider(); virtual ~ftFileProvider();
virtual bool getFileData(uint64_t offset, uint32_t chunk_size, void *data);
/* array already allocated - std::string getHash();
* just move chunk_size bytes to void *data buffer. uint64_t getFileSize();
*/
virtual bool getFileData(uint64_t offset, uint32_t chunk_size, void *data);
/* File Info */
std::string getHash();
uint64_t getFileSize();
protected: protected:
virtual int initializeFileAttrs(); virtual int initializeFileAttrs();
std::string file_name;
std::string hash;
uint64_t total_size; uint64_t total_size;
std::string hash;
std::string file_name;
FILE *fd; FILE *fd;
/* Statistics */ /*
* Structure to gather statistics FIXME: lastRequestor - figure out a
* way to get last requestor (peerID)
*/
std::string lastRequestor; std::string lastRequestor;
uint64_t req_loc; uint64_t req_loc;
uint32_t req_size; uint32_t req_size;
time_t lastTS; time_t lastTS;
/* Mutex Required for stuff below */
/*
* Mutex Required for stuff below
*/
RsMutex ftPMutex; RsMutex ftPMutex;
}; };

View File

@ -1,6 +1,6 @@
#include "ftfileprovider.h" #include "ftfileprovider.h"
main(){ int main(){
ftFileProvider fp("dummy.txt",1,"ABCDEF"); ftFileProvider fp("dummy.txt",1,"ABCDEF");
char data[2]; char data[2];
long offset = 0; long offset = 0;
@ -29,4 +29,5 @@ main(){
} }
offset+=2; offset+=2;
} }
return 1;
} }