shared data modularized and splitted, room, users, tables, users at tables
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Sun, 27 Mar 2011 15:27:46 +0000 (17:27 +0200)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Sun, 27 Mar 2011 15:27:46 +0000 (17:27 +0200)
web/Obj/brisk.phh
web/briskin5/Obj/briskin5.phh
web/briskin5/index_rd.php

index 09b1da8..c74c141 100644 (file)
@@ -854,6 +854,36 @@ class User {
       return (FALSE);
   }
 
+  static function step_get($sess)
+  {
+      $fp = FALSE;
+      do {
+          if (validate_sess($sess) == FALSE)
+              break;
+          
+          if (file_exists(PROXY_PATH) == FALSE)
+              mkdir(PROXY_PATH);
+          if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
+              break;
+          if (($s = fread($fp, 8)) == FALSE)
+              break;
+          if (strlen($s) != 8)
+              break;
+          $arr = unpack('Ls/Li', $s);
+          fclose($fp);
+          
+          // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
+          return ($arr);
+      } while (0);
+      
+      if ($fp != FALSE)
+          fclose($fp);
+      
+      log_rd2("STEP_GET [".$sess."]: return false ");
+      
+      return (FALSE);
+  }
+
   function reset() {
     $curtime = time();
     log_legal($curtime, $this, "STAT:LOGOUT", '');
@@ -872,137 +902,104 @@ class User {
     $this->the_end = FALSE;
   }
 
-
-
-  // SHSPLIT save and load function for the User class.
   static function load_data($id, $sess) 
   {
-    // error_log("User::load_data BEGIN", 0);
-
-    log_main("load_data: id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
-
-    $doexit = FALSE;
-    do {
-      if (($tok = @ftok(FTOK_PATH."/user".$id, "B")) == -1) {
-       log_main("ftok failed");
-       $doexit = TRUE;
-       break;
-      }
-    
-      if (($shm_sz = sharedmem_sz($tok)) == -1) {
-       log_main("shmop_open failed");
-      }
-       
-      if ($shm_sz == -1)
-       $shm_sz = SHM_DIMS_U_MIN;
-
-      if ($shm = shm_attach($tok, $shm_sz)) {
-       $user = @shm_get_var($shm, $tok);
-       
-        if ($sess != FALSE && $user->sess != $sess) {
-            $doexit = TRUE;
-            break;
-        }
-       log_only("user ==  ".($user == FALSE ?   "FALSE" : "TRUE")."  user ===  ".($user === FALSE ? "FALSE" : "TRUE")."  user isset ".(isset($user) ?   "TRUE" : "FALSE"));
-       if (isset($user)) 
-         log_only("bri count ".count($user));
-       
-       if ($user == FALSE) {
-         log_only("INIT MAIN DATA");
-         
-          // SHSPLIT FIXME: init_data for User class ??
-         $user = User::create($id, "", "");
-         if (@shm_put_var($shm, $tok, $user) == FALSE) {
-              log_only("PUT_VAR FALLITA ".strlen(serialize($user)));
-              log_only(serialize($user));
-         }
-       }
-
-       $user->shm_sz = $shm_sz;
-       
-       shm_detach($shm);
-      }
+      // error_log("User::load_data BEGIN", 0);
       
-      //  
-      // SHSPLIT: load users from the shared memory
-      //
-      return ($user);
-    } while (0);
-    
-    if ($doexit)
-      exit();
-    
-    return (FALSE);
+      log_main("load_data: id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
+      
+      $doexit = FALSE;
+      do {
+          if (($tok = @ftok(FTOK_PATH."/user".$id, "B")) == -1) {
+              log_main("ftok failed");
+              $doexit = TRUE;
+              break;
+          }
+          
+          if (($shm_sz = sharedmem_sz($tok)) == -1) {
+              log_main("shmop_open failed");
+          }
+          
+          if ($shm_sz == -1)
+              $shm_sz = SHM_DIMS_U_MIN;
+          
+          if ($shm = shm_attach($tok, $shm_sz)) {
+              $user = @shm_get_var($shm, $tok);
+              
+              if ($sess != FALSE && $user->sess != $sess) {
+                  $doexit = TRUE;
+                  break;
+              }
+              log_only("user ==  ".($user == FALSE ?   "FALSE" : "TRUE")."  user ===  ".($user === FALSE ? "FALSE" : "TRUE")."  user isset ".(isset($user) ?   "TRUE" : "FALSE"));
+              if (isset($user)) 
+                  log_only("bri count ".count($user));
+              
+              if ($user == FALSE) {
+                  log_only("INIT MAIN DATA");
+                  
+                  // SHSPLIT FIXME: init_data for User class ??
+                  $user = User::create($id, "", "");
+                  if (@shm_put_var($shm, $tok, $user) == FALSE) {
+                      log_only("PUT_VAR FALLITA ".strlen(serialize($user)));
+                      log_only(serialize($user));
+                  }
+              }
+              
+              $user->shm_sz = $shm_sz;
+              
+              shm_detach($shm);
+          }
+          
+          //  
+          // SHSPLIT: load users from the shared memory
+          //
+          return ($user);
+      } while (0);
+      
+      if ($doexit)
+          exit();
+      
+      return (FALSE);
   }
   
 
-  function save_data($user, $id) 
+  static function save_data($user, $id) 
   {
-    GLOBAL $sess;
-    
-    $shm =   FALSE;
-    
-    // var_dump($user);
-    // error_log("User::save_data BEGIN", 0);
-
-    if (($tok = @ftok(FTOK_PATH."/user".$id, "B")) == -1) {
-      return (FALSE);
-    }
-    while ($user->shm_sz < SHM_DIMS_U_MAX) {
-      if (($shm = shm_attach($tok, $user->shm_sz)) == FALSE)
-       break;
+      GLOBAL $sess;
       
-      // log_only("PUT_VAR DI ".strlen(serialize($user)));
-      if (shm_put_var($shm, $tok, $user) != FALSE) {
-       shm_detach($shm);
-       log_main("User[".$id."] saved.");
-       return (TRUE);
-      }
-      if (shm_remove($shm) === FALSE) {
-       log_only("REMOVE FALLITA");
-       break;
+      $shm =   FALSE;
+      
+      // var_dump($user);
+      // error_log("User::save_data BEGIN", 0);
+      
+      if (($tok = @ftok(FTOK_PATH."/user".$id, "B")) == -1) {
+          return (FALSE);
       }
-      shm_detach($shm);
-      $user->shm_sz += SHM_DIMS_U_DLT;
-    } 
-
-    if ($shm)
-      shm_detach($shm);
-    
-    return (FALSE);
-  }
-
-
-  static function step_get($sess)
-  {
-      $fp = FALSE;
-      do {
-          if (validate_sess($sess) == FALSE)
+      while ($user->shm_sz < SHM_DIMS_U_MAX) {
+          if (($shm = shm_attach($tok, $user->shm_sz)) == FALSE)
               break;
           
-          if (file_exists(PROXY_PATH) == FALSE)
-              mkdir(PROXY_PATH);
-          if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
-              break;
-          if (($s = fread($fp, 8)) == FALSE)
-              break;
-          if (strlen($s) != 8)
+          // log_only("PUT_VAR DI ".strlen(serialize($user)));
+          if (shm_put_var($shm, $tok, $user) != FALSE) {
+              shm_detach($shm);
+              log_main("User[".$id."] saved.");
+              return (TRUE);
+          }
+          if (shm_remove($shm) === FALSE) {
+              log_only("REMOVE FALLITA");
               break;
-          $arr = unpack('Ls/Li', $s);
-          fclose($fp);
-          
-          // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
-          return ($arr);
-      } while (0);
-      
-      if ($fp != FALSE)
-          fclose($fp);
+          }
+          shm_detach($shm);
+          $user->shm_sz += SHM_DIMS_U_DLT;
+      } 
       
-      log_rd2("STEP_GET [".$sess."]: return false ");
+      if ($shm)
+          shm_detach($shm);
       
       return (FALSE);
   }
 
+
 } // end class User
 
 
index 90cc082..d9427fa 100644 (file)
@@ -415,95 +415,270 @@ class Bin5_table extends Table {
 
     return ($ct);
   }
-} // end class Table_briskin5
+} // end class Bin5_table
 
 
 
 
 define(BIN5_USER_FLAG_RING_ENDAUCT, 0x01);
 
-class User_briskin5 extends User {
-  var $asta_card;  // 
-  var $asta_pnt;   //
-  var $handpt;     // Total card points at the beginning of the current hand.
-  var $exitislock; // Player can exit from the table ?
-  var $privflags;  // Flags for briskin5 only 
-
-  function User() {
-  }
-
-  /* CREATE NOT USED
-  function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
-    if (($thiz =& new User()) == FALSE)
-      return (FALSE);
-
-    $thiz->asta_card = -2;
-    $thiz->asta_pnt  = -1;
-    $thiz->handpt = -1;
-    $thiz->exitislock = TRUE;
-    $thiz->privflags = 0;
-
-    return ($thiz);
-  }
-  */
-
-  function parentcopy(&$from)
-  {
-    parent::copy($from);
-  }
-
-  function copy(&$from)
-  {
-    $this->parentcopy($from);
-
-    $this->asta_card  = $from->asta_card;
-    $this->asta_pnt   = $from->asta_pnt;
-    $this->handpt     = $from->handpt;
-    $this->exitislock = $from->exitislock;
-    $this->privflags  = $from->privflags;
-  }
-
-  /* CLONE NOT USED
-  function myclone(&$from)
-  {
-    if (($thiz =& new User()) == FALSE)
-      return (FALSE);
-
-    $thiz->copy($from);
-
-    return ($thiz);
-  } 
-  */
-  
-  function spawn(&$from, $table, $table_pos)
-  {
-    GLOBAL $CO_bin5_pref_ring_endauct;
+class Bin5_user extends User {
+    var $asta_card;  // 
+    var $asta_pnt;   //
+    var $handpt;     // Total card points at the beginning of the current hand.
+    var $exitislock; // Player can exit from the table ?
+    var $privflags;  // Flags for briskin5 only 
     
-    if (($thiz =& new User_briskin5()) == FALSE)
-      return (FALSE);
+    function User() {
+    }
     
-    $thiz->parentcopy($from);
-
-    $thiz->asta_card = -2;
-    $thiz->asta_pnt  = -1;
-    $thiz->handpt = -1;
-    $thiz->exitislock = TRUE;
-
-    log_wr("Briskin5 constructor");
-
-    $this->privflags  = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
+    /* CREATE NOT USED
+     function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
+     if (($thiz =& new User()) == FALSE)
+     return (FALSE);
+     
+     $thiz->asta_card = -2;
+     $thiz->asta_pnt  = -1;
+     $thiz->handpt = -1;
+     $thiz->exitislock = TRUE;
+     $thiz->privflags = 0;
+     
+     return ($thiz);
+     }
+    */
+    
+    function parentcopy(&$from)
+    {
+        parent::copy($from);
+    }
+    
+    function copy(&$from)
+    {
+        $this->parentcopy($from);
+        
+        $this->asta_card  = $from->asta_card;
+        $this->asta_pnt   = $from->asta_pnt;
+        $this->handpt     = $from->handpt;
+        $this->exitislock = $from->exitislock;
+        $this->privflags  = $from->privflags;
+    }
+    
+    /* CLONE NOT USED
+     function myclone(&$from)
+     {
+     if (($thiz =& new User()) == FALSE)
+     return (FALSE);
+     
+     $thiz->copy($from);
+     
+     return ($thiz);
+     } 
+    */
+    
+    function spawn($from, $table, $table_pos)
+    {
+        GLOBAL $CO_bin5_pref_ring_endauct;
+        
+        if (($thiz = new Bin5_user()) == FALSE)
+            return (FALSE);
+        
+        $thiz->parentcopy($from);
+        
+        /* NOTE: at this moment idx and table_pos fields have the same value 
+                 but diffentent functions, we keep them separated for a while */
+        $thiz->idx        = $table_pos;
+        $thiz->asta_card  = -2;
+        $thiz->asta_pnt   = -1;
+        $thiz->handpt     = -1;
+        $thiz->exitislock = TRUE;
+        
+        log_wr("Bin5 constructor");
+        
+        $this->privflags  = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
+        
+        $thiz->table_orig = $table;
+        $thiz->table      = 0;
+        $thiz->table_pos  = $table_pos;
+        
+        $thiz->step_inc();
+
+        return ($thiz);
+    }
 
-    $thiz->table_orig = $table;
-    $thiz->table      = 0;
-    $thiz->table_pos  = $table_pos;
+    function step_set($step) 
+    {
+        $this->step = $step & 0x7fffffff;
+        
+        do {
+            if (validate_sess($this->sess) == FALSE)
+                break;
+            if (file_exists(BIN5_PROXY_PATH) == FALSE)
+                mkdir(BIN5_PROXY_PATH, 0775, TRUE);
+            if (($fp = @fopen(BIN5_PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
+                break;
+            fwrite($fp, pack("LL",$this->step, $this->idx));
+            fclose($fp);
+            
+            log_main("step_set [".$this->sess. "] [".$this->step."]"); 
+            
+            return (TRUE);
+        } while (0);
+        
+        return (FALSE);
+    }
+    
+    function step_inc($delta = 1) {
+        $this->step += $delta;
+        /* modularization because unpack() not manage unsigned 32bit int correctly */
+        $this->step &= 0x7fffffff;
+        
+        if (validate_sess($this->sess)) {
+            if (file_exists(BIN5_PROXY_PATH) == FALSE)
+                mkdir(BIN5_PROXY_PATH);
+            $fp = fopen(BIN5_PROXY_PATH."/".$this->sess.".step", 'w');
+            fwrite($fp, pack("LL", $this->step, $this->idx));
+            fclose($fp);
+            
+            log_main("step_inc [".$this->sess. "] [".$this->step."]"); 
+            
+            return (TRUE);
+        }
+        
+        return (FALSE);
+    }
+    
+    static function step_get($sess)
+    {
+        $fp = FALSE;
+        do {
+            if (validate_sess($sess) == FALSE)
+                break;
+            
+            if (file_exists(BIN5_PROXY_PATH) == FALSE)
+                mkdir(BIN5_PROXY_PATH);
+            if (($fp = @fopen(BIN5_PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
+                break;
+            if (($s = fread($fp, 8)) == FALSE)
+                break;
+            if (strlen($s) != 8)
+                break;
+            $arr = unpack('Ls/Li', $s);
+            fclose($fp);
+            
+            // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
+            return ($arr);
+        } while (0);
+        
+        if ($fp != FALSE)
+            fclose($fp);
+        
+        log_rd2("STEP_GET [".$sess."]: return false ");
+        
+        return (FALSE);
+    }
+    
+    static function load_data($tab_id, $id, $sess) 
+    {
+        // error_log("Bin5_user::load_data BEGIN", 0);
+        error_log("Bin5_user::load_data BEGIN id [".$id."]", 0);
+        
+        log_main("load_data: tab_id [".$tab_id."] id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
+        
+        $doexit = FALSE;
+        do {
+            if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$id, "B")) == -1) {
+                log_main("ftok failed");
+                $doexit = TRUE;
+                break;
+            }
+            
+            if (($shm_sz = sharedmem_sz($tok)) == -1) {
+                log_main("shmop_open failed");
+            }
+            
+            if ($shm_sz == -1)
+                $shm_sz = SHM_DIMS_U_MIN;
+            
+            if ($shm = shm_attach($tok, $shm_sz)) {
+                $user = @shm_get_var($shm, $tok);
+                
+                if ($sess != FALSE && $user->sess != $sess) {
+                    $doexit = TRUE;
+                    break;
+                }
+                log_only("user ==  ".($user == FALSE ?   "FALSE" : "TRUE")."  user ===  ".($user === FALSE ? "FALSE" : "TRUE")."  user isset ".(isset($user) ?   "TRUE" : "FALSE"));
+                if (isset($user)) 
+                    log_only("bri count ".count($user));
+                
+                if ($user == FALSE) {
+                    log_only("INIT MAIN DATA");
+                    
+                    // SHSPLIT FIXME: init_data for User class ??
+                    $user = User::create($id, "", "");
+                    if (@shm_put_var($shm, $tok, $user) == FALSE) {
+                        log_only("PUT_VAR FALLITA ".strlen(serialize($user)));
+                        log_only(serialize($user));
+                    }
+                }
+                
+                $user->shm_sz = $shm_sz;
+                
+                shm_detach($shm);
+            }
+            
+            //  
+            // SHSPLIT: load users from the shared memory
+            //
+            return ($user);
+        } while (0);
+        
+        if ($doexit)
+            exit();
+        
+        return (FALSE);
+    }
 
-    return ($thiz);
+  static function save_data($user, $tab_id, $id) 
+  {
+      GLOBAL $sess;
+      
+      $shm =   FALSE;
+      
+      // var_dump($user);
+      error_log("Bin5_user::save_data BEGIN id [".$id."]", 0);
+      
+      if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$id, "B")) == -1) {
+          return (FALSE);
+      }
+      while ($user->shm_sz < SHM_DIMS_U_MAX) {
+          if (($shm = shm_attach($tok, $user->shm_sz)) == FALSE)
+              break;
+          
+          // log_only("PUT_VAR DI ".strlen(serialize($user)));
+          if (shm_put_var($shm, $tok, $user) != FALSE) {
+              shm_detach($shm);
+              log_main("User[".$id."] saved.");
+              return (TRUE);
+          }
+          if (shm_remove($shm) === FALSE) {
+              log_only("REMOVE FALLITA");
+              break;
+          }
+          shm_detach($shm);
+          $user->shm_sz += SHM_DIMS_U_DLT;
+      } 
+      
+      if ($shm)
+          shm_detach($shm);
+      
+      return (FALSE);
   }
-} // end class User_briskin5
 
+} // end class Bin5_user
 
 
-class Briskin5 {
+
+class Bin5 {
   var $user;
   var $table;
   var $comm; // commands for many people
@@ -620,29 +795,25 @@ class Briskin5 {
       }
       log_rd2($user_cur->sess." GARBAGE UPDATED!");
       
-      $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
+      // externalized $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
+      Bin5::garbage_time_expire_set($curtime + GARBAGE_TIMEOUT);
+
       $ismod = TRUE;
     }
 
     return ($ismod);
   }
 
-
-
-
-  //
-  //  static functions
-  //
-  function load_data($table_idx, $table_token = "") 
+  // Bin5::load_data
+  static function load_data($table_idx, $table_token = "") 
   {
-    GLOBAL $G_false, $sess;
     $doexit = FALSE;
     $shm = FALSE;
 
-    log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
+    log_wr("TABLE_IDX ".FTOK_PATH."/bin5/table".$table_idx."/table");
     
     do {
-      if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
+      if (($tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
        log_main("ftok failed");
        $doexit = TRUE;
        break;
@@ -679,49 +850,60 @@ class Briskin5 {
 
     log_wr("briskin5 load_data failed");
     if ($doexit)
-      exit();
+        exit();
     
     return (FALSE);
   }
   
 
 
-  function save_data(&$bri) 
+  function save_data($bri) 
   {
-    GLOBAL $sess;
-    
-    $ret =   FALSE;
-    $shm =   FALSE;
-    
-    log_main("SAVE BRISKIN5 DATA");
-    
-    if (!isset($bri->tok))
-      return (FALSE);
-    
-    while ($bri->shm_sz < BRISKIN5_SHM_MAX) {
-      if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
-       break;
+      GLOBAL $sess;
+      
+      $ret =   FALSE;
+      $shm =   FALSE;
       
-      if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
-       shm_detach($shm);
-       return (TRUE);
+      log_main("SAVE BRISKIN5 DATA");
+      
+      if (!isset($bri->tok))
+          return (FALSE);
+      
+      $user_park = array();
+      for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
+          $user_park[$i] = $bri->user[$i];
+          $bri->user[$i] = FALSE;
       }
-      if (shm_remove($shm) === FALSE) {
-       log_only("REMOVE FALLITA");
-       break;
+      
+      while ($bri->shm_sz < BIN5_SHM_MAX) {
+          if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
+              break;
+          
+          if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
+              $ret = TRUE;
+              break;
+          }
+          if (shm_remove($shm) === FALSE) {
+              log_only("REMOVE FALLITA");
+              break;
+          }
+          shm_detach($shm);
+          $bri->shm_sz += BIN5_SHM_DLT;
+      } 
+      
+      if ($shm)
+          shm_detach($shm);
+      
+      // SHSPLIT: reattach users to the room class
+      for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
+          Bin5_user::save_data($user_park[$i], $bri->table_idx, $i);
+          $bri->user[$i] = $user_park[$i];
       }
-      shm_detach($shm);
-      $bri->shm_sz += BRISKIN5_SHM_DLT;
-    } 
-
-    log_crit("save data failed!");
-
-    if ($shm)
-      shm_detach($shm);
-    
-    return ($ret);
+      log_load("FINISH: ".($ret == TRUE ? "TRUE" : "FALSE"));
+      
+      return ($ret);
   }
-
+  
 
 
   function destroy_data($table_idx) 
@@ -735,7 +917,7 @@ class Briskin5 {
     do {
       log_main("DESTROY2 BRISKIN5 DATA");
 
-      if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) 
+      if (($tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) 
        break;
 
       if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
@@ -763,11 +945,11 @@ class Briskin5 {
   {
     GLOBAL $sess; 
     
-    log_lock("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
+    log_lock("LOCK_DATA ".FTOK_PATH."/bin5/table".$table_idx."/table");
     //  echo "LOCK: ".FTOK_PATH."/main";
     //  exit;
     // WARNING monitor this step
-    if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
+    if (($tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
       return (FALSE);
     }
     // WARNING monitor this step
index 5828065..c141dc8 100644 (file)
@@ -75,211 +75,230 @@ function page_sync($sess, $page)
 
 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $table_idx, $table_token)
 {
-  GLOBAL $is_page_streaming, $first_loop;
-  
-  $ret = FALSE;
-  $bri = FALSE;
-
-  // log_rd2("M");
-  /* Sync check (read only without modifications */
-  ignore_user_abort(TRUE);
-  if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { 
-    // Aggiorna l'expire time lato server
-    if  ($first_loop == TRUE) {
-      log_only("F");
-
-      // VERIFICARE TUTTE LE LOAD_DATA E PRENDERE CONTROMISURE NEL CASO FALLISCANO //
-
-      if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) {
-       Briskin5::unlock_data($sem);
-        ignore_user_abort(FALSE);
-       return (unrecerror());
-      }
-      if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
-       Briskin5::unlock_data($sem);
-        ignore_user_abort(FALSE);
-       return (unrecerror());
-      }
-      log_auth($sess, "bin5::update lacc");
-      $user->lacc = time();
-
-      $bri->garbage_manager(FALSE);
-      
-      Briskin5::save_data($bri);
-      $first_loop = FALSE;
+    GLOBAL $is_page_streaming, $first_loop, $S_load_stat;
+    
+    $ret = FALSE;
+    $bri = FALSE;
+    $user = FALSE;
+    $curtime = time();
+    
+    if (($proxy_step = Bin5_user::step_get($sess)) == FALSE) {
+        log_only2("R");
+        return (FALSE);
     }
+    
+    error_log("maincheck: step di i [".$proxy_step['i']."]", 0);
 
-    log_lock("U");
-    Briskin5::unlock_data($sem);
-    ignore_user_abort(FALSE);
-  }
-  else {
-    return (FALSE);
-  }
-  
-  if (($proxy_step = User::step_get($sess)) != FALSE) {
-    // log_rd2("Postget".$proxy_step."zizi");
+    // log_rd2("M");
+    /* Sync check (read only without modifications */
+    ignore_user_abort(TRUE);
+    if  ($first_loop == TRUE) {
+        if (($sem = Bin5::lock_data($table_idx)) != FALSE) { 
+            // Aggiorna l'expire time lato server
+            $S_load_stat['U_first_loop']++;
+            if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
+                Bin5::unlock_data();
+                ignore_user_abort(FALSE);
+                return (unrecerror());
+            }
+            $user->lacc = $curtime;
+            Bin5_user::save_data($user, $proxy_step['i'], $user->idx);
+            
+            if (Bin5::garbage_time_is_expired($curtime)) {
+                log_only("F");
+                
+                $S_load_stat['R_garbage']++;
+                if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
+                    Bin5::unlock_data($sem);
+                    ignore_user_abort(FALSE);
+                    return (unrecerror());
+                }
+                
+                $bri->garbage_manager(FALSE);
+                
+                Bin5::save_data($bri);
+                unset($bri);
+            }
+            log_lock("U");
+            Bin5::unlock_data($sem);
+            ignore_user_abort(FALSE);
+        } // if (($sem = Bin5::lock_data($table ...
+        else {
+            ignore_user_abort(FALSE);
+            
+            return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
+        }
+        
+        $first_loop = FALSE;
+    } // if  ($first_loop == TRUE) {
     
     if ($cur_step == $proxy_step['s']) {
-      log_lock("P");
-      return (FALSE);
+        log_lock("P");
+        return (FALSE);
     }
     else {
-      log_only2("R");
+        log_only2("R");
     }
-  }
-  else {
-    log_only2("R");
-  }
-  
-  if ($bri == FALSE) {
-    do {
-      ignore_user_abort(TRUE);
-      if (($sem = Briskin5::lock_data($table_idx)) == FALSE) 
-       break;
-      
-      log_lock("P");
-      if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) 
-       break;
-    } while (0);
     
-    if ($sem != FALSE)
-      Briskin5::unlock_data($sem);
+    if ($user == FALSE) {
+        do {
+            ignore_user_abort(TRUE);
+            if (($sem = Bin5::lock_data($table_idx)) == FALSE) 
+                break;
+            
+            log_lock("P");
+            $S_load_stat['U_heavy']++;
+            // if (($bri = &Bin5::load_data($table_idx, $table_token)) == FALSE) 
+            // if (($user = Bin5_user::load_data($table_idx, $table_token)) == FALSE) 
+            if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
+                break;
+            }
+        } while (0);
+        
+        if ($sem != FALSE)
+            Bin5::unlock_data($sem);
+        
+        ignore_user_abort(FALSE);
+        if ($user == FALSE) 
+            return (unrecerror());
+    }
     
-    ignore_user_abort(FALSE);
-    if ($bri == FALSE) 
-      return (unrecerror());
-  }
-  
-  if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
-    return (unrecerror());
-  }
-
-  /* Nothing changed, return. */
-  if ($cur_step == $user->step) 
-    return;
-
-  log_rd2("do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
-
-  if ($cur_step == -1) {
-    // FUNZIONE from_scratch DA QUI 
-    ignore_user_abort(TRUE);
-    $sem = Briskin5::lock_data($table_idx);
-    $bri = &Briskin5::load_data($table_idx, $table_token);
-    if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
-      Briskin5::unlock_data($sem);
-      ignore_user_abort(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."]");
+
+    if ($cur_step == -1) {
+        /*
+         *  if $cur_step == -1 load the current state from the main struct
+         */
+        ignore_user_abort(TRUE);
+        $sem = Bin5::lock_data($table_idx);
+        $bri = Bin5::load_data($table_idx, $table_token);
+        $S_load_stat['R_minusone']++;
+        
+        /* unset the $user var to reload it from main structure */
+        unset($user);
+        if (($user = $bri->get_user($sess, $idx)) == FALSE) {
+            Bin5::unlock_data($sem);
+            ignore_user_abort(FALSE);
+            return (unrecerror());
+        }
+        if ($user->the_end) {
+            log_rd2("main_check: the end".var_export(debug_backtrace()));
+            $is_page_streaming = TRUE;
+        }
+        
+        if ($user->trans_step != -1) {
+            log_rd2("TRANS USATO ".$user->trans_step);
+            $cur_step = $user->trans_step;
+            $user->trans_step = -1;
+            
+            Bin5::save_data($bri);
+            Bin5::unlock_data($sem);
+            ignore_user_abort(FALSE);
+        }
+        else {
+            log_rd2("TRANS NON ATTIVATO");
+            
+            //       ARRAY_POP DISABLED
+            //       while (array_pop($user->comm) != NULL);
+            //       // $user->step_inc(COMM_N + 1);
+            //       Bin5::save_data($bri);
+            
+            Bin5::unlock_data($sem);
+            ignore_user_abort(FALSE);
+        }
     }
-    if ($user->the_end) 
-      $is_page_streaming = TRUE;
-
-
-    if ($user->trans_step != -1) {
-      log_rd2("TRANS USATO ".$user->trans_step);
-      $cur_step = $user->trans_step;
-      $user->trans_step = -1;
-
-
-      Briskin5::save_data($bri);
-      Briskin5::unlock_data($sem);
-      ignore_user_abort(FALSE);
+    
+    if ($cur_step == -1) {
+        log_rd2("PRE-NEWSTAT.");
+        
+        /***************
+         *             *
+         *    TABLE    *
+         *             *
+         ***************/
+        if ($user->stat == "table") {      
+            $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
+            
+            log_rd2("SENDED TO THE STREAM: ".$ret);
+        }
+        log_rd2("NEWSTAT: ".$user->stat);
+        
+        $new_stat =  $user->stat;
+        $new_subst = $user->subst;
+        $new_step =  $user->step;
     }
     else {
-      log_rd2("TRANS NON ATTIVATO");
-
-//       ARRAY_POP DISABLED
-//       while (array_pop($user->comm) != NULL);
-//       // $user->step_inc(COMM_N + 1);
-//       Briskin5::save_data($bri);
-
-      Briskin5::unlock_data($sem);
-      ignore_user_abort(FALSE);
-    }
-  }
-      
-  if ($cur_step == -1) {
-    log_rd2("PRE-NEWSTAT.");
-
-    /***************
-     *             *
-     *    TABLE    *
-     *             *
-     ***************/
-    if ($user->stat == "table") {      
-      $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
-
-      log_rd2("SENDED TO THE STREAM: ".$ret);
-    }
-    log_rd2("NEWSTAT: ".$user->stat);
-
-    $new_stat =  $user->stat;
-    $new_subst = $user->subst;
-    $new_step =  $user->step;
-  }
-  else {
-    ignore_user_abort(TRUE);
-    $sem = Briskin5::lock_data($table_idx);
-    if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) {
-      Briskin5::unlock_data($sem);
-      ignore_user_abort(FALSE);
-      return (unrecerror());
-    }
-    if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
-      Briskin5::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;
-           Briskin5::unlock_data($sem);
-           ignore_user_abort(FALSE);
-           return (page_sync($user->sess, $to_stat == "table" ? "index.php" : "../index.php"));
-         }
-         log_rd2("lost history, refresh from scratch");
-         $new_step = -1;
-         break;
-       } 
-       for ($i = $cur_step ; $i < $user->step ; $i++) {
-         $ii = $i % COMM_N;
-         log_wr("TRY RET ".$i."  COMM_N ".COMM_N."  II ".$ii);
-         $ret .= $user->comm[$ii];
-       }
-       $new_stat =  $user->stat;
-       $new_subst = $user->subst;
-       $new_step =  $user->step;
-      } while (0);
-
-      log_mop($user->step, 'bin::index_rd.php: after ret set');
-      
-      if ($user->the_end == TRUE) {
-       log_rd2("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);
-       else if ($user->subst == 'standup')
-         $bri->room_outstandup(&$user);
-       else
-         log_rd2("LOGOUT FROM WHAT ???");
-         
-       Briskin5::save_data($bri);
-      }
-    }
-         
-    Briskin5::unlock_data($sem);
-    ignore_user_abort(FALSE);
+        ignore_user_abort(TRUE);
+        $sem = Bin5::lock_data($table_idx);
+        // if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
+        if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
+            Bin5::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;
+                        Bin5::unlock_data($sem);
+                        ignore_user_abort(FALSE);
+                        return (page_sync($user->sess, $to_stat == "table" ? "index.php" : "../index.php"));
+                    }
+                    log_rd2("lost history, refresh from scratch");
+                    $new_step = -1;
+                    break;
+                } 
+                for ($i = $cur_step ; $i < $user->step ; $i++) {
+                    $ii = $i % COMM_N;
+                    log_wr("TRY RET ".$i."  COMM_N ".COMM_N."  II ".$ii);
+                    $ret .= $user->comm[$ii];
+                }
+                $new_stat =  $user->stat;
+                $new_subst = $user->subst;
+                $new_step =  $user->step;
+            } while (0);
+            
+            log_mop($user->step, 'bin::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']++;
+                $bri = Bin5::load_data($table_idx, $table_token);
+                unset($user);
+                if (($user = $bri->get_user($sess, $idx)) == FALSE) {
+                    Bin5::unlock_data($sem);
+                    ignore_user_abort(FALSE);
+                    return (unrecerror());
+                }
+                
+                $tmp_sess = $user->sess;
+                $user->sess = "";
+                step_unproxy($tmp_sess);
+                $user->name = "";
+                $user->the_end = FALSE;
+                
+                if ($user->subst == 'sitdown')
+                    $bri->room_wakeup($user);
+                else if ($user->subst == 'standup')
+                    $bri->room_outstandup($user);
+                else
+                    log_rd2("LOGOUT FROM WHAT ???");
+                
+                Bin5::save_data($bri);
+            }
+        }
+        
+        Bin5::unlock_data($sem);
+        ignore_user_abort(FALSE);
   }
-
+  
   
   return ($ret);
 }