diff --git a/libretroshare/src/ft/ftfileprovider.cc b/libretroshare/src/ft/ftfileprovider.cc index fda50f94b..217a99ec1 100644 --- a/libretroshare/src/ft/ftfileprovider.cc +++ b/libretroshare/src/ft/ftfileprovider.cc @@ -7,10 +7,13 @@ ftFileProvider::ftFileProvider(std::string path, uint64_t size, std::string hash) : mSize(size), hash(hash), file_name(path), fd(NULL),transfer_rate(0),total_size(0) { + std::cout << "Creating file provider for " << hash << std::endl ; lastTS = time(NULL) ; + lastTS_t = times(NULL) ; } ftFileProvider::~ftFileProvider(){ + std::cout << "Destroying file provider for " << hash << std::endl ; if (fd!=NULL) { fclose(fd); } @@ -57,20 +60,6 @@ bool ftFileProvider::FileDetails(FileInfo &info) inf.peerId = lastRequestor ; inf.status = FT_STATE_DOWNLOADING ; - long int clk = sysconf(_SC_CLK_TCK) ; - time_t now = time(NULL) ; - - clock_t now_t = times(NULL) ; - - long int diff = (long int)now_t - (long int)lastTS_t ; // in bytes/s. Average over multiple samples - - if(diff > 100) - { - transfer_rate = total_size / (float)diff * clk ; - total_size = 0 ; - lastTS_t = now_t ; - } - inf.tfRate = transfer_rate/1024.0 ; info.tfRate = transfer_rate/1024.0 ; info.peers.push_back(inf) ; @@ -92,10 +81,6 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t &chunk_size, void *da RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/ - /* - * Use private data, which has a pointer to file, total size etc - */ - /* * FIXME: Warning of comparison between unsigned and signed int? */ @@ -140,9 +125,25 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t &chunk_size, void *da * (d) timestamp */ - time_t now = time(NULL); + long int clk = sysconf(_SC_CLK_TCK) ; + clock_t now_t = times(NULL) ; + + long int diff = (long int)now_t - (long int)lastTS_t ; // in bytes/s. Average over multiple samples + + std::cout << "diff = " << diff << std::endl ; + + if(diff > 200) + { + transfer_rate = total_size / (float)diff * clk ; +#ifdef DEBUG_FT_FILE_PROVIDER + std::cout << "updated TR = " << transfer_rate << ", total_size=" << total_size << std::endl ; +#endif + lastTS_t = now_t ; + total_size = 0 ; + } + req_loc = offset; - lastTS = now ; + lastTS = time(NULL) ; req_size = data_size; total_size += req_size ; } diff --git a/libretroshare/src/ft/ftserver.cc b/libretroshare/src/ft/ftserver.cc index e255031a5..da6318b5f 100644 --- a/libretroshare/src/ft/ftserver.cc +++ b/libretroshare/src/ft/ftserver.cc @@ -193,8 +193,11 @@ void ftServer::run() { if (FileDetails(sit->first,RS_FILE_HINTS_UPLOAD,info)) { - if ((now - info.lastTS) > 5) + if ((now - info.lastTS) > 10) { +#ifdef SERVER_DEBUG + std::cout << "info.lastTS = " << info.lastTS << ", now=" << now << std::endl ; +#endif toDels.push_back(sit->first); } } @@ -204,8 +207,12 @@ void ftServer::run() for (it = toDels.begin();it != toDels.end(); it++) { sit = mFtDataplex->mServers.find(*it); - delete sit->second; - mFtDataplex->mServers.erase(sit); + + if(mFtDataplex->mServers.end() != sit) + { + delete sit->second; + mFtDataplex->mServers.erase(sit); + } } #ifdef WIN32 @@ -229,6 +236,7 @@ void ftServer::run() bool ftServer::FileRequest(std::string fname, std::string hash, uint64_t size, std::string dest, uint32_t flags, std::list srcIds) { + std::cerr << "Requesting " << fname << std::endl ; return mFtController->FileRequest(fname, hash, size, dest, flags, srcIds); }