diff --git a/libretroshare/src/ft/ftchunkmap.cc b/libretroshare/src/ft/ftchunkmap.cc index 1580bf623..459a9575a 100644 --- a/libretroshare/src/ft/ftchunkmap.cc +++ b/libretroshare/src/ft/ftchunkmap.cc @@ -41,7 +41,7 @@ static const uint32_t INACTIVE_CHUNK_TIME_LAPSE = 60 ; //! TTL for an inactive std::ostream& operator<<(std::ostream& o,const ftChunk& c) { - return o << "\tChunk [" << c.offset << "] size: " << c.size << " ChunkId: " << c.id << " Age: " << time(NULL) - c.ts ; + return o << "\tChunk [" << c.offset << "] size: " << c.size << " ChunkId: " << c.id << " Age: " << time(NULL) - c.ts << ", owner: " << c.peer_id ; } // Chunk: very bold implementation for now. We should compress the bits to have @@ -221,6 +221,7 @@ bool ChunkMap::getDataChunk(const std::string& peer_id,uint32_t size_hint,ftChun if(_active_chunks_feed[peer_id].empty()) _active_chunks_feed.erase(_active_chunks_feed.find(peer_id)) ; + chunk.peer_id = peer_id ; #ifdef DEBUG_FTCHUNK std::cout << "*** ChunkMap::getDataChunk: returning slice " << chunk << " for peer " << peer_id << std::endl ; #endif diff --git a/libretroshare/src/ft/ftchunkmap.h b/libretroshare/src/ft/ftchunkmap.h index 7b79259fd..da8137fb3 100644 --- a/libretroshare/src/ft/ftchunkmap.h +++ b/libretroshare/src/ft/ftchunkmap.h @@ -42,6 +42,7 @@ class ftChunk uint64_t size; ChunkId id ; time_t ts; + std::string peer_id ; }; // This class handles a single fixed-sized chunk. Although each chunk is requested at once, diff --git a/libretroshare/src/ft/ftfilecreator.cc b/libretroshare/src/ft/ftfilecreator.cc index fe14ee871..6c43f3730 100644 --- a/libretroshare/src/ft/ftfilecreator.cc +++ b/libretroshare/src/ft/ftfilecreator.cc @@ -8,8 +8,8 @@ * #define FILE_DEBUG 1 ******/ -#define CHUNK_MAX_AGE 20 - +#define CHUNK_MAX_AGE 40 +#define MAX_FTCHUNKS_PER_PEER 5 /*********************************************************** * @@ -202,6 +202,7 @@ void ftFileCreator::removeInactiveChunks() { std::map::iterator tmp(it) ; ++it ; + --mChunksPerPeer[tmp->second.peer_id].cnt ; mChunks.erase(tmp) ; } else @@ -320,8 +321,11 @@ int ftFileCreator::locked_notifyReceived(uint64_t offset, uint32_t chunk_size) chunk.offset += chunk_size; mChunks[chunk.offset] = chunk; } - else // notify the chunkmap that the slice is finished + else // notify the chunkmap that the slice is finished, and decrement the number of chunks for this peer. + { chunkMap.dataReceived(chunk.id) ; + --mChunksPerPeer[chunk.peer_id].cnt ; + } _last_recv_time_t = time(NULL) ; @@ -371,26 +375,36 @@ bool ftFileCreator::getMissingChunk(const std::string& peer_id,uint32_t size_hin /* check for freed chunks */ time_t ts = time(NULL); - time_t old = ts-CHUNK_MAX_AGE; +// time_t old = ts-CHUNK_MAX_AGE; +// +// std::map::iterator it; +// for(it = mChunks.begin(); it != mChunks.end(); it++) +// { +// /* very simple algorithm */ +// if (it->second.ts < old) +// { +//#ifdef FILE_DEBUG +// std::cerr << "ffc::getMissingChunk() Re-asking for an old chunk"; +// std::cerr << std::endl; +//#endif +// +// /* retry this one */ +// it->second.ts = ts; +// size = it->second.size; +// offset = it->second.offset; +// +// return true; +// } +// } - std::map::iterator it; - for(it = mChunks.begin(); it != mChunks.end(); it++) + uint32_t& chunks_for_this_peer(mChunksPerPeer[peer_id].cnt) ; + + if(chunks_for_this_peer >= MAX_FTCHUNKS_PER_PEER) { - /* very simple algorithm */ - if (it->second.ts < old) - { #ifdef FILE_DEBUG - std::cerr << "ffc::getMissingChunk() Re-asking for an old chunk"; - std::cerr << std::endl; + std::cerr << "ffc::getMissingChunk() too many chunks for peer " << peer_id << std::endl ; #endif - - /* retry this one */ - it->second.ts = ts; - size = it->second.size; - offset = it->second.offset; - - return true; - } + return false ; } /* else allocate a new chunk */ @@ -409,6 +423,8 @@ bool ftFileCreator::getMissingChunk(const std::string& peer_id,uint32_t size_hin offset = chunk.offset ; size = chunk.size ; + ++chunks_for_this_peer ; // increase number of chunks for this peer. + return true; /* cos more data to get */ } @@ -436,6 +452,10 @@ bool ftFileCreator::locked_printChunkMap() for(it = mChunks.begin(); it != mChunks.end(); it++) std::cerr << " " << it->second << std::endl ; + std::cerr << "Active chunks per peer:" << std::endl ; + for(std::map::const_iterator it(mChunksPerPeer.begin());it!=mChunksPerPeer.end();++it) + std::cerr << " " << it->first << "\t: " << it->second.cnt << std::endl; + return true; } diff --git a/libretroshare/src/ft/ftfilecreator.h b/libretroshare/src/ft/ftfilecreator.h index 7915d95ae..f2d6cde18 100644 --- a/libretroshare/src/ft/ftfilecreator.h +++ b/libretroshare/src/ft/ftfilecreator.h @@ -36,6 +36,13 @@ #include "ftchunkmap.h" #include +class ZeroInitCounter +{ + public: + ZeroInitCounter(): cnt(0) {} + uint32_t cnt ; +}; + class ftFileCreator: public ftFileProvider { public: @@ -131,6 +138,7 @@ class ftFileCreator: public ftFileProvider uint64_t mEnd; std::map mChunks; + std::map mChunksPerPeer ; ChunkMap chunkMap ;