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 |
douglas |
484 |
#include <menes-dbi/driver.hpp> |
12 |
|
|
#include <menes-dbi/resultset.hpp> |
13 |
douglas |
471 |
#include <menes-etl/fnbind.hpp> |
14 |
douglas |
474 |
#include <menes-ios/helpers.hpp> |
15 |
douglas |
471 |
#include <menes-net/http/request.hpp> |
16 |
|
|
#include <menes-net/http/response.hpp> |
17 |
douglas |
470 |
|
18 |
douglas |
471 |
#include "DecentralizedMedia.hpp" |
19 |
douglas |
464 |
|
20 |
douglas |
471 |
int Main(const app::Options& options) |
21 |
douglas |
464 |
{ |
22 |
douglas |
474 |
_L<ext::String> extensions, locals; |
23 |
|
|
api::Pcre::RegEx extension(_B("^-extension=(.+)$")), local(_B("^-local=(.+)$")); |
24 |
douglas |
464 |
|
25 |
douglas |
474 |
_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 |
douglas |
481 |
_S<DecentralizedMedia> media(extensions, locals); |
39 |
douglas |
474 |
|
40 |
douglas |
472 |
media.Block(); |
41 |
|
|
|
42 |
douglas |
471 |
return 0; |
43 |
|
|
} |
44 |
douglas |
469 |
|
45 |
douglas |
484 |
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 |
douglas |
471 |
{ |
47 |
douglas |
474 |
if (!locals.IsEmpty()) |
48 |
|
|
{ |
49 |
|
|
_L<ext::String> args(locals); |
50 |
|
|
|
51 |
|
|
_foreach (const _L<ext::String>, extension, extensions) |
52 |
|
|
{ |
53 |
douglas |
476 |
if (extension != _set.Begin()) |
54 |
|
|
args.InsertLast(_B("-or")); |
55 |
|
|
|
56 |
douglas |
474 |
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 |
douglas |
476 |
/*media.Insert(file)*/; |
65 |
douglas |
474 |
|
66 |
|
|
find->Join(); |
67 |
|
|
} |
68 |
|
|
|
69 |
douglas |
471 |
AddPort(6996); |
70 |
|
|
} |
71 |
douglas |
469 |
|
72 |
douglas |
471 |
DecentralizedMedia::~DecentralizedMedia() |
73 |
douglas |
469 |
{ |
74 |
douglas |
471 |
if (!process.IsEmpty()) |
75 |
|
|
{ |
76 |
|
|
_H<api::Thread> thread(new api::Thread(etl::BindAll(&DecentralizedMedia::Destroy, this))); |
77 |
douglas |
470 |
|
78 |
douglas |
471 |
bmp.Quit(); |
79 |
douglas |
469 |
|
80 |
douglas |
471 |
thread->Join(); |
81 |
|
|
} |
82 |
douglas |
464 |
} |
83 |
douglas |
471 |
|
84 |
|
|
void DecentralizedMedia::Process(const net::Http::Request& request, net::Http::Response& response) |
85 |
|
|
{ |
86 |
douglas |
474 |
if (request.method_ == _B("EXTENSIONS")) |
87 |
|
|
{ |
88 |
|
|
response.SetStatus(200); |
89 |
|
|
|
90 |
|
|
_foreach (const _L<ext::String>, extension, extensions) |
91 |
douglas |
476 |
response << *extension << ios::NewLineNoFlush; |
92 |
douglas |
474 |
} |
93 |
|
|
else if (request.method_ == _B("MEDIA")) |
94 |
|
|
{ |
95 |
|
|
// XXX: implement |
96 |
|
|
} |
97 |
douglas |
471 |
else |
98 |
|
|
waf::Server::Process(request, response); |
99 |
|
|
} |