1 |
// Google Tron |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <cxx/standard.hh> |
8 |
|
9 |
#include <api/exename.hpp> |
10 |
#include <api/pcre/regex.hpp> |
11 |
#include <api/process.hpp> |
12 |
#include <app/simple.hpp> |
13 |
#include <hop/bind.hpp> |
14 |
#include <xml/textwriter.hpp> |
15 |
|
16 |
#include "GoogleTron.hpp" |
17 |
#include "Sleep.hpp" |
18 |
#include "Zlib/GzipWriter.hpp" |
19 |
|
20 |
namespace |
21 |
{ |
22 |
|
23 |
static struct Apr |
24 |
{ |
25 |
Apr() |
26 |
{ |
27 |
api::Apr::CheckError(::apr_initialize()); |
28 |
} |
29 |
|
30 |
~Apr() |
31 |
{ |
32 |
::apr_terminate(); |
33 |
} |
34 |
} |
35 |
arp; |
36 |
|
37 |
} |
38 |
|
39 |
int Main(const app::Options &options) |
40 |
{ |
41 |
cse::String sitemap(_B("sitemap.gz")), base; |
42 |
_L<_R<Command> > commands; |
43 |
|
44 |
api::Pcre::RegEx sitemap_(_B("^-sitemap=(.+)$")), base_(_B("^-base=(.+)$")), fs(_B("^-fs=(.*)$")), blog(_B("^-blog=(.*)$")), wiki(_B("^-wiki=(.*)$")); |
45 |
|
46 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
47 |
{ |
48 |
api::Pcre::RegEx::Match match; |
49 |
|
50 |
if (match = sitemap_(*arg)) |
51 |
sitemap = match[1]; |
52 |
else if (match = base_(*arg)) |
53 |
base = match[1]; |
54 |
else if (match = fs(*arg)) |
55 |
commands.InsertLast(new _H<FileSystemCommand>(match[1])); |
56 |
else if (match = blog(*arg)) |
57 |
commands.InsertLast(new _H<BlogCommand>(match[1])); |
58 |
else if (match = wiki(*arg)) |
59 |
commands.InsertLast(new _H<WikiCommand>(match[1])); |
60 |
else |
61 |
goto usage; |
62 |
} |
63 |
|
64 |
if (base.IsEmpty() || commands.IsEmpty()) |
65 |
{ |
66 |
usage: api::Cout << _B("Usage: ") << api::GetExecutablePath().GetName() << _B(" [-sitemap=.+] -base=.+ [-fs=.*] [-blog=.*] [-wiki=.*] [...]") << ios::NewLine; |
67 |
|
68 |
return 1; |
69 |
} |
70 |
|
71 |
GoogleTron tron(sitemap, base, commands); |
72 |
|
73 |
return 0; |
74 |
} |
75 |
|
76 |
GoogleTron::GoogleTron(const cse::String &sitemap, const cse::String &base, const _L<_R<Command> > &commands) : working(true), sitemap(sitemap), base(base), output(hop::BindAll(&GoogleTron::Output, this)) |
77 |
{ |
78 |
_foreach (const _L<_R<Command> >, command_, commands) |
79 |
if (_R<FileSystemCommand> command = dynamic_cast<FileSystemCommand *>(command_->GetValue())) |
80 |
threads.Add(hop::BindAll(&GoogleTron::FileSystem_, this, command)); |
81 |
else if (_R<BlogCommand> command = dynamic_cast<BlogCommand *>(command_->GetValue())) |
82 |
threads.Add(hop::BindAll(&GoogleTron::Blog_, this, command)); |
83 |
else if (_R<WikiCommand> command = dynamic_cast<WikiCommand *>(command_->GetValue())) |
84 |
threads.Add(hop::BindAll(&GoogleTron::Wiki_, this, command)); |
85 |
} |
86 |
|
87 |
GoogleTron::~GoogleTron() |
88 |
{ |
89 |
threads.Join(); |
90 |
|
91 |
working = false; |
92 |
|
93 |
output.Join(); |
94 |
} |
95 |
|
96 |
int GoogleTron::Output() |
97 |
{ |
98 |
_S<Zlib::GzipWriter> gzip(sitemap); |
99 |
_S<xml::TextWriter> writer(gzip); |
100 |
xml::ScopeElement urlset(writer, _B("urlset")); |
101 |
|
102 |
writer.SetAttribute(_B("xmlns"), _B("http://www.google.com/schemas/sitemap/0.84")); |
103 |
writer.SetAttribute(_B("xmlns:xsi"), _B("http://www.w3.org/2001/XMLSchema-instance")); |
104 |
writer.SetAttribute(_B("xsi:schemaLocation"), _B("http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd")); |
105 |
|
106 |
_synchronized (queueLock) |
107 |
do |
108 |
_desynchronized (queueLock) |
109 |
{ |
110 |
Sleep(); |
111 |
|
112 |
_synchronized (queueLock) |
113 |
if (queue.GetSize()) |
114 |
{ |
115 |
xml::ScopeElement url_(writer, _B("url")); |
116 |
const Url &url(queue.Front()); |
117 |
|
118 |
{ |
119 |
xml::ScopeElement loc(writer, _B("loc")); |
120 |
|
121 |
writer.OutputText(url.GetLocation()); |
122 |
} |
123 |
|
124 |
{ |
125 |
xml::ScopeElement lastmod(writer, _B("lastmod")); |
126 |
|
127 |
writer.OutputText(url.GetModified()); |
128 |
} |
129 |
|
130 |
{ |
131 |
xml::ScopeElement changefreq(writer, _B("changefreq")); |
132 |
|
133 |
writer.OutputText(url.GetFrequency()); |
134 |
} |
135 |
|
136 |
{ |
137 |
xml::ScopeElement priority(writer, _B("priority")); |
138 |
|
139 |
writer.OutputText(url.GetPriority()); |
140 |
} |
141 |
|
142 |
api::Cout << _B("Url: ") << url.GetLocation() << ios::NewLine << _B(" ") << url.GetModified() << _B(" ") << url.GetFrequency() << _B(" ") << url.GetPriority() << ios::NewLine; |
143 |
|
144 |
queue.Pop(); |
145 |
} |
146 |
} |
147 |
while (working || queue.GetSize()); |
148 |
|
149 |
return 0; |
150 |
} |
151 |
|
152 |
int GoogleTron::FileSystem_(const _R<FileSystemCommand> &command) |
153 |
{ |
154 |
FileSystem fs(sitemap, base, queue, queueLock, command); |
155 |
|
156 |
return 0; |
157 |
} |
158 |
|
159 |
int GoogleTron::Blog_(const _R<BlogCommand> &command) |
160 |
{ |
161 |
Blog blog(sitemap, base, queue, queueLock, command); |
162 |
|
163 |
return 0; |
164 |
} |
165 |
|
166 |
int GoogleTron::Wiki_(const _R<WikiCommand> &command) |
167 |
{ |
168 |
Wiki wiki(sitemap, base, queue, queueLock, command); |
169 |
|
170 |
return 0; |
171 |
} |