* Automatically add variables to XMLRPC unless added as private.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@898 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2007-05-04 16:15:16 +00:00
parent a864d5938d
commit c986357bab
11 changed files with 93 additions and 112 deletions
+15 -15
View File
@@ -174,21 +174,21 @@ initialize_command_events() {
utils::VariableMap* variables = control->variable();
core::DownloadList* downloadList = control->core()->download_list();
ADD_COMMAND_SLOT("on_insert", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_insert()));
ADD_COMMAND_SLOT("on_erase", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_erase()));
ADD_COMMAND_SLOT("on_open", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_open()));
ADD_COMMAND_SLOT("on_close", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_close()));
ADD_COMMAND_SLOT("on_start", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_start()));
ADD_COMMAND_SLOT("on_stop", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_stop()));
ADD_COMMAND_SLOT("on_hash_queued", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_hash_queued()));
ADD_COMMAND_SLOT("on_hash_removed", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_hash_removed()));
ADD_COMMAND_SLOT("on_hash_done", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_hash_done()));
ADD_COMMAND_SLOT("on_finished", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_finished()));
ADD_COMMAND_SLOT_PRIVATE("on_insert", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_insert()));
ADD_COMMAND_SLOT_PRIVATE("on_erase", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_erase()));
ADD_COMMAND_SLOT_PRIVATE("on_open", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_open()));
ADD_COMMAND_SLOT_PRIVATE("on_close", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_close()));
ADD_COMMAND_SLOT_PRIVATE("on_start", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_start()));
ADD_COMMAND_SLOT_PRIVATE("on_stop", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_stop()));
ADD_COMMAND_SLOT_PRIVATE("on_hash_queued", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_hash_queued()));
ADD_COMMAND_SLOT_PRIVATE("on_hash_removed", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_hash_removed()));
ADD_COMMAND_SLOT_PRIVATE("on_hash_done", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_hash_done()));
ADD_COMMAND_SLOT_PRIVATE("on_finished", call_list, rak::bind_ptr_fn(&apply_on_state_change, &downloadList->slot_map_finished()));
ADD_COMMAND_SLOT("stop_on_ratio", call_list, rak::ptr_fn(&apply_stop_on_ratio));
ADD_COMMAND_SLOT_PRIVATE("stop_on_ratio", call_list, rak::ptr_fn(&apply_stop_on_ratio));
ADD_COMMAND_SLOT("start_tied", call_string, utils::object_fn(&apply_start_tied));
ADD_COMMAND_SLOT("stop_untied", call_string, utils::object_fn(&apply_stop_untied));
ADD_COMMAND_SLOT("close_untied", call_string, utils::object_fn(&apply_close_untied));
ADD_COMMAND_SLOT("remove_untied", call_string, utils::object_fn(&apply_remove_untied));
ADD_COMMAND_SLOT_PRIVATE("start_tied", call_string, utils::object_fn(&apply_start_tied));
ADD_COMMAND_SLOT_PRIVATE("stop_untied", call_string, utils::object_fn(&apply_stop_untied));
ADD_COMMAND_SLOT_PRIVATE("close_untied", call_string, utils::object_fn(&apply_close_untied));
ADD_COMMAND_SLOT_PRIVATE("remove_untied", call_string, utils::object_fn(&apply_remove_untied));
}
+25 -11
View File
@@ -72,26 +72,40 @@ add_variable("get_" key, "set_" key, key, &utils::CommandVariable::get_value, &u
#define ADD_VARIABLE_STRING(key, defaultValue) \
add_variable("get_" key, "set_" key, key, &utils::CommandVariable::get_string, &utils::CommandVariable::set_string, std::string(defaultValue));
#define ADD_COMMAND_SLOT(key, function, slot) \
#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);
#define ADD_COMMAND_SLOT_PRIVATE(key, function, slot) \
commandSlotsItr->set_slot(slot); \
variables->insert(key, commandSlotsItr++, &utils::CommandSlot::function, utils::VariableMap::flag_dont_delete);
#define ADD_COMMAND_COPY(key, function) \
#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);
#define ADD_COMMAND_COPY_PRIVATE(key, function) \
variables->insert(key, (commandSlotsItr - 1), &utils::CommandSlot::function, utils::VariableMap::flag_dont_delete);
#define ADD_COMMAND_VALUE_TRI(key, set, get) \
ADD_COMMAND_SLOT(key, call_value, utils::object_value_fn(set)) \
ADD_COMMAND_COPY("set_" key, call_value) \
ADD_COMMAND_SLOT("get_" key, call_unknown, utils::object_void_fn(get))
ADD_COMMAND_SLOT_PRIVATE(key, call_value, utils::object_value_fn(set)) \
ADD_COMMAND_COPY("set_" key, call_value, "i:i", "") \
ADD_COMMAND_SLOT("get_" key, call_unknown, utils::object_void_fn(get), "i:", "")
#define ADD_COMMAND_VALUE_TRI_KB(key, set, get) \
ADD_COMMAND_SLOT(key, call_value_kb, utils::object_value_fn(set)) \
ADD_COMMAND_COPY("set_" key, call_value) \
ADD_COMMAND_SLOT("get_" key, call_unknown, utils::object_void_fn(get))
ADD_COMMAND_SLOT_PRIVATE(key, call_value_kb, utils::object_value_fn(set)) \
ADD_COMMAND_COPY("set_" key, call_value, "i:i", "") \
ADD_COMMAND_SLOT("get_" key, call_unknown, utils::object_void_fn(get), "i:", "")
#define ADD_COMMAND_STRING_TRI(key, set, get) \
ADD_COMMAND_SLOT(key, call_string, utils::object_string_fn(set)) \
ADD_COMMAND_COPY("set_" key, call_string) \
ADD_COMMAND_SLOT("get_" key, call_unknown, utils::object_void_fn(get))
ADD_COMMAND_SLOT_PRIVATE(key, call_string, utils::object_string_fn(set)) \
ADD_COMMAND_COPY("set_" key, call_string, "i:s", "") \
ADD_COMMAND_SLOT("get_" key, call_unknown, utils::object_void_fn(get), "s:", "")
#define ADD_COMMAND_STRING(key, slot) \
ADD_COMMAND_SLOT(key, call_string, slot, "i:s", "")
#define ADD_COMMAND_LIST(key, slot) \
ADD_COMMAND_SLOT(key, call_list, slot, "i:", "")
#endif
+2 -2
View File
@@ -143,9 +143,9 @@ initialize_command_network() {
ADD_VARIABLE_VALUE("tracker_numwant", -1);
ADD_COMMAND_SLOT("encryption", call_list, rak::ptr_fn(&apply_encryption));
ADD_COMMAND_LIST("encryption", rak::ptr_fn(&apply_encryption));
ADD_COMMAND_SLOT("tos", call_string, rak::ptr_fn(&apply_tos));
ADD_COMMAND_STRING("tos", rak::ptr_fn(&apply_tos));
ADD_COMMAND_STRING_TRI("bind", rak::make_mem_fun(control->core(), &core::Manager::set_bind_address), rak::make_mem_fun(control->core(), &core::Manager::bind_address));
ADD_COMMAND_STRING_TRI("ip", rak::make_mem_fun(control->core(), &core::Manager::set_local_address), rak::make_mem_fun(control->core(), &core::Manager::local_address));
+8 -8
View File
@@ -104,16 +104,16 @@ initialize_command_ui() {
ADD_VARIABLE_STRING("key_layout", "qwerty");
ADD_COMMAND_SLOT("view_add", call_string, utils::object_string_fn(rak::make_mem_fun(control->view_manager(), &core::ViewManager::insert_throw)));
ADD_COMMAND_STRING("view_add", utils::object_string_fn(rak::make_mem_fun(control->view_manager(), &core::ViewManager::insert_throw)));
ADD_COMMAND_SLOT("view_filter", call_list, rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_filter));
ADD_COMMAND_SLOT("view_filter_on", call_list, rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_filter_on));
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));
ADD_COMMAND_SLOT("view_sort", call_list, rak::ptr_fn(&apply_view_sort));
ADD_COMMAND_SLOT("view_sort_new", call_list, rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_sort_new));
ADD_COMMAND_SLOT("view_sort_current", call_list, rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_sort_current));
ADD_COMMAND_LIST("view_sort", rak::ptr_fn(&apply_view_sort));
ADD_COMMAND_LIST("view_sort_new", rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_sort_new));
ADD_COMMAND_LIST("view_sort_current", rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_sort_current));
// ADD_COMMAND_SLOT("view_sort_current", call_list, rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_sort_current));
// ADD_COMMAND_LIST("view_sort_current", rak::bind_ptr_fn(&apply_view_filter, &core::ViewManager::set_sort_current));
ADD_COMMAND_SLOT("print", call_string, utils::object_string_fn(rak::make_mem_fun(control->core(), &core::Manager::push_log)));
ADD_COMMAND_STRING("print", utils::object_string_fn(rak::make_mem_fun(control->core(), &core::Manager::push_log)));
}
+1
View File
@@ -250,6 +250,7 @@ ViewManager::set_filter(const std::string& name, const filter_args& args) {
iterator viewItr = find_throw(name);
(*viewItr)->set_filter(build_filter_list(args));
(*viewItr)->filter();
}
void
+6 -3
View File
@@ -151,8 +151,11 @@ apply_fast_cgi(const std::string& arg) {
if (control->xmlrpc() == NULL) {
control->set_xmlrpc(new rpc::XmlRpc);
control->xmlrpc()->set_slot_call_command_get(rak::mem_fn(control->variable(), &utils::VariableMap::call_command_get));
control->xmlrpc()->set_slot_call_command_set(rak::mem_fn(control->variable(), &utils::VariableMap::call_command_set));
control->xmlrpc()->set_slot_call_command(rak::mem_fn(control->variable(), &utils::VariableMap::call_command));
for (utils::VariableMap::const_iterator itr = control->variable()->begin(), last = control->variable()->end(); itr != last; itr++)
if (itr->second.m_flags & utils::VariableMap::flag_public_xmlrpc)
control->xmlrpc()->insert_command(itr->first, itr->second.m_parm, itr->second.m_doc);
}
control->set_fast_cgi(new rpc::FastCgi(arg));
@@ -227,7 +230,7 @@ initialize_variables() {
variables->insert("import", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_import)));
variables->insert("try_import", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_try_import)));
ADD_COMMAND_SLOT("schedule", call_list, rak::ptr_fn(&apply_schedule));
ADD_COMMAND_LIST("schedule", rak::ptr_fn(&apply_schedule));
variables->insert("schedule_remove", new utils::VariableStringSlot(NULL, rak::mem_fn<const std::string&>(control->command_scheduler(), &CommandScheduler::erase)));
variables->insert("download_scheduler", new utils::VariableVoidSlot(rak::mem_fn(control->scheduler(), &core::Scheduler::update)));
+9 -28
View File
@@ -48,6 +48,8 @@
namespace rpc {
XmlRpc::slot_call_command XmlRpc::m_slotCall;
#ifdef HAVE_XMLRPC_C
torrent::Object
@@ -113,25 +115,15 @@ xmlrpc_to_object(xmlrpc_env* env, xmlrpc_value* value) {
}
}
struct server_info_t {
server_info_t(const char* command, XmlRpc::slot_call_command* callCommand) :
m_command(command), m_callCommand(callCommand) {}
const char* m_command;
XmlRpc::slot_call_command* m_callCommand;
};
xmlrpc_value*
xmlrpc_call_command(xmlrpc_env* env, xmlrpc_value* args, void* voidServerInfo) {
XmlRpc::call_command(xmlrpc_env* env, xmlrpc_value* args, void* voidServerInfo) {
torrent::Object object = xmlrpc_to_object(env, args);
if (env->fault_occurred)
return NULL;
try {
server_info_t* serverInfo = reinterpret_cast<server_info_t*>(voidServerInfo);
const torrent::Object& resultObject = (*serverInfo->m_callCommand)(serverInfo->m_command, object);
const torrent::Object& resultObject = XmlRpc::m_slotCall((const char*)voidServerInfo, object);
xmlrpc_value* result;
xmlrpc_int32 tmpInt;
@@ -162,22 +154,6 @@ xmlrpc_call_command(xmlrpc_env* env, xmlrpc_value* args, void* voidServerInfo) {
XmlRpc::XmlRpc() : m_env(new xmlrpc_env) {
xmlrpc_env_init(m_env);
m_registry = xmlrpc_registry_new(m_env);
// Add a helper function for this...
xmlrpc_registry_add_method_w_doc(m_env, m_registry, NULL, "call.set_upload_rate", &xmlrpc_call_command, new server_info_t("upload_rate", &m_slotSet), "i:i", "");
xmlrpc_registry_add_method_w_doc(m_env, m_registry, NULL, "call.get_upload_rate", &xmlrpc_call_command, new server_info_t("upload_rate", &m_slotGet), "i:", "");
xmlrpc_registry_add_method_w_doc(m_env, m_registry, NULL, "call.get_directory", &xmlrpc_call_command, new server_info_t("get_directory", &m_slotGet), "s:", "");
xmlrpc_registry_add_method_w_doc(m_env, m_registry, NULL, "call.set_directory", &xmlrpc_call_command, new server_info_t("set_directory", &m_slotSet), "i:s", "");
xmlrpc_registry_add_method_w_doc(m_env, m_registry, NULL, "call.get_bind", &xmlrpc_call_command, new server_info_t("get_bind", &m_slotGet), "s:", "");
xmlrpc_registry_add_method_w_doc(m_env, m_registry, NULL, "call.set_bind", &xmlrpc_call_command, new server_info_t("set_bind", &m_slotSet), "i:s", "");
xmlrpc_registry_add_method_w_doc(m_env, m_registry, NULL, "call.get_ip", &xmlrpc_call_command, new server_info_t("get_ip", &m_slotGet), "s:", "");
xmlrpc_registry_add_method_w_doc(m_env, m_registry, NULL, "call.set_ip", &xmlrpc_call_command, new server_info_t("set_ip", &m_slotSet), "i:s", "");
xmlrpc_registry_add_method_w_doc(m_env, m_registry, NULL, "call.print", &xmlrpc_call_command, new server_info_t("print", &m_slotSet), "i:s", "");
}
XmlRpc::~XmlRpc() {
@@ -201,6 +177,11 @@ XmlRpc::process(const char* inBuffer, uint32_t length, slot_write slotWrite) {
return result;
}
void
XmlRpc::insert_command(const char* name, const char* parm, const char* doc) {
xmlrpc_registry_add_method_w_doc(m_env, m_registry, NULL, name, &XmlRpc::call_command, const_cast<char*>(name), parm, doc);
}
#else
XmlRpc::XmlRpc() { throw torrent::resource_error("XMLRPC not supported."); }
+6 -4
View File
@@ -40,6 +40,7 @@
#include <rak/functional_fun.h>
typedef struct _xmlrpc_env xmlrpc_env;
typedef struct _xmlrpc_value xmlrpc_value;
typedef struct _xmlrpc_registry xmlrpc_registry;
namespace torrent {
@@ -58,15 +59,16 @@ public:
bool process(const char* inBuffer, uint32_t length, slot_write slotWrite);
void set_slot_call_command_get(slot_call_command::base_type* s) { m_slotGet.set(s); }
void set_slot_call_command_set(slot_call_command::base_type* s) { m_slotSet.set(s); }
void insert_command(const char* name, const char* parm, const char* doc);
static xmlrpc_value* call_command(xmlrpc_env* env, xmlrpc_value* args, void* voidServerInfo);
static void set_slot_call_command(slot_call_command::base_type* s) { m_slotCall.set(s); }
private:
xmlrpc_env* m_env;
xmlrpc_registry* m_registry;
slot_call_command m_slotGet;
slot_call_command m_slotSet;
static slot_call_command m_slotCall;
};
}
+1
View File
@@ -97,6 +97,7 @@ CommandSlot::call_value_base(Variable* rawVariable, const torrent::Object& rawAr
switch (arg.type()) {
case torrent::Object::TYPE_VALUE:
// Should shift this one too, so it gives the right unit.
return command->m_slot(arg);
case torrent::Object::TYPE_STRING:
+3 -30
View File
@@ -62,13 +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, 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)));
base_type::insert(itr, value_type(key, variable_map_data_type(variable, genericSlot, NULL, flags, parm, doc)));
}
const VariableMap::mapped_type
@@ -279,32 +280,4 @@ 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_get(key_type key, 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_genericSlot != NULL)
return itr->second.m_genericSlot(itr->second.m_variable, arg);
return get(key);
}
const VariableMap::mapped_type
VariableMap::call_command_set(key_type key, 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_genericSlot != NULL)
return itr->second.m_genericSlot(itr->second.m_variable, arg);
set(key, arg);
return Variable::m_emptyObject;
}
}
+17 -11
View File
@@ -66,14 +66,18 @@ struct variable_map_data_type {
typedef const torrent::Object (*generic_slot)(Variable*, const torrent::Object&);
typedef const torrent::Object (*download_slot)(Variable*, core::Download*, const torrent::Object&);
variable_map_data_type(Variable* variable, generic_slot genericSlot, download_slot downloadSlot, int flags) :
m_variable(variable), m_genericSlot(genericSlot), m_downloadSlot(downloadSlot), m_flags(flags) {}
variable_map_data_type(Variable* variable, generic_slot genericSlot, download_slot downloadSlot, int flags,
const char* parm, const char* doc) :
m_variable(variable), m_genericSlot(genericSlot), m_downloadSlot(downloadSlot), m_flags(flags), m_parm(parm), m_doc(doc) {}
Variable* m_variable;
generic_slot m_genericSlot;
download_slot m_downloadSlot;
int m_flags;
const char* m_parm;
const char* m_doc;
};
class VariableMap : public std::map<const char*, variable_map_data_type, variable_map_comp> {
@@ -90,11 +94,15 @@ public:
using base_type::key_type;
using base_type::value_type;
using base_type::begin;
using base_type::end;
static const int max_size_key = 128;
static const int max_size_opt = 1024;
static const int max_size_line = max_size_key + max_size_opt + 64;
static const int flag_dont_delete = 0x1;
static const int flag_dont_delete = 0x1;
static const int flag_public_xmlrpc = 0x2;
VariableMap() {}
~VariableMap();
@@ -104,15 +112,16 @@ 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, int flags = 0,
const char* parm = "", const char* doc = "");
// Consider uninlining the helper functions.
const mapped_type get(key_type key) const;
const mapped_type get_d(core::Download* download, key_type key) const;
const mapped_type get(key_type key) const;
const mapped_type get_d(core::Download* download, key_type key) const;
const std::string get_string(key_type key) const { return get(key).as_string(); }
const std::string get_d_string(core::Download* download, key_type key) const { return get_d(download, key).as_string(); }
const std::string get_string(key_type key) const { return get(key).as_string(); }
const std::string get_d_string(core::Download* download, key_type key) const { return get_d(download, key).as_string(); }
mapped_value_type get_value(key_type key) const { return get(key).as_value(); }
mapped_value_type get_d_value(core::Download* download, key_type key) const { return get_d(download, key).as_value(); }
@@ -145,9 +154,6 @@ public:
// requires seperate calls to get and set. These will be merged.
const mapped_type call_command(key_type key, const mapped_type& arg);
const mapped_type call_command_get(key_type key, const mapped_type& arg);
const mapped_type call_command_set(key_type key, const mapped_type& arg);
private:
VariableMap(const VariableMap&);
void operator = (const VariableMap&);