Changed the structure of the bigscreen main loop.
authorSteinar H. Gunderson <sesse@samfundet.no>
Sat, 19 Feb 2005 00:57:02 +0000 (00:57 +0000)
committerSteinar H. Gunderson <sesse@samfundet.no>
Sat, 19 Feb 2005 00:57:02 +0000 (00:57 +0000)
bigscreen/ccbs_bigscreen.cpp

index 03a9496..3b9d6ae 100644 (file)
@@ -3,43 +3,63 @@
 #include <pqxx/pqxx>
 #include "glwindow.h"
 
-/*
- * A trigger that exits whenever it's trigged (used when we change big things
- * such as starting a new tournament, and it's not really worth doing a clean
- * change).
- */
-class ExitTrigger : pqxx::trigger {
+/* A trigger that sets a flag whenever it's trigged. */
+class FlagTrigger : pqxx::trigger {
+private:
+       bool flag;
+       
 public:
-       ExitTrigger(pqxx::connection_base &conn, const PGSTD::string &name)
-               : pqxx::trigger(conn, name) {}
-       virtual ~ExitTrigger() throw () {}
+       FlagTrigger(pqxx::connection_base &conn, const PGSTD::string &name)
+               : pqxx::trigger(conn, name), flag(false) {}
+       virtual ~FlagTrigger() throw () {}
        
        virtual void operator() (int pid)
        {
-               std::fprintf(stderr, "Received an exit trigger from pid %u\n", pid);
-               exit(0);
+               flag = true;
+               std::fprintf(stderr, "Received a flag trigger from pid %u\n", pid);
+       }
+
+       bool get_flag() const
+       {
+               return flag;
+       }
+
+       void reset_flag()
+       {
+               flag = false;
        }
 };
 
+void main_loop(pqxx::connection &conn)
+{
+       pqxx::work t(conn, "trx");
+
+       // fetch all songs
+       pqxx::result res( t.exec("SELECT * FROM songs") );
+       for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
+               // std::fprintf(stderr, "%s\n", i["title"].c_str());
+       }
+       t.commit();
+       
+       sleep(1);
+}
+
 int main(int argc, char **argv)
 {
        GLWindow glw("CCBS bigscreen", 800, 600, 32, false, 16, -1);
        try {
                pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
-               ExitTrigger et(conn, "bs_tournament_changed");
+               FlagTrigger tournament_changed(conn, "active_tournament");
                
-               sleep(1);
-
-               pqxx::work t(conn, "trx");
-
-               // fetch all songs
-               pqxx::result res( t.exec("SELECT * FROM songs") );
-               for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
-                       std::fprintf(stderr, "%s\n", i["title"].c_str());
+               // when active_tournament is changed, we destroy everything and start from scratch
+               for ( ;; ) {
+                       tournament_changed.reset_flag();
+                       do {
+                               main_loop(conn);
+                               conn.get_notifs();
+                       } while (!tournament_changed.get_flag());
+                       std::fprintf(stderr, "active_tournament changed, resetting...\n");
                }
-               t.commit();
-               conn.get_notifs();
-               sleep(1);
        } catch (const std::exception &e) {
                std::fprintf(stderr, "Exception: %s\n", e.what());
                exit(1);