ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/zoe/trunk/Zoe.cpp
(Generate patch)

Comparing trunk/Zoe.cpp (file contents):
Revision 31 by douglas, 2004-07-20T00:11:56-07:00 vs.
Revision 58 by douglas, 2004-09-12T15:18:25-07:00

# Line 8 | Line 8
8   #include "Collector.hpp"
9   #include "Publisher.hpp"
10  
11 + #include <menes-api/exename.hpp>
12 + #include <menes-app/application.hpp>
13 +
14   #include <algorithm>
15   #include <cstring>
16   #include <iterator>
# Line 19 | Line 22 | extern "C"
22   #include <unistd.h>
23   }
24  
25 < int main(int argc, char* argv[])
25 > struct ZoeCommand : public app::Application
26   {
27 <        Zoe::program = argv[0];
25 <
26 <        for (int index(1); index < argc; ++index)
27 >        virtual int Run(const app::ArgumentList& args)
28          {
29 <                ext::String arg(argv[index]);
29 <                Matcher matcher;
29 >                Zoe::program = api::GetExecutableName();
30  
31 <                if (arg == matcher("^-config=(.*)$"))
32 <                {
33 <                        Zoe::config = matcher[1];
34 <                }
35 <                else if (arg == "-collector")
36 <                {
37 <                        if (!Zoe::collector) Zoe::collector = true;
38 <                }
39 <                else if (arg == "-publisher")
40 <                {
41 <                        if (!Zoe::publisher) Zoe::publisher = true;
42 <                }
43 <                else if (arg == "-D")
44 <                {
45 <                        if (!Zoe::debug) Zoe::debug = true;
46 <                }
47 <                else
31 >                for (size_t index(0); index < args.GetSize(); ++index)
32                  {
33 <                        cerr << bright << "Usage: " << cyan << Zoe::program << reset << " ["
34 <                                << bright << blue <<  "-config=" << yellow << "config" << reset
51 <                                << "] [" << bright << blue << "-collector" << reset << "] ["
52 <                                << bright << blue << "-publisher" << reset << "] [" << bright
53 <                                << blue << "-D" << reset << "]\n";
33 >                        ext::String arg(args[index]);
34 >                        Matcher matcher;
35  
36 <                        return 1;
36 >                        if (arg == matcher("^-config=(.*)$"))
37 >                        {
38 >                                Zoe::config = matcher[1];
39 >                        }
40 >                        else if (arg == "-collector")
41 >                        {
42 >                                if (!Zoe::collector) Zoe::collector = true;
43 >                        }
44 >                        else if (arg == "-publisher")
45 >                        {
46 >                                if (!Zoe::publisher) Zoe::publisher = true;
47 >                        }
48 >                        else if (arg == "-color")
49 >                        {
50 >                                if (!Zoe::color) Zoe::color = true;
51 >                        }
52 >                        else if (arg == "-D")
53 >                        {
54 >                                if (!Zoe::debug) Zoe::debug = true;
55 >                        }
56 >                        else Zoe::usage();
57                  }
57        }
58  
59 <        Zoe zoe;
59 >                Zoe zoe;
60  
61 <        return 0;
62 < }
61 >                return 0;
62 >        }
63 > } zoe;
64  
65   Zoe::Zoe()
66   {
67 +        if (!(collector || publisher)) usage();
68 +        
69          configure();
70          initialize();
71  
# Line 70 | Line 73 | Zoe::Zoe()
73          Publisher publisher(buddies, database, Zoe::publisher);
74   }
75  
76 < bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false);
76 > bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false),
77 >        Zoe::color(false);
78   ext::String Zoe::program, Zoe::config("zoe.xml");
79  
80 < ext::String Zoe::generator()
80 > void Zoe::usage()
81 > {
82 >        cerr << "Usage: " << Zoe::program << " [-config=config] [-collector] "
83 >                "[-publisher] [-color] [-D]\n";
84 >
85 >        exit(1);
86 > }
87 >
88 > ext::String Zoe::generator(Generator generator)
89   {
90 <        utsname system;
90 >        ext::String generator_(generator == all || generator == agent ? "Zoe" : "");
91 >
92 >        switch (generator)
93 >        {
94 >        case all:
95 >                generator_.InsertAllLast("/0.9");
96 >        case agent:
97 >                {
98 >                        utsname system;
99  
100 <        uname(&system);
100 >                        uname(&system);
101  
102 <        return ext::String("Zoe/0.9 (") + system.sysname + "; http://computers.doug"
103 <                + "lasthrift.net/zoe.xml)";
102 >                        generator_.InsertAllLast(_S(" (") + system.sysname);
103 >                }
104 >
105 >                if (generator == agent)
106 >                {
107 >                        generator_.InsertAllLast(")");
108 >
109 >                        break;
110 >                }
111 >                else generator_.InsertAllLast("; ");
112 >        case url:
113 >                generator_.InsertAllLast("http://computers.douglasthrift.net/zoe.xml");
114 >
115 >                if (generator == all) generator_.InsertAllLast(")");
116 >
117 >                break;
118 >        case version:
119 >                generator_ = "0.9";
120 >        }
121 >
122 >        return generator_;
123   }
124  
125   void Zoe::configure()
# Line 90 | Line 129 | void Zoe::configure()
129          ext::Handle<xml::Document> document(xml::Parse(config));
130          ext::Handle<xml::Node> zoe(*document/"zoe");
131  
132 <        login = std::string(*zoe/"login");
133 <        password = std::string(*zoe/"password");
134 <        database.driver = std::string(*zoe/"database"/"driver");
135 <        database.host = std::string(*zoe/"database"/"host");
136 <        database.user = std::string(*zoe/"database"/"user");
132 >        login = *zoe/"login";
133 >        password = *zoe/"password";
134 >        database.driver = *zoe/"database"/"driver";
135 >        database.host = *zoe/"database"/"host";
136 >        database.user = *zoe/"database"/"user";
137  
138          if ((*zoe/"database"/"password").IsEmpty())
139          {
140                  ext::String prompt(database.user + (!database.host.IsEmpty() ? "@" : "")
141 <                        + database.host + (!database.user.IsEmpty() ? "'s " : "") + "Databa"
142 <                        + "se Password: ");
141 >                        + database.host + (!database.user.IsEmpty() ? "'s " : "")
142 >                        + "Database Password: ");
143                  char* password(getpass(prompt.NullTerminate()));
144  
145                  database.password = password;
# Line 108 | Line 147 | void Zoe::configure()
147                  for (size_t index(std::strlen(password)); index > 0; --index)
148                          password[index - 1] = '\0';
149          }
150 <        else database.password = std::string(*zoe/"database"/"password");
150 >        else database.password = *zoe/"database"/"password";
151  
152 <        database.db = std::string(*zoe/"database"/"db");
152 >        database.db = *zoe/"database"/"db";
153  
154 <        if (debug) cerr << "login = " << login << "\npassword = "
155 <                << std::string(password.GetSize(), '*') << "\ndatabase = {\n   driver ="
156 <                << ' ' << database.driver << "\n   host=" << database.host << "\n   use"
157 <                << "r = " << database.user << "\n   password = "
158 <                << std::string(database.password.GetSize(), '*') << "\n   db = "
159 <                << database.db << "\n}\n";
154 >        if (debug) cerr << "login = " << login << "\npassword = "
155 >                << std::string(password.GetSize(), '*') << "\ndatabase = {\n"
156 >                << "   driver = " << database.driver << "\n   host = " << database.host
157 >                << "\n   user = " << database.user << '\n'
158 >                << "   password = " << std::string(database.password.GetSize(), '*')
159 >                << "\n   db = " << database.db << "\n}\n";
160  
161 +        ext::String link(*zoe/"link");
162          xml::NodeSet buddies(*zoe/"buddy");
163  
164          for (xml::NodeSet::Iterator buddy(buddies.Begin()); buddy != buddies.End();
165 <                ++buddy) this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss"));
165 >                ++buddy)
166 >        {
167 >                Buddy buddy_(**buddy/"login", **buddy/"rss", **buddy/"atom",
168 >                        !(**buddy/"link").IsEmpty() ? **buddy/"link" : (link
169 >                        + ext::String(**buddy/"login") + ".html"));
170 >
171 >                this->buddies.insert(buddy_);
172 >        }
173  
174          if (debug)
175          {
176                  cerr << "buddies = {\n";
177  
178 <                for (std::set<Buddy>::const_iterator buddy(this->buddies.begin()); buddy
179 <                        != this->buddies.end(); ++buddy) cerr << "   " << *buddy << " = "
180 <                        << buddy->getRss() << '\n';
178 >                for (std::set<Buddy>::const_iterator buddy(this->buddies.begin());
179 >                        buddy != this->buddies.end(); ++buddy)
180 >                {
181 >                        cerr << "   " << *buddy << " = {\n      rss = " << buddy->getRss()
182 >                                << "\n      atom = " << buddy->getAtom() << "\n      link = "
183 >                                << buddy->getLink() << "\n   }\n";
184 >                }
185  
186                  cerr << "}\n";
187          }
188  
138        if (!collector && !publisher)
139        {
140                collector = true;
141                publisher = true;
142        }
143
189          if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector)
190 <                << "\npublisher = " << lexical_cast<ext::String>(publisher) << '\n';
190 >                << "\npublisher = " << lexical_cast<ext::String>(publisher)
191 >                << "\ncolor = " << lexical_cast<ext::String>(color) << '\n';
192   }
193  
194   void Zoe::initialize()
# Line 154 | Line 200 | void Zoe::initialize()
200  
201          while (buddies->MoveNext())
202          {
203 <                Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
203 >                if (this->buddies.find(buddies->GetString("buddy")) != this->buddies.end())
204 >                {
205 >                        Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
206  
207 <                buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
208 <                buddies_.insert(buddy);
207 >                        buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
208 >                        buddies_.insert(buddy);
209 >                }
210          }
211  
212          std::vector<Buddy> difference;
# Line 166 | Line 215 | void Zoe::initialize()
215                  buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference,
216                  difference.begin()));
217  
218 <        for (std::vector<Buddy>::iterator buddy(difference.begin()); buddy !=
219 <                difference.end(); ++buddy)
218 >        for (std::vector<Buddy>::iterator buddy(difference.begin());
219 >                buddy != difference.end(); ++buddy)
220          {
221                  ext::Uuid id;
222  
223                  api::Uuid::CreateSequential(id);
224  
225                  buddy->setId(id);
226 <                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('" + id + "', '" +
227 <                        *buddy + "')");
226 >                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('"
227 >                        + lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy)
228 >                        + "')");
229                  
230                  buddies_.insert(*buddy);
231          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines