mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-12-25 23:49:35 -05:00
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:
parent
1db82dee51
commit
eeb13e7b18
@ -1,14 +1,24 @@
|
||||
#include "ftfilecreator.h"
|
||||
|
||||
/***********************************************************
|
||||
*
|
||||
* ftFileCreator methods
|
||||
*
|
||||
***********************************************************/
|
||||
|
||||
ftFileCreator::ftFileCreator(std::string path, uint64_t size, std::string
|
||||
hash, std::string chunker="default"): ftFileProvider(path,size,hash) {
|
||||
/* any inits to do?*/
|
||||
hash, std::string chunker="default"): ftFileProvider(path,size,hash)
|
||||
{
|
||||
/*
|
||||
* FIXME any inits to do?
|
||||
*/
|
||||
initialize(chunker);
|
||||
}
|
||||
|
||||
void ftFileCreator::initialize(std::string chunker) {
|
||||
void ftFileCreator::initialize(std::string chunker)
|
||||
{
|
||||
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
||||
|
||||
if (chunker == "default")
|
||||
fileChunker = new ftFileChunker(total_size);
|
||||
else
|
||||
@ -17,45 +27,40 @@ void ftFileCreator::initialize(std::string chunker) {
|
||||
fileChunker->splitFile();
|
||||
}
|
||||
|
||||
int ftFileCreator::initializeFileAttrs() //not override?
|
||||
int ftFileCreator::initializeFileAttrs()
|
||||
{
|
||||
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
||||
/* check if the file exists */
|
||||
//RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
||||
|
||||
{
|
||||
std::cout <<
|
||||
"ftFileProvider::initializeFileAttrs() Filename: " << file_name;
|
||||
}
|
||||
|
||||
/* attempt to open file in writing mode*/
|
||||
/*
|
||||
* check if the file exists
|
||||
*/
|
||||
std::cout << "ftFileProvider::initializeFileAttrs() Filename: " << file_name;
|
||||
|
||||
/*
|
||||
* attempt to open file in writing mode
|
||||
*/
|
||||
|
||||
fd = fopen(file_name.c_str(), "w+b");
|
||||
if (!fd)
|
||||
{
|
||||
std::cout <<
|
||||
"ftFileProvider::initializeFileAttrs() Failed to open (w+b): "
|
||||
<< file_name << std::endl;
|
||||
std::cout << "ftFileProvider::initializeFileAttrs() Failed to open (w+b): " << file_name << std::endl;
|
||||
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))
|
||||
{
|
||||
std::cerr << "ftFileProvider::initializeFileAttrs() Seek Failed" << std::endl;
|
||||
//s->status = (PQIFILE_FAIL | PQIFILE_FAIL_NOT_SEEK);*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*s->recv_size = ftell(fd); /* get the file length */
|
||||
//s->total_size = s->size; /* save the total length */
|
||||
//s->fd = fd;*/
|
||||
recv_size = ftell(fd); /* get the file length */
|
||||
/*total_size is unchanged its set at construction*/
|
||||
|
||||
|
||||
/*
|
||||
* get the file length
|
||||
*/
|
||||
recv_size = ftell(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -64,23 +69,28 @@ bool ftFileCreator::addFileData(uint64_t offset, uint32_t chunk_size, void *data
|
||||
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
||||
/* check the status */
|
||||
|
||||
if (fd==NULL) {
|
||||
if (fd==NULL)
|
||||
{
|
||||
int init = initializeFileAttrs();
|
||||
if (init ==0) {
|
||||
std::cerr <<"Initialization Failed" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
/* check its at the correct location */
|
||||
|
||||
/*
|
||||
* check its at the correct location
|
||||
*/
|
||||
if (offset + chunk_size > this->total_size)
|
||||
{
|
||||
chunk_size = total_size - offset;
|
||||
std::cerr <<"Chunk Size greater than total file size, adjusting chunk "
|
||||
<< "size " << chunk_size << std::endl;
|
||||
std::cerr <<"Chunk Size greater than total file size, adjusting chunk " << "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))
|
||||
{
|
||||
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);
|
||||
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))
|
||||
{
|
||||
std::cerr << "ftFileCreator::addFileData() Bad fwrite" << std::endl;
|
||||
std::cerr << "ftFileCreator::addFileData() Bad fwrite" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
this->recv_size += chunk_size;
|
||||
|
||||
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);
|
||||
|
||||
/* if we've completed the request this time */
|
||||
/*if (s->req_loc + s->req_size == s->recv_size)
|
||||
{
|
||||
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
|
||||
}*/
|
||||
/*
|
||||
* FIXME HANDLE COMPLETION HERE - Any better way?
|
||||
*/
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ftFileCreator::~ftFileCreator(){
|
||||
ftFileCreator::~ftFileCreator()
|
||||
{
|
||||
/*
|
||||
* FIXME Any cleanups specific to filecreator?
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
bool ftFileCreator::getMissingChunk(uint64_t &offset, uint32_t &chunk) {
|
||||
fileChunker->getMissingChunk(offset, chunk);
|
||||
bool ftFileCreator::getMissingChunk(uint64_t &offset, uint32_t &chunk)
|
||||
{
|
||||
return fileChunker->getMissingChunk(offset, chunk);
|
||||
}
|
||||
|
||||
/***********************************************************
|
||||
*
|
||||
* FtFileChunker methods
|
||||
* ftFileChunker methods
|
||||
*
|
||||
***********************************************************/
|
||||
|
||||
ftFileChunker::ftFileChunker(uint64_t size): file_size(size), std_chunk_size(10000), monitorPeriod(30) {
|
||||
/* any inits to do?*/
|
||||
std::cout << "Constructor ftFileChunker\n";
|
||||
ftFileChunker::ftFileChunker(uint64_t size): file_size(size), std_chunk_size(10000), monitorPeriod(30)
|
||||
{
|
||||
/*
|
||||
* srand for randomized version - move it to the sub-class?
|
||||
*/
|
||||
srand ( time(NULL) );
|
||||
aggregate_status = 0;
|
||||
}
|
||||
|
||||
ftFileChunker::~ftFileChunker(){
|
||||
std::cout << "Destructor of ftFileChunker\n";
|
||||
ftFileChunker::~ftFileChunker()
|
||||
{
|
||||
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? */
|
||||
}
|
||||
if(!allocationTable.empty()){
|
||||
std::cerr << "Empty table\n";
|
||||
allocationTable.clear();
|
||||
}
|
||||
}
|
||||
@ -164,15 +168,20 @@ ftFileChunker::~ftFileChunker(){
|
||||
|
||||
int ftFileChunker::splitFile(){
|
||||
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;
|
||||
|
||||
int index=0;
|
||||
std::cout << "splitFile\n";
|
||||
|
||||
unsigned int index=0;
|
||||
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;
|
||||
time_t now = time(NULL);
|
||||
max_chunk_size = file_size - (index * std_chunk_size);
|
||||
@ -180,33 +189,49 @@ int ftFileChunker::splitFile(){
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
* This method sets the offset, chunk may be reset if needed
|
||||
*/
|
||||
|
||||
bool ftFileChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk) {
|
||||
//This method sets the offset, chunk may be reset if needed
|
||||
bool ftFileChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk)
|
||||
{
|
||||
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;
|
||||
int chunks_after = 0;
|
||||
int chunks_rem = 0;
|
||||
|
||||
/*
|
||||
* This signals file completion
|
||||
* FIXME Does it need to be more explicit
|
||||
*/
|
||||
|
||||
if(aggregate_status == num_chunks * ftChunk::RECEIVED)
|
||||
return found;
|
||||
|
||||
|
||||
while(i<allocationTable.size()) {
|
||||
if(allocationTable.at(i)->max_chunk_size >=chunk){
|
||||
while(i<allocationTable.size())
|
||||
{
|
||||
if(allocationTable.at(i)->max_chunk_size >=chunk)
|
||||
{
|
||||
offset = allocationTable.at(i)->offset;
|
||||
chunks_after = chunk/std_chunk_size; //10KB
|
||||
/*
|
||||
* FIXME Handling remaining chunk < 10KB
|
||||
*/
|
||||
chunks_rem = chunk % std_chunk_size;
|
||||
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)->timestamp = time(NULL);
|
||||
allocationTable.at(i)->chunk_status = ftChunk::ALLOCATED;
|
||||
@ -216,20 +241,24 @@ bool ftFileChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk) {
|
||||
i++;
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
if (!found)
|
||||
{
|
||||
i=0;
|
||||
uint64_t min = allocationTable.at(i)->max_chunk_size - chunk;
|
||||
uint64_t diff = min;
|
||||
int mini = -1;
|
||||
while(i<allocationTable.size()) {
|
||||
while(i<allocationTable.size())
|
||||
{
|
||||
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;
|
||||
mini = i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (min > -1) {
|
||||
if (mini > -1) //mini or min
|
||||
{
|
||||
offset = allocationTable.at(mini)->offset;
|
||||
chunk = allocationTable.at(mini)->max_chunk_size;
|
||||
chunks_after = chunk/std_chunk_size; //10KB
|
||||
@ -244,31 +273,44 @@ bool ftFileChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk) {
|
||||
} //if not found
|
||||
|
||||
if (found) {
|
||||
std::cout << "Chunks remaining " << chunks_rem << std::endl;
|
||||
// update all previous chunks max available size
|
||||
for(int j=0;j<i;j++){
|
||||
if (allocationTable.at(j)->max_chunk_size >0)
|
||||
allocationTable.at(j)->max_chunk_size -= chunk;
|
||||
}
|
||||
std::cout << "Chunks remaining " << chunks_rem << std::endl;
|
||||
/*
|
||||
* update all previous chunks max available size
|
||||
* Expensive? Can it be smarter FIXME
|
||||
*/
|
||||
|
||||
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++){
|
||||
allocationTable.at(j)->max_chunk_size = 0;
|
||||
allocationTable.at(j)->chunk_status = ftChunk::ALLOCATED;
|
||||
|
||||
}
|
||||
for(unsigned int j=i;j<i+chunks_after;j++)
|
||||
{
|
||||
allocationTable.at(j)->max_chunk_size = 0;
|
||||
allocationTable.at(j)->chunk_status = ftChunk::ALLOCATED;
|
||||
}
|
||||
|
||||
for(int j=0;j<allocationTable.size();j++){
|
||||
std::cout << "After allocation " << allocationTable.at(j)->max_chunk_size << " " << allocationTable.at(j)->chunk_status << std::endl;
|
||||
}
|
||||
// DEBUGGER - Uncomment
|
||||
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;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
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){
|
||||
allocationTable.at(j)->chunk_status = ftChunk::AVAIL;
|
||||
reset++;
|
||||
@ -277,11 +319,13 @@ int ftFileChunker::monitor() {
|
||||
return reset;
|
||||
}
|
||||
|
||||
void ftFileChunker::setMonitorPeriod(int period) {
|
||||
void ftFileChunker::setMonitorPeriod(int period)
|
||||
{
|
||||
monitorPeriod = period;
|
||||
}
|
||||
|
||||
void ftFileChunker::run(){
|
||||
void ftFileChunker::run()
|
||||
{
|
||||
|
||||
while(1)
|
||||
{
|
||||
@ -289,37 +333,54 @@ void ftFileChunker::run(){
|
||||
for(int i = 0; i < monitorPeriod; i++)
|
||||
{
|
||||
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
/******************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#ifndef WINDOWS_SYS
|
||||
sleep(1);
|
||||
#else
|
||||
|
||||
Sleep(1000);
|
||||
#endif
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
/******************* WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
}
|
||||
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):
|
||||
ftFileChunker(size) {
|
||||
ftFileChunker(size)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ftFileRandomizeChunker::~ftFileRandomizeChunker(){
|
||||
std::cout << "Destructor of ftFileRandomizeChunker\n";
|
||||
ftFileRandomizeChunker::~ftFileRandomizeChunker()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 ******/
|
||||
std::cerr << "Calling getMissingChunk with chunk="<< chunk << std::endl;
|
||||
int i =0;
|
||||
unsigned int i =0;
|
||||
bool found = false;
|
||||
int chunks_after = 0;
|
||||
int chunks_rem = 0;
|
||||
@ -328,15 +389,19 @@ bool ftFileRandomizeChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk)
|
||||
return found;
|
||||
|
||||
std::vector<int> randomIndex;
|
||||
while(i<allocationTable.size()) {
|
||||
while(i<allocationTable.size())
|
||||
{
|
||||
if(allocationTable.at(i)->max_chunk_size >=chunk){
|
||||
randomIndex.push_back(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();
|
||||
i = randomIndex.at(rnum);
|
||||
std::cout << "i=" <<i << " rnum " << rnum << std::endl;
|
||||
@ -351,20 +416,24 @@ bool ftFileRandomizeChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk)
|
||||
found = true;
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
if (!found)
|
||||
{
|
||||
i=0;
|
||||
uint64_t min = allocationTable.at(i)->max_chunk_size - chunk;
|
||||
uint64_t diff = min;
|
||||
int mini = -1;
|
||||
while(i<allocationTable.size()) {
|
||||
while(i<allocationTable.size())
|
||||
{
|
||||
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;
|
||||
mini = i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (min > -1) {
|
||||
if (mini > -1)
|
||||
{
|
||||
offset = allocationTable.at(mini)->offset;
|
||||
chunk = allocationTable.at(mini)->max_chunk_size;
|
||||
chunks_after = chunk/std_chunk_size; //10KB
|
||||
@ -378,39 +447,39 @@ bool ftFileRandomizeChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk)
|
||||
|
||||
} //if not found
|
||||
|
||||
if (found) {
|
||||
std::cout << "Chunks remaining " << chunks_rem << std::endl;
|
||||
// update all previous chunks max available size
|
||||
for(int j=0;j<i;j++){
|
||||
if (allocationTable.at(j)->max_chunk_size >0)
|
||||
allocationTable.at(j)->max_chunk_size -= chunk;
|
||||
}
|
||||
if (found)
|
||||
{
|
||||
// update all previous chunks max available size
|
||||
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++){
|
||||
allocationTable.at(j)->max_chunk_size = 0;
|
||||
allocationTable.at(j)->chunk_status = ftChunk::ALLOCATED;
|
||||
|
||||
}
|
||||
for(unsigned int j=i;j<i+chunks_after;j++)
|
||||
{
|
||||
allocationTable.at(j)->max_chunk_size = 0;
|
||||
allocationTable.at(j)->chunk_status = ftChunk::ALLOCATED;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}*/
|
||||
/* 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;
|
||||
}*/
|
||||
}
|
||||
return found;
|
||||
}
|
||||
/***********************************************************
|
||||
*
|
||||
* ftChunk methods
|
||||
*
|
||||
***********************************************************/
|
||||
|
||||
|
||||
|
||||
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;
|
||||
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){
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ftChunk::~ftChunk()
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -46,21 +46,30 @@ public:
|
||||
|
||||
~ftFileCreator();
|
||||
|
||||
/* overloaded from FileProvider */
|
||||
//virtual bool getFileData(uint64_t offset, uint32_t chunk_size, void *data);
|
||||
int initializeFileAttrs(); //not override?
|
||||
/*
|
||||
* overloaded from FileProvider FIXME
|
||||
* 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 */
|
||||
bool getMissingChunk(uint64_t &offset, uint32_t &chunk);
|
||||
bool addFileData(uint64_t offset, uint32_t chunk_size, void *data);
|
||||
int initializeFileAttrs();
|
||||
/*
|
||||
* creation functions for FileCreator
|
||||
*/
|
||||
bool getMissingChunk(uint64_t &offset, uint32_t &chunk);
|
||||
bool addFileData(uint64_t offset, uint32_t chunk_size, void *data);
|
||||
|
||||
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;
|
||||
std::string hash;
|
||||
ftFileChunker *fileChunker;
|
||||
@ -74,14 +83,18 @@ private:
|
||||
*/
|
||||
class ftFileChunker : public RsThread {
|
||||
public:
|
||||
/* Does this require hash?? */
|
||||
/*
|
||||
* FIXME Does filechunker require hash??
|
||||
*/
|
||||
ftFileChunker(uint64_t size);
|
||||
virtual ~ftFileChunker();
|
||||
/* Breaks up the file into evenly sized chunks
|
||||
Initializes all chunks to never_requested
|
||||
*/
|
||||
int splitFile();
|
||||
virtual void run();
|
||||
|
||||
/*
|
||||
* Breaks up the file into evenly sized chunks
|
||||
* Initializes all chunks to never_requested
|
||||
*/
|
||||
int splitFile();
|
||||
virtual void run();
|
||||
virtual bool getMissingChunk(uint64_t &offset, uint32_t &chunk);
|
||||
bool getMissingChunkRandom(uint64_t &offset, uint32_t &chunk);
|
||||
int notifyReceived(uint64_t offset, uint32_t chunk_size);
|
||||
@ -97,7 +110,6 @@ protected:
|
||||
RsMutex chunkerMutex; /********** STACK LOCKED MTX ******/
|
||||
};
|
||||
|
||||
|
||||
class ftFileRandomizeChunker : public ftFileChunker {
|
||||
public:
|
||||
ftFileRandomizeChunker(uint64_t size);
|
||||
@ -106,7 +118,6 @@ public:
|
||||
|
||||
};
|
||||
|
||||
|
||||
class ftChunk {
|
||||
public:
|
||||
enum Status {AVAIL, ALLOCATED, RECEIVED};
|
||||
@ -115,7 +126,7 @@ public:
|
||||
uint64_t max_chunk_size;
|
||||
time_t timestamp;
|
||||
Status chunk_status;
|
||||
|
||||
~ftChunk();
|
||||
};
|
||||
|
||||
#endif // FT_FILE_PROVIDER_HEADER
|
||||
|
@ -1,33 +1,8 @@
|
||||
#include "ftfilecreator.h"
|
||||
|
||||
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;
|
||||
uint32_t csize = 40000; /* 40KB*/
|
||||
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);
|
||||
uint32_t csize;
|
||||
/*Test file creator*/
|
||||
ftFileCreator fcreator("somefile",100000,"hash","default");
|
||||
csize = 40000;
|
||||
@ -45,7 +20,8 @@ main(){
|
||||
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";
|
||||
std::cerr << "Call to addFileData =" << fcreator.addFileData(10,10,alpha );
|
||||
char* numerical = "1234567890";
|
||||
@ -106,7 +82,6 @@ main(){
|
||||
std::cout << "getMissing chunk returned nothing" << std::endl;
|
||||
}
|
||||
|
||||
fc.join();
|
||||
free(allA);
|
||||
free(allB);
|
||||
}
|
||||
|
@ -2,32 +2,35 @@
|
||||
|
||||
ftFileProvider::ftFileProvider(std::string path, uint64_t size, std::string
|
||||
hash) : total_size(size), hash(hash), file_name(path), fd(NULL) {
|
||||
//open a file and read it into a binary array!
|
||||
|
||||
|
||||
}
|
||||
|
||||
ftFileProvider::~ftFileProvider(){
|
||||
std::cout << "ftFileProvider d'tor" << std::endl;
|
||||
if (fd!=NULL) {
|
||||
std::cout <<"fd is not null"<<std::endl;
|
||||
fclose(fd);
|
||||
}
|
||||
else {
|
||||
std::cout <<"fd is null"<<std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *data)
|
||||
{
|
||||
RsStackMutex stack(ftPMutex); /********** STACK LOCKED MTX ******/
|
||||
if (fd==NULL) {
|
||||
if (fd==NULL)
|
||||
{
|
||||
int init = initializeFileAttrs();
|
||||
if (init ==0) {
|
||||
if (init ==0)
|
||||
{
|
||||
std::cerr <<"Initialization Failed" << std::endl;
|
||||
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;
|
||||
long base_loc = offset;
|
||||
|
||||
@ -38,13 +41,18 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *dat
|
||||
}
|
||||
|
||||
if (data_size > 0)
|
||||
{
|
||||
|
||||
/* seek for base_loc */
|
||||
{
|
||||
/*
|
||||
* seek for base_loc
|
||||
*/
|
||||
fseek(fd, base_loc, SEEK_SET);
|
||||
|
||||
void *data = malloc(chunk_size);
|
||||
/* read the data */
|
||||
|
||||
/*
|
||||
* read the data
|
||||
*/
|
||||
|
||||
if (1 != fread(data, data_size, 1, fd))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/* 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.
|
||||
* (a) Id,
|
||||
* (b) Offset,
|
||||
@ -61,8 +69,7 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *dat
|
||||
* (d) timestamp
|
||||
*/
|
||||
|
||||
time_t now = time(NULL);
|
||||
//s->id = id;
|
||||
time_t now = time(NULL);
|
||||
req_loc = offset;
|
||||
req_size = data_size;
|
||||
lastTS = now;
|
||||
@ -74,44 +81,42 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t chunk_size, void *dat
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int ftFileProvider::initializeFileAttrs()
|
||||
{
|
||||
/* check if the file exists */
|
||||
/*
|
||||
* check if the file exists
|
||||
*/
|
||||
|
||||
{
|
||||
std::cout <<
|
||||
"ftFileProvider::initializeFileAttrs() Filename: " << file_name;
|
||||
}
|
||||
|
||||
/* attempt to open file */
|
||||
/*
|
||||
* attempt to open file
|
||||
*/
|
||||
|
||||
fd = fopen(file_name.c_str(), "r+b");
|
||||
if (!fd)
|
||||
{
|
||||
std::cout <<
|
||||
"ftFileProvider::initializeFileAttrs() Failed to open (r+b): "
|
||||
<< file_name << std::endl;
|
||||
"ftFileProvider::initializeFileAttrs() Failed to open (r+b): "<< file_name << std::endl;
|
||||
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))
|
||||
{
|
||||
std::cerr << "ftFileProvider::initializeFileAttrs() Seek Failed" << std::endl;
|
||||
//s->status = (PQIFILE_FAIL | PQIFILE_FAIL_NOT_SEEK);*/
|
||||
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);
|
||||
std::cout <<"exit init\n";
|
||||
return 1;
|
||||
}
|
||||
|
@ -36,37 +36,31 @@
|
||||
class ftFileProvider
|
||||
{
|
||||
public:
|
||||
|
||||
ftFileProvider(std::string path, uint64_t size, std::string hash);
|
||||
virtual ~ftFileProvider();
|
||||
|
||||
/* array already allocated -
|
||||
* just move chunk_size bytes to void *data buffer.
|
||||
*/
|
||||
virtual bool getFileData(uint64_t offset, uint32_t chunk_size, void *data);
|
||||
|
||||
/* File Info */
|
||||
std::string getHash();
|
||||
uint64_t getFileSize();
|
||||
|
||||
|
||||
virtual bool getFileData(uint64_t offset, uint32_t chunk_size, void *data);
|
||||
std::string getHash();
|
||||
uint64_t getFileSize();
|
||||
protected:
|
||||
virtual int initializeFileAttrs();
|
||||
std::string file_name;
|
||||
std::string hash;
|
||||
uint64_t total_size;
|
||||
|
||||
std::string hash;
|
||||
std::string file_name;
|
||||
FILE *fd;
|
||||
|
||||
/* Statistics */
|
||||
/*
|
||||
* Structure to gather statistics FIXME: lastRequestor - figure out a
|
||||
* way to get last requestor (peerID)
|
||||
*/
|
||||
std::string lastRequestor;
|
||||
uint64_t req_loc;
|
||||
uint32_t req_size;
|
||||
time_t lastTS;
|
||||
/* Mutex Required for stuff below */
|
||||
|
||||
/*
|
||||
* Mutex Required for stuff below
|
||||
*/
|
||||
RsMutex ftPMutex;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "ftfileprovider.h"
|
||||
|
||||
main(){
|
||||
int main(){
|
||||
ftFileProvider fp("dummy.txt",1,"ABCDEF");
|
||||
char data[2];
|
||||
long offset = 0;
|
||||
@ -29,4 +29,5 @@ main(){
|
||||
}
|
||||
offset+=2;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user