libktorrent  2.1.1
downloader.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 BTDOWNLOADER_H
21 #define BTDOWNLOADER_H
22 
23 #include <qobject.h>
24 #include <util/ptrmap.h>
25 #include <util/constants.h>
26 #include <ktorrent_export.h>
27 #include "download/webseed.h"
28 #include "download/chunkdownload.h"
29 #include "peer/peermanager.h"
30 
31 class QUrl;
32 
33 namespace bt
34 {
35  class BitSet;
36  class Torrent;
37  class ChunkManager;
38  class PeerManager;
39  class Peer;
40  class Chunk;
41  class Piece;
42  class ChunkSelectorInterface;
43  class PieceDownloader;
44  class MonitorInterface;
45  class WebSeedChunkDownload;
46 
47  typedef PtrMap<Uint32,ChunkDownload>::iterator CurChunkItr;
48  typedef PtrMap<Uint32,ChunkDownload>::const_iterator CurChunkCItr;
49 
50  #define CURRENT_CHUNK_MAGIC 0xABCDEF00
51 
53  {
54  Uint32 magic; // CURRENT_CHUNK_MAGIC
55  Uint32 major;
56  Uint32 minor;
57  Uint32 num_chunks;
58  };
59 
67  class KTORRENT_EXPORT Downloader : public QObject,public PieceHandler
68  {
69  Q_OBJECT
70 
71  public:
79  ~Downloader() override;
80 
86  void setGroupIDs(Uint32 up,Uint32 down);
87 
89  Uint32 getNumWebSeeds() const {return webseeds.count();}
90 
92  const WebSeed* getWebSeed(Uint32 i) const {return i < (Uint32)webseeds.count() ? webseeds[i] : 0;}
93 
95  WebSeed* getWebSeed(Uint32 i) {return i < (Uint32)webseeds.count() ? webseeds[i] : 0;}
96 
98  WebSeed* addWebSeed(const QUrl &url);
99 
101  bool removeWebSeed(const QUrl &url);
102 
105 
107  void saveWebSeeds(const QString & file);
108 
110  void loadWebSeeds(const QString & file);
111 
113  Uint64 bytesDownloaded() const {return bytes_downloaded + curr_chunks_downloaded;}
114 
116  Uint32 downloadRate() const;
117 
119  Uint32 numActiveDownloads() const {return current_chunks.count() + active_webseed_downloads;}
120 
122  bool isFinished() const;
123 
128 
132  void pause();
133 
134  CurChunkCItr beginDownloads() const {return current_chunks.begin();}
135  CurChunkCItr endDownloads() const {return current_chunks.end();}
136 
137 
143  ChunkDownload* download(Uint32 chunk);
144 
150  const ChunkDownload* download(Uint32 chunk) const;
151 
157  bool downloading(Uint32 chunk) const;
158 
164  bool canDownloadFromWebSeed(Uint32 chunk) const;
165 
171  Uint32 numDownloadersForChunk(Uint32 chunk) const;
172 
174  bool endgameMode() const;
175 
180  void saveDownloads(const QString & file);
181 
186  void loadDownloads(const QString & file);
187 
193  Uint32 getDownloadedBytesOfCurrentChunksFile(const QString & file);
194 
199  void corrupted(Uint32 chunk);
200 
203 
209 
215 
217  static void setUseWebSeeds(bool on);
218  public Q_SLOTS:
222  void update();
223 
229 
236  void dataChecked(const BitSet & ok_chunks, bt::Uint32 from, bt::Uint32 to);
237 
242 
243  private Q_SLOTS:
244  void pieceReceived(const bt::Piece & p) override;
245  bool finished(ChunkDownload* c);
246 
252  void onExcluded(Uint32 from,Uint32 to);
253 
259  void onIncluded(Uint32 from,Uint32 to);
260 
265  void onChunkReady(Chunk* c);
266 
267  void chunkDownloadStarted(WebSeedChunkDownload* cd,Uint32 chunk);
268  void chunkDownloadFinished(WebSeedChunkDownload* cd,Uint32 chunk);
269 
270  Q_SIGNALS:
275  void ioError(const QString & msg);
276 
281  void chunkDownloaded(Uint32 chunk);
282 
283  private:
284  bool downloadFrom(PieceDownloader* pd);
285  void downloadFrom(WebSeed* ws);
286  void normalUpdate();
287  bool findDownloadForPD(PieceDownloader* pd);
288  ChunkDownload* selectCD(PieceDownloader* pd,Uint32 num);
289  ChunkDownload* selectWorst(PieceDownloader* pd);
290 
291  private:
292  Torrent & tor;
293  PeerManager & pman;
294  ChunkManager & cman;
295  Uint64 bytes_downloaded;
296  Uint64 curr_chunks_downloaded;
297  Uint64 unnecessary_data;
298  PtrMap<Uint32,ChunkDownload> current_chunks;
299  QList<PieceDownloader*> piece_downloaders;
300  MonitorInterface* tmon;
301  ChunkSelectorInterface* chunk_selector;
302  QList<WebSeed*> webseeds;
303  PtrMap<Uint32,WebSeed> webseeds_chunks;
304  Uint32 active_webseed_downloads;
305  bool webseeds_on;
306  Uint32 webseed_range_size;
307  bool webseed_endgame_mode;
308 
309  static bool use_webseeds;
310  };
311 
312 
313 
314 }
315 
316 #endif
bt::BitSet
Simple implementation of a BitSet.
Definition: bitset.h:55
bt::Downloader::clearDownloads
void clearDownloads()
bt::CurrentChunksHeader
Definition: downloader.h:53
bt::Downloader::setChunkSelector
void setChunkSelector(ChunkSelectorInterface *csel)
Set the ChunkSelector, 0 means KT will reset to the default selector.
bt::Downloader::removeWebSeed
bool removeWebSeed(const QUrl &url)
Remove a webseed.
bt::Downloader::recalcDownloaded
void recalcDownloaded()
bt::Piece
Definition: piece.h:50
bt::Downloader::download
const ChunkDownload * download(Uint32 chunk) const
bt::PtrMap
Map of pointers.
Definition: ptrmap.h:56
bt::Downloader::getDownloadedBytesOfCurrentChunksFile
Uint32 getDownloadedBytesOfCurrentChunksFile(const QString &file)
bt::ChunkSelectorInterface
Definition: chunkselectorinterface.h:60
bt::Downloader::numDownloadersForChunk
Uint32 numDownloadersForChunk(Uint32 chunk) const
bt::Downloader::removePieceDownloader
void removePieceDownloader(PieceDownloader *peer)
bt::ChunkManager
Definition: chunkmanager.h:61
bt::Downloader::isFinished
bool isFinished() const
See if the download is finished.
bt::Downloader::downloadRate
Uint32 downloadRate() const
Get the current dowload rate.
bt::WebSeedChunkDownload
Definition: webseed.h:227
bt::Chunk
Keep track of a piece of the file.
Definition: chunk.h:64
bt::Downloader::saveDownloads
void saveDownloads(const QString &file)
bt::PieceDownloader
Definition: piecedownloader.h:57
bt::PeerManager
Manages all the Peers.
Definition: peermanager.h:67
bt::Downloader::update
void update()
bt::Downloader::canDownloadFromWebSeed
bool canDownloadFromWebSeed(Uint32 chunk) const
bt::Downloader::addWebSeed
WebSeed * addWebSeed(const QUrl &url)
Add a webseed.
bt::Downloader::dataChecked
void dataChecked(const BitSet &ok_chunks, bt::Uint32 from, bt::Uint32 to)
bt::Downloader::getWebSeed
const WebSeed * getWebSeed(Uint32 i) const
Get a webseed.
Definition: downloader.h:92
bt::Downloader::addPieceDownloader
void addPieceDownloader(PieceDownloader *peer)
bt::Downloader::downloading
bool downloading(Uint32 chunk) const
bt::Downloader::removeAllWebSeeds
void removeAllWebSeeds()
Remove all webseeds.
bt::Torrent
Loads a .torrent file.
Definition: torrent.h:80
bt::Downloader
Manages the downloading.
Definition: downloader.h:68
bt::Downloader::setGroupIDs
void setGroupIDs(Uint32 up, Uint32 down)
bt::MonitorInterface
Interface for classes who want to monitor a TorrentInterface.
Definition: monitorinterface.h:57
bt::Downloader::saveWebSeeds
void saveWebSeeds(const QString &file)
Save the user created webseeds.
bt::Downloader::pause
void pause()
bt::Downloader::getWebSeed
WebSeed * getWebSeed(Uint32 i)
Get a webseed.
Definition: downloader.h:95
bt::Downloader::loadDownloads
void loadDownloads(const QString &file)
bt::Downloader::chunkDownloaded
void chunkDownloaded(Uint32 chunk)
bt::Downloader::numActiveDownloads
Uint32 numActiveDownloads() const
Get the number of chunks we are dowloading.
Definition: downloader.h:119
bt::PieceHandler
Base class for handling pieces.
Definition: peermanager.h:52
bt::Downloader::loadWebSeeds
void loadWebSeeds(const QString &file)
Add the user created webseeds.
bt::Downloader::bytesDownloaded
Uint64 bytesDownloaded() const
Get the number of bytes we have downloaded.
Definition: downloader.h:113
bt::Downloader::setUseWebSeeds
static void setUseWebSeeds(bool on)
Enable or disable the use of webseeds.
bt::Downloader::Downloader
Downloader(Torrent &tor, PeerManager &pman, ChunkManager &cman)
bt::Downloader::corrupted
void corrupted(Uint32 chunk)
bt::Downloader::endgameMode
bool endgameMode() const
Are we in endgame mode.
bt::WebSeed
Definition: webseed.h:67
bt::Downloader::ioError
void ioError(const QString &msg)
bt::ChunkDownload
Handles the download off one Chunk off a Peer.
Definition: chunkdownload.h:108
bt::Downloader::setMonitor
void setMonitor(MonitorInterface *tmo)
bt::Downloader::download
ChunkDownload * download(Uint32 chunk)
bt::Downloader::getNumWebSeeds
Uint32 getNumWebSeeds() const
Get the number of webseeds.
Definition: downloader.h:89