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