19 |
|
|
20 |
|
int Main(const app::Options &options) |
21 |
|
{ |
22 |
< |
cse::String sitemap(_B("sitemap.gz")); |
22 |
> |
cse::String sitemap(_B("sitemap.gz")), base; |
23 |
> |
_L<_R<Command> > commands; |
24 |
|
|
25 |
< |
api::Pcre::RegEx sitemap_(_B("^-sitemap=(.+)$")); |
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 |
|
{ |
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 |
< |
{ |
43 |
< |
api::Cout << _B("Usage: ") << api::GetExecutablePath().GetName() << _B(" [-sitemap=sitemap]") << ios::NewLine; |
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; |
37 |
< |
} |
49 |
> |
return 1; |
50 |
|
} |
51 |
|
|
52 |
< |
GoogleTron tron(sitemap); |
52 |
> |
GoogleTron tron(sitemap, base, commands); |
53 |
|
|
54 |
|
return 0; |
55 |
|
} |
56 |
|
|
57 |
< |
GoogleTron::GoogleTron(const cse::String &sitemap) : working(true), sitemap(sitemap), output(new _H<api::Thread>(hop::BindAll(&GoogleTron::Output, this))) |
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 |
< |
// XXX: start some threads |
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 |
< |
// XXX: wait on threads |
73 |
< |
|
72 |
> |
threads.Join(); |
73 |
> |
|
74 |
|
working = false; |
75 |
|
|
76 |
< |
output->Join(); |
76 |
> |
output.Join(); |
77 |
> |
|
78 |
> |
::apr_terminate(); |
79 |
|
} |
80 |
|
|
81 |
|
int GoogleTron::Output() |
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 |
< |
do |
92 |
< |
{ |
93 |
< |
Sleep(); |
72 |
< |
|
73 |
< |
_synchronized (queueLock) |
74 |
< |
if (queue.size()) |
91 |
> |
_synchronized (queueLock) |
92 |
> |
do |
93 |
> |
_desynchronized (queueLock) |
94 |
|
{ |
95 |
< |
xml::ScopeElement url_(writer, _B("url")); |
96 |
< |
const Url &url(queue.front()); |
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 |
< |
{ |
107 |
< |
xml::ScopeElement loc(writer, _B("loc")); |
106 |
> |
writer.OutputText(url.GetLocation()); |
107 |
> |
} |
108 |
|
|
109 |
< |
writer.OutputText(url.GetLocation()); |
110 |
< |
} |
109 |
> |
{ |
110 |
> |
xml::ScopeElement lastmod(writer, _B("lastmod")); |
111 |
|
|
112 |
< |
{ |
113 |
< |
xml::ScopeElement lastmod(writer, _B("lastmod")); |
112 |
> |
writer.OutputText(url.GetModified()); |
113 |
> |
} |
114 |
|
|
115 |
< |
writer.OutputText(url.GetModified()); |
116 |
< |
} |
115 |
> |
{ |
116 |
> |
xml::ScopeElement changefreq(writer, _B("changefreq")); |
117 |
|
|
118 |
< |
{ |
119 |
< |
xml::ScopeElement changefreq(writer, _B("changefreq")); |
118 |
> |
writer.OutputText(url.GetFrequency()); |
119 |
> |
} |
120 |
|
|
121 |
< |
writer.OutputText(url.GetFrequency()); |
122 |
< |
} |
121 |
> |
{ |
122 |
> |
xml::ScopeElement priority(writer, _B("priority")); |
123 |
|
|
124 |
< |
{ |
125 |
< |
xml::ScopeElement priority(writer, _B("priority")); |
124 |
> |
writer.OutputText(url.GetPriority()); |
125 |
> |
} |
126 |
|
|
127 |
< |
writer.OutputText(url.GetPriority()); |
101 |
< |
} |
127 |
> |
api::Cout << _B("Url: ") << url.GetLocation() << ios::NewLine << _B(" ") << url.GetModified() << _B(" ") << url.GetFrequency() << _B(" ") << url.GetPriority() << ios::NewLine; |
128 |
|
|
129 |
< |
queue.pop(); |
129 |
> |
queue.Pop(); |
130 |
> |
} |
131 |
|
} |
132 |
< |
} |
133 |
< |
while (working); |
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 |
|
} |