* Replaced DownloadFactory's m_variables with a torrent::Object map.

* Starting work on moving the download commands to the new command
framework.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@901 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2007-05-16 14:39:03 +00:00
parent 032bae7030
commit 9fe72cb3b5
11 changed files with 414 additions and 56 deletions
+44 -1
View File
@@ -37,9 +37,52 @@
#include "config.h"
#include <functional>
#include <torrent/data/file.h>
#include <torrent/data/file_list.h>
#include "core/download.h"
#include "utils/command_download_slot.h"
#include "globals.h"
#include "control.h"
#include "command_helpers.h"
std::string
retrieve_d_base_path(core::Download* download) {
if (download->file_list()->is_multi_file())
return download->file_list()->root_dir();
else
return download->file_list()->at(0)->frozen_path();
}
std::string
retrieve_d_base_filename(core::Download* download) {
std::string base;
if (download->file_list()->is_multi_file())
base = download->file_list()->root_dir();
else
base = download->file_list()->at(0)->frozen_path();
std::string::size_type split = base.rfind('/');
if (split == std::string::npos)
return base;
else
return base.substr(split + 1);
}
#define ADD_COMMAND_DOWNLOAD_SLOT(key, function, slot, parm, doc) \
commandDownloadSlotsItr->set_slot(slot); \
variables->insert(key, commandDownloadSlotsItr++, NULL, &utils::CommandDownloadSlot::function, utils::VariableMap::flag_dont_delete | utils::VariableMap::flag_public_xmlrpc, parm, doc);
#define ADD_COMMAND_DOWNLOAD_VOID(key, slot) \
ADD_COMMAND_DOWNLOAD_SLOT(key, call_unknown, utils::object_d_fn(slot), "i:", "")
void
initialize_command_download() {
// utils::VariableMap* variables = control->variable();
utils::VariableMap* variables = control->download_variables();
ADD_COMMAND_DOWNLOAD_VOID("base_path", &retrieve_d_base_path);
ADD_COMMAND_DOWNLOAD_VOID("base_filename", &retrieve_d_base_filename);
}
+10 -7
View File
@@ -40,15 +40,18 @@
#include "utils/command_slot.h"
#include "utils/command_variable.h"
#include "utils/command_download_slot.h"
#include "globals.h"
#include "control.h"
#include "command_helpers.h"
utils::CommandSlot commandSlots[COMMAND_SLOTS_SIZE];
utils::CommandSlot* commandSlotsItr = commandSlots;
utils::CommandVariable commandVariables[COMMAND_VARIABLES_SIZE];
utils::CommandVariable* commandVariablesItr = commandVariables;
utils::CommandSlot commandSlots[COMMAND_SLOTS_SIZE];
utils::CommandSlot* commandSlotsItr = commandSlots;
utils::CommandVariable commandVariables[COMMAND_VARIABLES_SIZE];
utils::CommandVariable* commandVariablesItr = commandVariables;
utils::CommandDownloadSlot commandDownloadSlots[COMMAND_DOWNLOAD_SLOTS_SIZE];
utils::CommandDownloadSlot* commandDownloadSlotsItr = commandDownloadSlots;
void initialize_download_variables();
void initialize_command_download();
@@ -83,9 +86,9 @@ add_variable(const char* getKey, const char* setKey, const char* defaultSetKey,
utils::CommandVariable* variable = commandVariablesItr++;
variable->set_variable(defaultObject);
control->variable()->insert(getKey, variable, getSlot, utils::VariableMap::flag_dont_delete);
control->variable()->insert(setKey, variable, setSlot, utils::VariableMap::flag_dont_delete);
control->variable()->insert(getKey, variable, getSlot, NULL, utils::VariableMap::flag_dont_delete);
control->variable()->insert(setKey, variable, setSlot, NULL, utils::VariableMap::flag_dont_delete);
if (defaultSetKey)
control->variable()->insert(defaultSetKey, variable, setSlot, utils::VariableMap::flag_dont_delete);
control->variable()->insert(defaultSetKey, variable, setSlot, NULL, utils::VariableMap::flag_dont_delete);
}
+14 -10
View File
@@ -42,19 +42,23 @@
namespace utils {
class CommandSlot;
class CommandVariable;
class CommandDownloadSlot;
}
// By using a static array we avoid allocating the variables on the
// heap. This should reduce memory use and improve cache locality.
#define COMMAND_SLOTS_SIZE 100
#define COMMAND_VARIABLES_SIZE 100
#define COMMAND_SLOTS_SIZE 100
#define COMMAND_VARIABLES_SIZE 100
#define COMMAND_DOWNLOAD_SLOTS_SIZE 50
#define ADDING_COMMANDS
extern utils::CommandSlot commandSlots[COMMAND_SLOTS_SIZE];
extern utils::CommandSlot* commandSlotsItr;
extern utils::CommandVariable commandVariables[COMMAND_VARIABLES_SIZE];
extern utils::CommandVariable* commandVariablesItr;
extern utils::CommandSlot commandSlots[COMMAND_SLOTS_SIZE];
extern utils::CommandSlot* commandSlotsItr;
extern utils::CommandVariable commandVariables[COMMAND_VARIABLES_SIZE];
extern utils::CommandVariable* commandVariablesItr;
extern utils::CommandDownloadSlot commandDownloadSlots[COMMAND_DOWNLOAD_SLOTS_SIZE];
extern utils::CommandDownloadSlot* commandDownloadSlotsItr;
void initialize_commands();
@@ -74,17 +78,17 @@ add_variable("get_" key, "set_" key, key, &utils::CommandVariable::get_string, &
#define ADD_COMMAND_SLOT(key, function, slot, parm, doc) \
commandSlotsItr->set_slot(slot); \
variables->insert(key, commandSlotsItr++, &utils::CommandSlot::function, utils::VariableMap::flag_dont_delete | utils::VariableMap::flag_public_xmlrpc, parm, doc);
variables->insert(key, commandSlotsItr++, &utils::CommandSlot::function, NULL, utils::VariableMap::flag_dont_delete | utils::VariableMap::flag_public_xmlrpc, parm, doc);
#define ADD_COMMAND_SLOT_PRIVATE(key, function, slot) \
commandSlotsItr->set_slot(slot); \
variables->insert(key, commandSlotsItr++, &utils::CommandSlot::function, utils::VariableMap::flag_dont_delete);
variables->insert(key, commandSlotsItr++, &utils::CommandSlot::function, NULL, utils::VariableMap::flag_dont_delete);
#define ADD_COMMAND_COPY(key, function, parm, doc) \
variables->insert(key, (commandSlotsItr - 1), &utils::CommandSlot::function, utils::VariableMap::flag_dont_delete | utils::VariableMap::flag_public_xmlrpc, parm, doc);
variables->insert(key, (commandSlotsItr - 1), &utils::CommandSlot::function, NULL, utils::VariableMap::flag_dont_delete | utils::VariableMap::flag_public_xmlrpc, parm, doc);
#define ADD_COMMAND_COPY_PRIVATE(key, function) \
variables->insert(key, (commandSlotsItr - 1), &utils::CommandSlot::function, utils::VariableMap::flag_dont_delete);
variables->insert(key, (commandSlotsItr - 1), &utils::CommandSlot::function, NULL, utils::VariableMap::flag_dont_delete);
#define ADD_COMMAND_VALUE_TRI(key, set, get) \
ADD_COMMAND_SLOT_PRIVATE(key, call_value, utils::object_value_fn(set)) \
+3 -3
View File
@@ -78,7 +78,7 @@ DownloadFactory::DownloadFactory(const std::string& uri, Manager* m) :
m_variables["connection_leech"] = control->variable()->call_command_void("get_connection_leech");
m_variables["connection_seed"] = control->variable()->call_command_void("get_connection_seed");
m_variables["directory"] = control->variable()->call_command_void("directory");
m_variables["directory"] = control->variable()->call_command_void("get_directory");
m_variables["tied_to_file"] = torrent::Object((int64_t)false);
}
@@ -175,7 +175,7 @@ DownloadFactory::receive_success() {
// Move to 'rtorrent'.
download->set("connection_leech", m_variables["connection_leech"]);
download->set("connection_seed", m_variables["get_connection_seed"]);
download->set("connection_seed", m_variables["connection_seed"]);
download->set("max_uploads", control->variable()->call_command_void("get_max_uploads"));
download->set("min_peers", control->variable()->call_command_void("get_min_peers"));
@@ -206,7 +206,7 @@ DownloadFactory::receive_success() {
control->variable()->call_command_string("split_suffix"));
if (!rtorrent->has_key_string("directory"))
download->set("directory", m_variables["get_directory"]);
download->set("directory", m_variables["directory"]);
else
download->set("directory", rtorrent->get_key("directory"));
-27
View File
@@ -191,31 +191,6 @@ apply_d_delete_link(core::Download* download, const torrent::Object::list_type&
; // control->core()->push_log("delete_link failed: " + std::string(rak::error_number::current().c_str()));
}
std::string
retrieve_d_base_path(core::Download* download) {
if (download->file_list()->is_multi_file())
return download->file_list()->root_dir();
else
return download->file_list()->at(0)->frozen_path();
}
std::string
retrieve_d_base_filename(core::Download* download) {
std::string base;
if (download->file_list()->is_multi_file())
base = download->file_list()->root_dir();
else
base = download->file_list()->at(0)->frozen_path();
std::string::size_type split = base.rfind('/');
if (split == std::string::npos)
return base;
else
return base.substr(split + 1);
}
void
initialize_download_variables() {
utils::VariableMap* variables = control->download_variables();
@@ -244,8 +219,6 @@ initialize_download_variables() {
variables->insert("directory", new utils::VariableDownloadStringSlot(rak::ftor_fn1(rak::on(std::mem_fun(&core::Download::file_list), std::mem_fun(&torrent::FileList::root_dir))),
rak::ftor_fn2(std::mem_fun(&core::Download::set_root_directory))));
variables->insert("base_path", new utils::VariableDownloadStringSlot(rak::ptr_fn(&retrieve_d_base_path), NULL));
variables->insert("base_filename", new utils::VariableDownloadStringSlot(rak::ptr_fn(&retrieve_d_base_filename), NULL));
variables->insert("min_peers", var_d_value(&core::Download::download, &torrent::Download::peers_min, &torrent::Download::set_peers_min));
variables->insert("max_peers", var_d_value(&core::Download::download, &torrent::Download::peers_max, &torrent::Download::set_peers_max));
+2
View File
@@ -3,6 +3,8 @@ noinst_LIBRARIES = libsub_utils.a
libsub_utils_a_SOURCES = \
command_slot.cc \
command_slot.h \
command_download_slot.cc \
command_download_slot.h \
command_variable.cc \
command_variable.h \
directory.cc \
+143
View File
@@ -0,0 +1,143 @@
// rTorrent - BitTorrent client
// Copyright (C) 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 "utils/parse.h"
#include "command_download_slot.h"
namespace utils {
const torrent::Object
CommandDownloadSlot::call_unknown(Variable* rawVariable, core::Download* download, const torrent::Object& rawArgs) {
CommandDownloadSlot* command = static_cast<CommandDownloadSlot*>(rawVariable);
return command->m_slot(download, rawArgs);
}
const torrent::Object
CommandDownloadSlot::call_list(Variable* rawVariable, core::Download* download, const torrent::Object& rawArgs) {
CommandDownloadSlot* command = static_cast<CommandDownloadSlot*>(rawVariable);
// const torrent::Object& arg = to_single_argument(rawArgs);
// switch (arg.type()) {
// case torrent::Object::TYPE_VALUE:
// variable->m_variable = arg.as_value() ? (int64_t)1 : (int64_t)0;
// break;
// case torrent::Object::TYPE_STRING:
// // Move the checks into some is_true, is_false think in Variable.
// if (arg.as_string() == "yes" || arg.as_string() == "true")
// variable->m_variable = (int64_t)1;
// else if (arg.as_string() == "no" || arg.as_string() == "false")
// variable->m_variable = (int64_t)0;
// else
// throw torrent::input_error("String does not parse as a boolean.");
// break;
// default:
// throw torrent::input_error("Input is not a boolean.");
// }
// return variable->m_variable;
switch (rawArgs.type()) {
// case torrent::Object::TYPE_STRING:
// break;
case torrent::Object::TYPE_LIST:
return command->m_slot(download, rawArgs);
default:
throw torrent::input_error("Not a list.");
}
}
const torrent::Object
CommandDownloadSlot::call_value_base(Variable* rawVariable, core::Download* download, const torrent::Object& rawArgs, int base, int unit) {
CommandDownloadSlot* command = static_cast<CommandDownloadSlot*>(rawVariable);
const torrent::Object& arg = to_single_argument(rawArgs);
switch (arg.type()) {
case torrent::Object::TYPE_VALUE:
// Should shift this one too, so it gives the right unit.
return command->m_slot(download, 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(download, argValue);
}
default:
throw torrent::input_error("Not a value.");
}
}
const torrent::Object
CommandDownloadSlot::call_string(Variable* rawVariable, core::Download* download, const torrent::Object& rawArgs) {
CommandDownloadSlot* command = static_cast<CommandDownloadSlot*>(rawVariable);
const torrent::Object& arg = to_single_argument(rawArgs);
switch (arg.type()) {
// case torrent::Object::TYPE_VALUE:
// break;
case torrent::Object::TYPE_STRING:
return command->m_slot(download, arg);
break;
default:
throw torrent::input_error("Not a string.");
}
}
// const torrent::Object&
// CommandDownloadSlot::get_generic(Variable* rawVariable, const torrent::Object& args) {
// CommandVariable* variable = static_cast<CommandVariable*>(rawVariable);
// return variable->m_variable;
// }
}
+176
View File
@@ -0,0 +1,176 @@
// rTorrent - BitTorrent client
// Copyright (C) 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_UTILS_COMMAND_DOWNLOAD_SLOT_H
#define RTORRENT_UTILS_COMMAND_DOWNLOAD_SLOT_H
#include <functional>
#include <limits>
#include <inttypes.h>
#include <torrent/object.h>
#include <rak/functional_fun.h>
#include "variable.h"
namespace core {
class Download;
}
namespace utils {
class CommandDownloadSlot : public Variable {
public:
typedef rak::function2<torrent::Object, core::Download*, const torrent::Object&> slot_type;
CommandDownloadSlot() {}
CommandDownloadSlot(slot_type::base_type* s) {
m_slot.set(s);
}
void set_slot(slot_type::base_type* s) { m_slot.set(s); }
static const torrent::Object call_unknown(Variable* rawVariable, core::Download* download, const torrent::Object& args);
static const torrent::Object call_list(Variable* rawVariable, core::Download* download, const torrent::Object& args);
static const torrent::Object call_string(Variable* rawVariable, core::Download* download, const torrent::Object& args);
static const torrent::Object call_value_base(Variable* rawVariable, core::Download* download, const torrent::Object& args, int base, int unit);
static const torrent::Object call_value(Variable* rawVariable, core::Download* download, const torrent::Object& args) { return call_value_base(rawVariable, download, args, 0, 1); }
static const torrent::Object call_value_kb(Variable* rawVariable, core::Download* download, const torrent::Object& args) { return call_value_base(rawVariable, download, args, 0, 1 << 10); }
template <int base, int unit>
static const torrent::Object call_value(Variable* rawVariable, core::Download* download, const torrent::Object& args) { return call_value_base(rawVariable, download, args, base, unit); }
// static const torrent::Object& get_list(Variable* rawVariable, const torrent::Object& args);
private:
slot_type m_slot;
};
// Some slots that convert torrent::Object arguments to proper
// function calls.
template <typename Func, typename Result = typename Func::result_type>
class object_d_void_fn_t : public rak::function_base2<torrent::Object, core::Download*, const torrent::Object&> {
public:
object_d_void_fn_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (core::Download* download, const torrent::Object& arg1) { return torrent::Object(m_func(download)); }
private:
Func m_func;
};
// template <typename Func>
// class object_void_fn_t<Func, void> : public rak::function_base1<torrent::Object, const torrent::Object&> {
// public:
// object_void_fn_t(Func func) : m_func(func) {}
// virtual torrent::Object operator () (const torrent::Object& arg1) {
// m_func();
// return torrent::Object();
// }
// private:
// Func m_func;
// };
// template <typename Func, typename Result = typename Func::result_type>
// class object_value_fn1_t : public rak::function_base1<torrent::Object, const torrent::Object&> {
// 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 <typename Func>
// class object_value_fn1_t<Func, void> : public rak::function_base1<torrent::Object, const torrent::Object&> {
// 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 <typename Func, typename Result = typename Func::result_type>
// class object_string_fn1_t : public rak::function_base1<torrent::Object, const torrent::Object&> {
// public:
// object_string_fn1_t(Func func) : m_func(func) {}
// virtual torrent::Object operator () (const torrent::Object& arg1) { return torrent::Object(m_func(arg1.as_string())); }
// private:
// Func m_func;
// };
// template <typename Func>
// class object_string_fn1_t<Func, void> : public rak::function_base1<torrent::Object, const torrent::Object&> {
// public:
// object_string_fn1_t(Func func) : m_func(func) {}
// virtual torrent::Object operator () (const torrent::Object& arg1) {
// m_func(arg1.as_string());
// return torrent::Object();
// }
// private:
// Func m_func;
// };
template <typename Return> object_d_void_fn_t<Return (*)(core::Download*), Return>*
object_d_fn(Return (*func)(core::Download*)) {
return new object_d_void_fn_t<Return (*)(core::Download*), Return>(func);
}
// template <typename Func> object_void_fn_t<Func>* object_void_fn(Func func) { return new object_void_fn_t<Func>(func); }
// template <typename Func> object_value_fn1_t<Func>* object_value_fn(Func func) { return new object_value_fn1_t<Func>(func); }
// template <typename Func> object_string_fn1_t<Func>* object_string_fn(Func func) { return new object_string_fn1_t<Func>(func); }
}
#endif
-5
View File
@@ -38,7 +38,6 @@
#define RTORRENT_UTILS_COMMAND_SLOT_H
#include <functional>
#include <string>
#include <limits>
#include <inttypes.h>
#include <torrent/object.h>
@@ -48,10 +47,6 @@
namespace utils {
// The CommandSlot class uses union instead of creating multiple
// derived classes so that it is possible to create an array
// containing different slot types.
class CommandSlot : public Variable {
public:
typedef rak::function1<torrent::Object, const torrent::Object&> slot_type;
+19 -2
View File
@@ -62,14 +62,14 @@ VariableMap::~VariableMap() {
}
void
VariableMap::insert(key_type key, Variable* variable, generic_slot genericSlot, int flags,
VariableMap::insert(key_type key, Variable* variable, generic_slot genericSlot, download_slot downloadSlot, int flags,
const char* parm, const char* doc) {
iterator itr = base_type::find(key);
if (itr != base_type::end())
throw torrent::internal_error("VariableMap::insert(...) tried to insert an already existing key.");
base_type::insert(itr, value_type(key, variable_map_data_type(variable, genericSlot, NULL, flags, parm, doc)));
base_type::insert(itr, value_type(key, variable_map_data_type(variable, genericSlot, downloadSlot, flags, parm, doc)));
}
const VariableMap::mapped_type
@@ -250,4 +250,21 @@ VariableMap::call_command(key_type key, const mapped_type& arg) {
return itr->second.m_genericSlot(itr->second.m_variable, arg);
}
const VariableMap::mapped_type
VariableMap::call_command_d(key_type key, core::Download* download, const mapped_type& arg) {
const_iterator itr = base_type::find(key);
if (itr == base_type::end())
throw torrent::input_error("Variable \"" + std::string(key) + "\" does not exist.");
if (itr->second.m_downloadSlot == NULL) {
if (itr->second.m_genericSlot == NULL)
throw torrent::input_error("Variable does not have a generic slot.");
return itr->second.m_genericSlot(itr->second.m_variable, arg);
}
return itr->second.m_downloadSlot(itr->second.m_variable, download, arg);
}
}
+3 -1
View File
@@ -112,7 +112,7 @@ public:
// Allow NULL slot as a temporary compatibility hack.
void insert(key_type key, Variable* variable, generic_slot genericSlot = NULL, int flags = 0,
void insert(key_type key, Variable* variable, generic_slot genericSlot = NULL, download_slot downloadSlot = NULL, int flags = 0,
const char* parm = "", const char* doc = "");
// Consider uninlining the helper functions.
@@ -147,6 +147,8 @@ public:
void call_command_set_string(key_type key, const std::string& arg) { call_command(key, mapped_type(arg)); }
void call_command_set_std_string(const std::string& key, const std::string& arg) { call_command(key.c_str(), mapped_type(arg)); }
const mapped_type call_command_d(key_type key, core::Download* download, const mapped_type& arg);
private:
VariableMap(const VariableMap&);
void operator = (const VariableMap&);