]> git.sesse.net Git - ccbs/blob - ccbs_bigscreen.cpp
3b9d6ae506ca8e11ebc4df62a7b8334dbb02948e
[ccbs] / ccbs_bigscreen.cpp
1 #include <cstdio>
2 #include <unistd.h>
3 #include <pqxx/pqxx>
4 #include "glwindow.h"
5
6 /* A trigger that sets a flag whenever it's trigged. */
7 class FlagTrigger : pqxx::trigger {
8 private:
9         bool flag;
10         
11 public:
12         FlagTrigger(pqxx::connection_base &conn, const PGSTD::string &name)
13                 : pqxx::trigger(conn, name), flag(false) {}
14         virtual ~FlagTrigger() throw () {}
15         
16         virtual void operator() (int pid)
17         {
18                 flag = true;
19                 std::fprintf(stderr, "Received a flag trigger from pid %u\n", pid);
20         }
21
22         bool get_flag() const
23         {
24                 return flag;
25         }
26
27         void reset_flag()
28         {
29                 flag = false;
30         }
31 };
32
33 void main_loop(pqxx::connection &conn)
34 {
35         pqxx::work t(conn, "trx");
36
37         // fetch all songs
38         pqxx::result res( t.exec("SELECT * FROM songs") );
39         for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
40                 // std::fprintf(stderr, "%s\n", i["title"].c_str());
41         }
42         t.commit();
43         
44         sleep(1);
45 }
46
47 int main(int argc, char **argv)
48 {
49         GLWindow glw("CCBS bigscreen", 800, 600, 32, false, 16, -1);
50         try {
51                 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
52                 FlagTrigger tournament_changed(conn, "active_tournament");
53                 
54                 // when active_tournament is changed, we destroy everything and start from scratch
55                 for ( ;; ) {
56                         tournament_changed.reset_flag();
57                         do {
58                                 main_loop(conn);
59                                 conn.get_notifs();
60                         } while (!tournament_changed.get_flag());
61                         std::fprintf(stderr, "active_tournament changed, resetting...\n");
62                 }
63         } catch (const std::exception &e) {
64                 std::fprintf(stderr, "Exception: %s\n", e.what());
65                 exit(1);
66         }
67         
68         return 0;
69 }