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 22 by douglas, 2004-07-17T00:54:46-07:00 vs.
Revision 59 by douglas, 2004-09-13T23:39:30-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 < int main(int argc, char* argv[])
25 > struct ZoeCommand : public app::Application
26   {
27 <        Zoe::program = argv[0];
24 <
25 <        for (int index(1); index < argc; ++index)
27 >        virtual int Run(const app::ArgumentList& args)
28          {
29 <                ext::String arg(argv[index]);
28 <                Matcher matcher;
29 >                Zoe::program = api::GetExecutableName();
30  
31 <                if (arg == matcher("^-config=(.*)$"))
31 <                {
32 <                        Zoe::config = matcher[1];
33 <                }
34 <                else if (arg == "-collector")
35 <                {
36 <                        if (!Zoe::collector) Zoe::collector = true;
37 <                }
38 <                else if (arg == "-publisher")
31 >                for (size_t index(0); index < args.GetSize(); ++index)
32                  {
33 <                        if (!Zoe::publisher) Zoe::publisher = true;
34 <                }
42 <                else if (arg == "-D")
43 <                {
44 <                        if (!Zoe::debug) Zoe::debug = true;
45 <                }
46 <                else
47 <                {
48 <                        cerr << bright << "Usage: " << cyan << Zoe::program << reset << " ["
49 <                                << bright << blue <<  "-config=" << yellow << "config" << reset
50 <                                << "] [" << bright << blue << "-collector" << reset << "] ["
51 <                                << bright << blue << "-publisher" << reset << "] [" << bright
52 <                                << 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                  }
56        }
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 69 | 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 + 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 +        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);
101 +
102 +                        generator_.InsertAllLast(_R(" (") + 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()
126   {
127          if (debug) cerr << "config = " << config << '\n';
# Line 79 | 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 97 | 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 << '\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  
126        if (!collector && !publisher)
127        {
128                collector = true;
129                publisher = true;
130        }
131
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()
195   {
196 <        ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host,
196 >        ext::Handle<dbi::Driver> driver(dbi::GetDriver(database.driver));
197 >        ext::Handle<dbi::Connection> db(driver->Connect(database.host,
198                  database.user, database.password, database.db));
199          ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies"));
200          std::set<Buddy> buddies_;
201  
202          while (buddies->MoveNext())
203          {
204 <                Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
204 >                if (this->buddies.find(buddies->GetString("buddy")) != this->buddies.end())
205 >                {
206 >                        Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
207  
208 <                buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
209 <                buddies_.insert(buddy);
208 >                        buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
209 >                        buddies_.insert(buddy);
210 >                }
211          }
212  
213          std::vector<Buddy> difference;
# Line 154 | Line 216 | void Zoe::initialize()
216                  buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference,
217                  difference.begin()));
218  
219 <        for (std::vector<Buddy>::iterator buddy(difference.begin()); buddy !=
220 <                difference.end(); ++buddy)
219 >        for (std::vector<Buddy>::iterator buddy(difference.begin());
220 >                buddy != difference.end(); ++buddy)
221          {
222                  ext::Uuid id;
223  
224                  api::Uuid::CreateSequential(id);
225  
226                  buddy->setId(id);
227 <                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('" +
228 <                        lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy) +
229 <                        "')");
227 >                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('"
228 >                        + lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy)
229 >                        + "')");
230                  
231                  buddies_.insert(*buddy);
232          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines