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")); |
41 |
> |
cse::String sitemap(_B("sitemap.gz")), base; |
42 |
> |
_L<_R<Command> > commands; |
43 |
|
|
44 |
< |
api::Pcre::RegEx sitemap_(_B("^-sitemap=(.+)$")); |
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 |
|
{ |
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 |
< |
{ |
62 |
< |
api::Cout << _B("Usage: ") << api::GetExecutablePath().GetName() << _B(" [-sitemap=sitemap]") << ios::NewLine; |
61 |
> |
goto usage; |
62 |
> |
} |
63 |
|
|
64 |
< |
return 1; |
65 |
< |
} |
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); |
71 |
> |
GoogleTron tron(sitemap, base, commands); |
72 |
|
|
73 |
|
return 0; |
74 |
|
} |
75 |
|
|
76 |
< |
GoogleTron::GoogleTron(const cse::String &sitemap) : working(true), sitemap(sitemap), output(new _H<api::Thread>(hop::BindAll(&GoogleTron::Output, this))) |
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 |
< |
// XXX: start some threads |
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 |
< |
// XXX: wait on threads |
90 |
< |
|
89 |
> |
threads.Join(); |
90 |
> |
|
91 |
|
working = false; |
92 |
|
|
93 |
< |
output->Join(); |
93 |
> |
output.Join(); |
94 |
|
} |
95 |
|
|
96 |
|
int GoogleTron::Output() |
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 |
< |
do |
107 |
< |
{ |
108 |
< |
Sleep(); |
72 |
< |
|
73 |
< |
_synchronized (queueLock) |
74 |
< |
if (queue.size()) |
106 |
> |
_synchronized (queueLock) |
107 |
> |
do |
108 |
> |
_desynchronized (queueLock) |
109 |
|
{ |
110 |
< |
xml::ScopeElement url_(writer, _B("url")); |
77 |
< |
const Url &url(queue.front()); |
110 |
> |
Sleep(); |
111 |
|
|
112 |
< |
{ |
113 |
< |
xml::ScopeElement loc(writer, _B("loc")); |
112 |
> |
_synchronized (queueLock) |
113 |
> |
if (queue.GetSize()) |
114 |
> |
{ |
115 |
> |
xml::ScopeElement url_(writer, _B("url")); |
116 |
> |
const Url &url(queue.Front()); |
117 |
|
|
118 |
< |
writer.OutputText(url.GetLocation()); |
119 |
< |
} |
118 |
> |
{ |
119 |
> |
xml::ScopeElement loc(writer, _B("loc")); |
120 |
|
|
121 |
< |
{ |
122 |
< |
xml::ScopeElement lastmod(writer, _B("lastmod")); |
121 |
> |
writer.OutputText(url.GetLocation()); |
122 |
> |
} |
123 |
|
|
124 |
< |
writer.OutputText(url.GetModified()); |
125 |
< |
} |
124 |
> |
{ |
125 |
> |
xml::ScopeElement lastmod(writer, _B("lastmod")); |
126 |
|
|
127 |
< |
{ |
128 |
< |
xml::ScopeElement changefreq(writer, _B("changefreq")); |
127 |
> |
writer.OutputText(url.GetModified()); |
128 |
> |
} |
129 |
|
|
130 |
< |
writer.OutputText(url.GetFrequency()); |
131 |
< |
} |
130 |
> |
{ |
131 |
> |
xml::ScopeElement changefreq(writer, _B("changefreq")); |
132 |
|
|
133 |
< |
{ |
134 |
< |
xml::ScopeElement priority(writer, _B("priority")); |
133 |
> |
writer.OutputText(url.GetFrequency()); |
134 |
> |
} |
135 |
|
|
136 |
< |
writer.OutputText(url.GetPriority()); |
137 |
< |
} |
136 |
> |
{ |
137 |
> |
xml::ScopeElement priority(writer, _B("priority")); |
138 |
|
|
139 |
< |
queue.pop(); |
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 |
< |
} |
148 |
< |
while (working); |
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 |
|
} |