File indexing completed on 2025-01-05 04:37:27

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef BTTRACKER_H
0007 #define BTTRACKER_H
0008 
0009 #include <QTimer>
0010 #include <QUrl>
0011 
0012 #include <interfaces/peersource.h>
0013 #include <interfaces/trackerinterface.h>
0014 #include <ktorrent_export.h>
0015 #include <peer/peerid.h>
0016 #include <util/sha1hash.h>
0017 
0018 class QUrl;
0019 
0020 namespace bt
0021 {
0022 /**
0023     Interface used by the Tracker to obtain the data it needs to know
0024     when announcing.
0025 */
0026 class KTORRENT_EXPORT TrackerDataSource
0027 {
0028 public:
0029     virtual ~TrackerDataSource()
0030     {
0031     }
0032 
0033     virtual Uint64 bytesDownloaded() const = 0;
0034     virtual Uint64 bytesUploaded() const = 0;
0035     virtual Uint64 bytesLeft() const = 0;
0036     virtual const SHA1Hash &infoHash() const = 0;
0037     virtual bool isPartialSeed() const = 0;
0038 };
0039 
0040 /**
0041  * Base class for all tracker classes.
0042  */
0043 class KTORRENT_EXPORT Tracker : public PeerSource, public TrackerInterface
0044 {
0045     Q_OBJECT
0046 public:
0047     Tracker(const QUrl &url, TrackerDataSource *tds, const PeerID &id, int tier);
0048     ~Tracker() override;
0049 
0050     /**
0051      * Set the custom IP
0052      * @param str
0053      */
0054     static void setCustomIP(const QString &str);
0055 
0056     /// get the tracker url
0057     QUrl trackerURL() const
0058     {
0059         return url;
0060     }
0061 
0062     /**
0063      * Delete the tracker in ms milliseconds, or when the stopDone signal is emitted.
0064      * @param ms Number of ms to wait
0065      */
0066     void timedDelete(int ms);
0067 
0068     /**
0069      * Get the number of failed attempts to reach a tracker.
0070      * @return The number of failed attempts
0071      */
0072     virtual Uint32 failureCount() const = 0;
0073 
0074     /**
0075      * Do a tracker scrape to get more accurate stats about a torrent.
0076      * Does nothing if the tracker does not support this.
0077      */
0078     virtual void scrape() = 0;
0079 
0080     /// Get the trackers tier
0081     int getTier() const
0082     {
0083         return tier;
0084     }
0085 
0086     /// Get the custom ip to use, null if none is set
0087     static QString getCustomIP();
0088 
0089     /// Handle a failure
0090     void handleFailure();
0091 
0092 protected:
0093     /// Reset the tracker stats
0094     void resetTrackerStats();
0095 
0096     /// Calculates the bytes downloaded to send with the request
0097     Uint64 bytesDownloaded() const;
0098 
0099     /// Calculates the bytes uploaded to send with the request
0100     Uint64 bytesUploaded() const;
0101 
0102     /// Emit the failure signal, and set the error
0103     void failed(const QString &err);
0104 
0105 public:
0106     void manualUpdate() override = 0;
0107 
0108 Q_SIGNALS:
0109     /**
0110      * Emitted when an error happens.
0111      * @param failure_reason The reason why we couldn't reach the tracker
0112      */
0113     void requestFailed(const QString &failure_reason);
0114 
0115     /**
0116      * Emitted when a stop is done.
0117      */
0118     void stopDone();
0119 
0120     /**
0121      * Emitted when a request to the tracker succeeded
0122      */
0123     void requestOK();
0124 
0125     /**
0126      * A request to the tracker has been started.
0127      */
0128     void requestPending();
0129 
0130     /**
0131      * Emitted when a scrape has finished
0132      * */
0133     void scrapeDone();
0134 
0135 protected:
0136     int tier;
0137     PeerID peer_id;
0138     TrackerDataSource *tds;
0139     Uint32 key;
0140     QTimer reannounce_timer;
0141     Uint64 bytes_downloaded_at_start;
0142     Uint64 bytes_uploaded_at_start;
0143 };
0144 }
0145 
0146 #endif