load_data return allways, all exit() calls removed, all load_data calls are protected
[brisk.git] / web / index_rd.php
index 3dec5e9..0850a36 100644 (file)
@@ -29,10 +29,10 @@ require_once("Obj/brisk.phh");
 require_once("briskin5/Obj/briskin5.phh");
 
 $S_load_stat = array( 'U_first_loop' => 0,
-                    'R_garbage'    => 0,
-                    'U_heavy'      => 0,
-                    'R_minusone'   => 0,
-                    'R_the_end'    => 0 );
+                      'U_heavy'      => 0,
+                      'R_garbage'    => 0,
+                      'R_minusone'   => 0,
+                      'R_the_end'    => 0 );
 
 $mlang_indrd = array( 
                      'btn_backtotab'  => array('it' => ' torna ai tavoli ',
@@ -58,10 +58,9 @@ if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
 
 function shutta()
 {
-  log_rd2("SHUTTA!".connection_status());
+  log_rd2("SHUTTA [".connection_status()."] !");
 }
 
-
 register_shutdown_function(shutta);
 
 function unrecerror()
@@ -105,7 +104,9 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
     $user = FALSE;
     $curtime = time();
     
-    if (($proxy_step = User::step_get($sess)) == FALSE) {
+    // NOTE: qui forse si potrebbe fallback-are a una User::load_data 
+    //       anche se non ce ne dovrebbe essere mai la necessità
+    if (($proxy_step = User::load_step($sess)) == FALSE) {
         log_only2("R");
         return (FALSE);
     }
@@ -118,7 +119,7 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
             // Aggiorna l'expire time lato server
             $S_load_stat['U_first_loop']++;
             if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
-                Room::unlock_data();
+                Room::unlock_data($sem);
                 ignore_user_abort(FALSE);
                 return (unrecerror());
             }
@@ -129,13 +130,17 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
                 log_only("F");
                 
                 $S_load_stat['R_garbage']++;
-                if (($room = Room::load_data()) != FALSE) {
-                    log_main("pre garbage_manager TRE");
-                    $room->garbage_manager(FALSE);
-                    Room::save_data($room);
+                if (($room = Room::load_data()) == FALSE) {
+                    Room::unlock_data($sem);
+                    ignore_user_abort(FALSE);
+                    return (unrecerror());
                 }
+                log_main("pre garbage_manager TRE");
+                $room->garbage_manager(FALSE);
+                Room::save_data($room);
+                unset($room);
             }
-            log_lock("U");
+            log_main("infolock: U");
             Room::unlock_data($sem);
             ignore_user_abort(FALSE);
         } // if (($sem = Room::lock_data()) != FALSE) { 
@@ -149,7 +154,7 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
     } // if  ($first_loop == TRUE) {
     
     if ($cur_step == $proxy_step['s']) {
-        log_lock("P");
+        log_main("infolock: P");
         return (FALSE);
     }
     else {
@@ -157,34 +162,31 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
     }
     
 
-    if (1 == 1) { // C'è veramente qualche caso in cui serve questo step ??
-        if ($user == FALSE) {
-            do {
-                ignore_user_abort(TRUE);
-                if (($sem = Room::lock_data()) == FALSE) 
-                    break;
-                
-                log_lock("P");
-                $S_load_stat['U_heavy']++;
-                if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
-                    break;
-                }
-            } while (0);
-            
-            if ($sem != FALSE)
-                Room::unlock_data($sem);
+    if ($user == FALSE) {
+        do {
+            ignore_user_abort(TRUE);
+            if (($sem = Room::lock_data()) == FALSE) 
+                break;
             
-            ignore_user_abort(FALSE);
-            if ($user == FALSE) {
-                return (unrecerror());
+            log_main("infolock: P");
+            $S_load_stat['U_heavy']++;
+            if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
+                break;
             }
-        }
+        } while (0);
         
-        /* Nothing changed, return. */
-        if ($cur_step == $user->step) 
-            return (FALSE);
-    } // if (0 == 1) { xx C'è veramente qualche caso in cui serve questo step ??
-
+        if ($sem != FALSE)
+            Room::unlock_data($sem);
+        
+        ignore_user_abort(FALSE);
+        if ($user == FALSE) {
+            return (unrecerror());
+        }
+    }
+    
+    /* Nothing changed, return. */
+    if ($cur_step == $user->step) 
+        return (FALSE);
     
     log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
     
