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