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 
44  struct ChunkDownloadHeader
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 
58  class DownloadStatus
59  {
60  public:
61  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:
97  ChunkDownload(Chunk* chunk);
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 
141  bool assign(PieceDownloader* pd);
142 
147  void release(PieceDownloader* pd);
148 
153  void killed(PieceDownloader* pd);
154 
159  void save(File & file);
160 
167  bool load(File & file,ChunkDownloadHeader & hdr,bool update_hash = true);
168 
172  void cancelAll();
173 
179  PieceDownloader* getOnlyDownloader();
180 
182  bool containsPeer(PieceDownloader *pd) {return pdown.contains(pd);}
183 
185  bool isChoked() const;
186 
188  void releaseAllPDs();
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::BitSet
Simple implementation of a BitSet.
Definition: bitset.h:55
bt::Piece
Definition: piece.h:50
bt::PtrMap
Map of pointers.
Definition: ptrmap.h:56
bt::Chunk
Keep track of a piece of the file.
Definition: chunk.h:64
bt::PieceDownloader
Definition: piecedownloader.h:57
bt::File
Wrapper class for stdio's FILE.
Definition: file.h:56
bt::ChunkDownloadInterface
Interface for a ChunkDownload.
Definition: chunkdownloadinterface.h:54
bt::DownloadStatus
Definition: chunkdownload.h:77
bt::Timer
Definition: timer.h:53
bt::SHA1HashGen
Definition: sha1hashgen.h:46
bt::Request
Request of a piece sent to other peers.
Definition: request.h:58
bt::SHA1Hash
Stores a SHA1 hash.
Definition: sha1hash.h:41
bt::ChunkDownloadHeader
Definition: chunkdownload.h:63
bt::ChunkDownloadInterface::Stats
Definition: chunkdownloadinterface.h:78
bt::ChunkDownload
Handles the download off one Chunk off a Peer.
Definition: chunkdownload.h:108