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 63 by douglas, 2004-09-17T16:35:17-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 <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>
18  
19 < inline std::ostream& operator<<(std::ostream& sout, Color color)
19 > extern "C"
20   {
21 <        sout << "\033[" << unsigned(color) << 'm';
22 <
23 <        return sout;
21 > #include <pwd.h>
22 > #include <sys/utsname.h>
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::GetExecutableName();
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")
40 <                {
41 <                        if (!Zoe::publisher) Zoe::publisher = true;
42 <                }
43 <                else if (arg == "-D")
32 >                for (size_t index(0); index < args.GetSize(); ++index)
33                  {
34 <                        if (!Zoe::debug) Zoe::debug = true;
35 <                }
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();
71  
72 <        if (collector) Collector collector(buddies);
73 <        if (publisher) Publisher publisher(buddies);
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 + 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 +        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) 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");
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 " : "")
142 >                        + "Database Password: ");
143 >                char* password(getpass(prompt.NullTerminate()));
144 >
145 >                database.password = password;
146 >
147 >                for (size_t index(std::strlen(password)); index > 0; --index)
148 >                        password[index - 1] = '\0';
149 >        }
150 >        else database.password = *zoe/"database"/"password";
151 >
152 >        database.db = *zoe/"database"/"db";
153 >
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)
166          {
167 <                this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss"));
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 >        initialize();
175 >
176 >        if (debug)
177 >        {
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) api::Cerr << "collector = " << collector << "\npublisher = "
193 >                << publisher << "\ncolor = " << color << "\n";
194 > }
195 >
196 > void Zoe::initialize()
197 > {
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 >                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);
212 >                }
213          }
214  
215 <        if (!collector && !publisher)
215 >        std::vector<Buddy> difference;
216 >
217 >        set_difference(this->buddies.begin(), this->buddies.end(), buddies_.begin(),
218 >                buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference,
219 >                difference.begin()));
220 >
221 >        for (std::vector<Buddy>::iterator buddy(difference.begin());
222 >                buddy != difference.end(); ++buddy)
223          {
224 <                collector = true;
225 <                publisher = true;
224 >                ext::Uuid id(api::Uuid::CreateSequential());
225 >
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 >                        + "')");
232 >                
233 >                buddies_.insert(*buddy);
234          }
235 +
236 +        this->buddies = buddies_;
237   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines