File indexing completed on 2024-06-02 05:05:14

0001 /*
0002     SPDX-FileCopyrightText: 2012 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kbucketentry.h"
0008 #include <util/functions.h>
0009 #include <util/log.h>
0010 
0011 namespace dht
0012 {
0013 KBucketEntry::KBucketEntry()
0014 {
0015     last_responded = bt::CurrentTime();
0016     failed_queries = 0;
0017     questionable_pings = 0;
0018 }
0019 
0020 KBucketEntry::KBucketEntry(const net::Address &addr, const Key &id)
0021     : addr(addr)
0022     , node_id(id)
0023 {
0024     last_responded = bt::CurrentTime();
0025     failed_queries = 0;
0026     questionable_pings = 0;
0027 }
0028 
0029 KBucketEntry::KBucketEntry(const KBucketEntry &other)
0030     : addr(other.addr)
0031     , node_id(other.node_id)
0032     , last_responded(other.last_responded)
0033     , failed_queries(other.failed_queries)
0034     , questionable_pings(other.questionable_pings)
0035 {
0036 }
0037 
0038 KBucketEntry::~KBucketEntry()
0039 {
0040 }
0041 
0042 KBucketEntry &KBucketEntry::operator=(const KBucketEntry &other)
0043 {
0044     addr = other.addr;
0045     node_id = other.node_id;
0046     last_responded = other.last_responded;
0047     failed_queries = other.failed_queries;
0048     questionable_pings = other.questionable_pings;
0049     return *this;
0050 }
0051 
0052 bool KBucketEntry::operator==(const KBucketEntry &entry) const
0053 {
0054     return addr == entry.addr && node_id == entry.node_id;
0055 }
0056 
0057 bool KBucketEntry::isGood() const
0058 {
0059     if (bt::CurrentTime() - last_responded > 15 * 60 * 1000)
0060         return false;
0061     else
0062         return true;
0063 }
0064 
0065 bool KBucketEntry::isQuestionable() const
0066 {
0067     if (bt::CurrentTime() - last_responded > 15 * 60 * 1000)
0068         return true;
0069     else
0070         return false;
0071 }
0072 
0073 bool KBucketEntry::isBad() const
0074 {
0075     if (isGood())
0076         return false;
0077 
0078     return failed_queries > 2 || questionable_pings > 2;
0079 }
0080 
0081 void KBucketEntry::hasResponded()
0082 {
0083     last_responded = bt::CurrentTime();
0084     failed_queries = 0; // reset failed queries
0085     questionable_pings = 0;
0086 }
0087 
0088 bool KBucketEntry::operator<(const dht::KBucketEntry &entry) const
0089 {
0090     return node_id < entry.node_id;
0091 }
0092 
0093 }