libktorrent  2.2.0
chunkdownload.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 BTCHUNKDOWNLOAD_H
21 #define BTCHUNKDOWNLOAD_H
22 
23 #include <QSet>
24 #include <QObject>
25 #include <QList>
26 #include <util/timer.h>
27 #include <util/ptrmap.h>
28 #include <util/sha1hashgen.h>
29 #include <interfaces/chunkdownloadinterface.h>
30 #include <util/bitset.h>
31 #include <diskio/piecedata.h>
32 
33 
34 namespace bt
35 {
36 
37  class File;
38  class Chunk;
39  class Piece;
40  class Peer;
41  class Request;
42  class PieceDownloader;
43 
45  {
46  Uint32 index;
47  Uint32 num_bits;
48  Uint32 buffered;
49  };
50 
51  struct PieceHeader
52  {
53  Uint32 piece;
54  Uint32 size;
55  Uint32 mapped;
56  };
57 
59  {
60  public:
62  ~DownloadStatus();
63 
64  void add(Uint32 p);
65  void remove(Uint32 p);
66  bool contains(Uint32 p);
67  void clear();
68 
69  void timeout() {timeouts++;}
70  Uint32 numTimeouts() const {return timeouts;}
71 
72  typedef QSet<Uint32>::iterator iterator;
73  iterator begin() {return status.begin();}
74  iterator end() {return status.end();}
75 
76  private:
77  Uint32 timeouts;
78  QSet<Uint32> status;
79  };
80 
81 
82 
89  class KTORRENT_EXPORT ChunkDownload : public QObject,public ChunkDownloadInterface
90  {
91  Q_OBJECT
92  public:
98 
99  ~ChunkDownload() override;
100 
102  Chunk* getChunk() {return chunk;}
103 
105  Uint32 getTotalPieces() const {return num;}
106 
108  Uint32 getPiecesDownloaded() const {return num_downloaded;}
109 
111  Uint32 bytesDownloaded() const;
112 
114  Uint32 getChunkIndex() const;
115 
117  QString getPieceDownloaderName() const;
118 
120  Uint32 getDownloadSpeed() const;
121 
123  void getStats(Stats & s) override;
124 
126  bool isIdle() const {return pdown.count() == 0;}
127 
134  bool piece(const Piece & p,bool & ok);
135 
142 
148 
154 
159  void save(File & file);
160 
167  bool load(File & file,ChunkDownloadHeader & hdr,bool update_hash = true);
168 
172  void cancelAll();
173 
180 
182  bool containsPeer(PieceDownloader *pd) {return pdown.contains(pd);}
183 
185  bool isChoked() const;
186 
189 
191  void update();
192 
194  bool needsToBeUpdated() const {return timer.getElapsedSinceUpdate() > 60 * 1000;}
195 
197  SHA1Hash getHash() const {return hash_gen.get();}
198 
200  Uint32 getNumDownloaders() const {return pdown.count();}
201 
202  private Q_SLOTS:
203  void onTimeout(const bt::Request & r);
204  void onRejected(const bt::Request & r);
205 
206  private:
207  void notDownloaded(const Request & r,bool reject);
208  void updateHash();
209  void sendRequests();
210  bool sendRequest(PieceDownloader* pd);
211  void sendCancels(PieceDownloader* pd);
212  void endgameCancel(const Piece & p);
213  Uint32 bestPiece(PieceDownloader* pd);
214 
215  private:
216  BitSet pieces;
217  Chunk* chunk;
218  Uint32 num;
219  Uint32 num_downloaded;
220  Uint32 last_size;
221  Timer timer;
222  QList<PieceDownloader*> pdown;
224  QSet<PieceDownloader*> piece_providers;
225  PieceData::Ptr* piece_data;
226  SHA1HashGen hash_gen;
227  Uint32 num_pieces_in_hash;
228 
229  friend File & operator << (File & out,const ChunkDownload & cd);
230  friend File & operator >> (File & in,ChunkDownload & cd);
231  };
232 }
233 
234 #endif
bt::ChunkDownload::load
bool load(File &file, ChunkDownloadHeader &hdr, bool update_hash=true)
bt::BitSet
Simple implementation of a BitSet.
Definition: bitset.h:37
bt::ChunkDownload::release
void release(PieceDownloader *pd)
bt::ChunkDownload::update
void update()
Send requests to peers.
bt::ChunkDownload::killed
void killed(PieceDownloader *pd)
bt::ChunkDownload::cancelAll
void cancelAll()
bt::Piece
Definition: piece.h:32
bt::PtrMap
Map of pointers.
Definition: ptrmap.h:38
bt::Chunk
Keep track of a piece of the file.
Definition: chunk.h:46
bt::ChunkDownload::getPieceDownloaderName
QString getPieceDownloaderName() const
Get the PeerID of the current peer.
bt::PieceDownloader
Definition: piecedownloader.h:38
bt::ChunkDownload::getHash
SHA1Hash getHash() const
Get the SHA1 hash of the downloaded chunk.
Definition: chunkdownload.h:197
bt::File
Wrapper class for stdio's FILE.
Definition: file.h:38
bt::ChunkDownloadInterface
Interface for a ChunkDownload.
Definition: chunkdownloadinterface.h:36
bt::DownloadStatus
Definition: chunkdownload.h:59
bt::ChunkDownload::needsToBeUpdated
bool needsToBeUpdated() const
See if this CD hasn't been active in the last update.
Definition: chunkdownload.h:194
bt::Timer
Definition: timer.h:35
bt::ChunkDownload::getTotalPieces
Uint32 getTotalPieces() const
Get the total number of pieces.
Definition: chunkdownload.h:105
bt::PieceHeader
Definition: chunkdownload.h:52
bt::ChunkDownload::containsPeer
bool containsPeer(PieceDownloader *pd)
See if a PieceDownloader is assigned to this chunk.
Definition: chunkdownload.h:182
bt::SHA1HashGen
Definition: sha1hashgen.h:46
bt::Request
Request of a piece sent to other peers.
Definition: request.h:40
bt::ChunkDownload::getChunkIndex
Uint32 getChunkIndex() const
Get the index of the chunk.
bt::ChunkDownload::isChoked
bool isChoked() const
See if the download is choked (i.e. all downloaders are choked)
bt::SHA1Hash
Stores a SHA1 hash.
Definition: sha1hash.h:41
bt::ChunkDownload::getChunk
Chunk * getChunk()
Get the chunk.
Definition: chunkdownload.h:102
bt::ChunkDownload::getDownloadSpeed
Uint32 getDownloadSpeed() const
Get the download speed.
bt::ChunkDownload::releaseAllPDs
void releaseAllPDs()
Release all PD's and clear the requested chunks.
bt::ChunkDownload::getOnlyDownloader
PieceDownloader * getOnlyDownloader()
bt::ChunkDownload::isIdle
bool isIdle() const
See if a chunkdownload is idle (i.e. has no downloaders)
Definition: chunkdownload.h:126
bt::ChunkDownload::bytesDownloaded
Uint32 bytesDownloaded() const
Get the number of bytes downloaded.
bt::ChunkDownload::getPiecesDownloaded
Uint32 getPiecesDownloaded() const
Get the number of pieces downloaded.
Definition: chunkdownload.h:108
bt::ChunkDownloadHeader
Definition: chunkdownload.h:45
bt::ChunkDownload::save
void save(File &file)
bt::ChunkDownload::getStats
void getStats(Stats &s) override
Get download stats.
bt::ChunkDownload::piece
bool piece(const Piece &p, bool &ok)
bt::ChunkDownloadInterface::Stats
Definition: chunkdownloadinterface.h:42
bt::ChunkDownload::getNumDownloaders
Uint32 getNumDownloaders() const
Get the number of downloaders.
Definition: chunkdownload.h:200
bt::ChunkDownload::ChunkDownload
ChunkDownload(Chunk *chunk)
bt::ChunkDownload
Handles the download off one Chunk off a Peer.
Definition: chunkdownload.h:90
bt::ChunkDownload::assign
bool assign(PieceDownloader *pd)