X-Git-Url: http://git.maemo.org/git/?p=qtrapids;a=blobdiff_plain;f=src%2Fserver%2FTorrentSession.cpp;h=d9d2b95990eb55e52b650f9a8a8eb5fc87082184;hp=94c2465cef845b6b418b27e70ba341f2b8c8ccbd;hb=1d97c0a567b80cf9fc67791669debfe3aa9c4c84;hpb=b62e6b5309eb1954c6ea4e1522767a424d76f15d diff --git a/src/server/TorrentSession.cpp b/src/server/TorrentSession.cpp index 94c2465..d9d2b95 100644 --- a/src/server/TorrentSession.cpp +++ b/src/server/TorrentSession.cpp @@ -1,3 +1,20 @@ +/*************************************************************************** + * Copyright (C) 2010 by Ixonos Plc * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; version 2 of the License. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ #include "TorrentSession.hpp" #include "TorrentHandle.hpp" #include "AlertWaiterThread.hpp" @@ -23,22 +40,41 @@ TorrentSession::TorrentSession(QObject *parent, QSettings *settings) QDBusConnection dbus = QDBusConnection::sessionBus(); dbus.registerObject("/qtrapids", this); dbus.registerService("com.ixonos.qtrapids"); - + alertWaiter_->allAlerts(); connect(alertWaiter_, SIGNAL(alert()), this, SLOT(on_alert())); 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); + +} + + +TorrentSession::~TorrentSession() +{ + alertWaiter_->stop(); + alertWaiter_->wait(); + emit sessionTerminated(); } + 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()); } void TorrentSession::on_alert() @@ -49,13 +85,17 @@ void TorrentSession::on_alert() torrent_alert_t *ta = dynamic_cast (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; } @@ -70,7 +110,9 @@ void TorrentSession::on_alert() state.up_rate = handle.uploadRate(); state.seeds = handle.numSeeds(); state.leeches = handle.numLeeches(); - + state.total_size = handle.getTotalSize(); + state.total_done = handle.getTotalDone(); + ParamsMap_t params; emit alert(state, params); } @@ -122,10 +164,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 tiTmp = new libtorrent::torrent_info (boost::filesystem::path(new_torrent_fname.toStdString())); @@ -171,7 +219,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,4 +241,44 @@ void TorrentSession::removeTorrent(const QString &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(); +} + + +void TorrentSession::terminateSession() +{ + qDebug() << "Terminate called"; + // Emiting terminate() here causes the server application to quit in main() + emit terminate(); +} + } // namespace qtrapids