renamed class brisco to Room, some free funcs moved to static class funcs, split...
[brisk.git] / web / index_rd.php
index 2c69a2b..e164229 100644 (file)
@@ -2,7 +2,7 @@
 /*
  *  brisk - index_rd.php
  *
- *  Copyright (C) 2006 matteo.nastasi@milug.org
+ *  Copyright (C) 2006-2007 matteo.nastasi@milug.org
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -23,7 +23,7 @@
 
 require_once("brisk.phh");
 
-log_load($sess, "LOAD: index_rd.php");
+log_load($sess, "LOAD: index_rd.php ".$QUERY_STRING);
 
 $first_loop = TRUE;
 $the_end = FALSE;
@@ -46,9 +46,18 @@ function unrecerror()
   GLOBAL $is_page_streaming;
 
   $is_page_streaming = TRUE;
+  log_rd2("XXX", "UNREC_ERROR");
   return (sprintf('the_end=true; window.onunload = null; document.location.assign("index.php");'));
 }
 
+function page_sync($sess, $page)
+{
+  GLOBAL $is_page_streaming;
+
+  $is_page_streaming = TRUE;
+  log_rd2($sess, "PAGE_SYNC");
+  return (sprintf('the_end=true; window.onunload = null; document.location.assign("%s");', $page));
+}
 
 
 
@@ -58,49 +67,84 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
   GLOBAL $is_page_streaming, $first_loop;
   
   $ret = FALSE;
-  
-  
+  $room = FALSE;
+
+  log_rd2($sess, "M");
   /* Sync check (read only without modifications */
