From 03c544d07d71deaabcdbcbdb68d6a08aa38eef5c Mon Sep 17 00:00:00 2001 From: rakshasa Date: Fri, 28 Mar 2008 16:47:17 +0000 Subject: [PATCH] * Added some more download commands that replace some of the start/stop/close wrapper functions in DownloadList. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1049 e378c898-3ddf-0310-93e7-cc216c733640 --- rak/functional_fun.h | 22 +++++++++ src/command_download.cc | 11 ++++- src/command_events.cc | 22 ++++++--- src/command_helpers.h | 6 +++ src/core/download.cc | 2 - src/core/download_factory.cc | 2 +- src/core/download_list.cc | 86 ------------------------------------ src/core/download_list.h | 7 --- src/core/scheduler.cc | 40 ++++++++--------- 9 files changed, 74 insertions(+), 124 deletions(-) diff --git a/rak/functional_fun.h b/rak/functional_fun.h index 79995058..0acb7c63 100644 --- a/rak/functional_fun.h +++ b/rak/functional_fun.h @@ -305,6 +305,22 @@ private: const Arg1 m_arg1; }; +template +class mem_fn1_b1_t : public function_base1 { +public: + typedef Result (Object::*Func)(Arg1, Arg2); + + mem_fn1_b1_t(Object* object, Func func, const Arg1 arg1) : m_object(object), m_func(func), m_arg1(arg1) {} + virtual ~mem_fn1_b1_t() {} + + virtual Result operator () (const Arg2 arg2) { return (m_object->*m_func)(m_arg1, arg2); } + +private: + Object* m_object; + Func m_func; + const Arg1 m_arg1; +}; + template class mem_fn1_b2_t : public function_base1 { public: @@ -516,6 +532,12 @@ bind_mem_fn(Object* object, Result (Object::*func)(Arg1), const Arg1 arg1) { return new mem_fn0_b1_t(object, func, arg1); } +template +inline function_base1* +bind_mem_fn(Object* object, Result (Object::*func)(Arg1, Arg2), const Arg1 arg1) { + return new mem_fn1_b1_t(object, func, arg1); +} + template inline function_base1* bind2_mem_fn(Object* object, Result (Object::*func)(Arg1, Arg2), const Arg2 arg2) { diff --git a/src/command_download.cc b/src/command_download.cc index 668b1676..f049e575 100644 --- a/src/command_download.cc +++ b/src/command_download.cc @@ -422,6 +422,7 @@ initialize_command_download() { ADD_CD_VOID("base_filename", &retrieve_d_base_filename); ADD_CD_STRING_UNI("name", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::name))); + // ????? ADD_CD_LIST_OBSOLETE("create_link", rak::bind_ptr_fn(&apply_d_change_link, 0)); ADD_CD_LIST_OBSOLETE("delete_link", rak::bind_ptr_fn(&apply_d_change_link, 1)); @@ -429,8 +430,12 @@ initialize_command_download() { ADD_CD_LIST("delete_link", rak::bind_ptr_fn(&apply_d_change_link, 1)); ADD_CD_V_VOID("delete_tied", &apply_d_delete_tied); - ADD_CD_F_VOID("start", rak::make_mem_fun(control->core()->download_list(), &core::DownloadList::start_normal)); - ADD_CD_F_VOID("stop", rak::make_mem_fun(control->core()->download_list(), &core::DownloadList::stop_normal)); + ADD_ANY_NONE("d.start", rak::bind_ptr_fn(&cmd_call, "d.set_hashing_failed=0 ;d.set_state=1 ;view.set_not_visible=stopped ;view.set_visible=started")); + ADD_ANY_NONE("d.stop", rak::bind_ptr_fn(&cmd_call, "d.set_state=0 ;view.set_visible=stopped ;view.set_not_visible=started")); + ADD_ANY_NONE("d.try_start", rak::bind_ptr_fn(&cmd_call, "branch=\"or={d.get_hashing_failed=,d.get_ignore_commands=}\",{},{d.set_state=1,view.set_not_visible=stopped,view.set_visible=started}")); + ADD_ANY_NONE("d.try_stop", rak::bind_ptr_fn(&cmd_call, "branch=d.get_ignore_commands=, {}, {d.set_state=0, view.set_visible=stopped, view.set_not_visible=started}")); + ADD_ANY_NONE("d.try_close", rak::bind_ptr_fn(&cmd_call, "branch=d.get_ignore_commands=, {}, {d.set_state=0, view.set_visible=stopped, view.set_not_visible=started, d.close=}")); + ADD_CD_F_VOID("resume", rak::make_mem_fun(control->core()->download_list(), &core::DownloadList::resume_default)); ADD_CD_F_VOID("pause", rak::make_mem_fun(control->core()->download_list(), &core::DownloadList::pause_default)); ADD_CD_F_VOID("open", rak::make_mem_fun(control->core()->download_list(), &core::DownloadList::open_throw)); @@ -488,6 +493,8 @@ initialize_command_download() { ADD_CD_STRING_BI("connection_current", std::ptr_fun(&apply_d_connection_type), std::ptr_fun(&retrieve_d_connection_type)); + ADD_CD_VALUE_BI("hashing_failed", std::mem_fun(&core::Download::set_hash_failed), std::mem_fun(&core::Download::is_hash_failed)); + // This command really needs to be improved, so we have proper // logging support. ADD_CD_STRING_BI("message", std::mem_fun(&core::Download::set_message), std::mem_fun(&core::Download::message)); diff --git a/src/command_events.cc b/src/command_events.cc index e4c0e9d3..8febed4d 100644 --- a/src/command_events.cc +++ b/src/command_events.cc @@ -107,11 +107,13 @@ apply_on_ratio(int action, const torrent::Object& rawArgs) { !(maxRatio > 0 && totalUpload * 100 > totalDone * maxRatio)) continue; - bool success; + bool success = true; switch (action) { - case core::DownloadList::SLOTS_CLOSE: success = downloadList->close_try(current); break; - case core::DownloadList::SLOTS_STOP: success = downloadList->stop_try(current); break; +// case core::DownloadList::SLOTS_CLOSE: success = downloadList->close_try(current); break; +// case core::DownloadList::SLOTS_STOP: success = downloadList->stop_try(current); break; + case core::DownloadList::SLOTS_CLOSE: rpc::parse_command_single(rpc::make_target(current), "d.try_close="); break; + case core::DownloadList::SLOTS_STOP: rpc::parse_command_single(rpc::make_target(current), "d.try_stop="); break; default: success = false; break; } @@ -137,7 +139,7 @@ apply_start_tied() { const std::string& tiedToFile = rpc::call_command_string("d.get_tied_to_file", rpc::make_target(*itr)); if (!tiedToFile.empty() && fs.update(rak::path_expand(tiedToFile))) - control->core()->download_list()->start_try(*itr); + rpc::parse_command_single(rpc::make_target(*itr), "d.try_start="); } return torrent::Object(); @@ -153,7 +155,7 @@ apply_stop_untied() { const std::string& tiedToFile = rpc::call_command_string("d.get_tied_to_file", rpc::make_target(*itr)); if (!tiedToFile.empty() && !fs.update(rak::path_expand(tiedToFile))) - control->core()->download_list()->stop_try(*itr); + rpc::parse_command_single(rpc::make_target(*itr), "d.try_stop="); } return torrent::Object(); @@ -166,7 +168,7 @@ apply_close_untied() { const std::string& tiedToFile = rpc::call_command_string("d.get_tied_to_file", rpc::make_target(*itr)); if (rpc::call_command_value("d.get_ignore_commands", rpc::make_target(*itr)) == 0 && !tiedToFile.empty() && !fs.update(rak::path_expand(tiedToFile))) - control->core()->download_list()->close(*itr); + rpc::parse_command_single(rpc::make_target(*itr), "d.try_close="); } return torrent::Object(); @@ -332,6 +334,14 @@ d_multicall(const torrent::Object& rawArgs) { return resultRaw; } +torrent::Object +cmd_call(const char* cmd, rpc::target_type target, const torrent::Object& rawArgs) { + rpc::parse_command_multiple(target, cmd); + + return torrent::Object(); +} + + void initialize_command_events() { core::DownloadList* downloadList = control->core()->download_list(); diff --git a/src/command_helpers.h b/src/command_helpers.h index 012d3067..865d6022 100644 --- a/src/command_helpers.h +++ b/src/command_helpers.h @@ -81,6 +81,8 @@ add_variable(const char* getKey, const char* setKey, const char* defaultSetKey, rpc::Command::generic_slot getSlot, rpc::Command::generic_slot setSlot, const torrent::Object& defaultObject); +extern torrent::Object cmd_call(const char* cmd, rpc::target_type target, const torrent::Object& rawArgs); + #define ADD_VARIABLE_BOOL(key, defaultValue) \ add_variable("get_" key, "set_" key, key, &rpc::CommandVariable::get_bool, &rpc::CommandVariable::set_bool, (int64_t)defaultValue); @@ -171,6 +173,10 @@ add_variable(key, NULL, NULL, &rpc::CommandVariable::get_string, NULL, std::stri #define ADD_COMMAND_NONE_L(key, slot) \ ADD_COMMAND_SLOT(key, call_unknown, slot, "A:", "") +// +// NEW COMMAND MACROS +// + // // DOWNLOAD RELATED COMMANDS // diff --git a/src/core/download.cc b/src/core/download.cc index 4ac5d96e..42520742 100644 --- a/src/core/download.cc +++ b/src/core/download.cc @@ -156,8 +156,6 @@ Download::receive_chunk_failed(__UNUSED uint32_t idx) { void Download::set_root_directory(const std::string& path) { - control->core()->push_log_std("Pushed " + path); - torrent::FileList* fileList = m_download.file_list(); control->core()->download_list()->close_directly(this); diff --git a/src/core/download_factory.cc b/src/core/download_factory.cc index d4c3cd7c..c8ab0601 100644 --- a/src/core/download_factory.cc +++ b/src/core/download_factory.cc @@ -273,7 +273,7 @@ DownloadFactory::receive_success() { } else { // Use the state thingie here, move below. if (m_start) - m_manager->download_list()->start_normal(download); + rpc::parse_command_single(rpc::make_target(download), "d.start="); m_manager->download_store()->save(download); } diff --git a/src/core/download_list.cc b/src/core/download_list.cc index c7ebcb9e..67efc17a 100644 --- a/src/core/download_list.cc +++ b/src/core/download_list.cc @@ -245,26 +245,6 @@ DownloadList::close(Download* download) { } } -bool -DownloadList::close_try(Download* download) { - try { - if (rpc::call_command_value("d.get_ignore_commands", rpc::make_target(download)) != 0) - return false; - - rpc::call_command("d.set_state", (int64_t)0, rpc::make_target(download)); - - (*control->view_manager()->find_throw("started"))->set_not_visible(download); - (*control->view_manager()->find_throw("stopped"))->set_visible(download); - - close_throw(download); - return true; - - } catch (torrent::local_error& e) { - control->core()->push_log(e.what()); - return false; - } -} - void DownloadList::close_directly(Download* download) { if (download->download()->is_active()) { @@ -318,72 +298,6 @@ DownloadList::close_throw(Download* download) { std::for_each(slot_map_close().begin(), slot_map_close().end(), download_list_call(download)); } -void -DownloadList::start_normal(Download* download) { - check_contains(download); - - // Clear hash failed as we're doing a manual start and want to try - // hashing again. - download->set_hash_failed(false); - rpc::call_command("d.set_state", (int64_t)1, rpc::make_target(download)); - - (*control->view_manager()->find_throw("stopped"))->set_not_visible(download); - (*control->view_manager()->find_throw("started"))->set_visible(download); - -// try { -// rpc::parse_command_multiple(rpc::make_target(download), "d.set_state=1 ;view.set_not_visible=stopped ;view.set_visible=started"); -// } catch (torrent::local_error& e) { -// control->core()->push_log(e.what()); -// } -} - -bool -DownloadList::start_try(Download* download) { - check_contains(download); - - // Also don't start if the state is one of those that indicate we - // were manually stopped? - - if (download->is_hash_failed() || rpc::call_command_value("d.get_ignore_commands", rpc::make_target(download)) != 0) - return false; - - // Don't clear the hash failed as this function is used by scripts, - // etc. - rpc::call_command("d.set_state", (int64_t)1, rpc::make_target(download)); - - (*control->view_manager()->find_throw("stopped"))->set_not_visible(download); - (*control->view_manager()->find_throw("started"))->set_visible(download); - -// rpc::parse_command_multiple(rpc::make_target(download), "d.set_state=1 ;view.set_not_visible=stopped ;view.set_visible=started"); - - return true; -} - -void -DownloadList::stop_normal(Download* download) { - check_contains(download); - - rpc::call_command("d.set_state", (int64_t)0, rpc::make_target(download)); - - (*control->view_manager()->find_throw("started"))->set_not_visible(download); - (*control->view_manager()->find_throw("stopped"))->set_visible(download); -} - -bool -DownloadList::stop_try(Download* download) { - check_contains(download); - - if (rpc::call_command_value("d.get_ignore_commands", rpc::make_target(download)) != 0) - return false; - - rpc::call_command("d.set_state", (int64_t)0, rpc::make_target(download)); - - (*control->view_manager()->find_throw("started"))->set_not_visible(download); - (*control->view_manager()->find_throw("stopped"))->set_visible(download); - - return true; -} - void DownloadList::resume(Download* download, int flags) { check_contains(download); diff --git a/src/core/download_list.h b/src/core/download_list.h index 87585204..6c3de33f 100644 --- a/src/core/download_list.h +++ b/src/core/download_list.h @@ -102,17 +102,10 @@ public: void open_throw(Download* d); void close(Download* d); - bool close_try(Download* d); void close_directly(Download* d); void close_quick(Download* d); void close_throw(Download* d); - void start_normal(Download* d); - bool start_try(Download* d); - - void stop_normal(Download* d); - bool stop_try(Download* d); - void resume(Download* d, int flags = 0); void pause(Download* d, int flags = 0); diff --git a/src/core/scheduler.cc b/src/core/scheduler.cc index f7402066..1bedebcb 100644 --- a/src/core/scheduler.cc +++ b/src/core/scheduler.cc @@ -71,37 +71,37 @@ Scheduler::active() const { void Scheduler::update() { - size_type curActive = active(); +// size_type curActive = active(); // size_type curInactive = m_view->size() - curActive; // Hmm... Perhaps we should use a more complex sorting thingie. - m_view->sort(); +// 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); +// size_type target = m_maxActive - std::min(m_cycle, m_maxActive); - for (View::iterator itr = m_view->begin_visible(), last = m_view->end_visible(); curActive > target; ++itr) { - if (itr == last) - throw torrent::internal_error("Scheduler::update() loop bork."); +// for (View::iterator itr = m_view->begin_visible(), last = m_view->end_visible(); curActive > target; ++itr) { +// if (itr == last) +// throw torrent::internal_error("Scheduler::update() loop bork."); - if ((*itr)->is_active()) { - m_downloadList->pause(*itr); - --curActive; - } - } +// if ((*itr)->is_active()) { +// m_downloadList->pause(*itr); +// --curActive; +// } +// } - m_view->sort(); +// m_view->sort(); - for (View::iterator itr = m_view->begin_visible(), last = m_view->end_visible(); curActive < m_maxActive; ++itr) { - if (itr == last) - throw torrent::internal_error("Scheduler::update() loop bork."); +// for (View::iterator itr = m_view->begin_visible(), last = m_view->end_visible(); curActive < m_maxActive; ++itr) { +// if (itr == last) +// throw torrent::internal_error("Scheduler::update() loop bork."); - if (!(*itr)->is_active()) { - m_downloadList->start_try(*itr); - ++curActive; - } - } +// if (!(*itr)->is_active()) { +// m_downloadList->start_try(*itr); +// ++curActive; +// } +// } } }