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