libktorrent  2.2.0
torrentcontrol.h
1 /***************************************************************************
2  * Copyright (C) 2005 by *
3  * Joris Guisson <joris.guisson@gmail.com> *
4  * Ivan Vasic <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 BTTORRENTCONTROL_H
22 #define BTTORRENTCONTROL_H
23 
24 #include <qdatetime.h>
25 #include <qobject.h>
26 #include <qtimer.h>
27 #include <QUrl>
28 #include <util/timer.h>
29 #include <interfaces/torrentinterface.h>
30 #include <interfaces/trackerslist.h>
31 #include <ktorrent_export.h>
32 #include "torrent.h"
33 #include "globals.h"
34 
35 class QStringList;
36 class QString;
37 class KJob;
38 
39 
40 namespace bt
41 {
42  class StatsFile;
43  class Choker;
44  class PeerSourceManager;
45  class ChunkManager;
46  class PeerManager;
47  class Downloader;
48  class Uploader;
49  class Peer;
50  class BitSet;
51  class QueueManagerInterface;
52  class TimeEstimator;
53  class WaitJob;
54  class MonitorInterface;
55  class ChunkSelectorFactoryInterface;
56  class CacheFactory;
57  class JobQueue;
58  class DataCheckerJob;
59 
68  class KTORRENT_EXPORT TorrentControl : public TorrentInterface, public FilePriorityListener
69  {
70  Q_OBJECT
71  public:
73  ~TorrentControl() override;
74 
76  const Torrent & getTorrent() const {return *tor;}
77 
88  const QByteArray & data,
89  const QString & tmpdir,
90  const QString & datadir);
91 
92 
94  void setPreallocateDiskSpace(bool pa) {prealloc = pa;}
95 
97  bool hasExistingFiles() const;
98 
99  const BitSet & downloadedChunksBitSet() const override;
100  const BitSet & availableChunksBitSet() const override;
101  const BitSet & excludedChunksBitSet() const override;
102  const BitSet & onlySeedChunksBitSet() const override;
103  bool changeTorDir(const QString & new_dir) override;
104  bool changeOutputDir(const QString& new_dir,int flags) override;
105  void rollback() override;
106  void setDisplayName(const QString & n) override;
108  const TrackersList* getTrackersList() const override;
109  QString getDataDir() const override {return outputdir;}
110  QString getTorDir() const override {return tordir;}
111  void setMonitor(MonitorInterface* tmo) override;
112  Uint32 getRunningTimeDL() const override;
113  Uint32 getRunningTimeUL() const override;
114  Uint32 getNumFiles() const override;
115  TorrentFileInterface & getTorrentFile(Uint32 index) override;
116  const TorrentFileInterface & getTorrentFile(Uint32 index) const override;
117  bool moveTorrentFiles(const QMap<TorrentFileInterface*,QString> & files) override;
118  void recreateMissingFiles() override;
119  void dndMissingFiles() override;
120  TorrentFileStream::Ptr createTorrentFileStream(bt::Uint32 index,bool streaming_mode,QObject* parent) override;
121  void addPeerSource(PeerSource* ps) override;
122  void removePeerSource(PeerSource* ps) override;
123  const QTextCodec* getTextCodec() const override;
124  void changeTextCodec(QTextCodec* tc) override;
125  Uint32 getNumWebSeeds() const override;
126  const WebSeedInterface* getWebSeed(Uint32 i) const override;
127  WebSeedInterface* getWebSeed(Uint32 i) override;
128  bool addWebSeed(const QUrl &url) override;
129  bool removeWebSeed(const QUrl &url) override;
130  bool readyForPreview() const override;
131  bool isMultimedia() const override;
133  int getPriority() const override { return istats.priority; }
134  void setPriority(int p) override;
135  bool overMaxRatio() override;
136  void setMaxShareRatio(float ratio) override;
137  float getMaxShareRatio() const override { return stats.max_share_ratio; }
138  bool overMaxSeedTime() override;
139  void setMaxSeedTime(float hours) override;
140  float getMaxSeedTime() const override {return stats.max_seed_time;}
141  void setAllowedToStart(bool on) override;
142  void setQueued(bool queued) override;
144  void networkUp() override;
145  bool announceAllowed() override;
146  Job* startDataCheck(bool auto_import, bt::Uint32 from, bt::Uint32 to) override;
147  bool hasMissingFiles(QStringList & sl) override;
148  bool isStorageMounted(QStringList& missing) override;
149  Uint32 getNumDHTNodes() const override;
150  const DHTNode & getDHTNode(Uint32 i) const override;
151  void deleteDataFiles() override;
152  const bt::PeerID & getOwnPeerID() const override;
153  QString getComments() const override;
154  const JobQueue* getJobQueue() const override {return job_queue;}
155  bool isFeatureEnabled(TorrentFeature tf) override;
156  void setFeatureEnabled(TorrentFeature tf,bool on) override;
157  bool checkDiskSpace(bool emit_sig = true) override;
158  void setTrafficLimits(Uint32 up,Uint32 down) override;
159  void getTrafficLimits(Uint32 & up,Uint32 & down) override;
160  void setAssuredSpeeds(Uint32 up,Uint32 down) override;
161  void getAssuredSpeeds(Uint32 & up,Uint32 & down) override;
162  const SHA1Hash & getInfoHash() const override;
163  void setUserModifiedFileName(const QString & n) override;
164  int getETA() override;
165  void setMoveWhenCompletedDir(const QString &dir) override {completed_dir = dir; saveStats();}
166  QString getMoveWhenCompletedDir() const override {return completed_dir;}
167  void setSuperSeeding(bool on) override;
168 
170  void createFiles();
171 
173  const PeerManager * getPeerMgr() const;
174 
179  void setChunkSelectorFactory(ChunkSelectorFactoryInterface* csfi);
180 
183 
184  public Q_SLOTS:
188  void update() override;
189 
193  void pause() override;
194 
198  void unpause() override;
199 
203  void start() override;
204 
209  void stop(WaitJob* wjob = 0) override;
210 
215  void updateTracker() override;
216 
220  void scrapeTracker() override;
221 
226 
231  static void setDataCheckWhenCompleted(bool on) {completed_datacheck = on;}
232 
237  static void setMinimumDiskSpace(Uint32 m) {min_diskspace = m;}
238 
239  protected:
241  void afterDataCheck(DataCheckerJob* job,const BitSet & result);
242  void beforeDataCheck();
243  void preallocFinished(const QString & error,bool completed);
244  void allJobsDone();
245  bool preallocate();
246 
247  private Q_SLOTS:
248  void onNewPeer(Peer* p);
249  void onPeerRemoved(Peer* p);
250  void doChoking();
251  void onIOError(const QString & msg);
253  void updateStats();
254  void corrupted(Uint32 chunk);
255  void moveDataFilesFinished(KJob* j);
256  void moveDataFilesWithMapFinished(KJob* j);
257  void downloaded(Uint32 chunk);
258  void moveToCompletedDir();
259  void emitFinished();
260 
261  private:
262  void updateTracker(const QString & ev,bool last_succes = true);
263  void updateStatus() override;
264  void saveStats();
265  void loadStats();
266  void loadOutputDir();
267  void loadEncoding();
268  void getSeederInfo(Uint32 & total,Uint32 & connected_to) const;
269  void getLeecherInfo(Uint32 & total,Uint32 & connected_to) const;
270  void continueStart();
271  void handleError(const QString & err) override;
272  void initInternal(QueueManagerInterface* qman,const QString & tmpdir,const QString & ddir);
273  void checkExisting(QueueManagerInterface* qman);
274  void setupDirs(const QString & tmpdir,const QString & ddir);
275  void setupStats();
276  void setupData();
277  void setUploadProps(Uint32 limit,Uint32 rate);
278  void setDownloadProps(Uint32 limit,Uint32 rate);
279  void downloadPriorityChanged(TorrentFile* tf, Priority newpriority, Priority oldpriority) override;
280  void updateRunningTimes();
281 
282  Q_SIGNALS:
283  void dataCheckFinished();
284 
285  private:
286  JobQueue* job_queue;
287  Torrent* tor;
288  PeerSourceManager* psman;
289  ChunkManager* cman;
290  PeerManager* pman;
291  Downloader* downloader;
292  Uploader* uploader;
293  Choker* choke;
294  TimeEstimator* m_eta;
295  MonitorInterface* tmon;
296  CacheFactory* cache_factory;
297  QString move_data_files_destination_path;
298  Timer choker_update_timer;
299  Timer stats_save_timer;
300  Timer stalled_timer;
301  Timer wanted_update_timer;
302  QString tordir;
303  QString old_tordir;
304  QString outputdir;
305  QString error_msg;
306  QString completed_dir;
307  bool prealloc;
308  TimeStamp last_diskspace_check;
309  bool loading_stats;
310 
311  struct InternalStats
312  {
313  QDateTime time_started_dl;
314  QDateTime time_started_ul;
315  Uint32 running_time_dl;
316  Uint32 running_time_ul;
317  Uint64 prev_bytes_dl;
318  Uint64 prev_bytes_ul;
319  Uint64 session_bytes_uploaded;
320  bool io_error;
321  bool custom_output_name;
322  Uint16 port;
323  int priority;
324  bool dht_on;
325  bool diskspace_warning_emitted;
326  };
327 
328  Uint32 upload_gid; // group ID for upload
329  Uint32 upload_limit;
330  Uint32 download_gid; // group ID for download
331  Uint32 download_limit;
332 
333  Uint32 assured_download_speed;
334  Uint32 assured_upload_speed;
335 
336  InternalStats istats;
337  StatsFile* stats_file;
338 
339  TorrentFileStream::WPtr stream;
340 
341  static bool completed_datacheck;
342  static Uint32 min_diskspace;
343 
344  friend class DataCheckerJob;
345  friend class PreallocationJob;
346  friend class JobQueue;
347  };
348 
349 
350 }
351 
352 #endif
bt::TorrentControl::getMoveWhenCompletedDir
QString getMoveWhenCompletedDir() const override
Definition: torrentcontrol.h:166
bt::CacheFactory
Definition: cachefactory.h:38
bt::BitSet
Simple implementation of a BitSet.
Definition: bitset.h:37
bt::TorrentControl::setSuperSeeding
void setSuperSeeding(bool on) override
bt::TorrentControl::getNumWebSeeds
Uint32 getNumWebSeeds() const override
Get the number of webseeds.
bt::TorrentControl::start
void start() override
bt::TorrentControl::update
void update() override
bt::Uploader
Definition: uploader.h:39
bt::TorrentControl::downloadedChunksBitSet
const BitSet & downloadedChunksBitSet() const override
bt::TorrentControl::getTorrent
const Torrent & getTorrent() const
Get the Torrent.
Definition: torrentcontrol.h:76
bt::TorrentFileInterface
Interface for a file in a multifile torrent.
Definition: torrentfileinterface.h:40
bt::TorrentControl::scrapeTracker
void scrapeTracker() override
bt::TorrentControl::overMaxRatio
bool overMaxRatio() override
Checks if a seeding torrent has reached its maximum share ratio.
bt::TorrentControl::getOwnPeerID
const bt::PeerID & getOwnPeerID() const override
Get our PeerID.
bt::TorrentControl::getNumDHTNodes
Uint32 getNumDHTNodes() const override
Get the number of initial DHT nodes.
bt::PeerSourceManager
Definition: peersourcemanager.h:49
bt::ChunkSelectorInterface
Definition: chunkselectorinterface.h:41
bt::TorrentControl::stop
void stop(WaitJob *wjob=0) override
bt::ChunkManager
Definition: chunkmanager.h:61
bt::TorrentControl::unpause
void unpause() override
bt::QueueManagerInterface
Definition: queuemanagerinterface.h:35
bt::TorrentControl::setMaxSeedTime
void setMaxSeedTime(float hours) override
Set the max seed time in hours (0 is no limit)
bt::JobQueue
Definition: jobqueue.h:36
bt::TorrentControl::updateTracker
void updateTracker() override
bt::Job
Definition: job.h:38
bt::TorrentControl::setChunkSelectorFactory
void setChunkSelectorFactory(ChunkSelectorFactoryInterface *csfi)
bt::TorrentControl::addWebSeed
bool addWebSeed(const QUrl &url) override
Add a webseed (return false, if there is already a webseed with the same url)
bt::Choker
Handles the choking.
Definition: choker.h:78
bt::TorrentControl::isFeatureEnabled
bool isFeatureEnabled(TorrentFeature tf) override
Is a feature enabled.
bt::TorrentControl::rollback
void rollback() override
bt::TorrentControl::createFiles
void createFiles()
Create all the necessary files.
bt::WaitJob
Definition: waitjob.h:39
bt::PeerManager
Manages all the Peers.
Definition: peermanager.h:67
bt::TorrentControl::getTrackersList
TrackersList * getTrackersList() override
Get a pointer to TrackersList object.
bt::TorrentControl::getDHTNode
const DHTNode & getDHTNode(Uint32 i) const override
Get a DHT node.
bt::DHTNode
Definition: torrentinterface.h:70
bt::TorrentControl::init
void init(QueueManagerInterface *qman, const QByteArray &data, const QString &tmpdir, const QString &datadir)
bt::TorrentControl::setChunkSelector
void setChunkSelector(ChunkSelectorInterface *csel) override
Set the ChunkSelector to use (0 resets to the default ChunkSelector)
bt::Peer
Manages the connection with a peer.
Definition: peer.h:59
bt::PeerSource
Definition: peersource.h:42
bt::TorrentControl::trackerScrapeDone
void trackerScrapeDone()
bt::TorrentControl::checkDiskSpace
bool checkDiskSpace(bool emit_sig=true) override
Check if there is enough diskspace available for this torrent.
bt::TorrentControl::setMonitor
void setMonitor(MonitorInterface *tmo) override
Set the monitor.
bt::TorrentControl::setMinimumDiskSpace
static void setMinimumDiskSpace(Uint32 m)
Definition: torrentcontrol.h:237
bt::TrackersList
Definition: trackerslist.h:44
bt::TorrentControl::getNumFiles
Uint32 getNumFiles() const override
Get the number of files in a multifile torrent (0 if we do not have a multifile torrent)
bt::TorrentControl::deleteDataFiles
void deleteDataFiles() override
bt::TorrentControl::pause
void pause() override
bt::TorrentControl::getPeerMgr
const PeerManager * getPeerMgr() const
Get the PeerManager.
bt::TorrentControl::setAssuredSpeeds
void setAssuredSpeeds(Uint32 up, Uint32 down) override
Set the assured speeds.
bt::TorrentFile
Definition: torrentfile.h:42
bt::TorrentControl::markExistingFilesAsDownloaded
void markExistingFilesAsDownloaded() override
Mark all existing files as downloaded (.
bt::TorrentControl::getPriority
int getPriority() const override
Get the torrent queue number. Zero if not in queue.
Definition: torrentcontrol.h:133
bt::TorrentControl::setMaxShareRatio
void setMaxShareRatio(float ratio) override
Set the max share ratio.
bt::TorrentControl::getMaxShareRatio
float getMaxShareRatio() const override
Get the max share ratio.
Definition: torrentcontrol.h:137
bt::PreallocationJob
Definition: preallocationjob.h:32
bt::TimeEstimator
Definition: timeestimator.h:79
bt::WebSeedInterface
Definition: webseedinterface.h:35
bt::TorrentControl::setAllowedToStart
void setAllowedToStart(bool on) override
Set whether the QM can start a torrent.
bt::TorrentControl::setCacheFactory
void setCacheFactory(CacheFactory *cf)
Set a custom Cache factory.
bt::Torrent
Loads a .torrent file.
Definition: torrent.h:80
bt::TorrentControl::setQueued
void setQueued(bool queued) override
bt::Downloader
Manages the downloading.
Definition: downloader.h:68
bt::Timer
Definition: timer.h:35
bt::MonitorInterface
Interface for classes who want to monitor a TorrentInterface.
Definition: monitorinterface.h:39
bt::TorrentControl::getRunningTimeDL
Uint32 getRunningTimeDL() const override
bt::TorrentControl::isStorageMounted
bool isStorageMounted(QStringList &missing) override
bt::TorrentControl::getInfoHash
const SHA1Hash & getInfoHash() const override
Get the info_hash.
bt::TorrentControl::setPreallocateDiskSpace
void setPreallocateDiskSpace(bool pa)
Tell the TorrentControl obj to preallocate diskspace in the next update.
Definition: torrentcontrol.h:94
bt::TorrentControl::getMaxSeedTime
float getMaxSeedTime() const override
Get the max seed time.
Definition: torrentcontrol.h:140
bt::TorrentControl::createTorrentFileStream
TorrentFileStream::Ptr createTorrentFileStream(bt::Uint32 index, bool streaming_mode, QObject *parent) override
bt::DataCheckerJob
Job which runs a DataChecker.
Definition: datacheckerjob.h:33
bt::TorrentControl::setFeatureEnabled
void setFeatureEnabled(TorrentFeature tf, bool on) override
Disable or enable a feature.
bt::TorrentControl::getTrackersList
const TrackersList * getTrackersList() const override
Get a pointer to TrackersList object.
bt::TorrentControl::getTrafficLimits
void getTrafficLimits(Uint32 &up, Uint32 &down) override
Get the traffic limits.
bt::TorrentControl::networkUp
void networkUp() override
After some network down time, the network is back up.
bt::TorrentControl::moveTorrentFiles
bool moveTorrentFiles(const QMap< TorrentFileInterface *, QString > &files) override
bt::TorrentControl::setTrafficLimits
void setTrafficLimits(Uint32 up, Uint32 down) override
Set the traffic limits for this torrent.
bt::TorrentControl::removeWebSeed
bool removeWebSeed(const QUrl &url) override
Remove a webseed (only user created ones can be removed)
bt::TorrentControl::getDataDir
QString getDataDir() const override
Get the data directory of this torrent.
Definition: torrentcontrol.h:109
bt::TorrentControl::getRunningTimeUL
Uint32 getRunningTimeUL() const override
bt::SHA1Hash
Stores a SHA1 hash.
Definition: sha1hash.h:41
bt::TorrentControl::getTorDir
QString getTorDir() const override
Definition: torrentcontrol.h:110
bt::TorrentControl::recreateMissingFiles
void recreateMissingFiles() override
bt::TorrentControl::getWebSeed
WebSeedInterface * getWebSeed(Uint32 i) override
Get a webseed (returns 0 if index is invalid)
bt::TorrentControl::changeOutputDir
bool changeOutputDir(const QString &new_dir, int flags) override
bt::TorrentControl::addPeerSource
void addPeerSource(PeerSource *ps) override
bt::TorrentControl::setPriority
void setPriority(int p) override
Set the torrent queue number.
bt::TorrentControl::setMoveWhenCompletedDir
void setMoveWhenCompletedDir(const QString &dir) override
Definition: torrentcontrol.h:165
bt::TorrentControl::removePeerSource
void removePeerSource(PeerSource *ps) override
bt::TorrentControl::getTorrentFile
const TorrentFileInterface & getTorrentFile(Uint32 index) const override
bt::TorrentControl::getTextCodec
const QTextCodec * getTextCodec() const override
Get the text codec used in the torrent.
bt::TorrentControl::availableChunksBitSet
const BitSet & availableChunksBitSet() const override
bt::TorrentControl
Controls just about everything.
Definition: torrentcontrol.h:69
bt::TorrentControl::startDataCheck
Job * startDataCheck(bool auto_import, bt::Uint32 from, bt::Uint32 to) override
bt::TorrentControl::getComments
QString getComments() const override
Get the comments.
bt::TorrentControl::getAssuredSpeeds
void getAssuredSpeeds(Uint32 &up, Uint32 &down) override
Get the assured speeds.
bt::TorrentControl::getTorrentFile
TorrentFileInterface & getTorrentFile(Uint32 index) override
bt::TorrentControl::setDataCheckWhenCompleted
static void setDataCheckWhenCompleted(bool on)
Definition: torrentcontrol.h:231
bt::PeerID
Definition: peerid.h:33
bt::TorrentControl::hasExistingFiles
bool hasExistingFiles() const
Test if the torrent has existing files, only works the first time a torrent is loaded.
bt::TorrentControl::announceAllowed
bool announceAllowed() override
Is manual announce allowed?
bt::TorrentControl::dndMissingFiles
void dndMissingFiles() override
bt::TorrentControl::setUserModifiedFileName
void setUserModifiedFileName(const QString &n) override
Set the user modified file or toplevel directory name.
bt::TorrentControl::hasMissingFiles
bool hasMissingFiles(QStringList &sl) override
bt::FilePriorityListener
Definition: torrent.h:64
bt::TorrentControl::changeTorDir
bool changeTorDir(const QString &new_dir) override
bt::StatsFile
This class is used for loading/storing torrent stats in a file.
Definition: statsfile.h:37
bt::TorrentControl::overMaxSeedTime
bool overMaxSeedTime() override
Checks if a seeding torrent has reached it's max seed timery / will be ret.
bt::TorrentControl::onlySeedChunksBitSet
const BitSet & onlySeedChunksBitSet() const override
bt::TorrentControl::getWebSeed
const WebSeedInterface * getWebSeed(Uint32 i) const override
Get a webseed (returns 0 if index is invalid)
bt::TorrentControl::changeTextCodec
void changeTextCodec(QTextCodec *tc) override
Set the text codec.
bt::TorrentControl::readyForPreview
bool readyForPreview() const override
bt::TorrentControl::excludedChunksBitSet
const BitSet & excludedChunksBitSet() const override
bt::TorrentControl::isMultimedia
bool isMultimedia() const override
See if this is a single file torrent and a multimedia files.
bt::TorrentControl::getETA
int getETA() override
bt::TorrentControl::setDisplayName
void setDisplayName(const QString &n) override
Set the displayed name.
bt::TorrentInterface
Interface for an object which controls one torrent.
Definition: torrentinterface.h:90
bt::TorrentControl::getJobQueue
const JobQueue * getJobQueue() const override
Get the JobQueue of the torrent.
Definition: torrentcontrol.h:154
bt::TorrentControl::afterDataCheck
void afterDataCheck(DataCheckerJob *job, const BitSet &result)
Called when a data check is finished by DataCheckerJob.