ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/DecentralizedMedia/DecentralizedMedia.cpp
Revision: 542
Committed: 2005-07-03T00:31:07-07:00 (19 years, 11 months ago) by douglas
File size: 3999 byte(s)
Log Message:
Grr!

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id