mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-08-15 14:42:30 +00:00
* Added the framework for a download scheduler.
* Added "state_changed" variable to downloads that returns the last time the download called DownloadList::pause/resume. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@674 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
@@ -28,6 +28,8 @@ libsub_core_a_SOURCES = \
|
||||
poll_manager_epoll.h \
|
||||
poll_manager_select.cc \
|
||||
poll_manager_select.h \
|
||||
scheduler.cc \
|
||||
scheduler.h \
|
||||
view_downloads.cc \
|
||||
view_downloads.h \
|
||||
view_manager.cc \
|
||||
|
||||
@@ -60,8 +60,8 @@ Download::Download(download_type d) :
|
||||
m_chunksFailed(0) {
|
||||
|
||||
m_connTrackerSucceded = m_download.signal_tracker_succeded(sigc::bind(sigc::mem_fun(*this, &Download::receive_tracker_msg), ""));
|
||||
m_connTrackerFailed = m_download.signal_tracker_failed(sigc::mem_fun(*this, &Download::receive_tracker_msg));
|
||||
m_connStorageError = m_download.signal_storage_error(sigc::mem_fun(*this, &Download::receive_storage_error));
|
||||
m_connTrackerFailed = m_download.signal_tracker_failed(sigc::mem_fun(*this, &Download::receive_tracker_msg));
|
||||
m_connStorageError = m_download.signal_storage_error(sigc::mem_fun(*this, &Download::receive_storage_error));
|
||||
|
||||
m_download.signal_chunk_failed(sigc::mem_fun(*this, &Download::receive_chunk_failed));
|
||||
|
||||
@@ -73,6 +73,11 @@ Download::Download(download_type d) :
|
||||
m_variables.insert("state", new utils::VariableObject(bencode(), "rtorrent", "state", torrent::Object::TYPE_STRING));
|
||||
m_variables.insert("tied_to_file", new utils::VariableObject(bencode(), "rtorrent", "tied_to_file", torrent::Object::TYPE_STRING));
|
||||
|
||||
// The "state_changed" variable is required to be a valid unix time
|
||||
// value, it indicates the last time the torrent changed its state,
|
||||
// resume/pause.
|
||||
m_variables.insert("state_changed", new utils::VariableObject(bencode(), "rtorrent", "state_changed", torrent::Object::TYPE_VALUE));
|
||||
|
||||
m_variables.insert("directory", new utils::VariableStringSlot(rak::mem_fn(&m_fileList, &torrent::FileList::root_dir),
|
||||
rak::mem_fn(this, &Download::set_root_directory)));
|
||||
|
||||
|
||||
@@ -59,6 +59,7 @@ public:
|
||||
~Download();
|
||||
|
||||
bool is_open() const { return m_download.is_open(); }
|
||||
bool is_active() const { return m_download.is_active(); }
|
||||
inline bool is_done() const { return m_download.chunks_done() == m_download.chunks_total(); }
|
||||
|
||||
void start();
|
||||
|
||||
@@ -164,18 +164,7 @@ DownloadFactory::receive_success() {
|
||||
|
||||
torrent::Object* rtorrent = &root->get_key("rtorrent");
|
||||
|
||||
if (!rtorrent->has_key_string("state") ||
|
||||
(rtorrent->get_key("state").as_string() != "stopped" &&
|
||||
rtorrent->get_key("state").as_string() != "started"))
|
||||
rtorrent->insert_key("state", "stopped");
|
||||
|
||||
if (!rtorrent->has_key_string("tied_to_file"))
|
||||
rtorrent->insert_key("tied_to_file", std::string());
|
||||
|
||||
if (rtorrent->has_key_value("priority"))
|
||||
(*itr)->variable()->set("priority", rtorrent->get_key("priority").as_value() % 4);
|
||||
else
|
||||
(*itr)->variable()->set("priority", (int64_t)2);
|
||||
initialize_rtorrent(*itr, rtorrent);
|
||||
|
||||
// Move to 'rtorrent'.
|
||||
(*itr)->variable()->set("connection_leech", m_variables.get("connection_leech"));
|
||||
@@ -184,20 +173,9 @@ DownloadFactory::receive_success() {
|
||||
(*itr)->variable()->set("max_peers", control->variable()->get("max_peers"));
|
||||
(*itr)->variable()->set("max_uploads", control->variable()->get("max_uploads"));
|
||||
|
||||
if (rtorrent->has_key_value("key")) {
|
||||
(*itr)->tracker_list()->set_key(rtorrent->get_key("key").as_value());
|
||||
|
||||
} else {
|
||||
(*itr)->tracker_list()->set_key(rand() % (std::numeric_limits<uint32_t>::max() - 1) + 1);
|
||||
rtorrent->insert_key("key", (*itr)->tracker_list()->key());
|
||||
}
|
||||
|
||||
if (!control->variable()->get_value("use_udp_trackers"))
|
||||
(*itr)->enable_udp_trackers(false);
|
||||
|
||||
if (rtorrent->has_key_value("total_uploaded"))
|
||||
(*itr)->download()->up_rate()->set_total(rtorrent->get_key("total_uploaded").as_value());
|
||||
|
||||
if (m_session) {
|
||||
if (!rtorrent->has_key_string("directory"))
|
||||
(*itr)->variable()->set("directory", m_variables.get("directory"));
|
||||
@@ -236,4 +214,36 @@ DownloadFactory::receive_failed(const std::string& msg) {
|
||||
m_slotFinished();
|
||||
}
|
||||
|
||||
void
|
||||
DownloadFactory::initialize_rtorrent(Download* download, torrent::Object* rtorrent) {
|
||||
if (!rtorrent->has_key_string("state") ||
|
||||
(rtorrent->get_key("state").as_string() != "stopped" && rtorrent->get_key("state").as_string() != "started")) {
|
||||
rtorrent->insert_key("state", "stopped");
|
||||
rtorrent->insert_key("state_changed", cachedTime.seconds());
|
||||
|
||||
} else if (!rtorrent->has_key_value("state_changed") ||
|
||||
rtorrent->get_key("state_changed").as_value() > cachedTime.seconds() || rtorrent->get_key("state_changed").as_value() == 0) {
|
||||
rtorrent->insert_key("state_changed", cachedTime.seconds());
|
||||
}
|
||||
|
||||
if (!rtorrent->has_key_string("tied_to_file"))
|
||||
rtorrent->insert_key("tied_to_file", std::string());
|
||||
|
||||
if (rtorrent->has_key_value("priority"))
|
||||
download->variable()->set("priority", rtorrent->get_key("priority").as_value() % 4);
|
||||
else
|
||||
download->variable()->set("priority", (int64_t)2);
|
||||
|
||||
if (rtorrent->has_key_value("key")) {
|
||||
download->tracker_list()->set_key(rtorrent->get_key("key").as_value());
|
||||
|
||||
} else {
|
||||
download->tracker_list()->set_key(rand() % (std::numeric_limits<uint32_t>::max() - 1) + 1);
|
||||
rtorrent->insert_key("key", download->tracker_list()->key());
|
||||
}
|
||||
|
||||
if (rtorrent->has_key_value("total_uploaded"))
|
||||
download->download()->up_rate()->set_total(rtorrent->get_key("total_uploaded").as_value());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -87,6 +87,8 @@ private:
|
||||
void receive_success();
|
||||
void receive_failed(const std::string& msg);
|
||||
|
||||
void initialize_rtorrent(Download* download, torrent::Object* rtorrent);
|
||||
|
||||
Manager* m_manager;
|
||||
std::iostream* m_stream;
|
||||
|
||||
|
||||
@@ -176,6 +176,8 @@ DownloadList::resume(Download* d) {
|
||||
// TODO: This can cause infinit looping?
|
||||
control->core()->hash_queue().insert(d, sigc::bind(sigc::mem_fun(*this, &DownloadList::resume), d));
|
||||
|
||||
d->variable()->set("state_changed", cachedTime.seconds());
|
||||
|
||||
} catch (torrent::local_error& e) {
|
||||
control->core()->push_log(e.what());
|
||||
}
|
||||
@@ -188,6 +190,8 @@ DownloadList::pause(Download* d) {
|
||||
if (d->download()->is_active())
|
||||
std::for_each(m_slotMapStop.begin(), m_slotMapStop.end(), download_list_call(d));
|
||||
|
||||
d->variable()->set("state_changed", cachedTime.seconds());
|
||||
|
||||
} catch (torrent::local_error& e) {
|
||||
control->core()->push_log(e.what());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
// rTorrent - BitTorrent client
|
||||
// Copyright (C) 2005-2006, Jari Sundell
|
||||
//
|
||||
// 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; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// 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
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations
|
||||
// including the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for
|
||||
// all of the code used other than OpenSSL. If you modify file(s)
|
||||
// with this exception, you may extend this exception to your version
|
||||
// of the file(s), but you are not obligated to do so. If you do not
|
||||
// wish to do so, delete this exception statement from your version.
|
||||
// If you delete this exception statement from all source files in the
|
||||
// program, then also delete it here.
|
||||
//
|
||||
// Contact: Jari Sundell <jaris@ifi.uio.no>
|
||||
//
|
||||
// Skomakerveien 33
|
||||
// 3185 Skoppum, NORWAY
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <rak/functional.h>
|
||||
|
||||
#include "download.h"
|
||||
#include "download_list.h"
|
||||
#include "scheduler.h"
|
||||
#include "view_downloads.h"
|
||||
|
||||
namespace core {
|
||||
|
||||
// Change to unlimited.
|
||||
Scheduler::Scheduler(DownloadList* dl) :
|
||||
m_view(NULL),
|
||||
m_downloadList(dl),
|
||||
|
||||
m_maxActive(2),
|
||||
m_cycle(1) {
|
||||
}
|
||||
|
||||
Scheduler::~Scheduler() {
|
||||
}
|
||||
|
||||
void
|
||||
Scheduler::set_view(ViewDownloads* view) {
|
||||
m_view = view;
|
||||
}
|
||||
|
||||
Scheduler::size_type
|
||||
Scheduler::active() const {
|
||||
return std::count_if(m_view->begin(), m_view->end(), std::mem_fun(&Download::is_active));
|
||||
}
|
||||
|
||||
void
|
||||
Scheduler::update() {
|
||||
size_type curActive = active();
|
||||
// size_type curInactive = m_view->size() - curActive;
|
||||
|
||||
// Hmm... Perhaps we should use a more complex sorting thingie.
|
||||
m_view->sort();
|
||||
|
||||
// Just a hack for now, need to take into consideration how many
|
||||
// inactive we can switch with.
|
||||
size_type target = m_maxActive - std::min(m_cycle, m_maxActive);
|
||||
|
||||
for (ViewDownloads::iterator itr = m_view->begin(), last = m_view->end(); curActive > target; ++itr) {
|
||||
if (itr == last)
|
||||
throw torrent::internal_error("Scheduler::update() loop bork.");
|
||||
|
||||
if ((*itr)->is_active()) {
|
||||
m_downloadList->pause(*itr);
|
||||
--curActive;
|
||||
}
|
||||
}
|
||||
|
||||
m_view->sort();
|
||||
|
||||
for (ViewDownloads::iterator itr = m_view->begin(), last = m_view->end(); curActive < m_maxActive; ++itr) {
|
||||
if (itr == last)
|
||||
throw torrent::internal_error("Scheduler::update() loop bork.");
|
||||
|
||||
if (!(*itr)->is_active()) {
|
||||
m_downloadList->resume(*itr);
|
||||
++curActive;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// rTorrent - BitTorrent client
|
||||
// Copyright (C) 2005-2006, Jari Sundell
|
||||
//
|
||||
// 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; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// 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
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations
|
||||
// including the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for
|
||||
// all of the code used other than OpenSSL. If you modify file(s)
|
||||
// with this exception, you may extend this exception to your version
|
||||
// of the file(s), but you are not obligated to do so. If you do not
|
||||
// wish to do so, delete this exception statement from your version.
|
||||
// If you delete this exception statement from all source files in the
|
||||
// program, then also delete it here.
|
||||
//
|
||||
// Contact: Jari Sundell <jaris@ifi.uio.no>
|
||||
//
|
||||
// Skomakerveien 33
|
||||
// 3185 Skoppum, NORWAY
|
||||
|
||||
#ifndef RTORRENT_CORE_SCHEDULER_H
|
||||
#define RTORRENT_CORE_SCHEDULER_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "view_downloads.h"
|
||||
|
||||
namespace core {
|
||||
|
||||
class DownloadList;
|
||||
class ViewDownloads;
|
||||
|
||||
class Scheduler {
|
||||
public:
|
||||
typedef uint32_t size_type;
|
||||
|
||||
static const size_type unlimited = ~size_type();
|
||||
|
||||
Scheduler(DownloadList* dl);
|
||||
~Scheduler();
|
||||
|
||||
void set_view(ViewDownloads* view);
|
||||
|
||||
size_type max_active() const { return m_maxActive; }
|
||||
void set_max_active(size_type v) { m_maxActive = v; }
|
||||
|
||||
size_type cycle() const { return m_cycle; }
|
||||
void set_cycle(size_type v) { m_cycle = v; }
|
||||
|
||||
size_type active() const;
|
||||
|
||||
void update();
|
||||
|
||||
private:
|
||||
ViewDownloads* m_view;
|
||||
DownloadList* m_downloadList;
|
||||
|
||||
size_type m_maxActive;
|
||||
size_type m_cycle;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -69,6 +69,19 @@ private:
|
||||
std::string m_value;
|
||||
};
|
||||
|
||||
class ViewSortVariableValue : public ViewSort {
|
||||
public:
|
||||
ViewSortVariableValue(const std::string& name) :
|
||||
m_name(name) {}
|
||||
|
||||
virtual bool less(Download* d1, Download* d2) const {
|
||||
return d1->variable()->get_value(m_name) < d2->variable()->get_value(m_name);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
};
|
||||
|
||||
class ViewSortReverse : public ViewSort {
|
||||
public:
|
||||
ViewSortReverse(ViewSort* s) : m_sort(s) {}
|
||||
@@ -87,11 +100,14 @@ ViewManager::ViewManager(DownloadList* dl) :
|
||||
|
||||
// m_sort["first"] = new ViewSortNot(new ViewSort());
|
||||
// m_sort["last"] = new ViewSort();
|
||||
m_sort["name"] = new ViewSortName();
|
||||
m_sort["name_reverse"] = new ViewSortReverse(new ViewSortName());
|
||||
m_sort["name"] = new ViewSortName();
|
||||
m_sort["name_reverse"] = new ViewSortReverse(new ViewSortName());
|
||||
|
||||
m_sort["started"] = new ViewSortVariable("state", "started");
|
||||
m_sort["stopped"] = new ViewSortVariable("state", "stopped");
|
||||
m_sort["started"] = new ViewSortVariable("state", "started");
|
||||
m_sort["stopped"] = new ViewSortVariable("state", "stopped");
|
||||
|
||||
m_sort["state_changed"] = new ViewSortVariableValue("state_changed");
|
||||
m_sort["state_changed_reverse"] = new ViewSortReverse(new ViewSortVariableValue("state_changed"));
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
Reference in New Issue
Block a user