ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/DecentralizedMedia/DecentralizedMedia.cpp
Revision: 491
Committed: 2005-06-17T02:19:32-07:00 (20 years ago) by douglas
File size: 3958 byte(s)
Log Message:
Whoa!

File Contents

# Content
1 // Decentralized Media
2 //
3 // Douglas Thrift
4 //
5 // $Id$
6
7 #include <menes/standard.hh>
8
9 #include <menes-api/pcre/regex.hpp>
10 #include <menes-app/simple.hpp>
11 #include <menes-dbi/driver.hpp>
12 #include <menes-dbi/resultset.hpp>
13 #include <menes-etl/fnbind.hpp>
14 #include <menes-ios/helpers.hpp>
15 #include <menes-net/http/request.hpp>
16 #include <menes-net/http/response.hpp>
17
18 #include "DecentralizedMedia.hpp"
19
20 int Main(const app::Options& options)
21 {
22 ext::RedBlackSet<ext::String> extensions, locals;
23 api::Pcre::RegEx extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$"));
24
25 _foreach (const app::ArgumentList, arg, app::GetArguments())
26 {
27 api::Pcre::RegEx::Match match;
28
29 if (match = extension(*arg))
30 extensions.Insert(match[1]);
31 else if (match = local(*arg))
32 locals.Insert(match[1]);
33 }
34
35 if (extensions.IsEmpty())
36 extensions.Insert(_B("mp3"));
37
38 locals.Insert("Media");
39
40 _S<DecentralizedMedia> media(extensions, locals);
41
42 media.Block();
43
44 return 0;
45 }
46
47 DecentralizedMedia::DecentralizedMedia(const ext::RedBlackSet<ext::String>& extensions, const ext::RedBlackSet<ext::String>& locals) : waf::Server(_B("Web")), process(bmp.IsRunning() ? NULL : new api::Process(_B("/usr/X11R6/bin/beep-media-player"))), connection(dbi::GetDriver("pgsql")->Connect("", "douglas", "", "media")), extensions(extensions)
48 {
49 if (!locals.IsEmpty())
50 {
51 _L<ext::String> args(locals.Begin(), locals.End());
52
53 _foreach (const ext::RedBlackSet<ext::String>, extension, extensions)
54 {
55 if (extension != _set.Begin())
56 args.InsertLast(_B("-or"));
57
58 args.InsertLast(_B("-name"));
59 args.InsertLast(_S<ios::String>() << _B("*.") << *extension);
60 }
61
62 _H<api::Process> find(new api::Process(_B("/usr/bin/find"), args));
63 ext::String file;
64
65 while (ios::ReadLine(*find->GetReader(), file))
66 /*media.Insert(file)*/;
67
68 find->Join();
69 }
70
71 AddPort(6996);
72 }
73
74 DecentralizedMedia::~DecentralizedMedia()
75 {
76 if (!process.IsEmpty())
77 {
78 _H<api::Thread> thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this)));
79
80 bmp.Quit();
81
82 thread->Join();
83 }
84 }
85
86 _L<ext::String> DecentralizedMedia::GetArtists() const
87 {
88 _H<dbi::ResultSet> artists_(connection->Execute(_B("SELECT DISTINCT artist FROM files WHERE live = TRUE")));
89 _L<ext::String> artists;
90
91 while (artists_->MoveNext())
92 artists.InsertLast(artists_->GetString(_B("artist")));
93
94 return artists;
95 }
96
97 _L<ext::String> DecentralizedMedia::GetTitles() const
98 {
99 _H<dbi::ResultSet> titles_(connection->Execute(_B("SELECT DISTINCT title FROM files WHERE live = TRUE")));
100 _L<ext::String> titles;
101
102 while (titles_->MoveNext())
103 titles.InsertLast(titles_->GetString(_B("title")));
104
105 return titles;
106 }
107
108 _L<ext::String> DecentralizedMedia::GetAlbums() const
109 {
110 _H<dbi::ResultSet> albums_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE")));
111 _L<ext::String> albums;
112
113 while (albums_->MoveNext())
114 albums.InsertLast(albums_->GetString(_B("album")));
115
116 return albums;
117 }
118
119 _L<ext::String> DecentralizedMedia::GetGenres() const
120 {
121 _H<dbi::ResultSet> genres_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE")));
122 _L<ext::String> genres;
123
124 while (genres_->MoveNext())
125 genres.InsertLast(genres_->GetString(_B("genre")));
126
127 return genres;
128 }
129
130 _L<MediaFolder> DecentralizedMedia::GetFolders() const
131 {
132 _H<dbi::ResultSet> paths(connection->Execute(_B("SELECT DISTINCT root FROM files WHERE live = TRUE")));
133 _L<MediaFolder> folders;
134
135 while (paths->MoveNext())
136 folders.InsertLast(MediaFolder(connection, paths->GetString(_B("path"))));
137
138 return folders;
139 }
140
141 void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response)
142 {
143 if (request.method_ == _B("EXTENSIONS"))
144 {
145 response.SetStatus(200);
146
147 _foreach (const _L<ext::String>, extension, extensions)
148 response << *extension << ios::NewLineNoFlush;
149 }
150 else if (request.method_ == _B("MEDIA"))
151 {
152 // XXX: implement
153 }
154 else
155 waf::Server::Process(request, response);
156 }

Properties

Name Value
svn:eol-style native
svn:keywords Id