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")); |
23 |
_L<_R<Command> > commands; |
24 |
|
25 |
api::Pcre::RegEx sitemap_(_B("^-sitemap=(.+)$")), fs_(_B("^-fs=(.*)$")), exclude(_B("^-exclude=(.+)$")); |
26 |
|
27 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
28 |
{ |
29 |
api::Pcre::RegEx::Match match; |
30 |
_R<FileSystemCommand> fs; |
31 |
|
32 |
if (match = sitemap_(*arg)) |
33 |
sitemap = match[1]; |
34 |
else if (match = fs_(*arg)) |
35 |
commands.InsertLast(new _H<FileSystemCommand>(match[1])); |
36 |
else if (commands.GetSize() && (fs = dynamic_cast<FileSystemCommand *>(commands.Last().GetValue())) && (match = exclude(*arg))) |
37 |
fs->excludes.InsertLast(match[1]); |
38 |
else |
39 |
{ |
40 |
api::Cout << _B("Usage: ") << api::GetExecutablePath().GetName() << _B(" [-sitemap=.+] [-fs=.* [-exclude=.+] ...] ...") << ios::NewLine; |
41 |
|
42 |
return 1; |
43 |
} |
44 |
} |
45 |
|
46 |
GoogleTron tron(sitemap, commands); |
47 |
|
48 |
return 0; |
49 |
} |
50 |
|
51 |
GoogleTron::GoogleTron(const cse::String &sitemap, const _L<_R<Command> > &commands) : working(true), sitemap(sitemap), output(hop::BindAll(&GoogleTron::Output, this)) |
52 |
{ |
53 |
_foreach (const _L<_R<Command> >, command_, commands) |
54 |
if (_R<FileSystemCommand> command = dynamic_cast<FileSystemCommand *>(command_->GetValue())) |
55 |
threads.Add(hop::BindAll(&GoogleTron::FileSystem_, this, command)); |
56 |
} |
57 |
|
58 |
GoogleTron::~GoogleTron() |
59 |
{ |
60 |
threads.Join(); |
61 |
|
62 |
working = false; |
63 |
|
64 |
output.Join(); |
65 |
} |
66 |
|
67 |
int GoogleTron::Output() |
68 |
{ |
69 |
_S<Zlib::GzipWriter> gzip(sitemap); |
70 |
_S<xml::TextWriter> writer(gzip); |
71 |
xml::ScopeElement urlset(writer, _B("urlset")); |
72 |
|
73 |
writer.SetAttribute(_B("xmlns"), _B("http://www.google.com/schemas/sitemap/0.84")); |
74 |
writer.SetAttribute(_B("xmlns:xsi"), _B("http://www.w3.org/2001/XMLSchema-instance")); |
75 |
writer.SetAttribute(_B("xsi:schemaLocation"), _B("http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd")); |
76 |
|
77 |
do |
78 |
{ |
79 |
Sleep(); |
80 |
|
81 |
_synchronized (queueLock) |
82 |
if (queue.size()) |
83 |
{ |
84 |
xml::ScopeElement url_(writer, _B("url")); |
85 |
const Url &url(queue.front()); |
86 |
|
87 |
{ |
88 |
xml::ScopeElement loc(writer, _B("loc")); |
89 |
|
90 |
writer.OutputText(url.GetLocation()); |
91 |
} |
92 |
|
93 |
{ |
94 |
xml::ScopeElement lastmod(writer, _B("lastmod")); |
95 |
|
96 |
writer.OutputText(url.GetModified()); |
97 |
} |
98 |
|
99 |
{ |
100 |
xml::ScopeElement changefreq(writer, _B("changefreq")); |
101 |
|
102 |
writer.OutputText(url.GetFrequency()); |
103 |
} |
104 |
|
105 |
{ |
106 |
xml::ScopeElement priority(writer, _B("priority")); |
107 |
|
108 |
writer.OutputText(url.GetPriority()); |
109 |
} |
110 |
|
111 |
queue.pop(); |
112 |
} |
113 |
} |
114 |
while (working); |
115 |
|
116 |
return 0; |
117 |
} |
118 |
|
119 |
int GoogleTron::FileSystem_(const _R<FileSystemCommand> &command) |
120 |
{ |
121 |
FileSystem fs(sitemap, queue, queueLock, command); |
122 |
|
123 |
return 0; |
124 |
} |