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 7 by douglas, 2004-07-09T17:12:54-07:00 vs.
Revision 34 by douglas, 2004-07-20T16:49:06-07:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 #include "Zoe.hpp"
7   #include "Matcher.hpp"
8 + #include "Collector.hpp"
9 + #include "Publisher.hpp"
10 +
11 + #include <algorithm>
12 + #include <cstring>
13 + #include <iterator>
14 +
15 + extern "C"
16 + {
17 + #include <pwd.h>
18 + #include <sys/utsname.h>
19 + #include <unistd.h>
20 + }
21  
22   int main(int argc, char* argv[])
23   {
# Line 20 | Line 32 | int main(int argc, char* argv[])
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
48                  {
49 <                        cerr << "Usage: " << Zoe::program << " [-config=config] [-D]\n";
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";
54 >
55 >                        return 1;
56                  }
57          }
58  
# Line 35 | Line 61 | int main(int argc, char* argv[])
61          return 0;
62   }
63  
64 < Zoe::Zoe() : rss("zoe.rss")
64 > Zoe::Zoe()
65   {
66          configure();
67 +        initialize();
68 +
69 +        Collector collector(login, password, buddies, database, Zoe::collector);
70 +        Publisher publisher(buddies, database, Zoe::publisher);
71   }
72  
73 < bool Zoe::debug(false);
73 > bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false);
74   ext::String Zoe::program, Zoe::config("zoe.xml");
75  
76 + ext::String Zoe::generator()
77 + {
78 +        utsname system;
79 +
80 +        uname(&system);
81 +
82 +        return _S("Zoe/0.9 (") + system.sysname + "; http://computers.douglasthrift.net/zoe.xml)";
83 + }
84 +
85   void Zoe::configure()
86   {
87 +        if (debug) cerr << "config = " << config << '\n';
88 +
89          ext::Handle<xml::Document> document(xml::Parse(config));
90          ext::Handle<xml::Node> zoe(*document/"zoe");
91  
92 <        login = *zoe/"login";
93 <        password = *zoe/"password";
92 >        login = std::string(*zoe/"login");
93 >        password = std::string(*zoe/"password");
94 >        database.driver = std::string(*zoe/"database"/"driver");
95 >        database.host = std::string(*zoe/"database"/"host");
96 >        database.user = std::string(*zoe/"database"/"user");
97 >
98 >        if ((*zoe/"database"/"password").IsEmpty())
99 >        {
100 >                ext::String prompt(database.user + (!database.host.IsEmpty() ? "@" : "") + database.host
101 >                        + (!database.user.IsEmpty() ? "'s " : "") + "Database Password: ");
102 >                char* password(getpass(prompt.NullTerminate()));
103 >
104 >                database.password = password;
105 >
106 >                for (size_t index(std::strlen(password)); index > 0; --index) password[index - 1] = '\0';
107 >        }
108 >        else database.password = std::string(*zoe/"database"/"password");
109 >
110 >        database.db = std::string(*zoe/"database"/"db");
111 >
112 >        if (debug) cerr << "login = " << login << "\npassword = " << std::string(password.GetSize(), '*') << "\ndatabase = {\n"
113 >                << "   driver = " << database.driver << "\n   host=" << database.host << "\n   user = " << database.user << '\n'
114 >                << "   password = " << std::string(database.password.GetSize(), '*') << "\n   db = " << database.db << "\n}\n";
115 >
116 >        xml::NodeSet buddies(*zoe/"buddy");
117 >
118 >        for (xml::NodeSet::Iterator buddy(buddies.Begin()); buddy != buddies.End(); ++buddy)
119 >                this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss"));
120 >
121 >        if (debug)
122 >        {
123 >                cerr << "buddies = {\n";
124 >
125 >                for (std::set<Buddy>::const_iterator buddy(this->buddies.begin()); buddy != this->buddies.end(); ++buddy)
126 >                        cerr << "   " << *buddy << " = " << buddy->getRss() << '\n';
127 >
128 >                cerr << "}\n";
129 >        }
130 >
131 >        if (!collector && !publisher)
132 >        {
133 >                collector = true;
134 >                publisher = true;
135 >        }
136 >
137 >        if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector)
138 >                << "\npublisher = " << lexical_cast<ext::String>(publisher) << '\n';
139 > }
140 >
141 > void Zoe::initialize()
142 > {
143 >        ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host, database.user, database.password,
144 >                database.db));
145 >        ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies"));
146 >        std::set<Buddy> buddies_;
147 >
148 >        while (buddies->MoveNext())
149 >        {
150 >                Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
151 >
152 >                buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
153 >                buddies_.insert(buddy);
154 >        }
155 >
156 >        std::vector<Buddy> difference;
157 >
158 >        set_difference(this->buddies.begin(), this->buddies.end(), buddies_.begin(), buddies_.end(),
159 >                std::insert_iterator<std::vector<Buddy> >(difference, difference.begin()));
160 >
161 >        for (std::vector<Buddy>::iterator buddy(difference.begin()); buddy != difference.end(); ++buddy)
162 >        {
163 >                ext::Uuid id;
164 >
165 >                api::Uuid::CreateSequential(id);
166 >
167 >                buddy->setId(id);
168 >                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('" + lexical_cast<ext::String>(id) + "', '"
169 >                        + ext::String(*buddy) + "')");
170 >                
171 >                buddies_.insert(*buddy);
172 >        }
173  
174 <        if (!(*zoe/"rss").IsEmpty()) rss = *zoe/"rss";
174 >        this->buddies = buddies_;
175   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines