ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/DecentralizedMedia/DecentralizedMedia.cpp
Revision: 557
Committed: 2005-07-17T00:11:06-07:00 (19 years, 11 months ago) by douglas
File size: 3888 byte(s)
Log Message:
Oh, yeah.

File Contents

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

Properties

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