diff --git a/rak/functional_fun.h b/rak/functional_fun.h index ecde2b6..ec5e775 100644 --- a/rak/functional_fun.h +++ b/rak/functional_fun.h @@ -56,36 +56,60 @@ namespace rak { template -class function_base { +class function_base0 { public: - virtual ~function_base() {} + virtual ~function_base0() {} virtual Result operator () () = 0; }; +template +class function_base1 { +public: + virtual ~function_base1() {} + + virtual Result operator () (Arg1 arg1) = 0; +}; + template -class function { +class function0 { public: - typedef Result result_type; - typedef function_base Base; + typedef Result result_type; + typedef function_base0 base_type; bool is_valid() const { return m_base.get() != NULL; } - void set(Base* base) { m_base = std::auto_ptr(base); } + void set(base_type* base) { m_base = std::auto_ptr(base); } Result operator () () { return (*m_base)(); } private: - std::auto_ptr m_base; + std::auto_ptr m_base; +}; + +template +class function1 { +public: + typedef Result result_type; + typedef function_base1 base_type; + + bool is_valid() const { return m_base.get() != NULL; } + + void set(base_type* base) { m_base = std::auto_ptr(base); } + + Result operator () (Arg1 arg1) { return (*m_base)(arg1); } + +private: + std::auto_ptr m_base; }; template -class _mem_fn0 : public function_base { +class mem_fn0_t : public function_base0 { public: typedef Result (Object::*Func)(); - _mem_fn0(Object* object, Func func) : m_object(object), m_func(func) {} - virtual ~_mem_fn0() {} + mem_fn0_t(Object* object, Func func) : m_object(object), m_func(func) {} + virtual ~mem_fn0_t() {} virtual Result operator () () { return (m_object->*m_func)(); } @@ -94,13 +118,28 @@ private: Func m_func; }; +template +class mem_fn1_t : public function_base1 { +public: + typedef Result (Object::*Func)(Arg1); + + mem_fn1_t(Object* object, Func func) : m_object(object), m_func(func) {} + virtual ~mem_fn1_t() {} + + virtual Result operator () (Arg1 arg1) { return (m_object->*m_func)(arg1); } + +private: + Object* m_object; + Func m_func; +}; + template -class _const_mem_fn0 : public function_base { +class const_mem_fn0_t : public function_base0 { public: typedef Result (Object::*Func)() const; - _const_mem_fn0(const Object* object, Func func) : m_object(object), m_func(func) {} - virtual ~_const_mem_fn0() {} + const_mem_fn0_t(const Object* object, Func func) : m_object(object), m_func(func) {} + virtual ~const_mem_fn0_t() {} virtual Result operator () () { return (m_object->*m_func)(); } @@ -109,16 +148,66 @@ private: Func m_func; }; +template +class const_mem_fn1_t : public function_base1 { +public: + typedef Result (Object::*Func)(Arg1) const; + + const_mem_fn1_t(const Object* object, Func func) : m_object(object), m_func(func) {} + virtual ~const_mem_fn1_t() {} + + virtual Result operator () (Arg1 arg1) { return (m_object->*m_func)(arg1); } + +private: + const Object* m_object; + Func m_func; +}; + +// Unary functor with a bound argument. +template +class mem_fn0_b1_t : public function_base0 { +public: + typedef Result (Object::*Func)(Arg1); + + mem_fn0_b1_t(Object* object, Func func, const Arg1 arg1) : m_object(object), m_func(func), m_arg1(arg1) {} + virtual ~mem_fn0_b1_t() {} + + virtual Result operator () () { return (m_object->*m_func)(m_arg1); } + +private: + Object* m_object; + Func m_func; + const Arg1 m_arg1; +}; + template -function_base* +function_base0* mem_fn(Object* object, Result (Object::*func)()) { - return new _mem_fn0(object, func); + return new mem_fn0_t(object, func); +} + +template +function_base1* +mem_fn(Object* object, Result (Object::*func)(Arg1)) { + return new mem_fn1_t(object, func); } template -function_base* +function_base0* mem_fn(const Object* object, Result (Object::*func)() const) { - return new _const_mem_fn0(object, func); + return new const_mem_fn0_t(object, func); +} + +template +function_base1* +mem_fn(const Object* object, Result (Object::*func)(Arg1) const) { + return new const_mem_fn1_t(object, func); +} + +template +function_base0* +bind_mem_fn(Object* object, Result (Object::*func)(Arg1), const Arg1 arg1) { + return new mem_fn0_b1_t(object, func, arg1); } } diff --git a/rak/priority_queue_default.h b/rak/priority_queue_default.h index 90432bb..36dd4e2 100644 --- a/rak/priority_queue_default.h +++ b/rak/priority_queue_default.h @@ -56,24 +56,24 @@ public: m_slot.set(NULL); } - bool is_valid() const { return m_slot.is_valid(); } - bool is_queued() const { return m_time != timer(); } + bool is_valid() const { return m_slot.is_valid(); } + bool is_queued() const { return m_time != timer(); } - void call() { m_slot(); } - void set_slot(function_base* s) { m_slot.set(s); } + void call() { m_slot(); } + void set_slot(function0::base_type* s) { m_slot.set(s); } - const timer& time() const { return m_time; } - void clear_time() { m_time = timer(); } - void set_time(const timer& t) { m_time = t; } + const timer& time() const { return m_time; } + void clear_time() { m_time = timer(); } + void set_time(const timer& t) { m_time = t; } - bool compare(const timer& t) const { return m_time > t; } + bool compare(const timer& t) const { return m_time > t; } private: priority_item(const priority_item&); void operator = (const priority_item&); timer m_time; - function m_slot; + function0 m_slot; }; struct priority_compare { @@ -105,12 +105,14 @@ priority_queue_insert(priority_queue_default* queue, priority_item* item, timer inline void priority_queue_erase(priority_queue_default* queue, priority_item* item) { - if (!item->is_valid()) - throw std::logic_error("priority_queue_erase(...) called on an invalid item."); - if (!item->is_queued()) return; + // Check is_valid() after is_queued() so that it is safe to call + // erase on untouched instances. + if (!item->is_valid()) + throw std::logic_error("priority_queue_erase(...) called on an invalid item."); + // Clear time before erasing to force it to the top. item->clear_time(); diff --git a/src/Makefile.am b/src/Makefile.am index 7866baa..de95b82 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,6 +15,10 @@ rtorrent_LDADD = \ $(top_srcdir)/src/utils/libsub_utils.a rtorrent_SOURCES = \ + command_scheduler.cc \ + command_scheduler.h \ + command_scheduler_item.cc \ + command_scheduler_item.h \ control.cc \ control.h \ globals.cc \ diff --git a/src/command_scheduler.cc b/src/command_scheduler.cc new file mode 100644 index 0000000..efcfef8 --- /dev/null +++ b/src/command_scheduler.cc @@ -0,0 +1,106 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005, 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 +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#include "config.h" + +#include +#include +#include + +#include "command_scheduler.h" +#include "command_scheduler_item.h" + +CommandScheduler::~CommandScheduler() { + std::for_each(begin(), end(), rak::call_delete()); +} + +CommandScheduler::iterator +CommandScheduler::find(const std::string& key) { + return std::find_if(begin(), end(), rak::equal(key, std::mem_fun(&CommandSchedulerItem::key))); +} + +CommandScheduler::iterator +CommandScheduler::insert(const std::string& key) { + if (key.empty()) + throw torrent::input_error("Scheduler received an empty key."); + + iterator itr = find(key); + + if (itr == end()) + itr = base_type::insert(end(), NULL); + else + delete *itr; + + *itr = new CommandSchedulerItem(key); + (*itr)->set_slot(rak::bind_mem_fn(this, &CommandScheduler::call_item, *itr)); + + return itr; +} + +void +CommandScheduler::erase(iterator itr) { + if (itr == end()) + return; + + delete *itr; + base_type::erase(itr); +} + +void +CommandScheduler::call_item(value_type item) { + if (item->is_queued()) + throw torrent::internal_error("CommandScheduler::call_item(...) called but item is still queued."); + + if (std::find(begin(), end(), item) == end()) + throw torrent::internal_error("CommandScheduler::call_item(...) called but the item isn't in the scheduler."); + + // Remove the item before calling the command if it should be + // removed. + + try { + m_slotCommand(item->command()); + + } catch (torrent::input_error& e) { + if (m_slotErrorMessage.is_valid()) + m_slotErrorMessage("Scheduled command failed: " + item->key() + ": " + e.what()); + } + + uint32_t interval = item->interval(); + + // Enable if we caught a torrrent::input_error? + if (interval != 0) + item->enable(interval); +} diff --git a/src/command_scheduler.h b/src/command_scheduler.h new file mode 100644 index 0000000..4e53f2f --- /dev/null +++ b/src/command_scheduler.h @@ -0,0 +1,77 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005, 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 +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#ifndef RTORRENT_COMMAND_SCHEDULER_H +#define RTORRENT_COMMAND_SCHEDULER_H + +#include +#include +#include + +class CommandSchedulerItem; + +class CommandScheduler : public std::vector { +public: + typedef rak::function1 SlotString; + typedef std::vector base_type; + + using base_type::value_type; + using base_type::begin; + using base_type::end; + + CommandScheduler() {} + ~CommandScheduler(); + + void set_slot_command(SlotString::base_type* s) { m_slotCommand.set(s); } + void set_slot_error_message(SlotString::base_type* s) { m_slotErrorMessage.set(s); } + + // slot_error_message or something. + + iterator find(const std::string& key); + + // If the key already exists then the old item is deleted. It is + // safe to call erase on end(). + iterator insert(const std::string& key); + void erase(iterator itr); + +private: + void call_item(value_type item); + + SlotString m_slotCommand; + SlotString m_slotErrorMessage; +}; + +#endif diff --git a/src/command_scheduler_item.cc b/src/command_scheduler_item.cc new file mode 100644 index 0000000..1dbf48c --- /dev/null +++ b/src/command_scheduler_item.cc @@ -0,0 +1,59 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005, 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 +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#include "config.h" + +#include "command_scheduler_item.h" + +CommandSchedulerItem::~CommandSchedulerItem() { + priority_queue_erase(&taskScheduler, &m_task); +} + +void +CommandSchedulerItem::enable(uint32_t first) { + if (is_queued()) + disable(); + + // If 'first' is zero then we execute the task + // immediately. ''interval()'' will not return zero so we never end + // up in an infinit loop. + priority_queue_insert(&taskScheduler, &m_task, (cachedTime + ((int64_t)first * 1000000)).round_seconds()); +} + +void +CommandSchedulerItem::disable() { + priority_queue_erase(&taskScheduler, &m_task); +} diff --git a/src/command_scheduler_item.h b/src/command_scheduler_item.h new file mode 100644 index 0000000..9a0bc04 --- /dev/null +++ b/src/command_scheduler_item.h @@ -0,0 +1,80 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005, 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 +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#ifndef RTORRENT_COMMAND_SCHEDULER_ITEM_H +#define RTORRENT_COMMAND_SCHEDULER_ITEM_H + +#include "globals.h" + +class CommandSchedulerItem { +public: + typedef rak::function0 Slot; + + CommandSchedulerItem(const std::string& key) : m_key(key), m_interval(0) {} + ~CommandSchedulerItem(); + + bool is_queued() const { return m_task.is_queued(); } + + //void enable() { enable(interval()); } + void enable(uint32_t first); + void disable(); + + const std::string& key() const { return m_key; } + + const std::string& command() const { return m_command; } + void set_command(const std::string& s) { m_command = s; } + + // 'interval()' should in the future return some more dynamic values. + uint32_t interval() const { return m_interval; } + void set_interval(uint32_t v) { m_interval = v; } + + void set_slot(Slot::base_type* s) { m_task.set_slot(s); } + +private: + CommandSchedulerItem(const CommandSchedulerItem&); + void operator = (const CommandSchedulerItem&); + + std::string m_key; + std::string m_command; + + uint32_t m_interval; + + rak::priority_item m_task; + + // Flags for various things. +}; + +#endif diff --git a/src/control.cc b/src/control.cc index 2ddfa8c..7879802 100644 --- a/src/control.cc +++ b/src/control.cc @@ -46,6 +46,8 @@ #include "input/input_event.h" #include "ui/root.h" +#include "command_scheduler.h" + #include "control.h" Control::Control() : @@ -55,7 +57,9 @@ Control::Control() : m_core(new core::Manager()), m_display(new display::Manager()), m_input(new input::Manager()), - m_inputStdin(new input::InputEvent(STDIN_FILENO)) { + m_inputStdin(new input::InputEvent(STDIN_FILENO)), + + m_commandScheduler(new CommandScheduler()) { m_inputStdin->slot_pressed(sigc::mem_fun(m_input, &input::Manager::pressed)); @@ -66,6 +70,8 @@ Control::~Control() { delete m_inputStdin; delete m_input; + delete m_commandScheduler; + delete m_ui; delete m_display; delete m_core; diff --git a/src/control.h b/src/control.h index b5b5fe8..071757e 100644 --- a/src/control.h +++ b/src/control.h @@ -58,6 +58,8 @@ namespace input { class Manager; } +class CommandScheduler; + class Control { public: Control(); @@ -66,16 +68,18 @@ public: bool is_shutdown_completed() { return m_shutdownReceived && torrent::is_inactive(); } bool is_shutdown_received() { return m_shutdownReceived; } + void initialize(); + void cleanup(); + + void receive_shutdown(); + ui::Root* ui() { return m_ui; } core::Manager* core() { return m_core; } display::Manager* display() { return m_display; } input::Manager* input() { return m_input; } input::InputEvent* input_stdin() { return m_inputStdin; } - void initialize(); - void cleanup(); - - void receive_shutdown(); + CommandScheduler* command_scheduler() { return m_commandScheduler; } private: Control(const Control&); @@ -89,6 +93,8 @@ private: input::Manager* m_input; input::InputEvent* m_inputStdin; + CommandScheduler* m_commandScheduler; + rak::priority_item m_taskShutdown; }; diff --git a/src/core/manager.h b/src/core/manager.h index ffaa609..da9826e 100644 --- a/src/core/manager.h +++ b/src/core/manager.h @@ -89,6 +89,8 @@ public: void check_hash(Download* d); + void push_log(const std::string& msg) { m_logImportant.push_front(msg); m_logComplete.push_front(msg); } + private: void listen_open(); diff --git a/src/display/window.h b/src/display/window.h index b3b7fc4..3d98c9e 100644 --- a/src/display/window.h +++ b/src/display/window.h @@ -93,7 +93,7 @@ protected: inline void Window::mark_dirty() { priority_queue_erase(&displayScheduler, &m_taskUpdate); - priority_queue_insert(&displayScheduler, &m_taskUpdate, cachedTime); + priority_queue_insert(&displayScheduler, &m_taskUpdate, cachedTime + 10); } } diff --git a/src/main.cc b/src/main.cc index 15e6feb..8a3c3a4 100644 --- a/src/main.cc +++ b/src/main.cc @@ -71,6 +71,8 @@ #include "option_handler.h" #include "option_handler_rules.h" #include "option_parser.h" +#include "command_scheduler.h" +#include "command_scheduler_item.h" uint32_t countTicks = 0; @@ -147,6 +149,8 @@ initialize_option_handler(Control* c, OptionHandler* optionHandler) { optionHandler->insert("use_udp_trackers", new OptionHandlerString(c, &apply_use_udp_trackers)); optionHandler->insert("http_proxy", new OptionHandlerString(c, &apply_http_proxy)); + optionHandler->insert("schedule", new OptionHandlerString(c, &apply_schedule)); + optionHandler->insert("schedule_remove", new OptionHandlerString(c, &apply_schedule_remove)); } void @@ -186,37 +190,50 @@ main(int argc, char** argv) { cachedTime = rak::timer::current(); OptionHandler optionHandler; - Control uiControl; + Control control; + control.command_scheduler()->set_slot_command(rak::mem_fn(&optionHandler, &OptionHandler::process_command)); + control.command_scheduler()->set_slot_error_message(rak::mem_fn(control.core(), &core::Manager::push_log)); + srandom(cachedTime.usec()); srand48(cachedTime.usec()); - initialize_option_handler(&uiControl, &optionHandler); + initialize_option_handler(&control, &optionHandler); OptionFile optionFile; optionFile.slot_option(sigc::mem_fun(optionHandler, &OptionHandler::process)); SignalHandler::set_ignore(SIGPIPE); - SignalHandler::set_handler(SIGINT, sigc::mem_fun(uiControl, &Control::receive_shutdown)); + SignalHandler::set_handler(SIGINT, sigc::mem_fun(control, &Control::receive_shutdown)); SignalHandler::set_handler(SIGSEGV, sigc::bind(sigc::ptr_fun(&do_panic), SIGSEGV)); SignalHandler::set_handler(SIGBUS, sigc::bind(sigc::ptr_fun(&do_panic), SIGBUS)); SignalHandler::set_handler(SIGFPE, sigc::bind(sigc::ptr_fun(&do_panic), SIGFPE)); - uiControl.core()->initialize_first(); + control.core()->initialize_first(); if (getenv("HOME") && !optionFile.process_file(getenv("HOME") + std::string("/.rtorrent.rc"))) - uiControl.core()->get_log_important().push_front("Could not load \"~/.rtorrent.rc\"."); + control.core()->get_log_important().push_front("Could not load \"~/.rtorrent.rc\"."); + + int firstArg = parse_options(&control, &optionHandler, argc, argv); - int firstArg = parse_options(&uiControl, &optionHandler, argc, argv); + control.initialize(); - uiControl.initialize(); + // Just to make sure we did all the stuff on the queue before + // loading any torrents. + while (!taskScheduler.empty() && taskScheduler.top()->time() <= cachedTime) { + rak::priority_item* v = taskScheduler.top(); + taskScheduler.pop(); + + v->clear_time(); + v->call(); + } - load_session_torrents(&uiControl); - load_arg_torrents(&uiControl, argv + firstArg, argv + argc); + load_session_torrents(&control); + load_arg_torrents(&control, argv + firstArg, argv + argc); - uiControl.display()->adjust_layout(); + control.display()->adjust_layout(); - while (!uiControl.is_shutdown_completed()) { + while (!control.is_shutdown_completed()) { countTicks++; cachedTime = rak::timer::current(); @@ -240,13 +257,13 @@ main(int argc, char** argv) { // This needs to be called every second or so. Currently done by // the throttle task in libtorrent. if (!displayScheduler.empty() && displayScheduler.top()->time() <= cachedTime) - uiControl.display()->do_update(); + control.display()->do_update(); // Do shutdown check before poll, not after. - uiControl.core()->get_poll_manager()->poll(!taskScheduler.empty() ? taskScheduler.top()->time() - cachedTime : 60 * 1000000); + control.core()->get_poll_manager()->poll(!taskScheduler.empty() ? taskScheduler.top()->time() - cachedTime : 60 * 1000000); } - uiControl.cleanup(); + control.cleanup(); } catch (torrent::base_error& e) { display::Canvas::cleanup(); diff --git a/src/option_file.cc b/src/option_file.cc index f275af6..b2e0bf3 100644 --- a/src/option_file.cc +++ b/src/option_file.cc @@ -83,7 +83,7 @@ OptionFile::parse_line(const char* line) { opt[0] = '\0'; // Check for empty lines, and options within "abc". - if ((result = std::sscanf(line, "%63s = \"%511[^\"]s", key, opt)) != 2 && + if ((result = std::sscanf(line, "%63s = \"%511[^\"]", key, opt)) != 2 && (result = std::sscanf(line, "%63s = %511s", key, opt)) != 2 && result == 1) throw torrent::input_error("Error parseing option file."); diff --git a/src/option_handler.cc b/src/option_handler.cc index cfc89d1..46c6a9c 100644 --- a/src/option_handler.cc +++ b/src/option_handler.cc @@ -83,3 +83,13 @@ OptionHandler::process(const std::string& key, const std::string& arg) const { itr->second->process(key, arg); } + +void +OptionHandler::process_command(const std::string& command) const { + std::string::size_type pos = command.find('='); + + if (pos == std::string::npos) + throw torrent::input_error("Option handler could not find '=' in command."); + + process(command.substr(0, pos), command.substr(pos + 1, std::string::npos)); +} diff --git a/src/option_handler.h b/src/option_handler.h index cda7e5e..ec2ed8a 100644 --- a/src/option_handler.h +++ b/src/option_handler.h @@ -79,6 +79,9 @@ public: // The caller must catch torrent::input_error in case of bad input. void process(const std::string& key, const std::string& arg) const; + + // Temporary. + void process_command(const std::string& command) const; }; #endif diff --git a/src/option_handler_rules.cc b/src/option_handler_rules.cc index e23abbc..98705c6 100644 --- a/src/option_handler_rules.cc +++ b/src/option_handler_rules.cc @@ -36,10 +36,12 @@ #include "config.h" +#include #include #include #include #include +#include #include #include @@ -49,6 +51,8 @@ #include "control.h" #include "option_handler_rules.h" +#include "command_scheduler.h" +#include "command_scheduler_item.h" void receive_tracker_dump(std::istream* s); @@ -217,3 +221,25 @@ apply_encoding_list(Control* m, const std::string& arg) { torrent::encoding_list()->push_back(arg); } +void +apply_schedule(Control* m, const std::string& arg) { + int first; + int interval; + char key[21]; + char command[2048]; + + if (std::sscanf(arg.c_str(), "%20[^,],%i,%i,%2047[^\n]", key, &first, &interval, command) != 4) + throw torrent::input_error("Invalid arguments to command."); + + CommandSchedulerItem* item = *m->command_scheduler()->insert(rak::trim(std::string(key))); + + item->set_command(rak::trim(std::string(command))); + item->set_interval(interval); + + item->enable(first); +} + +void +apply_schedule_remove(Control* m, const std::string& arg) { + m->command_scheduler()->erase(m->command_scheduler()->find(rak::trim(arg))); +} diff --git a/src/option_handler_rules.h b/src/option_handler_rules.h index 82b0928..46c3a88 100644 --- a/src/option_handler_rules.h +++ b/src/option_handler_rules.h @@ -80,6 +80,9 @@ void apply_http_proxy(Control* m, const std::string& arg); void apply_session_directory(Control* m, const std::string& arg); void apply_encoding_list(Control* m, const std::string& arg); +void apply_schedule(Control* m, const std::string& arg); +void apply_schedule_remove(Control* m, const std::string& arg); + class OptionHandlerInt : public OptionHandlerBase { public: typedef void (*Apply)(Control*, int);