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 21 by douglas, 2004-07-16T23:37:41-07:00 vs.
Revision 60 by douglas, 2004-09-15T19:43:29-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 15 | Line 18
18   extern "C"
19   {
20   #include <pwd.h>
21 + #include <sys/utsname.h>
22   #include <unistd.h>
23   }
24  
25 < enum Color { reset, bright, dim, underscore = 4, blink, reverse = 7, hidden,
22 <        black = 30, red, green, yellow, blue, magenta, cyan, white, _black = 40,
23 <        _red, _green, _yellow, _blue, _magenta, _cyan, _white };
24 <
25 < inline std::ostream& operator<<(std::ostream& sout, Color color)
25 > struct ZoeCommand : public app::Application
26   {
27 <        sout << "\033[" << unsigned(color) << 'm';
28 <
29 <        return sout;
30 < }
31 <
32 < int main(int argc, char* argv[])
33 < {
34 <        Zoe::program = argv[0];
35 <
36 <        for (int index(1); index < argc; ++index)
27 >        virtual int Run(const app::ArgumentList& args)
28          {
29 <                ext::String arg(argv[index]);
39 <                Matcher matcher;
29 >                Zoe::program = api::GetExecutableName();
30  
31 <                if (arg == matcher("^-config=(.*)$"))
42 <                {
43 <                        Zoe::config = matcher[1];
44 <                }
45 <                else if (arg == "-collector")
46 <                {
47 <                        if (!Zoe::collector) Zoe::collector = true;
48 <                }
49 <                else if (arg == "-publisher")
50 <                {
51 <                        if (!Zoe::publisher) Zoe::publisher = true;
52 <                }
53 <                else if (arg == "-D")
31 >                for (size_t index(0); index < args.GetSize(); ++index)
32                  {
33 <                        if (!Zoe::debug) Zoe::debug = true;
34 <                }
57 <                else
58 <                {
59 <                        cerr << bright << "Usage: " << cyan << Zoe::program << reset << " ["
60 <                                << bright << blue <<  "-config=" << yellow << "config" << reset
61 <                                << "] [" << bright << blue << "-collector" << reset << "] ["
62 <                                << bright << blue << "-publisher" << reset << "] [" << bright
63 <                                << 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                  }
67        }
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();
77        initialize();
70  
71          Collector collector(login, password, buddies, database, Zoe::collector);
72          Publisher publisher(buddies, database, Zoe::publisher);
73   }
74  
75 < bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false);
75 > bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false),
76 >        Zoe::color(false);
77   ext::String Zoe::program, Zoe::config("zoe.xml");
78  
79 + void Zoe::usage()
80 + {
81 +        api::Cerr << "Usage: " << Zoe::program << " [-config=config] [-collector] "
82 +                "[-publisher] [-color] [-D]\n";
83 +
84 +        exit(1);
85 + }
86 +
87 + ext::String Zoe::generator(Generator generator)
88 + {
89 +        ext::String generator_(generator == all || generator == agent ? "Zoe" : "");
90 +
91 +        switch (generator)
92 +        {
93 +        case all:
94 +                generator_.InsertAllLast("/0.9");
95 +        case agent:
96 +                {
97 +                        utsname system;
98 +
99 +                        uname(&system);
100 +
101 +                        generator_.InsertAllLast(_R(" (") + system.sysname);
102 +                }
103 +
104 +                if (generator == agent)
105 +                {
106 +                        generator_.InsertAllLast(")");
107 +
108 +                        break;
109 +                }
110 +                else generator_.InsertAllLast("; ");
111 +        case url:
112 +                generator_.InsertAllLast("http://computers.douglasthrift.net/zoe.xml");
113 +
114 +                if (generator == all) generator_.InsertAllLast(")");
115 +
116 +                break;
117 +        case version:
118 +                generator_ = "0.9";
119 +        }
120 +
121 +        return generator_;
122 + }
123 +
124   void Zoe::configure()
125   {
126 <        if (debug) cerr << "config = " << config << '\n';
126 >        if (debug) api::Cerr << "config = " << config << "\n";
127  
128          ext::Handle<xml::Document> document(xml::Parse(config));
129          ext::Handle<xml::Node> zoe(*document/"zoe");
130  
131 <        login = std::string(*zoe/"login");
132 <        password = std::string(*zoe/"password");
133 <        database.driver = std::string(*zoe/"database"/"driver");
134 <        database.host = std::string(*zoe/"database"/"host");
135 <        database.user = std::string(*zoe/"database"/"user");
131 >        login = *zoe/"login";
132 >        password = *zoe/"password";
133 >        database.driver = *zoe/"database"/"driver";
134 >        database.host = *zoe/"database"/"host";
135 >        database.user = *zoe/"database"/"user";
136  
137          if ((*zoe/"database"/"password").IsEmpty())
138          {
139                  ext::String prompt(database.user + (!database.host.IsEmpty() ? "@" : "")
140 <                        + database.host + (!database.user.IsEmpty() ? "'s " : "") + "Databa"
141 <                        + "se Password: ");
140 >                        + database.host + (!database.user.IsEmpty() ? "'s " : "")
141 >                        + "Database Password: ");
142                  char* password(getpass(prompt.NullTerminate()));
143  
144                  database.password = password;
# Line 108 | Line 146 | void Zoe::configure()
146                  for (size_t index(std::strlen(password)); index > 0; --index)
147                          password[index - 1] = '\0';
148          }
149 <        else database.password = std::string(*zoe/"database"/"password");
149 >        else database.password = *zoe/"database"/"password";
150  
151 <        database.db = std::string(*zoe/"database"/"db");
151 >        database.db = *zoe/"database"/"db";
152  
153 <        if (debug) cerr << "login = " << login << "\npassword = "
154 <                << std::string(password.GetSize(), '*') << "\ndatabase = {\n   driver ="
155 <                << ' ' << database.driver << "\n   host=" << database.host << "\n   use"
156 <                << "r = " << database.user << "\n   password = "
157 <                << std::string(database.password.GetSize(), '*') << "\n   db = "
158 <                << database.db << "\n}\n";
153 >        if (debug) api::Cerr << "login = " << login << "\npassword = "
154 >                << std::string(password.GetSize(), '*') << "\ndatabase = {\n"
155 >                << "   driver = " << database.driver << "\n   host = " << database.host
156 >                << "\n   user = " << database.user << "\n"
157 >                << "   password = " << std::string(database.password.GetSize(), '*')
158 >                << "\n   db = " << database.db << "\n}\n";
159  
160 +        ext::String link(*zoe/"link");
161          xml::NodeSet buddies(*zoe/"buddy");
162  
163          for (xml::NodeSet::Iterator buddy(buddies.Begin()); buddy != buddies.End();
164 <                ++buddy) this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss"));
126 <
127 <        if (debug)
164 >                ++buddy)
165          {
166 <                cerr << "buddies = {\n";
167 <
168 <                for (std::set<Buddy>::const_iterator buddy(this->buddies.begin()); buddy
132 <                        != this->buddies.end(); ++buddy) cerr << "   " << *buddy << '\n';
166 >                Buddy buddy_(**buddy/"login", **buddy/"rss", **buddy/"atom",
167 >                        !(**buddy/"link").IsEmpty() ? **buddy/"link" : (link
168 >                        + ext::String(**buddy/"login") + ".html"));
169  
170 <                cerr << "}\n";
170 >                this->buddies.insert(buddy_);
171          }
172  
173 <        if (!collector && !publisher)
173 >        initialize();
174 >
175 >        if (debug)
176          {
177 <                collector = true;
178 <                publisher = true;
177 >                api::Cerr << "buddies = {\n";
178 >
179 >                for (std::set<Buddy>::const_iterator buddy(this->buddies.begin());
180 >                        buddy != this->buddies.end(); ++buddy)
181 >                {
182 >                        api::Cerr << "   " << *buddy << " = {\n      rss = "
183 >                                << buddy->getRss() << "\n      atom = " << buddy->getAtom()
184 >                                << "\n      link = " << buddy->getLink() << "\n      id = "
185 >                                << buddy->getId() << "\n   }\n";
186 >                }
187 >
188 >                api::Cerr << "}\n";
189          }
190  
191 <        if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector)
192 <                << "\npublisher = " << lexical_cast<ext::String>(publisher) << '\n';
191 >        if (debug) api::Cerr << "collector = " << collector << "\npublisher = "
192 >                << publisher << "\ncolor = " << color << "\n";
193   }
194  
195   void Zoe::initialize()
196   {
197 <        ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host,
197 >        ext::Handle<dbi::Driver> driver(dbi::GetDriver(database.driver));
198 >        ext::Handle<dbi::Connection> db(driver->Connect(database.host,
199                  database.user, database.password, database.db));
200          ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies"));
201          std::set<Buddy> buddies_;
202  
203          while (buddies->MoveNext())
204          {
205 <                Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
205 >                if (this->buddies.find(buddies->GetString("buddy")) != this->buddies.end())
206 >                {
207 >                        Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
208  
209 <                buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
210 <                buddies_.insert(buddy);
209 >                        buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
210 >                        buddies_.insert(buddy);
211 >                }
212          }
213  
214          std::vector<Buddy> difference;
# Line 165 | Line 217 | void Zoe::initialize()
217                  buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference,
218                  difference.begin()));
219  
220 <        for (std::vector<Buddy>::iterator buddy(difference.begin()); buddy !=
221 <                difference.end(); ++buddy)
220 >        for (std::vector<Buddy>::iterator buddy(difference.begin());
221 >                buddy != difference.end(); ++buddy)
222          {
223                  ext::Uuid id;
224  
225                  api::Uuid::CreateSequential(id);
226  
227                  buddy->setId(id);
228 <                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('" +
229 <                        lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy) +
230 <                        "')");
228 >                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('"
229 >                        + lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy)
230 >                        + "')");
231                  
232                  buddies_.insert(*buddy);
233          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines