libktorrent  2.2.0
kbucket.h
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #ifndef DHTKBUCKET_H
21 #define DHTKBUCKET_H
22 
23 #include <set>
24 #include <QList>
25 #include <util/constants.h>
26 #include <net/address.h>
27 #include "key.h"
28 #include "rpccall.h"
29 #include "kbucketentry.h"
30 
31 namespace bt
32 {
33  class BEncoder;
34 }
35 
36 namespace dht
37 {
38  class RPCServerInterface;
39  class KClosestNodesSearch;
40  class Task;
41 
42  const bt::Uint32 K = 20;
43  const bt::Uint32 BUCKET_MAGIC_NUMBER = 0xB0C4B0C4;
44  const bt::Uint32 BUCKET_REFRESH_INTERVAL = 15 * 60 * 1000;
45 
46 
55  class KBucket : public RPCCallListener
56  {
57  Q_OBJECT
58 
59  public:
60  KBucket(RPCServerInterface* srv, const Key & our_id);
61  KBucket(const dht::Key & min_key, const dht::Key & max_key, RPCServerInterface* srv, const Key & our_id);
62  ~KBucket() override;
63 
64  typedef QSharedPointer<KBucket> Ptr;
65 
67  const dht::Key & minKey() const {return min_key;}
68 
70  const dht::Key & maxKey() const {return max_key;}
71 
73  bool keyInRange(const dht::Key & k) const;
74 
76  bool splitAllowed() const;
77 
79  {};
80 
86  std::pair<KBucket::Ptr, KBucket::Ptr> split() throw (UnableToSplit);
87 
93  bool insert(const KBucketEntry & entry);
94 
96  const KBucketEntry & leastRecentlySeen() const {return entries[0];}
97 
99  bt::Uint32 getNumEntries() const {return entries.count();}
100 
102  bool contains(const KBucketEntry & entry) const;
103 
110 
115  bool onTimeout(const net::Address & addr);
116 
118  bool needsToBeRefreshed() const;
119 
121  void save(bt::BEncoder & enc);
122 
124  void load(bt::BDictNode* dict);
125 
128 
131 
132  private:
133  void onResponse(RPCCall* c, RPCMsg::Ptr rsp) override;
134  void onTimeout(RPCCall* c) override;
135  void pingQuestionable(const KBucketEntry & replacement_entry);
136  bool replaceBadEntry(const KBucketEntry & entry);
137 
138  private Q_SLOTS:
139  void onFinished(Task* t);
140 
141  private:
142  dht::Key min_key, max_key;
143  QList<KBucketEntry> entries, pending_entries;
144  RPCServerInterface* srv;
145  Key our_id;
146  QMap<RPCCall*, KBucketEntry> pending_entries_busy_pinging;
147  mutable bt::TimeStamp last_modified;
148  Task* refresh_task;
149  };
150 }
151 
152 template <class T>
153 inline uint qHash(const T & e)
154 {
155  return e.hash();
156 }
157 
158 #endif
bt::BEncoder
Helper class to b-encode stuff.
Definition: bencoder.h:92
dht::KBucket::getNumEntries
bt::Uint32 getNumEntries() const
Get the number of entries.
Definition: kbucket.h:99
dht::KBucket::updateRefreshTimer
void updateRefreshTimer()
Update the refresh timer of the bucket.
net::Address
Definition: address.h:41
dht::KBucket::findKClosestNodes
void findKClosestNodes(KClosestNodesSearch &kns)
dht::KBucket::save
void save(bt::BEncoder &enc)
save the bucket to a file
dht::Key
Key in the distributed hash table.
Definition: key.h:39
dht::KBucket::load
void load(bt::BDictNode *dict)
Load the bucket from a file.
dht::KBucket::onTimeout
bool onTimeout(const net::Address &addr)
dht::KClosestNodesSearch
Definition: kclosestnodessearch.h:38
dht::Task
Definition: task.h:47
dht::KBucket::splitAllowed
bool splitAllowed() const
Are we allowed to split.
dht::KBucket::split
std::pair< KBucket::Ptr, KBucket::Ptr > split()
dht::RPCCallListener
Definition: rpccall.h:35
dht::KBucket::keyInRange
bool keyInRange(const dht::Key &k) const
Does the key k lies in in the range of this bucket.
dht::KBucket::maxKey
const dht::Key & maxKey() const
Get the max key.
Definition: kbucket.h:70
dht::KBucket
Definition: kbucket.h:56
dht::KBucket::UnableToSplit
Definition: kbucket.h:79
dht::KBucketEntry
Definition: kbucketentry.h:38
dht::RPCServerInterface
Definition: rpcserverinterface.h:34
dht::KBucket::minKey
const dht::Key & minKey() const
Get the min key.
Definition: kbucket.h:67
dht::KBucket::contains
bool contains(const KBucketEntry &entry) const
See if this bucket contains an entry.
dht::KBucket::setRefreshTask
void setRefreshTask(Task *t)
Set the refresh task.
dht::KBucket::insert
bool insert(const KBucketEntry &entry)
bt::BDictNode
Represents a dictionary in bencoded data.
Definition: bnode.h:101
dht::KBucket::leastRecentlySeen
const KBucketEntry & leastRecentlySeen() const
Get the least recently seen node.
Definition: kbucket.h:96
dht::KBucket::needsToBeRefreshed
bool needsToBeRefreshed() const
Check if the bucket needs to be refreshed.
dht::RPCCall
Definition: rpccall.h:60