diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index f37f80b55..e5f17d518 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -286,7 +286,7 @@ bool p3PeerMgrIMPL::setOwnVisState(uint16_t vs_disc, uint16_t vs_dht) void p3PeerMgrIMPL::tick() { - static const time_t INTERVAL_BETWEEN_LOCATION_CLEANING = 300 ; // Remove unused locations and clean IPs every 10 minutes. + static const time_t INTERVAL_BETWEEN_LOCATION_CLEANING = 100 ; // Remove unused locations and clean IPs every 10 minutes. static time_t last_friends_check = time(NULL) ; // first cleaning after 1 hour. @@ -2841,121 +2841,48 @@ bool p3PeerMgrIMPL::removeBannedIps() bool p3PeerMgrIMPL::removeUnusedLocations() { std::list toRemove; - - std::map hasRecentLocation; std::map mostRecentTime; - std::map mostRecentLocation; - - // init maps - { - std::list pgpList; - - if(!rsPeers->getGPGAcceptedList(pgpList)) - return false; - - std::list::iterator it; - for(it = pgpList.begin(); it != pgpList.end(); ++it) - { - hasRecentLocation[*it] = false; - mostRecentTime[*it] = (time_t)0; - } - } const time_t now = time(NULL); - RsPgpId pgpID; - + + std::list pgpList; + + if(!rsPeers->getGPGAcceptedList(pgpList)) + return false; { RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + // First put a sensible number in all PGP ids + + for(std::list::const_iterator it = pgpList.begin(); it != pgpList.end(); ++it) + mostRecentTime[*it] = (time_t)0; + #ifdef PEER_DEBUG std::cerr << "p3PeerMgr::removeUnusedLocations()" << std::endl; #endif + // Then compute the most recently used location for all PGP ids - std::map::iterator it; - for(it = mFriendList.begin(); it != mFriendList.end(); ++it) - { - pgpID = it->second.gpg_id; + for( std::map::iterator it = mFriendList.begin(); it != mFriendList.end(); ++it) + { + time_t& bst(mostRecentTime[it->second.gpg_id]) ; + bst = std::max(bst,it->second.lastcontact) ; + } - // store some references to speed up accessing - RsPeerId &idRef = mostRecentLocation[pgpID]; - bool &recentRef = hasRecentLocation[pgpID]; + // And remove all locations that are too old and also older than the most recent location. Doing this we're sure to always keep at least one location per PGP id. - if (now > it->second.lastcontact + RS_PEER_OFFLINE_DELETE) - { - // location is too old - if(recentRef) - { - // there is already one location that won't get removed - // -> we can safely remove this one - toRemove.push_back(it->first); + for( std::map::iterator it = mFriendList.begin(); it != mFriendList.end(); ++it) + { + if (now > it->second.lastcontact + RS_PEER_OFFLINE_DELETE && it->second.lastcontact < mostRecentTime[it->second.gpg_id]) + toRemove.push_back(it->first); #ifdef PEER_DEBUG - std::cerr << "p3PeerMgr::removeUnusedLocations() removing Old SSL Id: " << it->first << std::endl; + std::cerr << "Location " << it->first << " PGP id " << it->second.gpg_id << " last contact " << it->second.lastcontact << " remove: " << (now > it->second.lastcontact + RS_PEER_OFFLINE_DELETE) << " most recent: " << mostRecentTime[it->second.gpg_id] + << ". Final result remove: " << (it->second.lastcontact < mostRecentTime[it->second.gpg_id] && now > it->second.lastcontact + RS_PEER_OFFLINE_DELETE )<< std::endl; #endif - } - else - { - // we need to take care that the most recent location it not removed - time_t &timeRef = mostRecentTime[pgpID]; - - if(timeRef > it->second.lastcontact) - { - // this (it) location is longer offline compared to mostRecentLocation - // -> we can remove this one - toRemove.push_back(it->first); -#ifdef PEER_DEBUG - std::cerr << "p3PeerMgr::removeUnusedLocations() removing Old SSL Id: " << it->first << std::endl; -#endif - } - else - { - // this (it) location is more recent compared to mostRecentLocation - // -> we can remove mostRecentLocation - if(!idRef.isNull()) - { - toRemove.push_back(idRef); -#ifdef PEER_DEBUG - std::cerr << "p3PeerMgr::removeUnusedLocations() removing Old SSL Id: " << it->first << std::endl; -#endif - } - // update maps - idRef = it->first; - timeRef = it->second.lastcontact; - } - } - } - else - { - // found a location that won't get removed - recentRef = true; - - // we can remove mostRecentLocation if it is set - if(!idRef.isNull()) - { - toRemove.push_back(idRef); -#ifdef PEER_DEBUG - std::cerr << "p3PeerMgr::removeUnusedLocations() removing Old SSL Id: " << it->first << std::endl; -#endif - } - } - -// if (isDummyFriend(it->first)) -// { -// toRemove.push_back(it->first); -// -//#ifdef PEER_DEBUG -// std::cerr << "p3PeerMgr::removeUnusedLocations() removing Dummy Id: " << it->first << std::endl; -//#endif -// -// } - - } + } } - std::list::iterator it; - for(it = toRemove.begin(); it != toRemove.end(); ++it) - { - removeFriend(*it, false); - } + for( std::list::iterator it = toRemove.begin(); it != toRemove.end(); ++it) + removeFriend(*it, false); return true; }