* Added 'get_d_ratio', 'view_list', 'get_d_tracker_focus',

'get_d_tracker_size', etc.

* Added tracker commands.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@945 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2007-07-29 16:59:34 +00:00
parent 1232cd44cb
commit ac92b56f37
19 changed files with 527 additions and 81 deletions
+1
View File
@@ -24,6 +24,7 @@ rtorrent_SOURCES = \
command_helpers.h \
command_local.cc \
command_network.cc \
command_tracker.cc \
command_ui.cc \
control.cc \
control.h \
+11
View File
@@ -200,6 +200,14 @@ retrieve_d_priority_str(core::Download* download) {
}
}
torrent::Object
apply_d_ratio(core::Download* download) {
int64_t bytesDone = download->download()->bytes_done();
int64_t upTotal = download->download()->up_rate()->total();
return bytesDone > 0 ? (1000 * upTotal) / bytesDone : 0;
}
#define ADD_CD_SLOT(key, function, slot, parm, doc) \
commandDownloadSlotsItr->set_slot(slot); \
rpc::commands.insert_download(key, commandDownloadSlotsItr++, &rpc::CommandDownloadSlot::function, rpc::CommandMap::flag_dont_delete, parm, doc);
@@ -340,6 +348,7 @@ initialize_command_download() {
ADD_CD_VALUE_MEM_UNI("skip_total", &torrent::Download::mutable_skip_rate, &torrent::Rate::total);
ADD_CD_VALUE_UNI("bytes_done", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::bytes_done)));
ADD_CD_VALUE_UNI("ratio", std::ptr_fun(&apply_d_ratio));
ADD_CD_VALUE_UNI("chunks_hashed", rak::on(std::mem_fun(&core::Download::download), std::mem_fun(&torrent::Download::chunks_hashed)));
ADD_CD_VALUE_UNI("free_diskspace", rak::on(std::mem_fun(&core::Download::file_list), std::mem_fun(&torrent::FileList::free_diskspace)));
@@ -354,6 +363,8 @@ initialize_command_download() {
ADD_CD_VALUE_UNI("chunk_size", rak::on(std::mem_fun(&core::Download::file_list), std::mem_fun(&torrent::FileList::chunk_size)));
ADD_CD_VALUE_MEM_BI("tracker_numwant", &core::Download::tracker_list, &torrent::TrackerList::set_numwant, &torrent::TrackerList::numwant);
ADD_CD_VALUE_UNI("tracker_focus", rak::on(std::mem_fun(&core::Download::tracker_list), std::mem_fun(&torrent::TrackerList::focus)));
ADD_CD_VALUE_UNI("tracker_size", rak::on(std::mem_fun(&core::Download::tracker_list), std::mem_fun(&torrent::TrackerList::size)));
ADD_CD_STRING_BI("directory", std::mem_fun(&core::Download::set_root_directory), rak::on(std::mem_fun(&core::Download::file_list), std::mem_fun(&torrent::FileList::root_dir)));
ADD_CD_VALUE_BI("priority", std::mem_fun(&core::Download::set_priority), std::mem_fun(&core::Download::priority));
+2 -2
View File
@@ -249,7 +249,7 @@ apply_download_list(const torrent::Object& rawArgs) {
if (argsItr != args.end() && !argsItr->as_string().empty())
viewItr = viewManager->find((argsItr++)->as_string());
else
viewItr = viewManager->find("main");
viewItr = viewManager->find("default");
if (viewItr == viewManager->end())
throw torrent::input_error("Could not find view.");
@@ -339,5 +339,5 @@ initialize_command_events() {
ADD_COMMAND_VALUE_UN("close_low_diskspace", std::ptr_fun(&apply_close_low_diskspace));
ADD_COMMAND_LIST("download_list", rak::ptr_fn(&apply_download_list));
ADD_COMMAND_LIST("call_download", rak::ptr_fn(&apply_call_download));
// ADD_COMMAND_LIST("call_download", rak::ptr_fn(&apply_call_download));
}
-39
View File
@@ -36,15 +36,11 @@
#include "config.h"
//#include <functional>
//#include <rak/file_stat.h>
#include <rak/error_number.h>
#include <rak/path.h>
//#include <torrent/rate.h>
#include <torrent/data/file.h>
#include <torrent/data/file_list.h>
//#include "core/download.h"
#include "core/manager.h"
#include "rpc/command_slot.h"
#include "rpc/command_file_slot.h"
@@ -102,29 +98,6 @@ apply_f_path_depth(torrent::File* file) {
#define ADD_CF_VOID(key, slot) \
ADD_CF_SLOT_PUBLIC("get_f_" key, call_unknown, rpc::object_f_fn(slot), "i:", "")
/*
#define ADD_CF_V_VOID(key, slot) \
ADD_CF_SLOT_PUBLIC("d_" key, call_unknown, rpc::object_f_fn(slot), "i:", "")
#define ADD_CF_F_VOID(key, slot) \
ADD_CF_SLOT_PUBLIC("d_" key, call_unknown, rpc::object_void_f_fn(slot), "i:", "")
#define ADD_CF_LIST(key, slot) \
ADD_CF_SLOT_PUBLIC(key, call_list, slot, "i:", "")
#define ADD_CF_VARIABLE_VALUE(key, firstKey, secondKey) \
ADD_CF_SLOT_PUBLIC("get_f_" key, call_unknown, rpc::get_variable_f_fn(firstKey, secondKey), "i:", ""); \
ADD_CF_SLOT("set_f_" key, call_value, rpc::set_variable_f_fn(firstKey, secondKey), "i:i", "");
#define ADD_CF_VARIABLE_VALUE_PUBLIC(key, firstKey, secondKey) \
ADD_CF_SLOT_PUBLIC("get_f_" key, call_unknown, rpc::get_variable_f_fn(firstKey, secondKey), "i:", ""); \
ADD_CF_SLOT_PUBLIC("set_f_" key, call_value, rpc::set_variable_f_fn(firstKey, secondKey), "i:i", "");
#define ADD_CF_VARIABLE_STRING(key, firstKey, secondKey) \
ADD_CF_SLOT_PUBLIC("get_f_" key, call_unknown, rpc::get_variable_f_fn(firstKey, secondKey), "i:", ""); \
ADD_CF_SLOT("set_f_" key, call_string, rpc::set_variable_f_fn(firstKey, secondKey), "i:s", "");
*/
#define ADD_CF_VALUE_UNI(key, get) \
ADD_CF_SLOT_PUBLIC("get_f_" key, call_unknown, rpc::object_void_f_fn(get), "i:", "")
@@ -132,20 +105,9 @@ apply_f_path_depth(torrent::File* file) {
ADD_CF_SLOT_PUBLIC("set_f_" key, call_value, rpc::object_value_f_fn(set), "i:i", "") \
ADD_CF_SLOT_PUBLIC("get_f_" key, call_unknown, rpc::object_void_f_fn(get), "i:", "")
/*
#define ADD_CF_VALUE_MEM_BI(key, target, set, get) \
ADD_CF_VALUE_BI(key, rak::on2(std::mem_fun(target), std::mem_fun(set)), rak::on(std::mem_fun(target), std::mem_fun(get)));
*/
#define ADD_CF_STRING_UNI(key, get) \
ADD_CF_SLOT_PUBLIC("get_f_" key, call_unknown, rpc::object_void_f_fn(get), "s:", "")
/*
#define ADD_CF_STRING_BI(key, set, get) \
ADD_CF_SLOT_PUBLIC("set_f_" key, call_string, rpc::object_string_f_fn(set), "i:s", "") \
ADD_CF_SLOT_PUBLIC("get_f_" key, call_unknown, rpc::object_void_f_fn(get), "s:", "")
*/
void
initialize_command_file() {
ADD_CF_VALUE_UNI("is_created", std::mem_fun(&torrent::File::is_created));
@@ -163,7 +125,6 @@ initialize_command_file() {
ADD_CF_STRING_UNI("path", std::ptr_fun(&apply_f_path));
ADD_CF_STRING_UNI("path_components", std::ptr_fun(&apply_f_path_components));
// ADD_CF_STRING_UNI("path_depth", rak::on(std::const_mem_fun_t<const torrent::Path*, torrent::File>(&torrent::File::path), std::mem_fun(&torrent::Path::size)));
ADD_CF_STRING_UNI("path_depth", std::ptr_fun(&apply_f_path_depth));
ADD_CF_STRING_UNI("frozen_path", std::mem_fun(&torrent::File::frozen_path));
+9 -2
View File
@@ -42,6 +42,7 @@
#include "rpc/command_variable.h"
#include "rpc/command_download_slot.h"
#include "rpc/command_file_slot.h"
#include "rpc/command_tracker_slot.h"
#include "globals.h"
#include "control.h"
@@ -55,12 +56,15 @@ rpc::CommandDownloadSlot commandDownloadSlots[COMMAND_DOWNLOAD_SLOTS_SIZE];
rpc::CommandDownloadSlot* commandDownloadSlotsItr = commandDownloadSlots;
rpc::CommandFileSlot commandFileSlots[COMMAND_FILE_SLOTS_SIZE];
rpc::CommandFileSlot* commandFileSlotsItr = commandFileSlots;
rpc::CommandTrackerSlot commandTrackerSlots[COMMAND_TRACKER_SLOTS_SIZE];
rpc::CommandTrackerSlot* commandTrackerSlotsItr = commandTrackerSlots;
void initialize_command_download();
void initialize_command_events();
void initialize_command_file();
void initialize_command_local();
void initialize_command_network();
void initialize_command_tracker();
void initialize_command_ui();
void
@@ -71,17 +75,20 @@ initialize_commands() {
initialize_command_ui();
initialize_command_download();
initialize_command_file();
initialize_command_tracker();
#ifdef ADDING_COMMANDS
if (commandSlotsItr > commandSlots + COMMAND_SLOTS_SIZE ||
commandVariablesItr > commandVariables + COMMAND_VARIABLES_SIZE ||
commandDownloadSlotsItr > commandDownloadSlots + COMMAND_DOWNLOAD_SLOTS_SIZE ||
commandFileSlotsItr > commandFileSlots + COMMAND_FILE_SLOTS_SIZE)
commandFileSlotsItr > commandFileSlots + COMMAND_FILE_SLOTS_SIZE ||
commandTrackerSlotsItr > commandTrackerSlots + COMMAND_TRACKER_SLOTS_SIZE)
#else
if (commandSlotsItr != commandSlots + COMMAND_SLOTS_SIZE ||
commandVariablesItr != commandVariables + COMMAND_VARIABLES_SIZE ||
commandDownloadSlotsItr != commandDownloadSlots + COMMAND_DOWNLOAD_SLOTS_SIZE ||
commandFileSlotsItr != commandFileSlots + COMMAND_FILE_SLOTS_SIZE)
commandFileSlotsItr != commandFileSlots + COMMAND_FILE_SLOTS_SIZE ||
commandTrackerSlotsItr != commandTrackerSlots + COMMAND_TRACKER_SLOTS_SIZE)
#endif
throw torrent::internal_error("initialize_commands() static command array size mismatch.");
}
+8 -1
View File
@@ -44,6 +44,7 @@ namespace rpc {
class CommandVariable;
class CommandDownloadSlot;
class CommandFileSlot;
class CommandTrackerSlot;
}
// By using a static array we avoid allocating the variables on the
@@ -51,7 +52,8 @@ namespace rpc {
#define COMMAND_SLOTS_SIZE 100
#define COMMAND_VARIABLES_SIZE 100
#define COMMAND_DOWNLOAD_SLOTS_SIZE 100
#define COMMAND_FILE_SLOTS_SIZE 25
#define COMMAND_FILE_SLOTS_SIZE 20
#define COMMAND_TRACKER_SLOTS_SIZE 15
#define ADDING_COMMANDS
@@ -63,6 +65,8 @@ extern rpc::CommandDownloadSlot commandDownloadSlots[COMMAND_DOWNLOAD_SLOTS_SIZ
extern rpc::CommandDownloadSlot* commandDownloadSlotsItr;
extern rpc::CommandFileSlot commandFileSlots[COMMAND_FILE_SLOTS_SIZE];
extern rpc::CommandFileSlot* commandFileSlotsItr;
extern rpc::CommandTrackerSlot commandTrackerSlots[COMMAND_TRACKER_SLOTS_SIZE];
extern rpc::CommandTrackerSlot* commandTrackerSlotsItr;
void initialize_commands();
@@ -132,4 +136,7 @@ add_variable("get_" key, "set_" key, key, &rpc::CommandVariable::get_string, &rp
#define ADD_COMMAND_LIST(key, slot) \
ADD_COMMAND_SLOT(key, call_list, slot, "i:", "")
#define ADD_COMMAND_NONE_L(key, slot) \
ADD_COMMAND_SLOT(key, call_unknown, slot, "A:", "")
#endif
+17 -5
View File
@@ -137,12 +137,23 @@ apply_enable_trackers(int64_t arg) {
torrent::File*
xmlrpc_find_file(core::Download* download, uint32_t index) {
torrent::FileList* fileList = download->download()->file_list();
if (index >= fileList->size_files())
if (index >= download->file_list()->size_files())
return NULL;
return (*fileList)[index];
return (*download->file_list())[index];
}
// Ergh... time to update the Tracker API to allow proper ptrs.
torrent::Tracker*
xmlrpc_find_tracker(core::Download* download, uint32_t index) {
if (index >= download->tracker_list()->size())
return NULL;
// HACK ALERT!!!!
static torrent::Tracker hack;
hack = download->tracker_list()->get(index);
return &hack;
}
void
@@ -150,6 +161,7 @@ initialize_xmlrpc() {
rpc::xmlrpc.initialize();
rpc::xmlrpc.set_slot_find_download(rak::mem_fn(control->core()->download_list(), &core::DownloadList::find_hex_ptr));
rpc::xmlrpc.set_slot_find_file(rak::ptr_fn(&xmlrpc_find_file));
rpc::xmlrpc.set_slot_find_tracker(rak::ptr_fn(&xmlrpc_find_tracker));
unsigned int count = 0;
@@ -161,6 +173,7 @@ initialize_xmlrpc() {
case rpc::CommandMap::target_generic: rpc::xmlrpc.insert_command(itr->first, itr->second.m_parm, itr->second.m_doc, rpc::XmlRpc::call_generic); break;
case rpc::CommandMap::target_download: rpc::xmlrpc.insert_command(itr->first, itr->second.m_parm, itr->second.m_doc, rpc::XmlRpc::call_download); break;
case rpc::CommandMap::target_file: rpc::xmlrpc.insert_command(itr->first, itr->second.m_parm, itr->second.m_doc, rpc::XmlRpc::call_file); break;
case rpc::CommandMap::target_tracker: rpc::xmlrpc.insert_command(itr->first, itr->second.m_parm, itr->second.m_doc, rpc::XmlRpc::call_tracker); break;
default: throw torrent::internal_error("XMLRPC: Bad entry.");
}
@@ -252,7 +265,6 @@ apply_xmlrpc_dialect(const std::string& arg) {
void
initialize_command_network() {
// core::DownloadList* downloadList = control->core()->download_list();
torrent::ConnectionManager* cm = torrent::connection_manager();
core::CurlStack* httpStack = control->core()->get_poll_manager()->get_http_stack();
+97
View File
@@ -0,0 +1,97 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2007, 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 <rak/error_number.h>
#include <torrent/tracker.h>
#include "core/manager.h"
#include "rpc/command_slot.h"
#include "rpc/command_tracker_slot.h"
#include "globals.h"
#include "control.h"
#include "command_helpers.h"
void
apply_t_set_enabled(torrent::Tracker* tracker, int64_t state) {
if (state)
tracker->enable();
else
tracker->disable();
}
#define ADD_CT_SLOT(key, function, slot, parm, doc) \
commandTrackerSlotsItr->set_slot(slot); \
rpc::commands.insert_tracker(key, commandTrackerSlotsItr++, &rpc::CommandTrackerSlot::function, rpc::CommandMap::flag_dont_delete, parm, doc);
#define ADD_CT_SLOT_PUBLIC(key, function, slot, parm, doc) \
commandTrackerSlotsItr->set_slot(slot); \
rpc::commands.insert_tracker(key, commandTrackerSlotsItr++, &rpc::CommandTrackerSlot::function, rpc::CommandMap::flag_dont_delete | rpc::CommandMap::flag_public_xmlrpc, parm, doc);
#define ADD_CT_VOID(key, slot) \
ADD_CT_SLOT_PUBLIC("get_t_" key, call_unknown, rpc::object_t_fn(slot), "i:", "")
#define ADD_CT_VALUE_UNI(key, get) \
ADD_CT_SLOT_PUBLIC("get_t_" key, call_unknown, rpc::object_void_t_fn(get), "i:", "")
#define ADD_CT_VALUE_BI(key, set, get) \
ADD_CT_SLOT_PUBLIC("set_t_" key, call_value, rpc::object_value_t_fn(set), "i:i", "") \
ADD_CT_SLOT_PUBLIC("get_t_" key, call_unknown, rpc::object_void_t_fn(get), "i:", "")
#define ADD_CT_STRING_UNI(key, get) \
ADD_CT_SLOT_PUBLIC("get_t_" key, call_unknown, rpc::object_void_t_fn(get), "s:", "")
void
initialize_command_tracker() {
ADD_CT_STRING_UNI("url", std::mem_fun(&torrent::Tracker::url));
ADD_CT_VALUE_UNI("group", std::mem_fun(&torrent::Tracker::group));
ADD_CT_VALUE_UNI("type", std::mem_fun(&torrent::Tracker::tracker_type));
ADD_CT_STRING_UNI("id", std::mem_fun(&torrent::Tracker::tracker_id));
ADD_CT_VALUE_BI("enabled", std::ptr_fun(&apply_t_set_enabled), std::mem_fun(&torrent::Tracker::is_enabled));
ADD_CT_VALUE_UNI("is_open", std::mem_fun(&torrent::Tracker::is_open));
ADD_CT_VALUE_UNI("normal_interval", std::mem_fun(&torrent::Tracker::normal_interval));
ADD_CT_VALUE_UNI("min_interval", std::mem_fun(&torrent::Tracker::min_interval));
ADD_CT_VALUE_UNI("scrape_time_last", std::mem_fun(&torrent::Tracker::scrape_time_last));
ADD_CT_VALUE_UNI("scrape_complete", std::mem_fun(&torrent::Tracker::scrape_complete));
ADD_CT_VALUE_UNI("scrape_incomplete", std::mem_fun(&torrent::Tracker::scrape_incomplete));
ADD_CT_VALUE_UNI("scrape_downloaded", std::mem_fun(&torrent::Tracker::scrape_downloaded));
}
+12
View File
@@ -97,6 +97,17 @@ apply_view_sort(const torrent::Object& rawArgs) {
return torrent::Object();
}
torrent::Object
apply_view_list(const torrent::Object&) {
torrent::Object rawResult(torrent::Object::TYPE_LIST);
torrent::Object::list_type& result = rawResult.as_list();
for (core::ViewManager::const_iterator itr = control->view_manager()->begin(), last = control->view_manager()->end(); itr != last; itr++)
result.push_back((*itr)->name());
return rawResult;
}
torrent::Object
apply_print(const torrent::Object& rawArgs) {
char buffer[1024];
@@ -128,6 +139,7 @@ initialize_command_ui() {
ADD_VARIABLE_STRING("key_layout", "qwerty");
ADD_COMMAND_STRING("view_add", rpc::object_string_fn(rak::make_mem_fun(control->view_manager(), &core::ViewManager::insert_throw)));
ADD_COMMAND_NONE_L("view_list", rak::ptr_fn(&apply_view_list));
ADD_COMMAND_LIST("view_filter", rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_filter));
ADD_COMMAND_LIST("view_filter_on", rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_filter_on));
+2 -2
View File
@@ -159,10 +159,10 @@ print_download_info(char* first, char* last, core::Download* d) {
first = print_buffer(first, last, " ");
}
first = print_buffer(first, last, " [%c%c R: %3.2f",
first = print_buffer(first, last, " [%c%c R: %4.2f",
rpc::call_command_d_string("get_d_tied_to_file", d).empty() ? ' ' : 'T',
rpc::call_command_d_value("get_d_ignore_commands", d) == 0 ? ' ' : 'I',
d->download()->bytes_done() > 0 ? (double)(100 * d->download()->up_rate()->total() / d->download()->bytes_done()) / 100 : 0.0);
(double)rpc::call_command_d_value("get_d_ratio", d) / 1000.0);
if (d->priority() != 2)
first = print_buffer(first, last, " %s]", rpc::call_command_d_string("get_d_priority_str", d).c_str());
+1
View File
@@ -168,6 +168,7 @@ main(int argc, char** argv) {
rpc::parse_command_multiple(
// Currently not doing any sorting on main.
"view_add = main\n"
"view_add = default\n"
"view_add = name\n"
"view_sort_new = name,name\n"
+2
View File
@@ -6,6 +6,8 @@ libsub_rpc_a_SOURCES = \
command_download_slot.h \
command_file_slot.cc \
command_file_slot.h \
command_tracker_slot.cc \
command_tracker_slot.h \
command_map.cc \
command_map.h \
command_scheduler.cc \
+10 -10
View File
@@ -36,12 +36,6 @@
#include "config.h"
#include <algorithm>
#include <cstdio>
#include <cctype>
#include <fstream>
#include <rak/functional.h>
#include <rak/path.h>
#include <torrent/exceptions.h>
#include <torrent/object.h>
@@ -50,10 +44,6 @@
namespace rpc {
struct command_map_get_ptr : std::unary_function<CommandMap::value_type&, Command*> {
Command* operator () (CommandMap::value_type& value) { return value.second.m_variable; }
};
CommandMap::~CommandMap() {
for (iterator itr = base_type::begin(), last = base_type::end(); itr != last; itr++)
if (!(itr->second.m_flags & flag_dont_delete))
@@ -94,6 +84,14 @@ CommandMap::insert_file(key_type key, Command* variable, file_slot targetSlot, i
itr->second.m_fileSlot = targetSlot;
}
void
CommandMap::insert_tracker(key_type key, Command* variable, tracker_slot targetSlot, int flags, const char* parm, const char* doc) {
iterator itr = insert(key, variable, flags, parm, doc);
itr->second.m_target = target_tracker;
itr->second.m_trackerSlot = targetSlot;
}
void
CommandMap::insert(key_type key, const command_map_data_type src) {
iterator itr = base_type::find(key);
@@ -109,6 +107,7 @@ CommandMap::insert(key_type key, const command_map_data_type src) {
case target_generic: itr->second.m_genericSlot = src.m_genericSlot; break;
case target_download: itr->second.m_downloadSlot = src.m_downloadSlot; break;
case target_file: itr->second.m_fileSlot = src.m_fileSlot; break;
case target_tracker: itr->second.m_trackerSlot = src.m_trackerSlot; break;
default: throw torrent::internal_error("CommandMap::insert(...) Invalid target.");
}
}
@@ -128,6 +127,7 @@ CommandMap::call_command(key_type key, const mapped_type& arg, target_type targe
case target_generic: return itr->second.m_genericSlot(itr->second.m_variable, arg);
case target_download: return itr->second.m_downloadSlot(itr->second.m_variable, (core::Download*)target.second, arg);
case target_file: return itr->second.m_fileSlot(itr->second.m_variable, (torrent::File*)target.second, arg);
case target_tracker: return itr->second.m_trackerSlot(itr->second.m_variable, (torrent::Tracker*)target.second, arg);
default: throw torrent::internal_error("CommandMap::call_command(...) Invalid target.");
}
}
+11 -4
View File
@@ -48,6 +48,7 @@ namespace core {
namespace torrent {
class File;
class Tracker;
}
namespace rpc {
@@ -62,9 +63,10 @@ struct command_map_data_type {
// Some commands will need to share data, like get/set a variable. So
// instead of using a single virtual member function, each command
// will register a member function pointer to be used instead.
typedef const torrent::Object (*generic_slot)(Command*, const torrent::Object&);
typedef const torrent::Object (*generic_slot) (Command*, const torrent::Object&);
typedef const torrent::Object (*download_slot)(Command*, core::Download*, const torrent::Object&);
typedef const torrent::Object (*file_slot)(Command*, torrent::File*, const torrent::Object&);
typedef const torrent::Object (*file_slot) (Command*, torrent::File*, const torrent::Object&);
typedef const torrent::Object (*tracker_slot) (Command*, torrent::Tracker*, const torrent::Object&);
command_map_data_type(Command* variable, int flags, const char* parm, const char* doc) :
m_variable(variable), m_flags(flags), m_parm(parm), m_doc(doc) {}
@@ -75,6 +77,7 @@ struct command_map_data_type {
generic_slot m_genericSlot;
download_slot m_downloadSlot;
file_slot m_fileSlot;
tracker_slot m_trackerSlot;
};
int m_flags;
@@ -91,6 +94,7 @@ public:
typedef command_map_data_type::generic_slot generic_slot;
typedef command_map_data_type::download_slot download_slot;
typedef command_map_data_type::file_slot file_slot;
typedef command_map_data_type::tracker_slot tracker_slot;
typedef torrent::Object mapped_type;
typedef mapped_type::value_type mapped_value_type;
@@ -108,6 +112,7 @@ public:
static const int target_generic = 0;
static const int target_download = 1;
static const int target_file = 2;
static const int target_tracker = 3;
static const int flag_dont_delete = 0x1;
static const int flag_public_xmlrpc = 0x2;
@@ -123,12 +128,14 @@ public:
void insert_generic(key_type key, Command* variable, generic_slot targetSlot, int flags, const char* parm, const char* doc);
void insert_download(key_type key, Command* variable, download_slot targetSlot, int flags, const char* parm, const char* doc);
void insert_file(key_type key, Command* variable, file_slot targetSlot, int flags, const char* parm, const char* doc);
void insert_tracker(key_type key, Command* variable, tracker_slot targetSlot, int flags, const char* parm, const char* doc);
void insert(key_type key, const command_map_data_type src);
const mapped_type call_command(key_type key, const mapped_type& arg, target_type target = target_type((int)target_generic, NULL));
const mapped_type call_command_d(key_type key, core::Download* download, const mapped_type& arg) { return call_command(key, arg, target_type((int)target_download, download)); }
const mapped_type call_command_f(key_type key, torrent::File* file, const mapped_type& arg) { return call_command(key, arg, target_type((int)target_file, file)); }
const mapped_type call_command_d(key_type key, core::Download* download, const mapped_type& arg) { return call_command(key, arg, target_type((int)target_download, download)); }
const mapped_type call_command_f(key_type key, torrent::File* file, const mapped_type& arg) { return call_command(key, arg, target_type((int)target_file, file)); }
const mapped_type call_command_t(key_type key, torrent::Tracker* tracker, const mapped_type& arg) { return call_command(key, arg, target_type((int)target_tracker, tracker)); }
private:
CommandMap(const CommandMap&);
+123
View File
@@ -0,0 +1,123 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2007, 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 <torrent/tracker.h>
#include "parse.h"
#include "command_tracker_slot.h"
namespace rpc {
const torrent::Object
CommandTrackerSlot::call_unknown(Command* rawCommand, torrent::Tracker* tracker, const torrent::Object& rawArgs) {
CommandTrackerSlot* command = static_cast<CommandTrackerSlot*>(rawCommand);
return command->m_slot(tracker, rawArgs);
}
const torrent::Object
CommandTrackerSlot::call_list(Command* rawCommand, torrent::Tracker* tracker, const torrent::Object& rawArgs) {
CommandTrackerSlot* command = static_cast<CommandTrackerSlot*>(rawCommand);
switch (rawArgs.type()) {
case torrent::Object::TYPE_LIST:
return command->m_slot(tracker, rawArgs);
case torrent::Object::TYPE_VALUE:
case torrent::Object::TYPE_STRING:
{
torrent::Object tmpList(torrent::Object::TYPE_LIST);
tmpList.as_list().push_back(rawArgs);
return command->m_slot(tracker, tmpList);
}
default:
throw torrent::input_error("Not a list.");
}
}
const torrent::Object
CommandTrackerSlot::call_value_base(Command* rawCommand, torrent::Tracker* tracker, const torrent::Object& rawArgs, int base, int unit) {
CommandTrackerSlot* command = static_cast<CommandTrackerSlot*>(rawCommand);
const torrent::Object& arg = convert_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(tracker, arg);
case torrent::Object::TYPE_STRING:
{
torrent::Object argValue(torrent::Object::TYPE_VALUE);
if (!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(tracker, argValue);
}
default:
throw torrent::input_error("Not a value.");
}
}
const torrent::Object
CommandTrackerSlot::call_string(Command* rawCommand, torrent::Tracker* tracker, const torrent::Object& rawArgs) {
CommandTrackerSlot* command = static_cast<CommandTrackerSlot*>(rawCommand);
const torrent::Object& arg = convert_to_single_argument(rawArgs);
switch (arg.type()) {
case torrent::Object::TYPE_STRING:
return command->m_slot(tracker, arg);
// case torrent::Object::TYPE_NONE:
// throw torrent::input_error("CDS: void.");
// case torrent::Object::TYPE_VALUE:
// throw torrent::input_error("CDS: value.");
// case torrent::Object::TYPE_LIST:
// throw torrent::input_error("CDS: list.");
default:
throw torrent::input_error("Not a string.");
}
}
}
+176
View File
@@ -0,0 +1,176 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2007, 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_RPC_COMMAND_TRACKER_SLOT_H
#define RTORRENT_RPC_COMMAND_TRACKER_SLOT_H
#include <limits>
#include <inttypes.h>
#include <torrent/object.h>
#include <rak/functional_fun.h>
#include "command.h"
namespace torrent {
class Tracker;
}
namespace rpc {
class CommandTrackerSlot : public Command {
public:
typedef rak::function2<torrent::Object, torrent::Tracker*, const torrent::Object&> slot_type;
CommandTrackerSlot() {}
CommandTrackerSlot(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(Command* rawCommand, torrent::Tracker* tracker, const torrent::Object& args);
static const torrent::Object call_list(Command* rawCommand, torrent::Tracker* tracker, const torrent::Object& args);
static const torrent::Object call_string(Command* rawCommand, torrent::Tracker* tracker, const torrent::Object& args);
static const torrent::Object call_value_base(Command* rawCommand, torrent::Tracker* tracker, const torrent::Object& args, int base, int unit);
static const torrent::Object call_value(Command* rawCommand, torrent::Tracker* tracker, const torrent::Object& args) { return call_value_base(rawCommand, tracker, args, 0, 1); }
static const torrent::Object call_value_kb(Command* rawCommand, torrent::Tracker* tracker, const torrent::Object& args) { return call_value_base(rawCommand, tracker, args, 0, 1 << 10); }
template <int base, int unit>
static const torrent::Object call_value(Command* rawCommand, torrent::Tracker* tracker, const torrent::Object& args) { return call_value_base(rawCommand, tracker, args, base, unit); }
// static const torrent::Object& get_list(Command* rawCommand, 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_void_t_fn_t : public rak::function_base2<torrent::Object, torrent::Tracker*, const torrent::Object&> {
public:
object_void_t_fn_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::Tracker* tracker, const torrent::Object& arg1) { return torrent::Object(m_func(tracker)); }
private:
Func m_func;
};
template <typename Func>
class object_void_t_fn_t<Func, void> : public rak::function_base2<torrent::Object, torrent::Tracker*, const torrent::Object&> {
public:
object_void_t_fn_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::Tracker* tracker, const torrent::Object& arg1) {
m_func(tracker);
return torrent::Object();
}
private:
Func m_func;
};
template <typename Func, typename Result = typename Func::result_type>
class object_value_t_fn1_t : public rak::function_base2<torrent::Object, torrent::Tracker*, const torrent::Object&> {
public:
object_value_t_fn1_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::Tracker* tracker, const torrent::Object& arg1) {
return torrent::Object((int64_t)m_func(tracker, arg1.as_value()));
}
private:
Func m_func;
};
template <typename Func>
class object_value_t_fn1_t<Func, void> : public rak::function_base2<torrent::Object, torrent::Tracker*, const torrent::Object&> {
public:
object_value_t_fn1_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::Tracker* tracker, const torrent::Object& arg1) {
m_func(tracker, arg1.as_value());
return torrent::Object();
}
private:
Func m_func;
};
template <typename Func, typename Result = typename Func::result_type>
class object_string_t_fn1_t : public rak::function_base2<torrent::Object, torrent::Tracker*, const torrent::Object&> {
public:
object_string_t_fn1_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::Tracker* tracker, const torrent::Object& arg1) { return torrent::Object(m_func(tracker, arg1.as_string())); }
private:
Func m_func;
};
template <typename Func>
class object_string_t_fn1_t<Func, void> : public rak::function_base2<torrent::Object, torrent::Tracker*, const torrent::Object&> {
public:
object_string_t_fn1_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::Tracker* tracker, const torrent::Object& arg1) {
m_func(tracker, arg1.as_string());
return torrent::Object();
}
private:
Func m_func;
};
template <typename Return> object_void_t_fn_t<Return (*)(torrent::Tracker*), Return>*
object_t_fn(Return (*func)(torrent::Tracker*)) {
return new object_void_t_fn_t<Return (*)(torrent::Tracker*), Return>(func);
}
template <typename Func> object_void_t_fn_t<Func>* object_void_t_fn(Func func) { return new object_void_t_fn_t<Func>(func); }
template <typename Func> object_value_t_fn1_t<Func>* object_value_t_fn(Func func) { return new object_value_t_fn1_t<Func>(func); }
template <typename Func> object_string_t_fn1_t<Func>* object_string_t_fn(Func func) { return new object_string_t_fn1_t<Func>(func); }
}
#endif
+2 -1
View File
@@ -105,7 +105,8 @@ inline void call_command_d_set_value(const char* key, core::Download*
inline void call_command_d_set_string(const char* key, core::Download* download, const std::string& arg) { commands.call_command_d(key, download, torrent::Object(arg)); }
inline void call_command_d_set_std_string(const std::string& key, core::Download* download, const std::string& arg) { commands.call_command_d(key.c_str(), download, torrent::Object(arg)); }
inline torrent::Object call_command_f(const char* key, torrent::File* file, const torrent::Object& obj) { return commands.call_command_f(key, file, obj); }
inline torrent::Object call_command_f(const char* key, torrent::File* file, const torrent::Object& obj) { return commands.call_command_f(key, file, obj); }
inline torrent::Object call_command_t(const char* key, torrent::Tracker* tracker, const torrent::Object& obj) { return commands.call_command_t(key, tracker, obj); }
inline torrent::Object
call_command_d_range(const char* key, core::Download* download, torrent::Object::list_type::const_iterator first, torrent::Object::list_type::const_iterator last) {
+33 -12
View File
@@ -51,9 +51,6 @@ namespace rpc {
#ifdef HAVE_XMLRPC_C
// xmlrpc_value* xmlrpc_call_command(xmlrpc_env* env, xmlrpc_value* args, void* voidServerInfo);
// xmlrpc_value* xmlrpc_call_command_d(xmlrpc_env* env, xmlrpc_value* args, void* voidServerInfo);
torrent::Object
xmlrpc_to_object(xmlrpc_env* env, xmlrpc_value* value) {
switch (xmlrpc_value_type(value)) {
@@ -155,8 +152,8 @@ xmlrpc_to_download(xmlrpc_env* env, xmlrpc_value* value) {
}
}
torrent::File*
xmlrpc_to_file(xmlrpc_env* env, xmlrpc_value* value, core::Download* download) {
void*
xmlrpc_to_index_type(xmlrpc_env* env, xmlrpc_value* value, int callType, core::Download* download) {
int index;
switch (xmlrpc_value_type(value)) {
@@ -187,7 +184,7 @@ xmlrpc_to_file(xmlrpc_env* env, xmlrpc_value* value, core::Download* download) {
::free((void*)str);
if (*str == '\0' || *end != '\0') {
xmlrpc_env_set_fault(env, XMLRPC_TYPE_ERROR, "Invalid file index.");
xmlrpc_env_set_fault(env, XMLRPC_TYPE_ERROR, "Invalid index.");
return NULL;
}
@@ -202,12 +199,18 @@ xmlrpc_to_file(xmlrpc_env* env, xmlrpc_value* value, core::Download* download) {
if (env->fault_occurred)
return NULL;
torrent::File* file = xmlrpc.get_slot_find_file()(download, index);
void* result;
if (file == NULL)
xmlrpc_env_set_fault(env, XMLRPC_TYPE_ERROR, "Invalid file index.");
switch (callType) {
case XmlRpc::call_file: result = xmlrpc.get_slot_find_file()(download, index); break;
case XmlRpc::call_tracker: result = xmlrpc.get_slot_find_tracker()(download, index); break;
default: result = NULL; break;
}
if (result == NULL)
xmlrpc_env_set_fault(env, XMLRPC_TYPE_ERROR, "Invalid index.");
return file;
return result;
}
// This should really be cleaned up and support for an array of
@@ -251,7 +254,7 @@ xmlrpc_to_object_target(xmlrpc_env* env, xmlrpc_value* value, int callType, void
break;
}
if (callType == XmlRpc::call_file) {
if (callType == XmlRpc::call_file || callType == XmlRpc::call_tracker) {
if (current == last) {
xmlrpc_env_set_fault(env, XMLRPC_TYPE_ERROR, "Too few arguments.");
break;
@@ -263,7 +266,7 @@ xmlrpc_to_object_target(xmlrpc_env* env, xmlrpc_value* value, int callType, void
if (env->fault_occurred)
break;
*target = xmlrpc_to_file(env, tmp, (core::Download*)*target);
*target = xmlrpc_to_index_type(env, tmp, callType, (core::Download*)*target);
xmlrpc_DECREF(tmp);
if (env->fault_occurred)
@@ -402,6 +405,23 @@ xmlrpc_call_command_f(xmlrpc_env* env, xmlrpc_value* args, void* voidServerInfo)
}
}
xmlrpc_value*
xmlrpc_call_command_t(xmlrpc_env* env, xmlrpc_value* args, void* voidServerInfo) {
torrent::Tracker* tracker = NULL;
torrent::Object object = xmlrpc_to_object_target(env, args, XmlRpc::call_tracker, (void**)&tracker);
if (env->fault_occurred)
return NULL;
try {
return object_to_xmlrpc(env, rpc::call_command_t((const char*)voidServerInfo, tracker, object));
} catch (torrent::local_error& e) {
xmlrpc_env_set_fault(env, XMLRPC_PARSE_ERROR, e.what());
return NULL;
}
}
void
XmlRpc::initialize() {
#ifndef XMLRPC_HAVE_I8
@@ -449,6 +469,7 @@ XmlRpc::insert_command(const char* name, const char* parm, const char* doc, int
switch (call) {
case call_download: callSlot = &xmlrpc_call_command_d; break;
case call_file: callSlot = &xmlrpc_call_command_f; break;
case call_tracker: callSlot = &xmlrpc_call_command_t; break;
default: callSlot = &xmlrpc_call_command; break;
}
+10 -3
View File
@@ -46,15 +46,17 @@ namespace core {
namespace torrent {
class File;
class Object;
class Tracker;
}
namespace rpc {
class XmlRpc {
public:
typedef rak::function1<core::Download*, const char*> slot_find_download;
typedef rak::function2<torrent::File*, core::Download*, uint32_t> slot_find_file;
typedef rak::function2<bool, const char*, uint32_t> slot_write;
typedef rak::function1<core::Download*, const char*> slot_find_download;
typedef rak::function2<torrent::File*, core::Download*, uint32_t> slot_find_file;
typedef rak::function2<torrent::Tracker*, core::Download*, uint32_t> slot_find_tracker;
typedef rak::function2<bool, const char*, uint32_t> slot_write;
static const int dialect_generic = 0;
static const int dialect_i8 = 1;
@@ -63,6 +65,7 @@ public:
static const int call_generic = 0;
static const int call_download = 1;
static const int call_file = 2;
static const int call_tracker = 3;
XmlRpc() : m_env(NULL), m_registry(NULL), m_dialect(dialect_i8) {}
@@ -84,6 +87,9 @@ public:
slot_find_file& get_slot_find_file() { return m_slotFindFile; }
void set_slot_find_file(slot_find_file::base_type* slot) { m_slotFindFile.set(slot); }
slot_find_tracker& get_slot_find_tracker() { return m_slotFindTracker; }
void set_slot_find_tracker(slot_find_tracker::base_type* slot) { m_slotFindTracker.set(slot); }
private:
void* m_env;
void* m_registry;
@@ -92,6 +98,7 @@ private:
slot_find_download m_slotFindDownload;
slot_find_file m_slotFindFile;
slot_find_tracker m_slotFindTracker;
};
}