garbage management refactored
[brisk.git] / web / briskin5 / Obj / briskin5.phh
index 7b26f01..fffd692 100644 (file)
@@ -520,7 +520,7 @@ class Bin5_user extends User {
         return (TRUE);
     }
 
-    static function load_step($sess)
+    static function load_step($tab_id, $sess)
     {
         $fp = FALSE;
         do {
@@ -529,7 +529,7 @@ class Bin5_user extends User {
             
             if (file_exists(BIN5_PROXY_PATH) == FALSE)
                 mkdir(BIN5_PROXY_PATH, 0775, TRUE);
-            if (($fp = @fopen(BIN5_PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
+            if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
                 break;
             if (($s = fread($fp, 8)) == FALSE)
                 break;
@@ -557,7 +557,7 @@ class Bin5_user extends User {
                 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)
+            if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
                 break;
             fwrite($fp, pack("LL",$this->step, $this->idx));
             fclose($fp);
@@ -581,7 +581,7 @@ class Bin5_user extends User {
     
     static function load_data($tab_id, $id, $sess) 
     {
-        log_main("Bin5_user::load_data: tab_id [".$tab_id."] id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
+        log_load("Bin5_user::load_data: tab_id [".$tab_id."] id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
         
         do {
             if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$id, "B")) == -1) {
@@ -623,7 +623,7 @@ class Bin5_user extends User {
                         // This part isn't strictly required but is good to verify
                         // the coerence of cached and User class saved value of step field.
                         $old_step = $user->step;
-                        $arr = Bin5_user::load_step($sess);
+                        $arr = Bin5_user::load_step($tab_id, $sess);
                         $user->step = $arr['s'];
                         if ($old_step != $user->step) {
                             log_crit("Bin5:: steps are diffetents User->step ".$user->step." Old_step: ".$old_step);
@@ -685,6 +685,36 @@ class Bin5_user extends User {
       return (FALSE);
   }
 
+  function destroy_data($tab_id) 
+  {
+      do {
+          if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
+              log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
+              break;
+          }
+          
+          if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
+              log_crit("BIN5 USER DATA REMOVE FAILED 2");
+              break;
+          }
+          if (shmop_delete($shm) == 0) {
+              log_crit("BIN5 USER DATA REMOVE FAILED 3");
+              break;
+          }
+          $shm = FALSE;
+
+          log_main("BIN5 USER DATA DESTROY SUCCESS");
+          
+          // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
+          $ret = TRUE;
+      } while (0);
+      
+      if ($shm)
+          shm_detach($shm);
+      
+      return ($ret);
+  }
+
 } // end class Bin5_user
 
 
@@ -731,7 +761,7 @@ class Bin5 {
 
     $this->table_idx = $table_idx;
     $this->table_token = $table_token;
-    Bin5::garbage_time_expire_set(0);
+    Bin5::garbage_time_expire_set($table_idx, 0);
     
     log_wr("Bin5 constructor end");
   }
@@ -770,7 +800,7 @@ class Bin5 {
     $ismod = FALSE;
     $curtime = time();
     // externalized if ($force || $this->garbage_timeout < $curtime) {
-    if ($force || Bin5::garbage_time_is_expired($curtime)) {
+    if ($force || Bin5::garbage_time_is_expired($this->table_idx, $curtime)) {
       for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
        $user_cur = $this->user[$i];
        if ($user_cur->sess == "" || 
@@ -809,7 +839,7 @@ class Bin5 {
       log_rd2($user_cur->sess." GARBAGE UPDATED!");
       
       // externalized $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
-      Bin5::garbage_time_expire_set($curtime + GARBAGE_TIMEOUT);
+      Bin5::garbage_time_expire_set($this->table_idx, $curtime + GARBAGE_TIMEOUT);
 
       $ismod = TRUE;
     }
@@ -924,7 +954,7 @@ class Bin5 {
   
 
 
-  function destroy_data($table_idx
+  function destroy_data() 
   {
     GLOBAL $sess;
 
@@ -1173,14 +1203,14 @@ class Bin5 {
   }
 
 
-  static function garbage_time_is_expired($tm) 
+  static function garbage_time_is_expired($tab_id, $tm) 
   {
       $ret = TRUE;
       $fp = FALSE;
       do {
           if (file_exists(BIN5_PROXY_PATH) == FALSE)
               mkdir(BIN5_PROXY_PATH, 0775, TRUE);
-          if (($fp = @fopen(BIN5_PROXY_PATH."/garbage_time.expired", 'rb')) == FALSE)
+          if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/garbage_time.expired", 'rb')) == FALSE)
               break;
           if (($s = fread($fp, 4)) == FALSE)
               break;
@@ -1199,12 +1229,12 @@ class Bin5 {
       return ($ret);
   }
 
-  static function garbage_time_expire_set($tm) 
+  static function garbage_time_expire_set($tab_id, $tm) 
   {
       do {
           if (file_exists(BIN5_PROXY_PATH) == FALSE)
               mkdir(BIN5_PROXY_PATH, 0775, TRUE);
-          if (($fp = @fopen(BIN5_PROXY_PATH."/garbage_time.expired", 'wb')) == FALSE)
+          if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/garbage_time.expired", 'wb')) == FALSE)
               break;
           fwrite($fp, pack("L",$tm));
           fclose($fp);