// Decentralized Media // // Douglas Thrift // // $Id$ #include #include #include #include #include #include #include #include "DecentralizedMedia.hpp" int Main(const app::Options& options) { _L extensions, locals; api::Pcre::RegEx extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$")); _foreach (const app::ArgumentList, arg, app::GetArguments()) { api::Pcre::RegEx::Match match; if (match = extension(*arg)) extensions.InsertLast(match[1]); else if (match = local(*arg)) locals.InsertLast(match[1]); } if (extensions.IsEmpty()) extensions.InsertLast(_B("mp3")); DecentralizedMedia media(extensions, locals); media.Block(); return 0; } DecentralizedMedia::DecentralizedMedia(const _L& extensions, const _L& locals) : waf::Server(_B("Web")), process(bmp.IsRunning() ? NULL : new api::Process(_B("/usr/X11R6/bin/beep-media-player"))), extensions(extensions) { if (!locals.IsEmpty()) { _L args(locals); _foreach (const _L, extension, extensions) { if (extension != _set.Begin()) args.InsertLast(_B("-or")); args.InsertLast(_B("-name")); args.InsertLast(_S() << _B("*.") << *extension); } _H find(new api::Process(_B("/usr/bin/find"), args)); ext::String file; while (ios::ReadLine(*find->GetReader(), file)) /*media.Insert(file)*/; find->Join(); } AddPort(6996); } DecentralizedMedia::~DecentralizedMedia() { if (!process.IsEmpty()) { _H thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this))); bmp.Quit(); thread->Join(); } } void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) { if (request.method_ == _B("EXTENSIONS")) { response.SetStatus(200); _foreach (const _L, extension, extensions) response << *extension << ios::NewLineNoFlush; } else if (request.method_ == _B("MEDIA")) { // XXX: implement } else waf::Server::Process(request, response); }