libktorrent  2.1.1
dht.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 DHTDHT_H
21 #define DHTDHT_H
22 
23 #include <qtimer.h>
24 #include <qstring.h>
25 #include <qmap.h>
26 #include <util/constants.h>
27 #include <util/timer.h>
28 #include "key.h"
29 #include "dhtbase.h"
30 #include "rpcmsg.h"
31 
32 
33 namespace net
34 {
35  class AddressResolver;
36 }
37 
38 namespace bt
39 {
40  class SHA1Hash;
41 }
42 
43 namespace dht
44 {
45  class Node;
46  class RPCServer;
47  class Database;
48  class TaskManager;
49  class Task;
50  class AnnounceTask;
51  class NodeLookup;
52  class KBucket;
53  class ErrMsg;
54  class PingReq;
55  class FindNodeReq;
56  class GetPeersReq;
57  class AnnounceReq;
58 
62  class DHT : public DHTBase
63  {
64  Q_OBJECT
65  public:
66  DHT();
67  ~DHT() override;
68 
69  void ping(const PingReq & r);
70  void findNode(const FindNodeReq & r);
71  void response(const RPCMsg & r);
72  void getPeers(const GetPeersReq & r);
73  void announce(const AnnounceReq & r);
74  void error(const ErrMsg & r);
75  void timeout(RPCMsg::Ptr r);
76 
82  void portReceived(const QString & ip,bt::Uint16 port) override;
83 
90  AnnounceTask* announce(const bt::SHA1Hash & info_hash,bt::Uint16 port) override;
91 
97  NodeLookup* refreshBucket(const dht::Key & id,KBucket & bucket);
98 
104 
107 
109  bool canStartTask() const;
110 
111  void start(const QString & table,const QString & key_file,bt::Uint16 port) override;
112  void stop() override;
113  void addDHTNode(const QString & host,bt::Uint16 hport) override;
114 
115  QMap<QString, int> getClosestGoodNodes(int maxNodes) override;
116 
117  private Q_SLOTS:
118  void update() override;
119  void onResolverResults(net::AddressResolver* ar);
120  void ownNodeLookupFinished(Task* t);
121  void expireDatabaseItems();
122 
123  private:
124  Node* node;
125  RPCServer* srv;
126  Database* db;
127  TaskManager* tman;
128  QTimer expire_timer;
129  QString table_file;
130  QTimer update_timer;
131  NodeLookup* our_node_lookup;
132  };
133 
134 }
135 
136 #endif
dht::ErrMsg
Definition: errmsg.h:51
net::AddressResolver
Definition: addressresolver.h:55
dht::DHT::portReceived
void portReceived(const QString &ip, bt::Uint16 port) override
dht::FindNodeReq
Definition: findnodereq.h:52
dht::DHT::getClosestGoodNodes
QMap< QString, int > getClosestGoodNodes(int maxNodes) override
dht::Key
Key in the distributed hash table.
Definition: key.h:57
dht::Database
Definition: database.h:97
dht::DHT::refreshBucket
NodeLookup * refreshBucket(const dht::Key &id, KBucket &bucket)
dht::RPCMsg
Definition: rpcmsg.h:67
dht::DHT::stop
void stop() override
dht::Task
Definition: task.h:47
dht::NodeLookup
Definition: nodelookup.h:55
dht::Node
Definition: node.h:51
dht::TaskManager
Definition: taskmanager.h:56
dht::DHT::findOwnNode
NodeLookup * findOwnNode()
Do a findNode for our node id.
dht::RPCServer
Definition: rpcserver.h:46
dht::KBucket
Definition: kbucket.h:56
dht::DHT::findNode
NodeLookup * findNode(const dht::Key &id)
dht::DHT::start
void start(const QString &table, const QString &key_file, bt::Uint16 port) override
bt::SHA1Hash
Stores a SHA1 hash.
Definition: sha1hash.h:41
dht::AnnounceReq
Definition: announcereq.h:52
dht::DHTBase
Definition: dhtbase.h:53
dht::PingReq
Definition: pingreq.h:51
dht::DHT::announce
AnnounceTask * announce(const bt::SHA1Hash &info_hash, bt::Uint16 port) override
dht::DHT::canStartTask
bool canStartTask() const
See if it is possible to start a task.
dht::DHT::addDHTNode
void addDHTNode(const QString &host, bt::Uint16 hport) override
dht::AnnounceTask
Definition: announcetask.h:68
dht::GetPeersReq
Definition: getpeersreq.h:52
dht::DHT
Definition: dht.h:63