* Fix TextElement* to use rpc::target_type.

* Moved most of src/ui/download.cc to use commands for TextElements.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@967 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2007-08-30 16:33:52 +00:00
parent 32b74ffc48
commit 73c41074ec
28 changed files with 476 additions and 115 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_file_itr_slot.cc \
command_file_itr_slot.h \
command_peer_slot.cc \
command_peer_slot.h \
command_tracker_slot.cc \
+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/data/file.h>
#include "parse.h"
#include "command_file_itr_slot.h"
namespace rpc {
const torrent::Object
CommandFileItrSlot::call_unknown(Command* rawCommand, torrent::FileListIterator* file, const torrent::Object& rawArgs) {
CommandFileItrSlot* command = static_cast<CommandFileItrSlot*>(rawCommand);
return command->m_slot(file, rawArgs);
}
const torrent::Object
CommandFileItrSlot::call_list(Command* rawCommand, torrent::FileListIterator* file, const torrent::Object& rawArgs) {
CommandFileItrSlot* command = static_cast<CommandFileItrSlot*>(rawCommand);
switch (rawArgs.type()) {
case torrent::Object::TYPE_LIST:
return command->m_slot(file, 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(file, tmpList);
}
default:
throw torrent::input_error("Not a list.");
}
}
const torrent::Object
CommandFileItrSlot::call_value_base(Command* rawCommand, torrent::FileListIterator* file, const torrent::Object& rawArgs, int base, int unit) {
CommandFileItrSlot* command = static_cast<CommandFileItrSlot*>(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(file, 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(file, argValue);
}
default:
throw torrent::input_error("Not a value.");
}
}
const torrent::Object
CommandFileItrSlot::call_string(Command* rawCommand, torrent::FileListIterator* file, const torrent::Object& rawArgs) {
CommandFileItrSlot* command = static_cast<CommandFileItrSlot*>(rawCommand);
const torrent::Object& arg = convert_to_single_argument(rawArgs);
switch (arg.type()) {
case torrent::Object::TYPE_STRING:
return command->m_slot(file, 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_FILE_ITR_SLOT_H
#define RTORRENT_RPC_COMMAND_FILE_ITR_SLOT_H
#include <limits>
#include <inttypes.h>
#include <torrent/object.h>
#include <rak/functional_fun.h>
#include "command.h"
namespace torrent {
class FileListIterator;
}
namespace rpc {
class CommandFileItrSlot : public Command {
public:
typedef rak::function2<torrent::Object, torrent::FileListIterator*, const torrent::Object&> slot_type;
CommandFileItrSlot() {}
CommandFileItrSlot(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::FileListIterator* file, const torrent::Object& args);
static const torrent::Object call_list(Command* rawCommand, torrent::FileListIterator* file, const torrent::Object& args);
static const torrent::Object call_string(Command* rawCommand, torrent::FileListIterator* file, const torrent::Object& args);
static const torrent::Object call_value_base(Command* rawCommand, torrent::FileListIterator* file, const torrent::Object& args, int base, int unit);
static const torrent::Object call_value(Command* rawCommand, torrent::FileListIterator* file, const torrent::Object& args) { return call_value_base(rawCommand, file, args, 0, 1); }
static const torrent::Object call_value_kb(Command* rawCommand, torrent::FileListIterator* file, const torrent::Object& args) { return call_value_base(rawCommand, file, args, 0, 1 << 10); }
template <int base, int unit>
static const torrent::Object call_value(Command* rawCommand, torrent::FileListIterator* file, const torrent::Object& args) { return call_value_base(rawCommand, file, 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_fi_fn_t : public rak::function_base2<torrent::Object, torrent::FileListIterator*, const torrent::Object&> {
public:
object_void_fi_fn_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::FileListIterator* file, const torrent::Object& arg1) { return torrent::Object(m_func(file)); }
private:
Func m_func;
};
template <typename Func>
class object_void_fi_fn_t<Func, void> : public rak::function_base2<torrent::Object, torrent::FileListIterator*, const torrent::Object&> {
public:
object_void_fi_fn_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::FileListIterator* file, const torrent::Object& arg1) {
m_func(file);
return torrent::Object();
}
private:
Func m_func;
};
template <typename Func, typename Result = typename Func::result_type>
class object_value_fi_fn1_t : public rak::function_base2<torrent::Object, torrent::FileListIterator*, const torrent::Object&> {
public:
object_value_fi_fn1_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::FileListIterator* file, const torrent::Object& arg1) {
return torrent::Object((int64_t)m_func(file, arg1.as_value()));
}
private:
Func m_func;
};
template <typename Func>
class object_value_fi_fn1_t<Func, void> : public rak::function_base2<torrent::Object, torrent::FileListIterator*, const torrent::Object&> {
public:
object_value_fi_fn1_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::FileListIterator* file, const torrent::Object& arg1) {
m_func(file, arg1.as_value());
return torrent::Object();
}
private:
Func m_func;
};
template <typename Func, typename Result = typename Func::result_type>
class object_string_fi_fn1_t : public rak::function_base2<torrent::Object, torrent::FileListIterator*, const torrent::Object&> {
public:
object_string_fi_fn1_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::FileListIterator* file, const torrent::Object& arg1) { return torrent::Object(m_func(file, arg1.as_string())); }
private:
Func m_func;
};
template <typename Func>
class object_string_fi_fn1_t<Func, void> : public rak::function_base2<torrent::Object, torrent::FileListIterator*, const torrent::Object&> {
public:
object_string_fi_fn1_t(Func func) : m_func(func) {}
virtual torrent::Object operator () (torrent::FileListIterator* file, const torrent::Object& arg1) {
m_func(file, arg1.as_string());
return torrent::Object();
}
private:
Func m_func;
};
template <typename Return> object_void_fi_fn_t<Return (*)(torrent::FileListIterator*), Return>*
object_fi_fn(Return (*func)(torrent::FileListIterator*)) {
return new object_void_fi_fn_t<Return (*)(torrent::FileListIterator*), Return>(func);
}
template <typename Func> object_void_fi_fn_t<Func>* object_void_fi_fn(Func func) { return new object_void_fi_fn_t<Func>(func); }
template <typename Func> object_value_fi_fn1_t<Func>* object_value_fi_fn(Func func) { return new object_value_fi_fn1_t<Func>(func); }
template <typename Func> object_string_fi_fn1_t<Func>* object_string_fi_fn(Func func) { return new object_string_fi_fn1_t<Func>(func); }
}
#endif
+26 -16
View File
@@ -76,14 +76,6 @@ CommandMap::insert_download(key_type key, Command* variable, download_slot targe
itr->second.m_downloadSlot = targetSlot;
}
void
CommandMap::insert_file(key_type key, Command* variable, file_slot targetSlot, int flags, const char* parm, const char* doc) {
iterator itr = insert(key, variable, flags, parm, doc);
itr->second.m_target = target_file;
itr->second.m_fileSlot = targetSlot;
}
void
CommandMap::insert_peer(key_type key, Command* variable, peer_slot targetSlot, int flags, const char* parm, const char* doc) {
iterator itr = insert(key, variable, flags, parm, doc);
@@ -100,6 +92,22 @@ CommandMap::insert_tracker(key_type key, Command* variable, tracker_slot targetS
itr->second.m_trackerSlot = targetSlot;
}
void
CommandMap::insert_file(key_type key, Command* variable, file_slot targetSlot, int flags, const char* parm, const char* doc) {
iterator itr = insert(key, variable, flags, parm, doc);
itr->second.m_target = target_file;
itr->second.m_fileSlot = targetSlot;
}
void
CommandMap::insert_file_itr(key_type key, Command* variable, file_itr_slot targetSlot, int flags, const char* parm, const char* doc) {
iterator itr = insert(key, variable, flags, parm, doc);
itr->second.m_target = target_file_itr;
itr->second.m_fileItrSlot = targetSlot;
}
void
CommandMap::insert(key_type key, const command_map_data_type src) {
iterator itr = base_type::find(key);
@@ -135,11 +143,12 @@ CommandMap::call_command(key_type key, const mapped_type& arg, target_type targe
// This _should_ be optimized int just two calls.
switch (itr->second.m_target) {
case target_generic: return itr->second.m_genericSlot(itr->second.m_variable, arg);
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_peer: return itr->second.m_peerSlot(itr->second.m_variable, (torrent::Peer*)target.second, arg);
case target_tracker: return itr->second.m_trackerSlot(itr->second.m_variable, (torrent::Tracker*)target.second, arg);
case target_peer: return itr->second.m_peerSlot (itr->second.m_variable, (torrent::Peer*)target.second, arg);
case target_tracker: return itr->second.m_trackerSlot (itr->second.m_variable, (torrent::Tracker*)target.second, arg);
case target_file: return itr->second.m_fileSlot (itr->second.m_variable, (torrent::File*)target.second, arg);
case target_file_itr: return itr->second.m_fileItrSlot (itr->second.m_variable, (torrent::FileListIterator*)target.second, arg);
default: throw torrent::internal_error("CommandMap::call_command(...) Invalid target.");
}
}
@@ -152,11 +161,12 @@ CommandMap::call_command(const_iterator itr, const mapped_type& arg, target_type
// This _should_ be optimized int just two calls.
switch (itr->second.m_target) {
case target_generic: return itr->second.m_genericSlot(itr->second.m_variable, arg);
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_peer: return itr->second.m_peerSlot(itr->second.m_variable, (torrent::Peer*)target.second, arg);
case target_tracker: return itr->second.m_trackerSlot(itr->second.m_variable, (torrent::Tracker*)target.second, arg);
case target_peer: return itr->second.m_peerSlot (itr->second.m_variable, (torrent::Peer*)target.second, arg);
case target_tracker: return itr->second.m_trackerSlot (itr->second.m_variable, (torrent::Tracker*)target.second, arg);
case target_file: return itr->second.m_fileSlot (itr->second.m_variable, (torrent::File*)target.second, arg);
case target_file_itr: return itr->second.m_fileItrSlot (itr->second.m_variable, (torrent::FileListIterator*)target.second, arg);
default: throw torrent::internal_error("CommandMap::call_command(...) Invalid target.");
}
}
+28 -10
View File
@@ -48,6 +48,7 @@ namespace core {
namespace torrent {
class File;
class FileListIterator;
class Peer;
class Tracker;
}
@@ -64,11 +65,12 @@ 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 (*download_slot)(Command*, core::Download*, const torrent::Object&);
typedef const torrent::Object (*file_slot) (Command*, torrent::File*, const torrent::Object&);
typedef const torrent::Object (*peer_slot) (Command*, torrent::Peer*, const torrent::Object&);
typedef const torrent::Object (*tracker_slot) (Command*, torrent::Tracker*, 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_itr_slot) (Command*, torrent::FileListIterator*, const torrent::Object&);
typedef const torrent::Object (*peer_slot) (Command*, torrent::Peer*, 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) {}
@@ -81,6 +83,7 @@ struct command_map_data_type {
generic_slot m_genericSlot;
download_slot m_downloadSlot;
file_slot m_fileSlot;
file_itr_slot m_fileItrSlot;
peer_slot m_peerSlot;
tracker_slot m_trackerSlot;
};
@@ -99,6 +102,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::file_itr_slot file_itr_slot;
typedef command_map_data_type::peer_slot peer_slot;
typedef command_map_data_type::tracker_slot tracker_slot;
@@ -118,9 +122,10 @@ public:
static const int target_generic = 0;
static const int target_download = 1;
static const int target_file = 2;
static const int target_peer = 3;
static const int target_tracker = 4;
static const int target_peer = 2;
static const int target_tracker = 3;
static const int target_file = 4;
static const int target_file_itr = 5;
static const int flag_dont_delete = 0x1;
static const int flag_public_xmlrpc = 0x2;
@@ -135,9 +140,10 @@ 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_peer (key_type key, Command* variable, peer_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_file (key_type key, Command* variable, file_slot targetSlot, int flags, const char* parm, const char* doc);
void insert_file_itr(key_type key, Command* variable, file_itr_slot targetSlot, int flags, const char* parm, const char* doc);
void insert(key_type key, const command_map_data_type src);
@@ -145,15 +151,27 @@ public:
const mapped_type call_command (const_iterator itr, 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_p(key_type key, torrent::Peer* peer, const mapped_type& arg) { return call_command(key, arg, target_type((int)target_peer, peer)); }
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)); }
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)); }
private:
CommandMap(const CommandMap&);
void operator = (const CommandMap&);
};
// Since it gets used so many places we might as well put it in the
// rpc namespace.
typedef CommandMap::target_type target_type;
inline target_type make_target() { return target_type((int)CommandMap::target_generic, NULL); }
inline target_type make_target(core::Download* target) { return target_type((int)CommandMap::target_download, target); }
inline target_type make_target(torrent::Peer* target) { return target_type((int)CommandMap::target_peer, target); }
inline target_type make_target(torrent::Tracker* target) { return target_type((int)CommandMap::target_tracker, target); }
inline target_type make_target(torrent::File* target) { return target_type((int)CommandMap::target_file, target); }
inline target_type make_target(torrent::FileListIterator* target) { return target_type((int)CommandMap::target_file_itr, target); }
inline target_type make_target(int type, void* target) { return target_type(type, target); }
}
#endif
-9
View File
@@ -50,20 +50,11 @@ namespace core {
namespace rpc {
typedef CommandMap::target_type target_type;
// Move to another file?
extern CommandMap commands;
extern XmlRpc xmlrpc;
extern ExecFile execFile;
inline target_type make_target() { return target_type((int)CommandMap::target_generic, NULL); }
inline target_type make_target(core::Download* target) { return target_type((int)CommandMap::target_download, target); }
inline target_type make_target(torrent::File* target) { return target_type((int)CommandMap::target_file, target); }
inline target_type make_target(torrent::Peer* target) { return target_type((int)CommandMap::target_peer, target); }
inline target_type make_target(torrent::Tracker* target) { return target_type((int)CommandMap::target_tracker, target); }
inline target_type make_target(int type, void* target) { return target_type(type, target); }
typedef std::pair<torrent::Object, const char*> parse_command_type;
// The generic parse command function, used by the rest. At some point
+3 -1
View File
@@ -65,8 +65,10 @@ public:
// These need to match CommandMap type values.
static const int call_generic = 0;
static const int call_download = 1;
static const int call_file = 2;
static const int call_peer = 2;
static const int call_tracker = 3;
static const int call_file = 4;
static const int call_file_itr = 5;
XmlRpc() : m_env(NULL), m_registry(NULL), m_dialect(dialect_i8) {}