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 |
_L<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.InsertLast(match[1]); |
31 |
else if (match = local(*arg)) |
32 |
locals.InsertLast(match[1]); |
33 |
} |
34 |
|
35 |
if (extensions.IsEmpty()) |
36 |
extensions.InsertLast(_B("mp3")); |
37 |
|
38 |
_S<DecentralizedMedia> media(extensions, locals); |
39 |
|
40 |
media.Block(); |
41 |
|
42 |
return 0; |
43 |
} |
44 |
|
45 |
DecentralizedMedia::DecentralizedMedia(const _L<ext::String>& extensions, const _L<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) |
46 |
{ |
47 |
if (!locals.IsEmpty()) |
48 |
{ |
49 |
_L<ext::String> args(locals); |
50 |
|
51 |
_foreach (const _L<ext::String>, extension, extensions) |
52 |
{ |
53 |
if (extension != _set.Begin()) |
54 |
args.InsertLast(_B("-or")); |
55 |
|
56 |
args.InsertLast(_B("-name")); |
57 |
args.InsertLast(_S<ios::String>() << _B("*.") << *extension); |
58 |
} |
59 |
|
60 |
_H<api::Process> find(new api::Process(_B("/usr/bin/find"), args)); |
61 |
ext::String file; |
62 |
|
63 |
while (ios::ReadLine(*find->GetReader(), file)) |
64 |
/*media.Insert(file)*/; |
65 |
|
66 |
find->Join(); |
67 |
} |
68 |
|
69 |
AddPort(6996); |
70 |
} |
71 |
|
72 |
DecentralizedMedia::~DecentralizedMedia() |
73 |
{ |
74 |
if (!process.IsEmpty()) |
75 |
{ |
76 |
_H<api::Thread> thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this))); |
77 |
|
78 |
bmp.Quit(); |
79 |
|
80 |
thread->Join(); |
81 |
} |
82 |
} |
83 |
|
84 |
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
85 |
{ |
86 |
if (request.method_ == _B("EXTENSIONS")) |
87 |
{ |
88 |
response.SetStatus(200); |
89 |
|
90 |
_foreach (const _L<ext::String>, extension, extensions) |
91 |
response << *extension << ios::NewLineNoFlush; |
92 |
} |
93 |
else if (request.method_ == _B("MEDIA")) |
94 |
{ |
95 |
// XXX: implement |
96 |
} |
97 |
else |
98 |
waf::Server::Process(request, response); |
99 |
} |