xhr refactoring: renaming xhr_rd to xhr, remove all xhr_rd_ prefixies from http_strea...
[brisk.git] / web / briskin5 / Obj / briskin5.phh
index ce2bdb8..d298928 100644 (file)
@@ -520,16 +520,16 @@ class Bin5_user extends User {
         return (TRUE);
     }
 
-    static function load_step($sess)
+    static function load_step($tab_id, $sess)
     {
         $fp = FALSE;
         do {
             if (validate_sess($sess) == FALSE)
                 break;
             
-            if (file_exists(BIN5_PROXY_PATH) == FALSE)
-                mkdir(BIN5_PROXY_PATH, 0775, TRUE);
-            if (($fp = @fopen(BIN5_PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
+            if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
+                mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
+            if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
                 break;
             if (($s = fread($fp, 8)) == FALSE)
                 break;
@@ -555,9 +555,9 @@ class Bin5_user extends User {
         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)
+            if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
+                mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
+            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);
@@ -570,10 +570,18 @@ class Bin5_user extends User {
         return (FALSE);
     }
     
+    static function unproxy_step($tab_id, $sess)
+    {
+        log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
+        if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
+            return;
+
+        @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
+    }
     
     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) {
@@ -615,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);
@@ -677,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
 
 
@@ -723,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");
   }
@@ -762,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 == "" || 
@@ -779,7 +817,7 @@ class Bin5 {
 
            $tmp_sess = $user_cur->sess;
            $user_cur->sess = "";
-           step_unproxy($tmp_sess);
+           Bin5_user::step_unproxy($tmp_sess);
            $user_cur->name = "";
            $user_cur->the_end = FALSE;
            
@@ -801,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;
     }
@@ -916,7 +954,7 @@ class Bin5 {
   
 
 
-  function destroy_data($table_idx
+  function destroy_data() 
   {
     GLOBAL $sess;
 
@@ -925,9 +963,12 @@ class Bin5 {
     log_main("DESTROY BRISKIN5 DATA");
     
     do {
-      log_main("DESTROY2 BRISKIN5 DATA");
-
-      if (($tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) 
+      log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
+      for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
+          $this->user[$i]->destroy_data($this->table_idx);
+          Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
+      }
+      if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1) 
        break;
 
       if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
@@ -951,34 +992,20 @@ class Bin5 {
     return ($ret);
   }
 
-  function lock_data($table_idx)
+  static function lock_data($is_exclusive, $table_idx)
   {
-    GLOBAL $sess; 
-    
-    log_main("lockinfo: DATA ".FTOK_PATH."/bin5/table".$table_idx."/table");
-    //  echo "LOCK: ".FTOK_PATH."/main";
-    //  exit;
-    // WARNING monitor this step
-    if (($tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
-      return (FALSE);
-    }
-    // WARNING monitor this step
-    if (($res = @sem_get($tok)) == FALSE) {
+      if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
+          self::$delta_t = microtime(TRUE);
+          log_lock("LOCK   table [".$table_idx."]         [".self::$delta_t[$table_idx]."]");
+          
+          return (new Vect(array('res' => $res, 'tab' => $table_idx)));
+      }
+      
       return (FALSE);
-    }
-    if (sem_acquire($res)) {   
-        self::$delta_t[$table_idx] = microtime(TRUE);
-        log_lock("LOCK   table [".$table_idx."]         [".self::$delta_t[$table_idx]."]");
-
-        return (new Vect(array('res' => $res, 'tab' => $table_idx)));
-    }
-    else {
-        log_lock("LOCK table ".$table_idx.":FAILED");
-        return (FALSE);
-    }
   }
+
   
-  function unlock_data($res_vect)
+  static function unlock_data($res_vect)
   {
     GLOBAL $sess; 
 
@@ -987,7 +1014,7 @@ class Bin5 {
 
     log_lock("UNLOCK table [".$tab."]         [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
 
-    return (sem_release($res));
+    file_unlock($res);
   }
 
 
@@ -1024,7 +1051,7 @@ class Bin5 {
        //      if ($user_cur->sess == '' || $user_cur->stat != 'room')
        if ($user_cur->sess == '')
          continue;
-       if ($user_cur->name == $name_new) {
+        if (strcasecmp($user_cur->name, $name_new) == 0) {
          $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
 
           $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
@@ -1078,10 +1105,7 @@ class Bin5 {
                       (BIN5_PLAYERS_N == 3 ? 0:  $this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->flags),
                       (BIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->name)));
            if ($user_cur == $user) {
-              $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
-              $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
-             $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', 
-                                                                  $itin, xcape($user->name,ENT_COMPAT,"UTF-8"), $itou);
+                $user_cur->comm[$user_cur->step % COMM_N] .= $user_cur->myname_innerHTML();
             }
            $user_cur->step_inc();
          }
@@ -1129,7 +1153,7 @@ class Bin5 {
       $user_cur->laccwr = $curtime;
 
       $ret = "gst.st = ".($user_cur->step+1)."; ";
-      $ret .= 'gst.st_loc++; the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
+      $ret .= 'gst.st_loc++; hstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
 
       log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
@@ -1162,14 +1186,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 (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
+              mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
+          if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/garbage_time.expired", 'rb')) == FALSE)
               break;
           if (($s = fread($fp, 4)) == FALSE)
               break;
@@ -1188,12 +1212,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 (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
+              mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
+          if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/garbage_time.expired", 'wb')) == FALSE)
               break;
           fwrite($fp, pack("L",$tm));
           fclose($fp);
@@ -1345,10 +1369,7 @@ function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
     $ret .= "background_set();";
     
     /* USERS INFO */
-    $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
-    $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
-
-    $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
+    $ret .= $user->myname_innerHTML();
     $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
                    $room->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
                    xcape($room->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),