ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/GoogleTron/GoogleTron.cpp
Revision: 688
Committed: 2006-03-07T02:05:22-08:00 (19 years, 3 months ago) by douglas
File size: 3417 byte(s)
Log Message:
Blog, some logic, removed some comment weirdness.

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id