- Added rate limit preferences to DBus client/server implementation
[qtrapids] / src / server / TorrentSession.cpp
index b5d6e9d..38140b1 100644 (file)
@@ -29,6 +29,13 @@ TorrentSession::TorrentSession(QObject *parent, QSettings *settings)
        alertWaiter_->start();
 
        loadState();
+       
+       // Lets force applying rate limits to local network also, at least for testing.
+       // Also, on the N900 device, rate limiting is necessary due to limited processign power
+       session_settings_t sessionSettings;
+       sessionSettings.ignore_limits_on_local_network = false;
+       btSession_.set_settings(sessionSettings);
+       
 }
 
 void TorrentSession::loadState()
@@ -36,7 +43,9 @@ void TorrentSession::loadState()
        TorrentDownloadInfo info;
        TorrentsStorage storage(*db_);
        while (storage.nextTorrent(info)) {
+#ifdef QTRAPIDS_DEBUG
                qDebug() << "adding " << info.path;
+#endif
                addTorrent_(info.path, info.download_path, ParamsMap_t(), true);
        }
        btSession_.listen_on(settings_->getListenPorts());
@@ -50,13 +59,17 @@ void TorrentSession::on_alert()
 
                torrent_alert_t *ta = dynamic_cast<torrent_alert_t*> (alertPtr.get());
 
+#ifdef QTRAPIDS_DEBUG
                qDebug()
-               << "QBittorrentSession::on_alert(): "
+               << "TorrentSession::on_alert(): "
                << QString::fromStdString(alertPtr->message());
+#endif
 
                if (ta) {
                        if (!ta->handle.is_valid()) {
+#ifdef QTRAPIDS_DEBUG
                                qDebug() << "handle is invalid";
+#endif
                                return;
                        }
 
@@ -123,10 +136,16 @@ void TorrentSession::addTorrent_(const QString &path, const QString &save_path
 
        QString new_torrent_fname(QDir(settings_->getTorrentsDir())
                                  .filePath(QFileInfo(path).fileName()));
+#ifdef QTRAPIDS_DEBUG
        qDebug() << "copy to " << new_torrent_fname;
-       torrent_file.copy(new_torrent_fname);
+#endif
 
+       torrent_file.copy(new_torrent_fname);
+       
+#ifdef QTRAPIDS_DEBUG
        qDebug() << "addTorrent: " << path << " save to " << save_path;
+#endif
+
        boost::intrusive_ptr<libtorrent::torrent_info> tiTmp
        = new libtorrent::torrent_info
        (boost::filesystem::path(new_torrent_fname.toStdString()));
@@ -172,7 +191,9 @@ void TorrentSession::removeTorrent(const QString &hash)
        torrents_t::iterator p = torrents_.find(hash);
 
        if (p == torrents_.end()) {
+#ifdef QTRAPIDS_DEBUG
                qDebug() << "Invalid request to remove torrent with hash " << hash;
+#endif
                return;
        }
        try {
@@ -191,11 +212,34 @@ void TorrentSession::removeTorrent(const QString &hash)
        db_->removeTorrent(hash);
 }
 
+
 void TorrentSession::setOptions(qtrapids::ParamsMap_t options)
 {
+       qtrapids::ParamsMapConstIterator_t end = options.end();
+       qtrapids::ParamsMapConstIterator_t tmpIter = options.find("net/downloadRate");
+       int rate = -1;
+
+       // Apply settings immediately to Bittorrent session:
+       // NOTE: QHash interface is not quite STL-like
+       
+       if (tmpIter != end) {
+               rate = tmpIter.value().toInt();
+               btSession_.set_download_rate_limit(rate);
+       }
+       
+       tmpIter = options.find("net/uploadRate");
+       if (tmpIter != end) {
+               rate = tmpIter.value().toInt();
+               btSession_.set_upload_rate_limit(rate);
+       }
+       
+       /// @todo Add more immediately applicable settings here, if needed.
+       
+       // Finally, save settings to persistent storage:
        settings_->setOptions(options);
 }
 
+
 qtrapids::ParamsMap_t TorrentSession::getOptions()
 {
        return settings_->getOptions();