From a864d5938d3e4c0bba32a8ee73ba7c9b775c54b1 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Tue, 1 May 2007 19:00:17 +0000 Subject: [PATCH] * More commands moved to the new system. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@897 e378c898-3ddf-0310-93e7-cc216c733640 --- rak/functional.h | 21 +++++++++++ src/Makefile.am | 1 + src/command_helpers.cc | 8 ++++ src/command_helpers.h | 21 ++++++++--- src/command_local.cc | 74 +++++++++++++++++++++++++++++++++++++ src/command_network.cc | 18 ++++----- src/option_handler_rules.cc | 31 ---------------- src/utils/command_slot.cc | 26 +++++++++++++ src/utils/command_slot.h | 35 ++++++++++++++++++ 9 files changed, 187 insertions(+), 48 deletions(-) create mode 100644 src/command_local.cc diff --git a/rak/functional.h b/rak/functional.h index 97a40f93..8b8ba9c7 100644 --- a/rak/functional.h +++ b/rak/functional.h @@ -457,6 +457,23 @@ bind2nd(const Operation& op, const Type& val) { // be replaced by TR1 stuff later. Requires an object to bind, instead // of using a seperate functor for that. +template +class ptr_fun0 { +public: + typedef Ret result_type; + typedef Ret (*Function)(); + + ptr_fun0() {} + ptr_fun0(Function f) : m_function(f) {} + + bool is_valid() const { return m_function; } + + Ret operator () () { return m_function(); } + +private: + Function m_function; +}; + template class mem_fun0 { public: @@ -569,6 +586,10 @@ private: Function m_function; }; +template +inline ptr_fun0 +ptr_fun(Ret (*f)()) { return ptr_fun0(f); } + template inline mem_fun0 make_mem_fun(Object* o, Ret (Object::*f)()) { diff --git a/src/Makefile.am b/src/Makefile.am index 65b8871e..13b1bead 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,6 +20,7 @@ rtorrent_SOURCES = \ command_events.cc \ command_helpers.cc \ command_helpers.h \ + command_local.cc \ command_network.cc \ command_ui.cc \ command_scheduler.cc \ diff --git a/src/command_helpers.cc b/src/command_helpers.cc index 0eb2efd7..bde9fe98 100644 --- a/src/command_helpers.cc +++ b/src/command_helpers.cc @@ -50,12 +50,20 @@ utils::CommandSlot* commandSlotsItr = commandSlots; utils::CommandVariable commandVariables[COMMAND_VARIABLES_SIZE]; utils::CommandVariable* commandVariablesItr = commandVariables; +void initialize_variables(); +void initialize_download_variables(); +void initialize_command_events(); +void initialize_command_local(); +void initialize_command_network(); +void initialize_command_ui(); + void initialize_commands() { initialize_variables(); initialize_download_variables(); initialize_command_events(); initialize_command_network(); + initialize_command_local(); initialize_command_ui(); #ifdef ADDING_COMMANDS diff --git a/src/command_helpers.h b/src/command_helpers.h index 1c38a0c9..36b906b5 100644 --- a/src/command_helpers.h +++ b/src/command_helpers.h @@ -56,12 +56,6 @@ extern utils::CommandSlot* commandSlotsItr; extern utils::CommandVariable commandVariables[COMMAND_VARIABLES_SIZE]; extern utils::CommandVariable* commandVariablesItr; -void initialize_variables(); -void initialize_download_variables(); -void initialize_command_events(); -void initialize_command_network(); -void initialize_command_ui(); - void initialize_commands(); void @@ -85,4 +79,19 @@ add_variable("get_" key, "set_" key, key, &utils::CommandVariable::get_string, & #define ADD_COMMAND_COPY(key, function) \ variables->insert(key, (commandSlotsItr - 1), &utils::CommandSlot::function, utils::VariableMap::flag_dont_delete); +#define ADD_COMMAND_VALUE_TRI(key, set, get) \ + ADD_COMMAND_SLOT(key, call_value, utils::object_value_fn(set)) \ + ADD_COMMAND_COPY("set_" key, call_value) \ + ADD_COMMAND_SLOT("get_" key, call_unknown, utils::object_void_fn(get)) + +#define ADD_COMMAND_VALUE_TRI_KB(key, set, get) \ + ADD_COMMAND_SLOT(key, call_value_kb, utils::object_value_fn(set)) \ + ADD_COMMAND_COPY("set_" key, call_value) \ + ADD_COMMAND_SLOT("get_" key, call_unknown, utils::object_void_fn(get)) + +#define ADD_COMMAND_STRING_TRI(key, set, get) \ + ADD_COMMAND_SLOT(key, call_string, utils::object_string_fn(set)) \ + ADD_COMMAND_COPY("set_" key, call_string) \ + ADD_COMMAND_SLOT("get_" key, call_unknown, utils::object_void_fn(get)) + #endif diff --git a/src/command_local.cc b/src/command_local.cc new file mode 100644 index 00000000..52965ce0 --- /dev/null +++ b/src/command_local.cc @@ -0,0 +1,74 @@ +// 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 +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#include "config.h" + +#include +#include +#include +//#include + +#include "core/download_list.h" +#include "core/manager.h" +#include "utils/command_slot.h" +#include "utils/command_variable.h" +#include "utils/variable_map.h" + +#include "globals.h" +#include "control.h" +#include "command_helpers.h" + +typedef torrent::ChunkManager CM_t; + +void +initialize_command_local() { + utils::VariableMap* variables = control->variable(); +// core::DownloadList* downloadList = control->core()->download_list(); + torrent::ChunkManager* chunkManager = torrent::chunk_manager(); + + ADD_VARIABLE_VALUE("max_file_size", -1); + ADD_VARIABLE_VALUE("split_file_size", -1); + ADD_VARIABLE_STRING("split_suffix", ".part"); + + ADD_COMMAND_VALUE_TRI("max_memory_usage", rak::make_mem_fun(chunkManager, &CM_t::set_max_memory_usage), rak::make_mem_fun(chunkManager, &CM_t::max_memory_usage)); + ADD_COMMAND_VALUE_TRI("safe_sync", rak::make_mem_fun(chunkManager, &CM_t::set_safe_sync), rak::make_mem_fun(chunkManager, &CM_t::safe_sync)); + ADD_COMMAND_VALUE_TRI("timeout_sync", rak::make_mem_fun(chunkManager, &CM_t::set_timeout_sync), rak::make_mem_fun(chunkManager, &CM_t::timeout_sync)); + ADD_COMMAND_VALUE_TRI("timeout_safe_sync", rak::make_mem_fun(chunkManager, &CM_t::set_timeout_safe_sync), rak::make_mem_fun(chunkManager, &CM_t::timeout_safe_sync)); + + ADD_COMMAND_VALUE_TRI("preload_type", rak::make_mem_fun(chunkManager, &CM_t::set_preload_type), rak::make_mem_fun(chunkManager, &CM_t::preload_type)); + ADD_COMMAND_VALUE_TRI("preload_min_size", rak::make_mem_fun(chunkManager, &CM_t::set_preload_min_size), rak::make_mem_fun(chunkManager, &CM_t::preload_min_size)); + ADD_COMMAND_VALUE_TRI_KB("preload_required_rate", rak::make_mem_fun(chunkManager, &CM_t::set_preload_required_rate), rak::make_mem_fun(chunkManager, &CM_t::preload_required_rate)); +} diff --git a/src/command_network.cc b/src/command_network.cc index f20999f4..c66b1161 100644 --- a/src/command_network.cc +++ b/src/command_network.cc @@ -43,6 +43,7 @@ #include "core/download_list.h" #include "core/manager.h" +#include "ui/root.h" #include "utils/command_slot.h" #include "utils/command_variable.h" #include "utils/variable_map.h" @@ -137,21 +138,16 @@ initialize_command_network() { ADD_VARIABLE_VALUE("max_downloads_div", 0); + ADD_COMMAND_VALUE_TRI_KB("download_rate", rak::make_mem_fun(control->ui(), &ui::Root::set_down_throttle_i64), rak::ptr_fun(&torrent::down_throttle)); + ADD_COMMAND_VALUE_TRI_KB("upload_rate", rak::make_mem_fun(control->ui(), &ui::Root::set_up_throttle_i64), rak::ptr_fun(&torrent::up_throttle)); + ADD_VARIABLE_VALUE("tracker_numwant", -1); ADD_COMMAND_SLOT("encryption", call_list, rak::ptr_fn(&apply_encryption)); ADD_COMMAND_SLOT("tos", call_string, rak::ptr_fn(&apply_tos)); - ADD_COMMAND_SLOT("bind", call_string, utils::object_string_fn(rak::make_mem_fun(control->core(), &core::Manager::set_bind_address))) - ADD_COMMAND_COPY("set_bind", call_string) - ADD_COMMAND_SLOT("get_bind", call_unknown, utils::object_void_fn(rak::make_mem_fun(control->core(), &core::Manager::bind_address))) - - ADD_COMMAND_SLOT("ip", call_string, utils::object_string_fn(rak::make_mem_fun(control->core(), &core::Manager::set_local_address))) - ADD_COMMAND_COPY("set_ip", call_string) - ADD_COMMAND_SLOT("get_ip", call_unknown, utils::object_void_fn(rak::make_mem_fun(control->core(), &core::Manager::local_address))) - - ADD_COMMAND_SLOT("proxy_address", call_string, utils::object_string_fn(rak::make_mem_fun(control->core(), &core::Manager::set_proxy_address))) - ADD_COMMAND_COPY("set_proxy_address", call_string) - ADD_COMMAND_SLOT("get_proxy_address", call_unknown, utils::object_void_fn(rak::make_mem_fun(control->core(), &core::Manager::proxy_address))) + ADD_COMMAND_STRING_TRI("bind", rak::make_mem_fun(control->core(), &core::Manager::set_bind_address), rak::make_mem_fun(control->core(), &core::Manager::bind_address)); + ADD_COMMAND_STRING_TRI("ip", rak::make_mem_fun(control->core(), &core::Manager::set_local_address), rak::make_mem_fun(control->core(), &core::Manager::local_address)); + ADD_COMMAND_STRING_TRI("proxy_address", rak::make_mem_fun(control->core(), &core::Manager::set_proxy_address), rak::make_mem_fun(control->core(), &core::Manager::proxy_address)); } diff --git a/src/option_handler_rules.cc b/src/option_handler_rules.cc index 75d05963..809c9fe6 100644 --- a/src/option_handler_rules.cc +++ b/src/option_handler_rules.cc @@ -217,11 +217,6 @@ initialize_variables() { variables->insert("max_downloads_global", new utils::VariableValueSlot(rak::mem_fn(control->ui(), &ui::Root::max_downloads_global), rak::mem_fn(control->ui(), &ui::Root::set_max_downloads_global))); - variables->insert("download_rate", new utils::VariableValueSlot(rak::ptr_fn(&torrent::down_throttle), rak::mem_fn(control->ui(), &ui::Root::set_down_throttle_i64), - 0, (1 << 10))); - variables->insert("upload_rate", new utils::VariableValueSlot(rak::ptr_fn(&torrent::up_throttle), rak::mem_fn(control->ui(), &ui::Root::set_up_throttle_i64), - 0, (1 << 10))); - variables->insert("hash_max_tries", new utils::VariableValueSlot(rak::ptr_fn(&torrent::hash_max_tries), rak::ptr_fn(&torrent::set_hash_max_tries))); variables->insert("max_open_files", new utils::VariableValueSlot(rak::ptr_fn(&torrent::max_open_files), rak::ptr_fn(&torrent::set_max_open_files))); variables->insert("max_open_sockets", new utils::VariableValueSlot(rak::mem_fn(torrent::connection_manager(), &torrent::ConnectionManager::max_size), @@ -243,32 +238,6 @@ initialize_variables() { variables->insert("receive_buffer_size", new utils::VariableValueSlot(rak::mem_fn(torrent::connection_manager(), &torrent::ConnectionManager::receive_buffer_size), rak::mem_fn(torrent::connection_manager(), &torrent::ConnectionManager::set_receive_buffer_size))); - variables->insert("max_memory_usage", new utils::VariableValueSlot(rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::max_memory_usage), - rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::set_max_memory_usage))); - - variables->insert("safe_sync", new utils::VariableValueSlot(rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::safe_sync), - rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::set_safe_sync))); - - variables->insert("timeout_sync", new utils::VariableValueSlot(rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::timeout_sync), - rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::set_timeout_sync))); - - variables->insert("timeout_safe_sync", new utils::VariableValueSlot(rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::timeout_safe_sync), - rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::set_timeout_safe_sync))); - - variables->insert("preload_type", new utils::VariableValueSlot(rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::preload_type), - rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::set_preload_type))); - - variables->insert("preload_min_size", new utils::VariableValueSlot(rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::preload_min_size), - rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::set_preload_min_size))); - - variables->insert("preload_required_rate", new utils::VariableValueSlot(rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::preload_required_rate), - rak::mem_fn(torrent::chunk_manager(), &torrent::ChunkManager::set_preload_required_rate), - 0, (1 << 10))); - - ADD_VARIABLE_VALUE("max_file_size", -1); - ADD_VARIABLE_VALUE("split_file_size", -1); - ADD_VARIABLE_STRING("split_suffix", ".part"); - variables->insert("hash_read_ahead", new utils::VariableValueSlot(rak::ptr_fn(torrent::hash_read_ahead), rak::ptr_fn(&apply_hash_read_ahead))); variables->insert("hash_interval", new utils::VariableValueSlot(rak::ptr_fn(torrent::hash_interval), rak::ptr_fn(&apply_hash_interval))); diff --git a/src/utils/command_slot.cc b/src/utils/command_slot.cc index ed6fb4c6..383699d5 100644 --- a/src/utils/command_slot.cc +++ b/src/utils/command_slot.cc @@ -36,6 +36,8 @@ #include "config.h" +#include "utils/parse.h" + #include "command_slot.h" namespace utils { @@ -87,6 +89,30 @@ CommandSlot::call_list(Variable* rawVariable, const torrent::Object& rawArgs) { } } +const torrent::Object +CommandSlot::call_value_base(Variable* rawVariable, const torrent::Object& rawArgs, int base, int unit) { + CommandSlot* command = static_cast(rawVariable); + + const torrent::Object& arg = to_single_argument(rawArgs); + + switch (arg.type()) { + case torrent::Object::TYPE_VALUE: + return command->m_slot(arg); + + case torrent::Object::TYPE_STRING: + { + torrent::Object argValue(torrent::Object::TYPE_VALUE); + + if (!utils::parse_whole_value_nothrow(arg.as_string().c_str(), &argValue.as_value(), base, unit)) + throw torrent::input_error("Not a value."); + + return command->m_slot(argValue); + } + default: + throw torrent::input_error("Not a value."); + } +} + const torrent::Object CommandSlot::call_string(Variable* rawVariable, const torrent::Object& rawArgs) { CommandSlot* command = static_cast(rawVariable); diff --git a/src/utils/command_slot.h b/src/utils/command_slot.h index 40f2b8a6..63ba6eb6 100644 --- a/src/utils/command_slot.h +++ b/src/utils/command_slot.h @@ -74,6 +74,14 @@ public: static const torrent::Object call_list(Variable* rawVariable, const torrent::Object& args); static const torrent::Object call_string(Variable* rawVariable, const torrent::Object& args); + static const torrent::Object call_value_base(Variable* rawVariable, const torrent::Object& args, int base, int unit); + + static const torrent::Object call_value(Variable* rawVariable, const torrent::Object& args) { return call_value_base(rawVariable, args, 0, 1); } + static const torrent::Object call_value_kb(Variable* rawVariable, const torrent::Object& args) { return call_value_base(rawVariable, args, 0, 1 << 10); } + + template + static const torrent::Object call_value(Variable* rawVariable, const torrent::Object& args) { return call_value_base(rawVariable, args, base, unit); } + // static const torrent::Object& get_list(Variable* rawVariable, const torrent::Object& args); private: @@ -108,6 +116,32 @@ private: Func m_func; }; +template +class object_value_fn1_t : public rak::function_base1 { +public: + object_value_fn1_t(Func func) : m_func(func) {} + + virtual torrent::Object operator () (const torrent::Object& arg1) { return torrent::Object((int64_t)m_func(arg1.as_value())); } + +private: + Func m_func; +}; + +template +class object_value_fn1_t : public rak::function_base1 { +public: + object_value_fn1_t(Func func) : m_func(func) {} + + virtual torrent::Object operator () (const torrent::Object& arg1) { + m_func(arg1.as_value()); + + return torrent::Object(); + } + +private: + Func m_func; +}; + template class object_string_fn1_t : public rak::function_base1 { public: @@ -137,6 +171,7 @@ private: template object_void_fn_t* object_fn(Return (*func)(void)) { return new object_void_fn_t(func); } template object_void_fn_t* object_void_fn(Func func) { return new object_void_fn_t(func); } +template object_value_fn1_t* object_value_fn(Func func) { return new object_value_fn1_t(func); } template object_string_fn1_t* object_string_fn(Func func) { return new object_string_fn1_t(func); } }