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

# Line 8 | Line 8
8   #include "Collector.hpp"
9   #include "Publisher.hpp"
10  
11 < enum Color { reset, bright, dim, underscore = 4, blink, reverse = 7, hidden,
12 <        black = 30, red, green, yellow, blue, magenta, cyan, white, _black = 40,
13 <        _red, _green, _yellow, _blue, _magenta, _cyan, _white };
11 > #include <algorithm>
12 > #include <cstring>
13 > #include <iterator>
14  
15 < inline std::ostream& operator<<(std::ostream& sout, Color color)
15 > extern "C"
16   {
17 <        sout << "\033[" << unsigned(color) << 'm';
18 <
19 <        return sout;
17 > #include <pwd.h>
18 > #include <sys/utsname.h>
19 > #include <unistd.h>
20   }
21  
22   int main(int argc, char* argv[])
# Line 64 | Line 64 | int main(int argc, char* argv[])
64   Zoe::Zoe()
65   {
66          configure();
67 +        initialize();
68  
69 <        if (collector) Collector collector(buddies);
70 <        if (publisher) Publisher publisher(buddies);
69 >        Collector collector(login, password, buddies, database, Zoe::collector);
70 >        Publisher publisher(buddies, database, Zoe::publisher);
71   }
72  
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 = 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();
86 <                ++buddy)
87 <        {
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)
# Line 93 | Line 133 | void Zoe::configure()
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 +        this->buddies = buddies_;
175   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines