summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-01-02 03:55:52 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2011-01-02 03:55:52 +0100
commite19efcb8e7ba2cf4d4ce59c5f76e78a41a19ba24 (patch)
treeef89bb164d0e06f30396e84804cbc33c074ac065
parent0473fa912e2d2531529fec14a201efa3b20e2ef3 (diff)
Command handling changes as a result of adding the update command.
-rw-r--r--commands.cpp33
-rw-r--r--commands.h28
-rw-r--r--main.cpp2
-rw-r--r--music.cpp16
-rw-r--r--music.h2
-rw-r--r--telnet_connection.cpp3
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);
}
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 <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
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 <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;
diff --git a/music.cpp b/music.cpp
index 6265771..9193b89 100644
--- a/music.cpp
+++ b/music.cpp
@@ -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");
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<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';