libktorrent  2.2.0
webseed.h
1 /***************************************************************************
2  * Copyright (C) 2008 by Joris Guisson and Ivan Vasic *
3  * joris.guisson@gmail.com *
4  * ivasic@gmail.com *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20  ***************************************************************************/
21 #ifndef BTWEBSEED_H
22 #define BTWEBSEED_H
23 
24 #include <QTimer>
25 #include <QUrl>
26 #include <ktorrent_export.h>
27 #include <util/constants.h>
28 #include <interfaces/webseedinterface.h>
29 #include <interfaces/chunkdownloadinterface.h>
30 #include <diskio/piecedata.h>
31 #include <peer/connectionlimit.h>
32 
33 
34 
35 namespace bt
36 {
37  class Torrent;
38  class HttpConnection;
39  class ChunkManager;
40  class Chunk;
41  class WebSeedChunkDownload;
42 
47  class KTORRENT_EXPORT WebSeed : public QObject,public WebSeedInterface
48  {
49  Q_OBJECT
50  public:
51  WebSeed(const QUrl &url,bool user,const Torrent & tor,ChunkManager & cman);
52  ~WebSeed() override;
53 
55  bool busy() const;
56 
58  bool inCurrentRange(Uint32 chunk) const {return chunk >= first_chunk && chunk <= last_chunk;}
59 
65  void download(Uint32 first,Uint32 last);
66 
73  void onExcluded(Uint32 from,Uint32 to);
74 
79  Uint32 update();
80 
85  void chunkDownloaded(Uint32 chunk);
86 
90  void cancel();
91 
93  Uint32 getDownloadRate() const override;
94 
95 
101  void setGroupIDs(Uint32 up,Uint32 down);
102 
103 
109  static void setProxy(const QString & host,bt::Uint16 port);
110 
116  static void setProxyEnabled(bool on);
117 
120 
121  void setEnabled(bool on) override;
122 
124  void disable(const QString & reason);
125 
127  Uint32 failedAttempts() const {return num_failures;}
128 
129  public Q_SLOTS:
133  void reset();
134 
135  Q_SIGNALS:
140  void chunkReady(Chunk* c);
141 
145  void finished();
146 
153 
160 
161  private Q_SLOTS:
162  void redirected(const QUrl &to_url);
163 
164  private:
165  struct Range
166  {
167  Uint32 file;
168  Uint64 off;
169  Uint64 len;
170  };
171 
172  class AutoDisabled {}; // Exception
173 
174  void fillRangeList(Uint32 chunk);
175  void handleData(const QByteArray & data);
176  void chunkStarted(Uint32 chunk);
177  void chunkStopped();
178  void connectToServer();
179  void continueCurChunk();
180  void readData();
181  void retryLater();
182 
183  private:
184  const Torrent & tor;
185  ChunkManager & cman;
186  HttpConnection* conn;
187  QList<QByteArray> chunks;
188  Uint32 first_chunk;
189  Uint32 last_chunk;
190  Uint32 cur_chunk;
191  Uint32 bytes_of_cur_chunk;
192  Uint32 num_failures;
193  Uint32 downloaded;
194  WebSeedChunkDownload* current;
195  Uint32 up_gid,down_gid;
196  QList<Range> range_queue;
197  QUrl redirected_url;
198  PieceData::Ptr cur_piece;
199  QTimer retry_timer;
200  ConnectionLimit::Token::Ptr token;
201 
202  static QString proxy_host;
203  static Uint16 proxy_port;
204  static bool proxy_enabled;
205  };
206 
208  {
209  public:
210  WebSeedChunkDownload(WebSeed* ws,const QString & url,Uint32 index,Uint32 total);
211  ~WebSeedChunkDownload() override;
212 
213  void getStats(Stats & s) override;
214 
215  WebSeed* ws;
216  QString url;
217  Uint32 chunk;
218  Uint32 total_pieces;
219  Uint32 pieces_downloaded;
220  };
221 
222 }
223 
224 #endif
bt::WebSeed::failedAttempts
Uint32 failedAttempts() const
Get the number of failed attempts.
Definition: webseed.h:127
bt::WebSeed::chunkDownloadStarted
void chunkDownloadStarted(WebSeedChunkDownload *cd, Uint32 chunk)
bt::WebSeed::setProxyEnabled
static void setProxyEnabled(bool on)
bt::WebSeed::chunkDownloadFinished
void chunkDownloadFinished(WebSeedChunkDownload *cd, Uint32 chunk)
bt::WebSeed::download
void download(Uint32 first, Uint32 last)
bt::ChunkManager
Definition: chunkmanager.h:61
bt::WebSeedChunkDownload
Definition: webseed.h:208
bt::Chunk
Keep track of a piece of the file.
Definition: chunk.h:46
bt::ChunkDownloadInterface
Interface for a ChunkDownload.
Definition: chunkdownloadinterface.h:36
bt::WebSeed::setProxy
static void setProxy(const QString &host, bt::Uint16 port)
bt::WebSeed::update
Uint32 update()
bt::WebSeed::disable
void disable(const QString &reason)
Disable the webseed.
bt::WebSeed::inCurrentRange
bool inCurrentRange(Uint32 chunk) const
Check if a chunk lies in the current range we are downloading.
Definition: webseed.h:58
bt::WebSeed::reset
void reset()
bt::WebSeedInterface
Definition: webseedinterface.h:35
bt::Torrent
Loads a .torrent file.
Definition: torrent.h:80
bt::WebSeed::onExcluded
void onExcluded(Uint32 from, Uint32 to)
bt::WebSeed::finished
void finished()
bt::WebSeed::cancel
void cancel()
bt::WebSeed::getDownloadRate
Uint32 getDownloadRate() const override
Get the current download rate.
bt::WebSeed::setGroupIDs
void setGroupIDs(Uint32 up, Uint32 down)
bt::WebSeed::chunkReady
void chunkReady(Chunk *c)
bt::WebSeed::chunkDownloaded
void chunkDownloaded(Uint32 chunk)
bt::WebSeed::busy
bool busy() const
Is this webseed busy ?
bt::WebSeed::setEnabled
void setEnabled(bool on) override
Disable or enable the webseed.
bt::WebSeed
Definition: webseed.h:48
bt::ChunkDownloadInterface::Stats
Definition: chunkdownloadinterface.h:42
bt::WebSeed::currentChunkDownload
WebSeedChunkDownload * currentChunkDownload()
Get the current webseed download.
Definition: webseed.h:119