1 |
douglas |
464 |
// Decentralized Media |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
douglas |
470 |
#include <menes/standard.hh> |
8 |
douglas |
464 |
|
9 |
douglas |
474 |
#include <menes-api/pcre/regex.hpp> |
10 |
douglas |
471 |
#include <menes-app/simple.hpp> |
11 |
|
|
#include <menes-etl/fnbind.hpp> |
12 |
douglas |
474 |
#include <menes-ios/helpers.hpp> |
13 |
douglas |
471 |
#include <menes-net/http/request.hpp> |
14 |
|
|
#include <menes-net/http/response.hpp> |
15 |
douglas |
470 |
|
16 |
douglas |
471 |
#include "DecentralizedMedia.hpp" |
17 |
douglas |
464 |
|
18 |
douglas |
471 |
int Main(const app::Options& options) |
19 |
douglas |
464 |
{ |
20 |
douglas |
474 |
_L<ext::String> extensions, locals; |
21 |
|
|
api::Pcre::RegEx extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$")); |
22 |
douglas |
464 |
|
23 |
douglas |
474 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
24 |
|
|
{ |
25 |
|
|
api::Pcre::RegEx::Match match; |
26 |
|
|
|
27 |
|
|
if (match = extension(*arg)) |
28 |
|
|
extensions.InsertLast(match[1]); |
29 |
|
|
else if (match = local(*arg)) |
30 |
|
|
locals.InsertLast(match[1]); |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
if (extensions.IsEmpty()) |
34 |
|
|
extensions.InsertLast(_B("mp3")); |
35 |
|
|
|
36 |
|
|
DecentralizedMedia media(extensions, locals); |
37 |
|
|
|
38 |
douglas |
472 |
media.Block(); |
39 |
|
|
|
40 |
douglas |
471 |
return 0; |
41 |
|
|
} |
42 |
douglas |
469 |
|
43 |
douglas |
474 |
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"))), extensions(extensions) |
44 |
douglas |
471 |
{ |
45 |
douglas |
474 |
if (!locals.IsEmpty()) |
46 |
|
|
{ |
47 |
|
|
_L<ext::String> args(locals); |
48 |
|
|
|
49 |
|
|
_foreach (const _L<ext::String>, extension, extensions) |
50 |
|
|
{ |
51 |
douglas |
476 |
if (extension != _set.Begin()) |
52 |
|
|
args.InsertLast(_B("-or")); |
53 |
|
|
|
54 |
douglas |
474 |
args.InsertLast(_B("-name")); |
55 |
|
|
args.InsertLast(_S<ios::String>() << _B("*.") << *extension); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
_H<api::Process> find(new api::Process(_B("/usr/bin/find"), args)); |
59 |
|
|
ext::String file; |
60 |
|
|
|
61 |
|
|
while (ios::ReadLine(*find->GetReader(), file)) |
62 |
douglas |
476 |
/*media.Insert(file)*/; |
63 |
douglas |
474 |
|
64 |
|
|
find->Join(); |
65 |
|
|
} |
66 |
|
|
|
67 |
douglas |
471 |
AddPort(6996); |
68 |
|
|
} |
69 |
douglas |
469 |
|
70 |
douglas |
471 |
DecentralizedMedia::~DecentralizedMedia() |
71 |
douglas |
469 |
{ |
72 |
douglas |
471 |
if (!process.IsEmpty()) |
73 |
|
|
{ |
74 |
|
|
_H<api::Thread> thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this))); |
75 |
douglas |
470 |
|
76 |
douglas |
471 |
bmp.Quit(); |
77 |
douglas |
469 |
|
78 |
douglas |
471 |
thread->Join(); |
79 |
|
|
} |
80 |
douglas |
464 |
} |
81 |
douglas |
471 |
|
82 |
|
|
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
83 |
|
|
{ |
84 |
douglas |
474 |
if (request.method_ == _B("EXTENSIONS")) |
85 |
|
|
{ |
86 |
|
|
response.SetStatus(200); |
87 |
|
|
|
88 |
|
|
_foreach (const _L<ext::String>, extension, extensions) |
89 |
douglas |
476 |
response << *extension << ios::NewLineNoFlush; |
90 |
douglas |
474 |
} |
91 |
|
|
else if (request.method_ == _B("MEDIA")) |
92 |
|
|
{ |
93 |
|
|
// XXX: implement |
94 |
|
|
} |
95 |
douglas |
471 |
else |
96 |
|
|
waf::Server::Process(request, response); |
97 |
|
|
} |