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