1 |
// Decentralized Media |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <menes/c++/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/hop/bind.hpp> |
13 |
#include <menes/ios/helpers.hpp> |
14 |
#include <menes/net/http/request.hpp> |
15 |
#include <menes/net/http/response.hpp> |
16 |
#include <menes/xml/document.hpp> |
17 |
#include <menes/xml/parse.hpp> |
18 |
|
19 |
#include "DecentralizedMedia.hpp" |
20 |
|
21 |
int Main(const app::Options& options) |
22 |
{ |
23 |
api::Pcre::RegEx configuration_(_B("^-configuration=(.+)$")), extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$")), player_("^-player=(.+)$"); |
24 |
cse::String configuration(_B("DecentralizedMedia.xml")); |
25 |
ext::RedBlackSet<cse::String> extensions; |
26 |
ext::RedBlackSet<api::Path> locals; |
27 |
int player(0); |
28 |
|
29 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
30 |
{ |
31 |
api::Pcre::RegEx::Match match; |
32 |
|
33 |
if (match = configuration_(*arg)) |
34 |
configuration = match[1]; |
35 |
else if (match = extension(*arg)) |
36 |
extensions.Insert(match[1]); |
37 |
else if (match = local(*arg)) |
38 |
locals.Insert(api::Path(match[1]).GetRealPath()); |
39 |
else if (match = player_(*arg)) |
40 |
player = lexical_cast<int>(match[1]); |
41 |
} |
42 |
|
43 |
if (extensions.IsEmpty()) |
44 |
extensions.Insert(_B("mp3")); |
45 |
|
46 |
{ |
47 |
api::Path media(_B("Media")); |
48 |
|
49 |
if (!media.Exists()) |
50 |
media.CreateDirectory(); |
51 |
|
52 |
locals.Insert(media.GetRealPath()); |
53 |
} |
54 |
|
55 |
_S<DecentralizedMedia> media(configuration, extensions, locals, player); |
56 |
|
57 |
media.Block(); |
58 |
|
59 |
return 0; |
60 |
} |
61 |
|
62 |
DecentralizedMedia::DecentralizedMedia(const cse::String& configuration, const ext::RedBlackSet<cse::String>& extensions, const ext::RedBlackSet<api::Path>& locals, int player) : waf::Server(_B("Web")), connector(new _H<Connector>(configuration)), extensions(extensions), library(connector), player(connector, player) |
63 |
{ |
64 |
{ |
65 |
_L<cse::String> args; |
66 |
|
67 |
_foreach (const ext::RedBlackSet<cse::String>, extension, extensions) |
68 |
args.InsertLast(_S<ios::String>() << "-extension=" << *extension); |
69 |
|
70 |
_foreach (const ext::RedBlackSet<api::Path>, local, locals) |
71 |
args.InsertLast(_S<ios::String>() << "-local=" << *local); |
72 |
|
73 |
_S<api::Process> media(_B("Util/media"), args); |
74 |
|
75 |
Media(*media.GetReader()); |
76 |
|
77 |
media.Join(); |
78 |
} |
79 |
|
80 |
{ |
81 |
_L<cse::String> args; |
82 |
|
83 |
args.InsertLast(_B("-s")); |
84 |
args.InsertLast(_B("object/DecentralizedMedia.hh.gch")); |
85 |
args.InsertLast(_B("LOCALS=DecentralizedMedia.hh")); |
86 |
|
87 |
_S<api::Process> make(_B(CFG_GNU_MAKE), args); |
88 |
|
89 |
make.ClearWriter(); |
90 |
|
91 |
_S<ios::String> error; |
92 |
|
93 |
ios::ReadToWrite(*make.GetReader(), error); |
94 |
|
95 |
if (make.Join() != 0) |
96 |
throw ext::StringException(error); |
97 |
} |
98 |
|
99 |
AddPort(6996); |
100 |
} |
101 |
|
102 |
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
103 |
{ |
104 |
api::Cout << request.client_ << _B(" "); |
105 |
|
106 |
if (request.method_ == _B("EXTENSIONS")) |
107 |
{ |
108 |
api::Cout << "EXTENSIONS" << ios::NewLine; |
109 |
|
110 |
response.SetStatus(200); |
111 |
|
112 |
_foreach (const ext::RedBlackSet<cse::String>, extension, extensions) |
113 |
response << *extension << ios::NewLineNoFlush; |
114 |
} |
115 |
else if (request.method_ == _B("MEDIA")) |
116 |
{ |
117 |
api::Cout << "MEDIA" << ios::NewLine; |
118 |
|
119 |
if (!request.content_.IsEmpty()) |
120 |
{ |
121 |
api::InternetAddress host(request.client_); |
122 |
|
123 |
host.SetPort(6996); |
124 |
|
125 |
Media(*request.content_, host); |
126 |
|
127 |
response.SetStatus(204); |
128 |
} |
129 |
else |
130 |
response.SetStatus(411); |
131 |
} |
132 |
else |
133 |
waf::Server::Process(request, response); |
134 |
} |
135 |
|
136 |
void DecentralizedMedia::Media(ios::Reader& media, const api::Address& host) |
137 |
{ |
138 |
_R<xml::Document> document(xml::Parse(media)); |
139 |
api::Pcre::RegEx share("^[0-9a-f]+$"); |
140 |
|
141 |
_foreach (const xml::NodeSet, folder, *document/"media"/"folder") |
142 |
{ |
143 |
cse::String path(**folder/"path"); |
144 |
|
145 |
if (api::Pcre::RegEx::Match match = share(path)) |
146 |
{ |
147 |
_R<Share> share(new _H<Share>(connector, host, match[0])); |
148 |
|
149 |
path = share->path.GetPath(); |
150 |
|
151 |
player.Add(share); |
152 |
} |
153 |
|
154 |
Media(*folder, path, path); |
155 |
} |
156 |
} |
157 |
|
158 |
void DecentralizedMedia::Media(const _R<xml::Node>& folder, const api::Path& path, const api::Path& root) |
159 |
{ |
160 |
MediaFolder(connector, path, root); |
161 |
|
162 |
_foreach (const xml::NodeSet, file, *folder/"file") |
163 |
MediaFile(connector, path.GetChild(**file/"path"), **file/"artist", **file/"title", **file/"album", **file/"genre", root); |
164 |
|
165 |
_foreach (const xml::NodeSet, folder_, *folder/"folder") |
166 |
Media(*folder_, path.GetChild(**folder_/"path"), root); |
167 |
} |