-  if (($sem = lock_data()) != FALSE) { 
-    $bri = &load_data();
+  ignore_user_abort(TRUE);
+  if (($sem = Room::lock_data()) != FALSE) { 
     // Aggiorna l'expire time lato server
     if  ($first_loop == TRUE) {
-      if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
-       unlock_data($sem);
+      log_only($sess, "F");
+      $room = &Room::load_data();
+      if (($user = &$room->get_user($sess, $idx)) == FALSE) {
+       Room::unlock_data($sem);
+        ignore_user_abort(FALSE);
        return (unrecerror());
       }
       log_auth($sess, "update lacc");
-      $user->lacc = time() + EXPIRE_TIME;
-      save_data($bri);
-      $first_loop = FALSE;
-    
-      
-      $bri->garbage_manager(FALSE);
+      $user->lacc = time();
+
+      $room->garbage_manager(FALSE);
       
-      save_data($bri);
+      Room::save_data($room);
+      $first_loop = FALSE;
     }
-    unlock_data($sem);
+
+    log_only($sess, "U");
+    Room::unlock_data($sem);
+    ignore_user_abort(FALSE);
   }
   else {
     return (FALSE);
   }
     
-  if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
+  if (($proxy_step = step_get($sess)) != FALSE) {
+    // log_rd2($sess, "Postget".$proxy_step."zizi");
+
+    if ($cur_step == $proxy_step) {
+      log_only2($sess, "P");
+      return (FALSE);
+    }
+    else {
+      log_only2($sess, "R");
+    }
+  }
+  else {
+      log_only2($sess, "R");
+  }
+
+  if ($room == FALSE) {
+    ignore_user_abort(TRUE);
+    if (($sem = Room::lock_data()) != FALSE) { 
+      log_only($sess, "P");
+      $room = &Room::load_data();
+      Room::unlock_data($sem);
+      ignore_user_abort(FALSE);
+    }
+    else {
+      return (FALSE);
+    }
+  }
+  
+  if (($user = &$room->get_user($sess, $idx)) == FALSE) {
     return (unrecerror());
   }
 
   /* Nothing changed, return. */
-  if ($cur_stat == $user->stat && $cur_step == $user->step) 
+  if ($cur_step == $user->step) 
     return;
 
   log_rd2($sess, "do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
 
   if ($cur_step == -1) {
     // FUNZIONE from_scratch DA QUI 
-    $sem = lock_data();
-    $bri = &load_data();
-    if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
-      unlock_data($sem);
+    ignore_user_abort(TRUE);
+    $sem = Room::lock_data();
+    $room = &Room::load_data();
+    if (($user = &$room->get_user($sess, $idx)) == FALSE) {
+      Room::unlock_data($sem);
+      ignore_user_abort(FALSE);
       return (unrecerror());
     }
     if ($user->the_end) 
@@ -113,12 +157,14 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
       $user->trans_step = -1;
 
 
-      save_data($bri);
-      unlock_data($sem);
+      Room::save_data($room);
+      Room::unlock_data($sem);
+      ignore_user_abort(FALSE);
     }
     else {
       log_rd2($sess, "TRANS NON ATTIVATO");
-      unlock_data($sem);
+      Room::unlock_data($sem);
+      ignore_user_abort(FALSE);
     }
   }
       
@@ -127,7 +173,7 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
 
     if ($user->stat == 'room') {
       log_rd($sess, "roomma");
-      $ret .= show_room(&$bri, &$user);
+      $ret .= show_room(&$room, &$user);
     }
     /***************
      *             *
@@ -135,7 +181,7 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
      *             *
      ***************/
     else if ($user->stat == 'table') {      
-      $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
+      $ret = show_table(&$room,&$user,$user->step,FALSE,FALSE);
 
       log_rd2($sess, "SENDED TO THE STREAM: ".$ret);
     }
@@ -146,15 +192,23 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
     $new_step =  $user->step;
   }
   else {
-    $sem = lock_data();
-    $bri = &load_data();
-    if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
-      unlock_data($sem);
+    ignore_user_abort(TRUE);
+    $sem = Room::lock_data();
+    $room = &Room::load_data();
+    if (($user = &$room->get_user($sess, $idx)) == FALSE) {
+      Room::unlock_data($sem);
+      ignore_user_abort(FALSE);
       return (unrecerror());
     }
     if ($cur_step < $user->step) {
       do {
        if ($cur_step + COMM_N < $user->step) {
+         if (($cur_stat != $user->stat)) {
+           $to_stat = $user->stat;
+           Room::unlock_data($sem);
+           ignore_user_abort(FALSE);
+           return (page_sync($user->sess, $to_stat == "table" ? "table.php" : "index.php"));
+         }
          log_rd2($sess, "lost history, refresh from scratch");
          $new_step = -1;
          break;
@@ -171,22 +225,26 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
       if ($user->the_end == TRUE) {
        log_rd2($sess, "LOGOUT BYE BYE!!");
        log_auth($user->sess, "Explicit logout.");
+       $tmp_sess = $user->sess;
        $user->sess = "";
+       step_unproxy($tmp_sess);
+       
        $user->name = "";
        $user->the_end = FALSE;
        
        if ($user->subst == 'sitdown')
-         $bri->room_wakeup(&$user);
+         $room->room_wakeup(&$user);
        else if ($user->subst == 'standup')
-         $bri->room_outstandup(&$user);
+         $room->room_outstandup(&$user);
        else
          log_rd2($sess, "LOGOUT FROM WHAT ???");
          
-       save_data($bri);
+       Room::save_data($room);
       }
     }
          
-    unlock_data($sem);
+    Room::unlock_data($sem);
+    ignore_user_abort(FALSE);
   }
 
   
@@ -211,6 +269,10 @@ $is_page_streaming =  ((stristr($HTTP_USER_AGENT, "linux") &&
 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
 
+if (!isset($myfrom))
+     $myfrom = "";
+if (!isset($subst))
+     $subst = "";
 log_rd2($sess, "FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
 
 
@@ -235,7 +297,7 @@ for ($i = 0 ; time() < $endtime ; $i++) {
   $old_subst = $subst;
   $old_step =  $step;
   // log_rd($sess, "POST MAIN ".$step);;
-  usleep(200000);
+  usleep(400000);
   if (($i % 5) == 0) {
     // log_rd2($sess, "TIME: ".time());
     echo '_';