* 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
+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;
};
}