Browse Source

* Added command scheduler, currently it only calls option handler.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@609 e378c898-3ddf-0310-93e7-cc216c733640
pull/30/head
rakshasa 20 years ago
parent
commit
e45f05ee98
  1. 123
      rak/functional_fun.h
  2. 26
      rak/priority_queue_default.h
  3. 4
      src/Makefile.am
  4. 106
      src/command_scheduler.cc
  5. 77
      src/command_scheduler.h
  6. 59
      src/command_scheduler_item.cc
  7. 80
      src/command_scheduler_item.h
  8. 8
      src/control.cc
  9. 14
      src/control.h
  10. 2
      src/core/manager.h
  11. 2
      src/display/window.h
  12. 45
      src/main.cc
  13. 2
      src/option_file.cc
  14. 10
      src/option_handler.cc
  15. 3
      src/option_handler.h
  16. 26
      src/option_handler_rules.cc
  17. 3
      src/option_handler_rules.h

123
rak/functional_fun.h

@ -56,36 +56,60 @@
namespace rak {
template <typename Result>
class function_base {
class function_base0 {
public:
virtual ~function_base() {}
virtual ~function_base0() {}
virtual Result operator () () = 0;
};
template <typename Result, typename Arg1>
class function_base1 {
public:
virtual ~function_base1() {}
virtual Result operator () (Arg1 arg1) = 0;
};
template <typename Result>
class function {
class function0 {
public:
typedef Result result_type;
typedef function_base<Result> Base;
typedef Result result_type;
typedef function_base0<Result> base_type;
bool is_valid() const { return m_base.get() != NULL; }
void set(Base* base) { m_base = std::auto_ptr<Base>(base); }
void set(base_type* base) { m_base = std::auto_ptr<base_type>(base); }
Result operator () () { return (*m_base)(); }
private:
std::auto_ptr<Base> m_base;
std::auto_ptr<base_type> m_base;
};
template <typename Result, typename Arg1>
class function1 {
public:
typedef Result result_type;
typedef function_base1<Result, Arg1> base_type;
bool is_valid() const { return m_base.get() != NULL; }
void set(base_type* base) { m_base = std::auto_ptr<base_type>(base); }
Result operator () (Arg1 arg1) { return (*m_base)(arg1); }
private:
std::auto_ptr<base_type> m_base;
};
template <typename Object, typename Result>
class _mem_fn0 : public function_base<Result> {
class mem_fn0_t : public function_base0<Result> {
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 <typename Object, typename Result, typename Arg1>
class mem_fn1_t : public function_base1<Result, Arg1> {
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 <typename Object, typename Result>
class _const_mem_fn0 : public function_base<Result> {
class const_mem_fn0_t : public function_base0<Result> {
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 <typename Object, typename Result, typename Arg1>
class const_mem_fn1_t : public function_base1<Result, Arg1> {
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 <typename Object, typename Result, typename Arg1>
class mem_fn0_b1_t : public function_base0<Result> {
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 <typename Object, typename Result>
function_base<Result>*
function_base0<Result>*
mem_fn(Object* object, Result (Object::*func)()) {
return new _mem_fn0<Object, Result>(object, func);
return new mem_fn0_t<Object, Result>(object, func);
}
template <typename Object, typename Result, typename Arg1>
function_base1<Result, Arg1>*
mem_fn(Object* object, Result (Object::*func)(Arg1)) {
return new mem_fn1_t<Object, Result, Arg1>(object, func);
}
template <typename Object, typename Result>
function_base<Result>*
function_base0<Result>*
mem_fn(const Object* object, Result (Object::*func)() const) {
return new _const_mem_fn0<Object, Result>(object, func);
return new const_mem_fn0_t<Object, Result>(object, func);
}
template <typename Object, typename Result, typename Arg1>
function_base1<Result, Arg1>*
mem_fn(const Object* object, Result (Object::*func)(Arg1) const) {
return new const_mem_fn1_t<Object, Result, Arg1>(object, func);
}
template <typename Object, typename Result, typename Arg1>
function_base0<Result>*
bind_mem_fn(Object* object, Result (Object::*func)(Arg1), const Arg1 arg1) {
return new mem_fn0_b1_t<Object, Result, Arg1>(object, func, arg1);
}
}

26
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<void>* s) { m_slot.set(s); }
void call() { m_slot(); }
void set_slot(function0<void>::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<void> m_slot;
function0<void> 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();

4
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 \

106
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 <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include <algorithm>
#include <rak/functional.h>
#include <torrent/exceptions.h>
#include "command_scheduler.h"
#include "command_scheduler_item.h"
CommandScheduler::~CommandScheduler() {
std::for_each(begin(), end(), rak::call_delete<CommandSchedulerItem>());
}
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);
}

77
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 <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_COMMAND_SCHEDULER_H
#define RTORRENT_COMMAND_SCHEDULER_H
#include <vector>
#include <string>
#include <rak/functional_fun.h>
class CommandSchedulerItem;
class CommandScheduler : public std::vector<CommandSchedulerItem*> {
public:
typedef rak::function1<void, const std::string&> SlotString;
typedef std::vector<CommandSchedulerItem*> 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

59
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 <jaris@ifi.uio.no>
//
// 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);
}

80
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 <jaris@ifi.uio.no>
//
// 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<void> 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

8
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;

14
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;
};

2
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();

2
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);
}
}

45
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();

2
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.");

10
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));
}

3
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

26
src/option_handler_rules.cc

@ -36,10 +36,12 @@
#include "config.h"
#include <cstdio>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <rak/string_manip.h>
#include <torrent/exceptions.h>
#include <torrent/torrent.h>
@ -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)));
}

3
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);

Loading…
Cancel
Save