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_KBUCKETTABLE_H
0008 #define DHT_KBUCKETTABLE_H
0009 
0010 #include <dht/kbucket.h>
0011 #include <list>
0012 
0013 namespace dht
0014 {
0015 /**
0016  * Holds a table of buckets.
0017  */
0018 class KBucketTable
0019 {
0020 public:
0021     KBucketTable(const Key &our_id);
0022     virtual ~KBucketTable();
0023 
0024     /// Insert a KBucketEntry into the table
0025     void insert(const KBucketEntry &entry, RPCServerInterface *srv);
0026 
0027     /// Get the number of entries
0028     int numEntries() const;
0029 
0030     /// Refresh the buckets
0031     void refreshBuckets(DHT *dh_table);
0032 
0033     /// Timeout happened
0034     void onTimeout(const net::Address &addr);
0035 
0036     /// Load the table from a file
0037     void loadTable(const QString &file, dht::RPCServerInterface *srv);
0038 
0039     /// Save table to a file
0040     void saveTable(const QString &file);
0041 
0042     /// Find the K closest nodes
0043     void findKClosestNodes(KClosestNodesSearch &kns) const;
0044 
0045 private:
0046     typedef std::list<KBucket::Ptr> KBucketList;
0047     inline KBucketList::iterator findBucket(const dht::Key &id);
0048 
0049 private:
0050     Key our_id;
0051     KBucketList buckets;
0052 };
0053 
0054 }
0055 
0056 #endif // DHT_KBUCKETTABLE_H