@@ -194,7 +196,11 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
          */
         ignore_user_abort(TRUE);
         $sem = Room::lock_data();
-        $room = Room::load_data();
+        if (($room = Room::load_data()) == FALSE) {
+            Room::unlock_data($sem);
+            ignore_user_abort(FALSE);
+            return (unrecerror());
+        }
         $S_load_stat['R_minusone']++;
         
         /* unset the $user var to reload it from main structure */
@@ -255,7 +261,7 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
                                         ($is_super ? 0 : $G_splash_timeout));
                 $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
             }
-            $ret .= $room->show_room($user->step, &$user);
+            $ret .= $room->show_room($user->step, $user);
             
             // TODO uncomment and test
             /* NOTE the sets went common */
@@ -280,7 +286,7 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
         $sem = Room::lock_data();
         $S_load_stat['U_heavy']++;
         if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
-            Room::unlock_data();
+            Room::unlock_data($sem);
             ignore_user_abort(FALSE);
             return (unrecerror());
         }
@@ -300,8 +306,9 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
                     break;
                 } 
                 for ($i = $cur_step ; $i < $user->step ; $i++) {
-                    log_rd2("ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
-                    $ret .= $user->comm[$i % COMM_N];
+                    $ii = $i % COMM_N;
+                    log_rd2("ADDED TO THE STREAM: ".$user->comm[$ii]);
+                    $ret .= $user->comm[$ii];
                 }
                 $new_stat =  $user->stat;
                 $new_subst = $user->subst;
@@ -310,39 +317,44 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
             
             log_mop($user->step, 'index_rd.php: after ret set');
             
-          if ($user->the_end == TRUE) {
-              log_rd2("LOGOUT BYE BYE!!");
-              log_auth($user->sess, "Explicit logout.");
-              
-              $S_load_stat['R_the_end']++;
-              $room = &Room::load_data();
-              unset($user);
-              if (($user = &$room->get_user($sess, $idx)) == FALSE) {
-                  Room::unlock_data($sem);
-                  ignore_user_abort(FALSE);
-                  return (unrecerror());
-              }              
-              $user->reset();
-              
-              if ($user->subst == 'sitdown') {
-                  log_load("ROOM WAKEUP");
-                  $room->room_wakeup($user);
-              }
-              else if ($user->subst == 'standup')
-                  $room->room_outstandup(&$user);
-              else
-                  log_rd2("LOGOUT FROM WHAT ???");
-              
-              Room::save_data($room);
-          }
-      }
-      
-      Room::unlock_data($sem);
-      ignore_user_abort(FALSE);
-  }
-  
-  
-  return ($ret);
+            if ($user->the_end == TRUE) {
+                log_rd2("LOGOUT BYE BYE!!");
+                log_auth($user->sess, "Explicit logout.");
+                
+                $S_load_stat['R_the_end']++;
+                if (($room = Room::load_data()) == FALSE) {
+                    Room::unlock_data($sem);
+                    ignore_user_abort(FALSE);
+                    return (unrecerror());
+                }
+
+                unset($user);
+                if (($user = $room->get_user($sess, $idx)) == FALSE) {
+                    Room::unlock_data($sem);
+                    ignore_user_abort(FALSE);
+                    return (unrecerror());
+                }              
+                $user->reset();
+                
+                if ($user->subst == 'sitdown') {
+                    log_load("ROOM WAKEUP");
+                    $room->room_wakeup($user);
+                }
+                else if ($user->subst == 'standup')
+                    $room->room_outstandup($user);
+                else
+                    log_rd2("LOGOUT FROM WHAT ???");
+                
+                Room::save_data($room);
+            }
+        }
+        
+        Room::unlock_data($sem);
+        ignore_user_abort(FALSE);
+    }
+    
+    
+    return ($ret);
 }
 
 /*
@@ -361,8 +373,6 @@ $is_page_streaming =  (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_A
 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
 header('Content-type: application/xml; charset="utf-8"',true);
-// header('Content-type: text/plain; charset="utf-8"',true);
-// header('Content-type: text/html; charset="utf-8"',true);
 
 if (!isset($myfrom))
      $myfrom = "";
@@ -406,7 +416,7 @@ $s = "[".$sess."] index_rd.php stats: ";
 foreach ($S_load_stat as $key => $value) {
     $s .= sprintf("%s: %d - ", $key, $value);
 }
-error_log($s, 0);
+log_crit($s);
 
 
 ?>