File indexing completed on 2025-01-05 04:37:11

0001 /*
0002     SPDX-FileCopyrightText: 2012 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef DHT_KBUCKETENTRY_H
0008 #define DHT_KBUCKETENTRY_H
0009 
0010 #include <dht/key.h>
0011 #include <net/address.h>
0012 #include <set>
0013 
0014 namespace dht
0015 {
0016 /**
0017  * @author Joris Guisson
0018  *
0019  * Entry in a KBucket, it basically contains an ip_address of a node,
0020  * the udp port of the node and a node_id.
0021  */
0022 class KBucketEntry
0023 {
0024 public:
0025     /**
0026      * Constructor, sets everything to 0.
0027      * @return
0028      */
0029     KBucketEntry();
0030 
0031     /**
0032      * Constructor, set the ip, port and key
0033      * @param addr socket address
0034      * @param id ID of node
0035      */
0036     KBucketEntry(const net::Address &addr, const Key &id);
0037 
0038     /**
0039      * Copy constructor.
0040      * @param other KBucketEntry to copy
0041      * @return
0042      */
0043     KBucketEntry(const KBucketEntry &other);
0044 
0045     /// Destructor
0046     virtual ~KBucketEntry();
0047 
0048     /**
0049      * Assignment operator.
0050      * @param other Node to copy
0051      * @return this KBucketEntry
0052      */
0053     KBucketEntry &operator=(const KBucketEntry &other);
0054 
0055     /// Equality operator
0056     bool operator==(const KBucketEntry &entry) const;
0057 
0058     /// Get the socket address of the node
0059     const net::Address &getAddress() const
0060     {
0061         return addr;
0062     }
0063 
0064     /// Get it's ID
0065     const Key &getID() const
0066     {
0067         return node_id;
0068     }
0069 
0070     /// Is this node a good node
0071     bool isGood() const;
0072 
0073     /// Is this node questionable (haven't heard from it in the last 15 minutes)
0074     bool isQuestionable() const;
0075 
0076     /// Is it a bad node. (Hasn't responded to a query
0077     bool isBad() const;
0078 
0079     /// Signal the entry that the peer has responded
0080     void hasResponded();
0081 
0082     /// A request timed out
0083     void requestTimeout()
0084     {
0085         failed_queries++;
0086     }
0087 
0088     /// The entry has been pinged because it is questionable
0089     void onPingQuestionable()
0090     {
0091         questionable_pings++;
0092     }
0093 
0094     /// The null entry
0095     static KBucketEntry null;
0096 
0097     /// < operator
0098     bool operator<(const KBucketEntry &entry) const;
0099 
0100 private:
0101     net::Address addr;
0102     Key node_id;
0103     bt::TimeStamp last_responded;
0104     bt::Uint32 failed_queries;
0105     bt::Uint32 questionable_pings;
0106 };
0107 
0108 class KBucketEntrySet : public std::set<KBucketEntry>
0109 {
0110 public:
0111     KBucketEntrySet()
0112     {
0113     }
0114     virtual ~KBucketEntrySet()
0115     {
0116     }
0117 
0118     bool contains(const KBucketEntry &entry) const
0119     {
0120         return find(entry) != end();
0121     }
0122 };
0123 
0124 }
0125 
0126 #endif // DHT_KBUCKETENTRY_H