08d2bf24576fad8da550d4c5f07e7ebdad037638
[ccbs] / bigscreen / ccbs_bigscreen.cpp
1 #include <cstdio>
2 #include <unistd.h>
3 #include <pqxx/pqxx>
4 #include "glwindow.h"
5
6 class Tournament {
7 public:
8         int id;
9         std::string name;
10 };
11
12 Tournament active_tournament;
13
14 /* A trigger that sets a flag whenever it's trigged. */
15 class FlagTrigger : pqxx::trigger {
16 private:
17         bool flag;
18         
19 public:
20         FlagTrigger(pqxx::connection_base &conn, const PGSTD::string &name)
21                 : pqxx::trigger(conn, name), flag(false) {}
22         virtual ~FlagTrigger() throw () {}
23         
24         virtual void operator() (int pid)
25         {
26                 flag = true;
27                 std::fprintf(stderr, "Received a flag trigger from pid %u\n", pid);
28         }
29
30         bool get_flag() const
31         {
32                 return flag;
33         }
34
35         void reset_flag()
36         {
37                 flag = false;
38         }
39 };
40
41 /* A transactor that fetches the current tournament and some information about it. */
42 class FetchCurrentTournament : public pqxx::transactor<> {
43 private:
44         Tournament *tourn;
45
46 public:
47         FetchCurrentTournament(Tournament *tourn) : tourn(tourn) {}
48         void operator() (pqxx::transaction<> &t)
49         {
50                 pqxx::result res( t.exec("SELECT * FROM bigscreen.active_tournament NATURAL JOIN tournaments") );
51                 try {
52                         pqxx::result::tuple tournament = res.at(0);
53
54                         tourn->id = tournament["tournament"].as(tourn->id);
55                         tourn->name = tournament["tournamentname"].as(tourn->name);
56                 } catch (PGSTD::out_of_range &e) {
57                         tourn->id = -1;
58                         tourn->name = "";
59                 }
60         }
61 };
62
63 void init(pqxx::connection &conn)
64 {
65         conn.perform(FetchCurrentTournament(&active_tournament));
66
67         if (active_tournament.id == -1) {
68                 std::fprintf(stderr, "No active tournament\n");
69         } else {
70                 std::fprintf(stderr, "Current tournament is %d (name: '%s')\n",
71                         active_tournament.id, active_tournament.name.c_str());
72         }
73 }
74
75 void main_loop(pqxx::connection &conn)
76 {
77         if (active_tournament.id == -1) {
78                 // No active tournament, sleep a second or so and exit
79                 sleep(1);
80                 return;
81         }
82         
83         pqxx::work t(conn, "trx");
84
85         // fetch all songs
86         pqxx::result res( t.exec("SELECT * FROM songs") );
87         for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
88                 std::fprintf(stderr, "%s\n", i["title"].c_str());
89         }
90         t.commit();
91         
92         sleep(1);
93 }
94
95 int main(int argc, char **argv)
96 {
97         GLWindow glw("CCBS bigscreen", 800, 600, 32, false, 16, -1);
98         try {
99                 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
100                 FlagTrigger tournament_changed(conn, "active_tournament");
101                 
102                 // when active_tournament is changed, we destroy everything and start from scratch
103                 for ( ;; ) {
104                         tournament_changed.reset_flag();
105                         init(conn);
106                         do {
107                                 main_loop(conn);
108                                 conn.get_notifs();
109                         } while (!tournament_changed.get_flag());
110                         std::fprintf(stderr, "active_tournament changed, resetting...\n");
111                 }
112         } catch (const std::exception &e) {
113                 std::fprintf(stderr, "Exception: %s\n", e.what());
114                 exit(1);
115         }
116         
117         return 0;
118 }