diff --git a/libretroshare/src/ft/ftdatamultiplex.cc b/libretroshare/src/ft/ftdatamultiplex.cc index 8101b3154..2ad23a4b9 100644 --- a/libretroshare/src/ft/ftdatamultiplex.cc +++ b/libretroshare/src/ft/ftdatamultiplex.cc @@ -427,6 +427,8 @@ bool ftDataMultiplex::locked_handleServerRequest(ftFileProvider *provider, if (provider->getFileData(offset, chunksize, data)) { + // setup info + provider->setPeerId(peerId) ; /* send data out */ sendData(peerId, hash, size, offset, chunksize, data); return true; diff --git a/libretroshare/src/ft/ftfileprovider.cc b/libretroshare/src/ft/ftfileprovider.cc index 28f000ad1..00cf7cbad 100644 --- a/libretroshare/src/ft/ftfileprovider.cc +++ b/libretroshare/src/ft/ftfileprovider.cc @@ -1,11 +1,13 @@ +#include #include "ftfileprovider.h" #include "util/rsdir.h" #include ftFileProvider::ftFileProvider(std::string path, uint64_t size, std::string -hash) : mSize(size), hash(hash), file_name(path), fd(NULL) +hash) : mSize(size), hash(hash), file_name(path), fd(NULL),transfer_rate(0) { + lastTS = times(NULL) ; } ftFileProvider::~ftFileProvider(){ @@ -14,6 +16,12 @@ ftFileProvider::~ftFileProvider(){ } } +void ftFileProvider::setPeerId(const std::string& id) +{ + RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/ + lastRequestor = id ; +} + bool ftFileProvider::fileOk() { RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/ @@ -34,12 +42,22 @@ uint64_t ftFileProvider::getFileSize() bool ftFileProvider::FileDetails(FileInfo &info) { - RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/ + RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/ info.hash = hash; info.size = mSize; info.path = file_name; info.fname = RsDirUtil::getTopDir(file_name); - info.lastTS = lastTS; + info.transfered = req_loc ; + info.status = FT_STATE_DOWNLOADING ; + + info.peers.clear() ; + + TransferInfo inf ; + inf.peerId = lastRequestor ; + inf.tfRate = transfer_rate/1024.0 ; + inf.status = FT_STATE_DOWNLOADING ; + info.peers.push_back(inf) ; + /* Use req_loc / req_size to estimate data rate */ return true; @@ -55,7 +73,7 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t &chunk_size, void *da if (!initializeFileAttrs()) return false; - RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/ + RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/ /* * Use private data, which has a pointer to file, total size etc @@ -105,9 +123,14 @@ bool ftFileProvider::getFileData(uint64_t offset, uint32_t &chunk_size, void *da * (d) timestamp */ - time_t now = time(NULL); + clock_t now = times(NULL); req_loc = offset; req_size = data_size; + + transfer_rate = req_size / (float)(std::max(sysconf(_SC_CLK_TCK),(long int)now - (long int)lastTS) / (float)sysconf(_SC_CLK_TCK)) ; // in bytes/s. Average over two samples + + std::cout << "req_size = " << req_size << ", now="<< now << ", lastTS=" << lastTS << ", lastTS-now=" << now-lastTS << ", speed = " << transfer_rate << ", clk=" << sysconf(_SC_CLK_TCK) << std::endl ; + lastTS = now; } else { diff --git a/libretroshare/src/ft/ftfileprovider.h b/libretroshare/src/ft/ftfileprovider.h index 1001c6e97..807bbc525 100644 --- a/libretroshare/src/ft/ftfileprovider.h +++ b/libretroshare/src/ft/ftfileprovider.h @@ -47,6 +47,8 @@ public: uint64_t getFileSize(); bool fileOk(); + void setPeerId(const std::string& id) ; + protected: virtual int initializeFileAttrs(); /* does for both */ @@ -63,7 +65,8 @@ virtual int initializeFileAttrs(); /* does for both */ std::string lastRequestor; uint64_t req_loc; uint32_t req_size; - time_t lastTS; + clock_t lastTS; + float transfer_rate ; /* * Mutex Required for stuff below