From d5e2905eadbbf6c03e7cb69f2d93b3aac33b8134 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Thu, 19 Nov 2009 08:14:04 +0000 Subject: [PATCH] - Clean up of bittorrent class --- src/bittorrent.cpp | 36 +++--- src/bittorrent.h | 284 +++++++++++++++++++++++---------------------- 2 files changed, 165 insertions(+), 155 deletions(-) diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index cfdcb39fd..7ea4d4d29 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -62,7 +62,7 @@ enum ProxyType {HTTP=1, SOCKS5=2, HTTP_PW=3, SOCKS5_PW=4}; // Main constructor -bittorrent::bittorrent() : DHTEnabled(false), preAllocateAll(false), addInPause(false), ratio_limit(-1), UPnPEnabled(false), NATPMPEnabled(false), LSDEnabled(false), queueingEnabled(false), geoipDBLoaded(false) { +bittorrent::bittorrent() : preAllocateAll(false), addInPause(false), ratio_limit(-1), UPnPEnabled(false), NATPMPEnabled(false), LSDEnabled(false), DHTEnabled(false), queueingEnabled(false), geoipDBLoaded(false) { resolve_countries = false; // To avoid some exceptions fs::path::default_name_check(fs::no_check); @@ -204,7 +204,7 @@ void bittorrent::configureSession() { qDebug("Configuring session"); // Downloads // * Save path - setDefaultSavePath(Preferences::getSavePath()); + defaultSavePath = Preferences::getSavePath(); if(Preferences::isTempPathEnabled()) { setDefaultTempPath(Preferences::getTempPath()); } else { @@ -1271,10 +1271,6 @@ void bittorrent::addTorrentsFromScanFolder(QStringList &pathList) { } } -void bittorrent::setDefaultSavePath(QString savepath) { - defaultSavePath = savepath; -} - QString bittorrent::getDefaultSavePath() const { return defaultSavePath; } @@ -1297,6 +1293,7 @@ void bittorrent::setDefaultTempPath(QString temppath) { h.move_storage(getSavePath(h.hash())); } } else { + // Moving all downloading torrents to temporary save path std::vector torrents = getTorrents(); std::vector::iterator torrentIT; for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { @@ -1597,12 +1594,8 @@ void bittorrent::readAlerts() { if(h.is_valid()){ // Authentication if(p->status_code != 401) { - QString hash = h.hash(); - qDebug("Received a tracker error for %s", p->url.c_str()); - QHash errors = trackersErrors.value(hash, QHash()); - // p->url requires at least libtorrent v0.13.1 - errors[misc::toQString(p->url)] = QString::fromUtf8(a->message().c_str()); - trackersErrors[hash] = errors; + qDebug("Received a tracker error for %s: %s", p->url.c_str(), p->msg.c_str()); + trackersErrors[h.hash()][misc::toQString(p->url)] = misc::toQString(p->message()); } else { emit trackerAuthenticationRequired(h); } @@ -1611,12 +1604,21 @@ void bittorrent::readAlerts() { else if (tracker_reply_alert* p = dynamic_cast(a.get())) { QTorrentHandle h(p->handle); if(h.is_valid()){ - qDebug("Received a tracker reply from %s", (const char*)h.current_tracker().toLocal8Bit()); - QString hash = h.hash(); - QHash errors = trackersErrors.value(hash, QHash()); - // p->url requires at least libtorrent v0.13.1 + qDebug("Received a tracker reply from %s", h.current_tracker().toLocal8Bit().data()); + // Connection was successful now. Remove possible old errors + QHash errors = trackersErrors.value(h.hash(), QHash()); + errors.remove(h.current_tracker()); + trackersErrors[h.hash()] = errors; + } + } else if (tracker_warning_alert* p = dynamic_cast(a.get())) { + QTorrentHandle h(p->handle); + if(h.is_valid()){ + // Connection was successful now. Remove possible old errors + QHash errors = trackersErrors.value(h.hash(), QHash()); errors.remove(h.current_tracker()); - trackersErrors[hash] = errors; + trackersErrors[h.hash()] = errors; + qDebug("Received a tracker warning from %s: %s", h.current_tracker().toLocal8Bit().data(), p->msg.c_str()); + // XXX: The tracker warning is silently ignored... do something with it. } } else if (portmap_error_alert* p = dynamic_cast(a.get())) { diff --git a/src/bittorrent.h b/src/bittorrent.h index 3a4148e72..4dcb65e5a 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -53,149 +53,157 @@ class HttpServer; class bittorrent : public QObject { Q_OBJECT - private: - session *s; - QPointer FSWatcher; - QPointer timerAlerts; - QPointer BigRatioTimer; - bool DHTEnabled; - QPointer downloader; - QString defaultSavePath; - QString defaultTempPath; - QHash > trackersErrors; - QStringList consoleMessages; - QStringList peerBanMessages; - bool preAllocateAll; - bool addInPause; - float ratio_limit; - bool UPnPEnabled; - bool NATPMPEnabled; - bool LSDEnabled; - QPointer filterParser; - QString filterPath; - bool queueingEnabled; - QStringList url_skippingDlg; - QHash savepath_fromurl; - bool resolve_countries; - bool geoipDBLoaded; - QPointer timerETA; - QHash > ETA_samples; - // Web UI - QPointer httpServer; +private: + // Bittorrent + session *s; + QPointer timerAlerts; + QHash savepath_fromurl; + QHash > trackersErrors; + // Ratio + QPointer BigRatioTimer; + // HTTP + QPointer downloader; + // File System + QPointer FSWatcher; + // Console / Log + QStringList consoleMessages; + QStringList peerBanMessages; + // Settings + bool preAllocateAll; + bool addInPause; + float ratio_limit; + bool UPnPEnabled; + bool NATPMPEnabled; + bool LSDEnabled; + bool DHTEnabled; + bool queueingEnabled; + QString defaultSavePath; + QString defaultTempPath; + // GeoIP + bool resolve_countries; + bool geoipDBLoaded; + // ETA Computation + QPointer timerETA; + QHash > ETA_samples; + // IP filtering + QPointer filterParser; + QString filterPath; + // Web UI + QPointer httpServer; + QStringList url_skippingDlg; - protected: - QString getSavePath(QString hash); - bool initWebUi(QString username, QString password, int port); +protected: + QString getSavePath(QString hash); + bool initWebUi(QString username, QString password, int port); - public: - // Constructor / Destructor - bittorrent(); - ~bittorrent(); - QTorrentHandle getTorrentHandle(QString hash) const; - std::vector getTorrents() const; - bool isFilePreviewPossible(QString fileHash) const; - bool isDHTEnabled() const; - float getPayloadDownloadRate() const; - float getPayloadUploadRate() const; - session_status getSessionStatus() const; - int getListenPort() const; - float getRealRatio(QString hash) const; - session* getSession() const; - QHash getTrackersErrors(QString hash) const; - bool has_filtered_files(QString hash) const; - bool hasActiveTorrents() const; - bool isQueueingEnabled() const; - int getMaximumActiveDownloads() const; - int getMaximumActiveTorrents() const; - int loadTorrentPriority(QString hash); - QStringList getConsoleMessages() const; - QStringList getPeerBanMessages() const; - qlonglong getETA(QString hash); - bool useTemporaryFolder() const; - QString getDefaultSavePath() const; +public: + // Constructor / Destructor + bittorrent(); + ~bittorrent(); + QTorrentHandle getTorrentHandle(QString hash) const; + std::vector getTorrents() const; + bool isFilePreviewPossible(QString fileHash) const; + bool isDHTEnabled() const; + float getPayloadDownloadRate() const; + float getPayloadUploadRate() const; + session_status getSessionStatus() const; + int getListenPort() const; + float getRealRatio(QString hash) const; + session* getSession() const; + QHash getTrackersErrors(QString hash) const; + bool has_filtered_files(QString hash) const; + bool hasActiveTorrents() const; + bool isQueueingEnabled() const; + int getMaximumActiveDownloads() const; + int getMaximumActiveTorrents() const; + int loadTorrentPriority(QString hash); + QStringList getConsoleMessages() const; + QStringList getPeerBanMessages() const; + qlonglong getETA(QString hash); + bool useTemporaryFolder() const; + QString getDefaultSavePath() const; - public slots: - QTorrentHandle addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false); - QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false); - void importOldTorrents(); - void applyFormerAttributeFiles(QTorrentHandle h); - void importOldTempData(QString torrent_path); - void loadSessionState(); - void saveSessionState(); - void downloadFromUrl(QString url); - void deleteTorrent(QString hash, bool delete_local_files = false); - void startUpTorrents(); - /* Needed by Web UI */ - void pauseAllTorrents(); - void pauseTorrent(QString hash); - void resumeTorrent(QString hash); - void resumeAllTorrents(); - /* End Web UI */ - void saveDHTEntry(); - void preAllocateAllFiles(bool b); - void saveFastResumeData(); - void enableDirectoryScanning(QString scan_dir); - void disableDirectoryScanning(); - void enableIPFilter(QString filter); - void disableIPFilter(); - void setQueueingEnabled(bool enable); - void handleDownloadFailure(QString url, QString reason); - void loadWebSeeds(QString fileHash); - void downloadUrlAndSkipDialog(QString url, QString save_path=QString::null); - // Session configuration - Setters - void setListeningPort(int port); - void setMaxConnections(int maxConnec); - void setMaxConnectionsPerTorrent(int max); - void setMaxUploadsPerTorrent(int max); - void setDownloadRateLimit(long rate); - void setUploadRateLimit(long rate); - void setGlobalRatio(float ratio); - void setDeleteRatio(float ratio); - void setDHTPort(int dht_port); - void setProxySettings(proxy_settings proxySettings, bool trackers=true, bool peers=true, bool web_seeds=true, bool dht=true); - void setSessionSettings(session_settings sessionSettings); - void startTorrentsInPause(bool b); - void setDefaultSavePath(QString savepath); - void setDefaultTempPath(QString temppath); - void applyEncryptionSettings(pe_settings se); - void loadFilesPriorities(QTorrentHandle& h); - void setDownloadLimit(QString hash, long val); - void setUploadLimit(QString hash, long val); - void enableUPnP(bool b); - void enableNATPMP(bool b); - void enableLSD(bool b); - bool enableDHT(bool b); - void addConsoleMessage(QString msg, QColor color=QApplication::palette().color(QPalette::WindowText)); - void addPeerBanMessage(QString msg, bool from_ipfilter); - void processDownloadedFile(QString, QString); - void saveTrackerFile(QString hash); - void addMagnetSkipAddDlg(QString uri); - void downloadFromURLList(const QStringList& urls); - void configureSession(); - void banIP(QString ip); +public slots: + QTorrentHandle addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false); + QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false); + void importOldTorrents(); + void applyFormerAttributeFiles(QTorrentHandle h); + void importOldTempData(QString torrent_path); + void loadSessionState(); + void saveSessionState(); + void downloadFromUrl(QString url); + void deleteTorrent(QString hash, bool delete_local_files = false); + void startUpTorrents(); + /* Needed by Web UI */ + void pauseAllTorrents(); + void pauseTorrent(QString hash); + void resumeTorrent(QString hash); + void resumeAllTorrents(); + /* End Web UI */ + void saveDHTEntry(); + void preAllocateAllFiles(bool b); + void saveFastResumeData(); + void enableDirectoryScanning(QString scan_dir); + void disableDirectoryScanning(); + void enableIPFilter(QString filter); + void disableIPFilter(); + void setQueueingEnabled(bool enable); + void handleDownloadFailure(QString url, QString reason); + void loadWebSeeds(QString fileHash); + void downloadUrlAndSkipDialog(QString url, QString save_path=QString::null); + // Session configuration - Setters + void setListeningPort(int port); + void setMaxConnections(int maxConnec); + void setMaxConnectionsPerTorrent(int max); + void setMaxUploadsPerTorrent(int max); + void setDownloadRateLimit(long rate); + void setUploadRateLimit(long rate); + void setGlobalRatio(float ratio); + void setDeleteRatio(float ratio); + void setDHTPort(int dht_port); + void setProxySettings(proxy_settings proxySettings, bool trackers=true, bool peers=true, bool web_seeds=true, bool dht=true); + void setSessionSettings(session_settings sessionSettings); + void startTorrentsInPause(bool b); + void setDefaultTempPath(QString temppath); + void applyEncryptionSettings(pe_settings se); + void loadFilesPriorities(QTorrentHandle& h); + void setDownloadLimit(QString hash, long val); + void setUploadLimit(QString hash, long val); + void enableUPnP(bool b); + void enableNATPMP(bool b); + void enableLSD(bool b); + bool enableDHT(bool b); + void addConsoleMessage(QString msg, QColor color=QApplication::palette().color(QPalette::WindowText)); + void addPeerBanMessage(QString msg, bool from_ipfilter); + void processDownloadedFile(QString, QString); + void saveTrackerFile(QString hash); + void addMagnetSkipAddDlg(QString uri); + void downloadFromURLList(const QStringList& urls); + void configureSession(); + void banIP(QString ip); - protected slots: - void addTorrentsFromScanFolder(QStringList&); - void readAlerts(); - void loadTrackerFile(QString hash); - void deleteBigRatios(); - void takeETASamples(); +protected slots: + void addTorrentsFromScanFolder(QStringList&); + void readAlerts(); + void loadTrackerFile(QString hash); + void deleteBigRatios(); + void takeETASamples(); - signals: - void addedTorrent(QTorrentHandle& h); - void deletedTorrent(QString hash); - void pausedTorrent(QTorrentHandle& h); - void resumedTorrent(QTorrentHandle& h); - void finishedTorrent(QTorrentHandle& h); - void fullDiskError(QTorrentHandle& h, QString msg); - void trackerError(QString hash, QString time, QString msg); - void trackerAuthenticationRequired(QTorrentHandle& h); - void newDownloadedTorrent(QString path, QString url); - void updateFileSize(QString hash); - void downloadFromUrlFailure(QString url, QString reason); - void torrentFinishedChecking(QTorrentHandle& h); - void metadataReceived(QTorrentHandle &h); - void torrentPaused(QTorrentHandle &h); +signals: + void addedTorrent(QTorrentHandle& h); + void deletedTorrent(QString hash); + void pausedTorrent(QTorrentHandle& h); + void resumedTorrent(QTorrentHandle& h); + void finishedTorrent(QTorrentHandle& h); + void fullDiskError(QTorrentHandle& h, QString msg); + void trackerError(QString hash, QString time, QString msg); + void trackerAuthenticationRequired(QTorrentHandle& h); + void newDownloadedTorrent(QString path, QString url); + void updateFileSize(QString hash); + void downloadFromUrlFailure(QString url, QString reason); + void torrentFinishedChecking(QTorrentHandle& h); + void metadataReceived(QTorrentHandle &h); + void torrentPaused(QTorrentHandle &h); }; #endif