From e19efcb8e7ba2cf4d4ce59c5f76e78a41a19ba24 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 2 Jan 2011 03:55:52 +0100 Subject: Command handling changes as a result of adding the update command. --- commands.cpp | 33 ++++++++++++++++++++------------- commands.h | 28 ++++++++++++++++++++++++---- main.cpp | 2 -- music.cpp | 16 ++++++++++++++++ music.h | 2 ++ telnet_connection.cpp | 3 ++- 6 files changed, 64 insertions(+), 20 deletions(-) diff --git a/commands.cpp b/commands.cpp index 03af48c..db693e5 100644 --- a/commands.cpp +++ b/commands.cpp @@ -1,12 +1,12 @@ #include "commands.h" -#include "music.h" #include #include +#include #include -static std::vector ls(const std::vector& args) { +std::vector commands::Commands::ls() { if(args.size() != 2) { throw commands::CommandException("usage: ls DIR"); } @@ -29,10 +29,7 @@ static std::vector ls(const std::vector& args) { return result; } -typedef boost::function (const std::string artist)> FindFunction; -std::map find_handlers; - -static std::vector find(const std::vector& args) { +std::vector commands::Commands::find() { if(args.size() != 3) { throw commands::CommandException("usage: find TYPE SEARCH"); } @@ -63,20 +60,30 @@ static std::vector find(const std::vector& args) { return result; } -std::map commands::handlers; +std::vector commands::Commands::update() { + if(args.size() != 2) { + throw commands::CommandException("usage: update DIR"); + } + + io_service.post(boost::bind(music::begin_update, args[1])); + + std::vector v; + return v; +} -void commands::init() { - handlers["ls"] = ls; - handlers["find"] = find; +commands::Commands::Commands(boost::asio::io_service& io_service_, std::vector& args_) : io_service(io_service_), args(args_) { + handlers["ls"] = &commands::Commands::ls; + handlers["find"] = &commands::Commands::find; find_handlers["artist"] = music::find_artist; + handlers["update"] = &commands::Commands::update; } -std::vector commands::execute(const std::vector& args) { +std::vector commands::Commands::operator()() { assert(args.size()); - Handler h = commands::handlers[args[0]]; + Handler h = handlers[args[0]]; if(!h) { throw CommandException(boost::str(boost::format("unknown command \"%s\"") % args[0])); } - return h(args); + return h(this); } diff --git a/commands.h b/commands.h index 42a9452..ae5dc84 100644 --- a/commands.h +++ b/commands.h @@ -1,7 +1,10 @@ #ifndef COMMANDS_H #define COMMANDS_H +#include "music.h" + #include +#include #include #include @@ -9,8 +12,7 @@ #include namespace commands { - typedef boost::function (const std::vector&)> Handler; - extern std::map handlers; + class Commands; class CommandException : public std::runtime_error { public: @@ -18,8 +20,26 @@ namespace commands { CommandException(std::string s) : std::runtime_error(s.c_str()) {}; }; - void init(); - std::vector execute(const std::vector& args); + class Commands { + private: + typedef boost::function (Commands*)> Handler; + std::map handlers; + + typedef boost::function (const std::string artist)> FindFunction; + std::map find_handlers; + + boost::asio::io_service& io_service; + std::vector& args; + + std::vector ls(); + std::vector find(); + std::vector update(); + + public: + Commands(boost::asio::io_service& io_service, std::vector& args); + std::vector operator()(); + + }; }; #endif diff --git a/main.cpp b/main.cpp index f9f2d8e..ccca2ad 100644 --- a/main.cpp +++ b/main.cpp @@ -4,7 +4,6 @@ #include "encoder.h" #include "httpd.h" #include "telnetd.h" -#include "commands.h" #include #include @@ -17,7 +16,6 @@ int main(int argc, char **argv) { music::init(config::vm["audist.music_root"].as()); decoder::init(); encoder::init(); - commands::init(); boost::asio::io_service io_service; diff --git a/music.cpp b/music.cpp index 6265771..9193b89 100644 --- a/music.cpp +++ b/music.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include fs::path music::root_directory; @@ -77,6 +78,21 @@ std::vector music::find_artist(const std::string artist) { return results; } +void music::begin_update(const std::string path) { + MusicDirectory::p dir = get_directory(path); + std::cout << boost::format("updater(%s) called") % path << std::endl; + if(dir) { + update(dir->path); + } +} + +void music::update(const MusicDirectory& dir) { + BOOST_FOREACH(fs::path t, dir.tracks) { + std::cout << "track " << t << std::endl; + } + std::for_each(dir.directories.begin(), dir.directories.end(), update); +} + void MusicDirectory::render(HTTP::Connection::p req, HTTPResponse& res) { res.add_header("content-type", "text/html"); diff --git a/music.h b/music.h index 3e2d24c..1362a7a 100644 --- a/music.h +++ b/music.h @@ -41,6 +41,8 @@ namespace music { MusicListing::p get(const std::string& path); MusicDirectory::p get_directory(const std::string& path); std::vector find_artist(const std::string artist); + void begin_update(const std::string path); + void update(const MusicDirectory& dir); }; #endif diff --git a/telnet_connection.cpp b/telnet_connection.cpp index 457d1ba..c9089a9 100644 --- a/telnet_connection.cpp +++ b/telnet_connection.cpp @@ -34,7 +34,8 @@ void telnet::Connection::handle_read(const boost::system::error_code& error, siz std::vector r; try { - r = commands::execute(args); + commands::Commands cmd(socket.get_io_service(), args); + r = cmd(); } catch(commands::CommandException& ce) { std::string s(ce.what()); s += '\n'; -- cgit v1.2.3