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> |
23 |
|
#include <unistd.h> |
24 |
|
} |
25 |
|
|
26 |
< |
int zoe(const std::vector<const char*> args) |
26 |
> |
struct ZoeCommand : public app::Application |
27 |
|
{ |
28 |
< |
Zoe::program = args[0]; |
25 |
< |
|
26 |
< |
for (int index(1); index < args.size(); ++index) |
28 |
> |
virtual int Run(const app::ArgumentList& args) |
29 |
|
{ |
30 |
< |
ext::String arg(args[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; |
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; |
61 |
> |
} |
62 |
|
} |
43 |
– |
else if (arg == "-color") |
44 |
– |
{ |
45 |
– |
if (!Zoe::color) Zoe::color = true; |
46 |
– |
} |
47 |
– |
else if (arg == "-D") |
48 |
– |
{ |
49 |
– |
if (!Zoe::debug) Zoe::debug = true; |
50 |
– |
} |
51 |
– |
else Zoe::usage(); |
52 |
– |
} |
53 |
– |
|
54 |
– |
Zoe zoe; |
63 |
|
|
64 |
< |
return 0; |
57 |
< |
} |
58 |
< |
|
59 |
< |
int main(int argc, char* argv[]) |
60 |
< |
{ |
61 |
< |
std::vector<const char*> args(argv, argv + argc); |
64 |
> |
Zoe zoe; |
65 |
|
|
66 |
< |
return app::be::ThreadMain_(etl::BindAll(&zoe, args)); |
67 |
< |
} |
66 |
> |
return 0; |
67 |
> |
} |
68 |
> |
} zoe; |
69 |
|
|
70 |
|
Zoe::Zoe() |
71 |
|
{ |
72 |
< |
if (!(collector || publisher)) usage(); |
72 |
> |
if (!(collector || publisher)) |
73 |
> |
{ |
74 |
> |
usage(); |
75 |
> |
|
76 |
> |
return; |
77 |
> |
} |
78 |
|
|
79 |
|
configure(); |
71 |
– |
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), |
86 |
< |
Zoe::color(false); |
86 |
> |
Zoe::color(false); |
87 |
|
ext::String Zoe::program, Zoe::config("zoe.xml"); |
88 |
|
|
89 |
|
void Zoe::usage() |
90 |
|
{ |
91 |
< |
cerr << "Usage: " << Zoe::program << " [-config=config] [-collector] " |
84 |
< |
"[-publisher] [-color] [-D]\n"; |
85 |
< |
|
86 |
< |
exit(1); |
91 |
> |
api::Cerr << "Usage: " << Zoe::program << " [-config=config] [-collector] [-publisher] [-color] [-D]\n"; |
92 |
|
} |
93 |
|
|
94 |
|
ext::String Zoe::generator(Generator generator) |
101 |
|
generator_.InsertAllLast("/0.9"); |
102 |
|
case agent: |
103 |
|
{ |
104 |
< |
utsname system; |
104 |
> |
::utsname system; |
105 |
|
|
106 |
< |
uname(&system); |
106 |
> |
::uname(&system); |
107 |
|
|
108 |
< |
generator_.InsertAllLast(_S(" (") + system.sysname); |
108 |
> |
generator_.InsertAllLast(ios::String() << " (" << system.sysname); |
109 |
|
} |
110 |
|
|
111 |
|
if (generator == agent) |
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"); |
157 |
|
|
158 |
|
database.db = *zoe/"database"/"db"; |
159 |
|
|
160 |
< |
if (debug) cerr << "login = " << login << "\npassword = " |
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' |
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 |
|
|
177 |
|
this->buddies.insert(buddy_); |
178 |
|
} |
179 |
|
|
180 |
+ |
initialize(); |
181 |
+ |
|
182 |
|
if (debug) |
183 |
|
{ |
184 |
< |
cerr << "buddies = {\n"; |
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 |
< |
cerr << " " << *buddy << " = {\n rss = " << buddy->getRss() |
190 |
< |
<< "\n atom = " << buddy->getAtom() << "\n link = " |
191 |
< |
<< buddy->getLink() << "\n }\n"; |
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 |
< |
cerr << "}\n"; |
195 |
> |
api::Cerr << "}\n"; |
196 |
|
} |
197 |
|
|
198 |
< |
if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector) |
199 |
< |
<< "\npublisher = " << lexical_cast<ext::String>(publisher) |
192 |
< |
<< "\ncolor = " << lexical_cast<ext::String>(color) << '\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, |
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_; |
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 ('" |