/* */ #include "DHTPeerAnnounceEntry.h" #include #include #include "Peer.h" #include "wallclock.h" namespace aria2 { DHTPeerAnnounceEntry::DHTPeerAnnounceEntry(const unsigned char* infoHash) { memcpy(infoHash_, infoHash, DHT_ID_LENGTH); } DHTPeerAnnounceEntry::~DHTPeerAnnounceEntry() {} void DHTPeerAnnounceEntry::addPeerAddrEntry(const PeerAddrEntry& entry) { auto i = std::find(peerAddrEntries_.begin(), peerAddrEntries_.end(), entry); if (i == peerAddrEntries_.end()) { peerAddrEntries_.push_back(entry); } else { (*i).notifyUpdate(); } notifyUpdate(); } size_t DHTPeerAnnounceEntry::countPeerAddrEntry() const { return peerAddrEntries_.size(); } void DHTPeerAnnounceEntry::removeStalePeerAddrEntry( const std::chrono::seconds& timeout) { peerAddrEntries_.erase( std::remove_if(std::begin(peerAddrEntries_), std::end(peerAddrEntries_), [&timeout](const PeerAddrEntry& entry) { return entry.getLastUpdated().difference( global::wallclock()) >= timeout; }), std::end(peerAddrEntries_)); } bool DHTPeerAnnounceEntry::empty() const { return peerAddrEntries_.empty(); } void DHTPeerAnnounceEntry::getPeers( std::vector>& peers) const { for (const auto& p : peerAddrEntries_) { peers.push_back(std::make_shared(p.getIPAddress(), p.getPort())); } } void DHTPeerAnnounceEntry::notifyUpdate() { lastUpdated_ = global::wallclock(); } } // namespace aria2