ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/DecentralizedMedia/DecentralizedMedia.cpp
Revision: 493
Committed: 2005-06-17T03:26:33-07:00 (20 years ago) by douglas
File size: 4323 byte(s)
Log Message:
Do it!

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 if (!api::Path(_B("Media")).Exists())
39 api::Posix::CheckError(::mkdir("Media", 0755));
40
41 locals.Insert(_B("Media"));
42
43 _S<DecentralizedMedia> media(extensions, locals);
44
45 media.Block();
46
47 return 0;
48 }
49
50 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)
51 {
52 foreach (const ext::RedBlackSet<ext::String>, local, locals) if (!connection->Parse(_B("SELECT path FROM folders WHERE path = ?"))->Execute(*local)->MoveNext())
53 connection->Parse(_B("INSERT INTO folders (path) VALUES (?)"))->Execute(*local);
54
55 {
56 _L<ext::String> args(locals.Begin(), locals.End());
57
58 args.InsertLast(_B("-type"));
59 args.InsertLast(_B("d"));
60
61 _foreach (const ext::RedBlackSet<ext::String>, extension, extensions)
62 {
63 args.InsertLast(_B("-or"));
64 args.InsertLast(_B("-iname"));
65 args.InsertLast(_S<ios::String>() << _B("*.") << *extension);
66 }
67
68 _H<api::Process> find(new api::Process(_B("/usr/bin/find"), args));
69 ext::String file;
70
71 while (ios::ReadLine(*find->GetReader(), file)) if (!locals.Contains(file))
72 {
73 }
74
75 find->Join();
76 }
77
78 AddPort(6996);
79 }
80
81 DecentralizedMedia::~DecentralizedMedia()
82 {
83 if (!process.IsEmpty())
84 {
85 _H<api::Thread> thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this)));
86
87 bmp.Quit();
88
89 thread->Join();
90 }
91 }
92
93 _L<ext::String> DecentralizedMedia::GetArtists() const
94 {
95 _H<dbi::ResultSet> artists_(connection->Execute(_B("SELECT DISTINCT artist FROM files WHERE live = TRUE")));
96 _L<ext::String> artists;
97
98 while (artists_->MoveNext())
99 artists.InsertLast(artists_->GetString(_B("artist")));
100
101 return artists;
102 }
103
104 _L<ext::String> DecentralizedMedia::GetTitles() const
105 {
106 _H<dbi::ResultSet> titles_(connection->Execute(_B("SELECT DISTINCT title FROM files WHERE live = TRUE")));
107 _L<ext::String> titles;
108
109 while (titles_->MoveNext())
110 titles.InsertLast(titles_->GetString(_B("title")));
111
112 return titles;
113 }
114
115 _L<ext::String> DecentralizedMedia::GetAlbums() const
116 {
117 _H<dbi::ResultSet> albums_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE")));
118 _L<ext::String> albums;
119
120 while (albums_->MoveNext())
121 albums.InsertLast(albums_->GetString(_B("album")));
122
123 return albums;
124 }
125
126 _L<ext::String> DecentralizedMedia::GetGenres() const
127 {
128 _H<dbi::ResultSet> genres_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE")));
129 _L<ext::String> genres;
130
131 while (genres_->MoveNext())
132 genres.InsertLast(genres_->GetString(_B("genre")));
133
134 return genres;
135 }
136
137 _L<MediaFolder> DecentralizedMedia::GetFolders() const
138 {
139 _H<dbi::ResultSet> paths(connection->Execute(_B("SELECT DISTINCT root FROM files WHERE live = TRUE")));
140 _L<MediaFolder> folders;
141
142 while (paths->MoveNext())
143 folders.InsertLast(MediaFolder(connection, paths->GetString(_B("path"))));
144
145 return folders;
146 }
147
148 void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response)
149 {
150 if (request.method_ == _B("EXTENSIONS"))
151 {
152 response.SetStatus(200);
153
154 _foreach (const ext::RedBlackSet<ext::String>, extension, extensions)
155 response << *extension << ios::NewLineNoFlush;
156 }
157 else if (request.method_ == _B("MEDIA"))
158 {
159 // XXX: implement
160 }
161 else
162 waf::Server::Process(request, response);
163 }

Properties

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