libktorrent  2.2.0
task.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 DHTTASK_H
21 #define DHTTASK_H
22 
23 #include "rpccall.h"
24 #include "rpcserver.h"
25 #include "kbucket.h"
26 
27 namespace net
28 {
29  class AddressResolver;
30 }
31 
32 namespace dht
33 {
34  class Node;
35  class Task;
36  class KClosestNodesSearch;
37 
38  const bt::Uint32 MAX_CONCURRENT_REQS = 16;
39 
46  class Task : public RPCCallListener
47  {
48  Q_OBJECT
49  public:
56  Task(RPCServer* rpc, Node* node, QObject* parent);
57  ~Task() override;
58 
65  void start(const KClosestNodesSearch & kns, bool queued);
66 
67 
71  void start();
72 
74  void onResponse(RPCCall* c, RPCMsg::Ptr rsp) override;
75 
77  void onTimeout(RPCCall* c) override;
78 
83  virtual void update() = 0;
84 
90  virtual void callFinished(RPCCall* c, RPCMsg::Ptr rsp) = 0;
91 
96  virtual void callTimeout(RPCCall* c) = 0;
97 
103  bool rpcCall(RPCMsg::Ptr req);
104 
106  bool canDoRequest() const {return outstanding_reqs < MAX_CONCURRENT_REQS;}
107 
109  bool isFinished() const {return task_finished;}
110 
112  bt::Uint32 getNumOutstandingRequests() const {return outstanding_reqs;}
113 
114  bool isQueued() const {return queued;}
115 
120 
122  void kill();
123 
129  void addDHTNode(const QString & ip, bt::Uint16 port);
130 
131  Q_SIGNALS:
136  void finished(Task* t);
137 
143  void dataReady(Task* t);
144 
145  protected:
146  void done();
147 
148  protected Q_SLOTS:
149  void onResolverResults(net::AddressResolver* res);
150 
151  protected:
152  dht::KBucketEntrySet visited; // nodes visited
153  dht::KBucketEntrySet todo; // nodes todo
154  Node* node;
155 
156  private:
157  RPCServer* rpc;
158  bt::Uint32 outstanding_reqs;
159  bool task_finished;
160  bool queued;
161  };
162 
163 }
164 
165 #endif
166 
167 
net::AddressResolver
Definition: addressresolver.h:55
dht::Task::update
virtual void update()=0
dht::Task::callFinished
virtual void callFinished(RPCCall *c, RPCMsg::Ptr rsp)=0
dht::Task::start
void start()
dht::Task::start
void start(const KClosestNodesSearch &kns, bool queued)
dht::KClosestNodesSearch
Definition: kclosestnodessearch.h:56
dht::Task::dataReady
void dataReady(Task *t)
dht::Task::getNumOutstandingRequests
bt::Uint32 getNumOutstandingRequests() const
Get the number of outstanding requests.
Definition: task.h:112
dht::Task
Definition: task.h:47
dht::Node
Definition: node.h:51
dht::Task::finished
void finished(Task *t)
dht::RPCServer
Definition: rpcserver.h:46
dht::RPCCallListener
Definition: rpccall.h:53
dht::Task::canDoRequest
bool canDoRequest() const
See if we can do a request.
Definition: task.h:106
dht::Task::kill
void kill()
Kills the task.
dht::Task::onTimeout
void onTimeout(RPCCall *c) override
Decrements the outstanding_reqs.
dht::Task::isFinished
bool isFinished() const
Is the task finished.
Definition: task.h:109
dht::Task::addDHTNode
void addDHTNode(const QString &ip, bt::Uint16 port)
dht::Task::onResponse
void onResponse(RPCCall *c, RPCMsg::Ptr rsp) override
Decrements the outstanding_reqs.
dht::Task::Task
Task(RPCServer *rpc, Node *node, QObject *parent)
dht::KBucketEntrySet
Definition: kbucketentry.h:130
dht::Task::callTimeout
virtual void callTimeout(RPCCall *c)=0
dht::Task::emitDataReady
void emitDataReady()
dht::RPCCall
Definition: rpccall.h:78
dht::Task::rpcCall
bool rpcCall(RPCMsg::Ptr req)