// Decentralized Media // // Douglas Thrift // // $Id$ #include #include #include #include #include #include #include #include #include #include #include #include "DecentralizedMedia.hpp" int Main(const app::Options& options) { _breakpoint(); ext::RedBlackSet extensions; ext::RedBlackSet 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.Insert(match[1]); else if (match = local(*arg)) locals.Insert(api::Path(match[1]).GetRealPath()); } if (extensions.IsEmpty()) extensions.Insert(_B("mp3")); { api::Path media(_B("Media")); if (!media.Exists()) media.CreateDirectory(); locals.Insert(media.GetRealPath()); } _S media(extensions, locals); media.Block(); return 0; } DecentralizedMedia::DecentralizedMedia(const ext::RedBlackSet& extensions, const ext::RedBlackSet& locals) : waf::Server(_B("Web")), process(bmp.IsRunning() ? NULL : new api::Process(_B("/usr/X11R6/bin/beep-media-player"))), connection(dbi::GetDriver("dbi::PgSql::Driver")->Connect("", "douglas", "", "media")), extensions(extensions) { if (!process.IsEmpty()) { process->ClearReader(); process->ClearWriter(); } connection->Execute(_B("UPDATE files SET live = FALSE")); { _L args; _foreach (const ext::RedBlackSet, extension, extensions) args.InsertLast(_S() << "-extension=" << *extension); _foreach (const ext::RedBlackSet, local, locals) args.InsertLast(_S() << "-local=" << *local); _S media(_B("Util/media"), args); Media(*media.GetReader()); media.Join(); } { _L args; args.InsertLast(_B("-s")); args.InsertLast(_B("object/DecentralizedMedia.hh.gch")); _S make(CFG_GNU_MAKE, args); make.ClearWriter(); _S error; ios::ReadToWrite(*make.GetReader(), error); if (make.Join() != 0) throw ext::StringException(error); } AddPort(6996); } DecentralizedMedia::~DecentralizedMedia() { if (!process.IsEmpty()) { _H thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this))); bmp.Quit(); thread->Join(); } } _L DecentralizedMedia::GetArtists() const { _H artists_(connection->Execute(_B("SELECT DISTINCT artist FROM files WHERE live = TRUE"))); _L artists; while (artists_->MoveNext()) artists.InsertLast(artists_->GetString(_B("artist"))); return artists; } _L DecentralizedMedia::GetTitles() const { _H titles_(connection->Execute(_B("SELECT DISTINCT title FROM files WHERE live = TRUE"))); _L titles; while (titles_->MoveNext()) titles.InsertLast(titles_->GetString(_B("title"))); return titles; } _L DecentralizedMedia::GetAlbums() const { _H albums_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); _L albums; while (albums_->MoveNext()) albums.InsertLast(albums_->GetString(_B("album"))); return albums; } _L DecentralizedMedia::GetGenres() const { _H genres_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); _L genres; while (genres_->MoveNext()) genres.InsertLast(genres_->GetString(_B("genre"))); return genres; } _L DecentralizedMedia::GetFolders() const { _H paths(connection->Execute(_B("SELECT DISTINCT root FROM files WHERE live = TRUE"))); _L folders; while (paths->MoveNext()) folders.InsertLast(MediaFolder(connection, paths->GetString(_B("path")))); return folders; } void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) { if (request.method_ == _B("EXTENSIONS") || request.method_ == _B("GET") && request.uri_.GetUri().IsEmpty()) { api::Cout << "EXTENSIONS" << ios::NewLine; response.SetStatus(200); _foreach (const ext::RedBlackSet, extension, extensions) response << *extension << ios::NewLineNoFlush; } else if (request.method_ == _B("MEDIA") || request.method_ == _B("POST") && request.uri_.GetUri().IsEmpty()) { api::Cout << "MEDIA" << ios::NewLine; if (!request.content_.IsEmpty()) { Media(*request.content_); response.SetStatus(204); } else response.SetStatus(402); } else waf::Server::Process(request, response); } void DecentralizedMedia::Media(ios::Reader& media) { _H document(xml::Parse(media)); api::Pcre::RegEx share("^//(.+)/(.+)$"); _foreach (const xml::NodeSet, folder, *document/"media"/"folder") { ext::String path(**folder/"path"); if (api::Pcre::RegEx::Match match = share(path)) { _H share(new Share(connection, match[1], match[2])); path = share->path.GetPath(); // XXX: these are probably going to need read/write locking sharesByPath[path] = share; sharesByHost[match[1]][match[2]] = share; share->Mount(); } Media(*folder, path, path); } } void DecentralizedMedia::Media(const _H& folder, const api::Path& path, const api::Path& root) { MediaFolder(connection, path, root); _foreach (const xml::NodeSet, file, *folder/"file") MediaFile(connection, path.GetChild(**file/"path"), **file/"artist", **file/"title", **file/"album", **file/"genre", root); _foreach (const xml::NodeSet, folder_, *folder/"folder") Media(*folder_, path.GetChild(**folder_/"path"), root); }