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 25 by douglas, 2004-07-17T16:29:02-07:00 vs.
Revision 69 by douglas, 2004-12-15T16:14:13-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 >                for (size_t index(0); index < args.GetSize(); ++index)
33                  {
34 <                        if (!Zoe::publisher) Zoe::publisher = true;
35 <                }
43 <                else if (arg == "-D")
44 <                {
45 <                        if (!Zoe::debug) Zoe::debug = true;
46 <                }
47 <                else
48 <                {
49 <                        cerr << bright << "Usage: " << cyan << Zoe::program << reset << " ["
50 <                                << bright << blue <<  "-config=" << yellow << "config" << reset
51 <                                << "] [" << bright << blue << "-collector" << reset << "] ["
52 <                                << bright << blue << "-publisher" << reset << "] [" << bright
53 <                                << blue << "-D" << reset << "]\n";
34 >                        ext::String arg(args[index]);
35 >                        Matcher matcher;
36  
37 <                        return 1;
37 >                        if (arg == matcher("^-config=(.*)$"))
38 >                        {
39 >                                Zoe::config = matcher[1];
40 >                        }
41 >                        else if (arg == "-collector")
42 >                        {
43 >                                if (!Zoe::collector) Zoe::collector = true;
44 >                        }
45 >                        else if (arg == "-publisher")
46 >                        {
47 >                                if (!Zoe::publisher) Zoe::publisher = true;
48 >                        }
49 >                        else if (arg == "-color")
50 >                        {
51 >                                if (!Zoe::color) Zoe::color = true;
52 >                        }
53 >                        else if (arg == "-D")
54 >                        {
55 >                                if (!Zoe::debug) Zoe::debug = true;
56 >                        }
57 >                        else Zoe::usage();
58                  }
57        }
59  
60 <        Zoe zoe;
60 >                Zoe zoe;
61  
62 <        return 0;
63 < }
62 >                return 0;
63 >        }
64 > } zoe;
65  
66   Zoe::Zoe()
67   {
68 +        if (!(collector || publisher)) usage();
69 +        
70          configure();
67        initialize();
71  
72          Collector collector(login, password, buddies, database, Zoe::collector);
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 >        api::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(ios::String() << " (" << 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';
127 >        if (debug) api::Cerr << "config = " << config << "\n";
128  
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) api::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"));
126 <
127 <        if (debug)
165 >                ++buddy)
166          {
167 <                cerr << "buddies = {\n";
167 >                Buddy buddy_(**buddy/"login", **buddy/"rss", **buddy/"atom",
168 >                        !(**buddy/"link").IsEmpty() ? **buddy/"link" : (link
169 >                        + ext::String(**buddy/"login") + ".html"));
170  
171 <                for (std::set<Buddy>::const_iterator buddy(this->buddies.begin()); buddy
132 <                        != this->buddies.end(); ++buddy) cerr << "   " << *buddy << '\n';
133 <
134 <                cerr << "}\n";
171 >                this->buddies.insert(buddy_);
172          }
173  
174 <        if (!collector && !publisher)
174 >        initialize();
175 >
176 >        if (debug)
177          {
178 <                collector = true;
179 <                publisher = true;
178 >                api::Cerr << "buddies = {\n";
179 >
180 >                for (std::set<Buddy>::const_iterator buddy(this->buddies.begin());
181 >                        buddy != this->buddies.end(); ++buddy)
182 >                {
183 >                        api::Cerr << "   " << *buddy << " = {\n      rss = "
184 >                                << buddy->getRss() << "\n      atom = " << buddy->getAtom()
185 >                                << "\n      link = " << buddy->getLink() << "\n      id = "
186 >                                << buddy->getId() << "\n   }\n";
187 >                }
188 >
189 >                api::Cerr << "}\n";
190          }
191  
192 <        if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector)
193 <                << "\npublisher = " << lexical_cast<ext::String>(publisher) << '\n';
192 >        if (debug) api::Cerr << "collector = " << collector << "\npublisher = "
193 >                << publisher << "\ncolor = " << color << "\n";
194   }
195  
196   void Zoe::initialize()
197   {
198 <        ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host,
198 >        ext::Handle<dbi::Driver> driver(dbi::GetDriver(database.driver));
199 >        ext::Handle<dbi::Connection> db(driver->Connect(database.host,
200                  database.user, database.password, database.db));
201          ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies"));
202          std::set<Buddy> buddies_;
203  
204          while (buddies->MoveNext())
205          {
206 <                Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
206 >                if (this->buddies.find(buddies->GetString("buddy")) != this->buddies.end())
207 >                {
208 >                        Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
209  
210 <                buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
211 <                buddies_.insert(buddy);
210 >                        buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
211 >                        buddies_.insert(buddy);
212 >                }
213          }
214  
215          std::vector<Buddy> difference;
# Line 165 | Line 218 | void Zoe::initialize()
218                  buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference,
219                  difference.begin()));
220  
221 <        for (std::vector<Buddy>::iterator buddy(difference.begin()); buddy !=
222 <                difference.end(); ++buddy)
221 >        for (std::vector<Buddy>::iterator buddy(difference.begin());
222 >                buddy != difference.end(); ++buddy)
223          {
224 <                ext::Uuid id;
224 >                ext::Uuid id(api::Uuid::CreateSequential());
225  
226 <                api::Uuid::CreateSequential(id);
226 > //              api::Uuid::CreateSequential(id);
227  
228                  buddy->setId(id);
229 <                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('" +
230 <                        lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy) +
231 <                        "')");
229 >                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('"
230 >                        + lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy)
231 >                        + "')");
232                  
233                  buddies_.insert(*buddy);
234          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines