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 34 by douglas, 2004-07-20T16:49:06-07:00 vs.
Revision 70 by douglas, 2004-12-16T11:43:53-08:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines