diff options
-rw-r--r-- | commands.cpp | 33 | ||||
-rw-r--r-- | commands.h | 28 | ||||
-rw-r--r-- | main.cpp | 2 | ||||
-rw-r--r-- | music.cpp | 16 | ||||
-rw-r--r-- | music.h | 2 | ||||
-rw-r--r-- | 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 <boost/format.hpp> #include <boost/algorithm/string/join.hpp> +#include <boost/bind.hpp> #include <iostream> -static std::vector<std::string> ls(const std::vector<std::string>& args) { +std::vector<std::string> commands::Commands::ls() { if(args.size() != 2) { throw commands::CommandException("usage: ls DIR"); } @@ -29,10 +29,7 @@ static std::vector<std::string> ls(const std::vector<std::string>& args) { return result; } -typedef boost::function<std::vector<MusicListing::p> (const std::string artist)> FindFunction; -std::map<std::string, FindFunction> find_handlers; - -static std::vector<std::string> find(const std::vector<std::string>& args) { +std::vector<std::string> commands::Commands::find() { if(args.size() != 3) { throw commands::CommandException("usage: find TYPE SEARCH"); } @@ -63,20 +60,30 @@ static std::vector<std::string> find(const std::vector<std::string>& args) { return result; } -std::map<std::string, commands::Handler> commands::handlers; +std::vector<std::string> 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<std::string> v; + return v; +} -void commands::init() { - handlers["ls"] = ls; - handlers["find"] = find; +commands::Commands::Commands(boost::asio::io_service& io_service_, std::vector<std::string>& 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<std::string> commands::execute(const std::vector<std::string>& args) { +std::vector<std::string> 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); } @@ -1,7 +1,10 @@ #ifndef COMMANDS_H #define COMMANDS_H +#include "music.h" + #include <boost/function.hpp> +#include <boost/asio.hpp> #include <string> #include <vector> @@ -9,8 +12,7 @@ #include <stdexcept> namespace commands { - typedef boost::function<std::vector<std::string> (const std::vector<std::string>&)> Handler; - extern std::map<std::string, commands::Handler> 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<std::string> execute(const std::vector<std::string>& args); + class Commands { + private: + typedef boost::function<std::vector<std::string> (Commands*)> Handler; + std::map<std::string, Handler> handlers; + + typedef boost::function<std::vector<MusicListing::p> (const std::string artist)> FindFunction; + std::map<std::string, FindFunction> find_handlers; + + boost::asio::io_service& io_service; + std::vector<std::string>& args; + + std::vector<std::string> ls(); + std::vector<std::string> find(); + std::vector<std::string> update(); + + public: + Commands(boost::asio::io_service& io_service, std::vector<std::string>& args); + std::vector<std::string> operator()(); + + }; }; #endif @@ -4,7 +4,6 @@ #include "encoder.h" #include "httpd.h" #include "telnetd.h" -#include "commands.h" #include <iostream> #include <vector> @@ -17,7 +16,6 @@ int main(int argc, char **argv) { music::init(config::vm["audist.music_root"].as<std::string>()); decoder::init(); encoder::init(); - commands::init(); boost::asio::io_service io_service; @@ -11,6 +11,7 @@ #include <boost/algorithm/string/split.hpp> #include <boost/cast.hpp> #include <boost/filesystem/fstream.hpp> +#include <boost/foreach.hpp> #include <soci/soci.h> fs::path music::root_directory; @@ -77,6 +78,21 @@ std::vector<MusicListing::p> 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"); @@ -41,6 +41,8 @@ namespace music { MusicListing::p get(const std::string& path); MusicDirectory::p get_directory(const std::string& path); std::vector<MusicListing::p> 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<std::string> 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'; |