ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/zoe/trunk/Publisher.cpp
(Generate patch)

Comparing trunk/Publisher.cpp (file contents):
Revision 21 by douglas, 2004-07-16T23:37:41-07:00 vs.
Revision 37 by douglas, 2004-07-22T16:00:52-07:00

# Line 5 | Line 5
5   // $Id$
6  
7   #include "Publisher.hpp"
8 + #include "Rss.hpp"
9 + #include "Atom.hpp"
10  
11 < Publisher::Publisher(const std::set<Buddy>& buddies, const Database& database,
12 <        bool start) : buddies(buddies), database(database), start(start)
11 > Publisher::Publisher(const std::set<Buddy>& buddies, const Database& database, bool start) : buddies(buddies),
12 >        database(database), start(start)
13   {
14 <        if (start)
14 >        if (start) publisher.Spawn(etl::Bind<0>(&Publisher::publish, this));
15 > }
16 >
17 > int Publisher::publish()
18 > {
19 >        cerr << bright << green << "Started publisher daemon.\n" << reset;
20 >
21 >        while (true)
22          {
23 <                publisher.Spawn(etl::Bind<0>(&Publisher::publish, this));
23 >                Stamp now, next(now + Hour(1));
24 >                api::Thread thread(etl::Bind<0>(&Publisher::update, this));
25 >
26 >                next.setSeconds(0);
27 >                next.setMinutes(0);
28 >
29 >                if (Zoe::debug) cerr << "now = " << now << "\nnext = " << next << '\n';
30 >
31 >                while (now < next - Minute(1))
32 >                {
33 >                        sleep(Minute(1));
34 >
35 >                        now = Stamp();
36 >                }
37 >
38 >                while (now < next)
39 >                {
40 >                        sleep(1);
41 >
42 >                        now = Stamp();
43 >                }
44 >
45 >                thread.Join();
46          }
47   }
48  
49 < int Publisher::publish()
49 > int Publisher::update()
50   {
51 <        cerr << "Publisher::publish()\n";
51 >        cerr << bright << yellow << "Started updating feeds.\n" << reset;
52 >
53 >        Stamp stamp;
54 >        ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host,
55 >                database.user, database.password, database.db));
56 >
57 >        db->Execute("DELETE FROM messages WHERE stamp<'" + ext::String(stamp - Day(30)) + "'");
58 >
59 >        for (std::set<Buddy>::const_iterator buddy(buddies.begin());
60 >                buddy != buddies.end(); ++buddy)
61 >        {
62 >                ext::Handle<dbi::ResultSet> messages(db->Execute("SELECT stamp, message FROM messages WHERE id='"
63 >                        + lexical_cast<ext::String>(buddy->getId()) + "'"));
64 >                std::vector<AwayMessage> messages_;
65 >
66 >                while (messages->MoveNext()) messages_.push_back(AwayMessage(messages->GetString("message"),
67 >                        messages->GetString("stamp")));
68 >
69 >                Rss rss(*buddy, messages_, stamp);
70 >                Atom atom(*buddy, messages_, stamp);
71 >
72 >                cerr << bright << yellow << "Updated feeds for " << blue << *buddy
73 >                        << yellow << ".\n" << reset;
74 >        }
75  
76 <        // publish
76 >        cerr << bright << yellow << "Finished updating feeds.\n" << reset;
77   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines