chat reset in user class
[brisk.git] / web / briskin5 / Obj / briskin5.phh
index ecd4b41..1ad1937 100644 (file)
@@ -1,5 +1,10 @@
 <?php
-define(MAX_BRISKIN5_PLAYERS, 3);
+define(BRISKIN5_PLAYERS_N, 3);
+define(BRISKIN5_MAX_PLAYERS, BRISKIN5_PLAYERS_N);
+// define(BRISKIN5_SHM_MIN, (50000 * BRISKIN5_MAX_PLAYERS));
+define(BRISKIN5_SHM_MIN, 32768);
+define(BRISKIN5_SHM_MAX, (BRISKIN5_SHM_MIN + 1048576));
+define(BRISKIN5_SHM_DLT, 32768);
 
 class Briskin5 {
   var $user;
@@ -11,13 +16,18 @@ class Briskin5 {
   var $step; // current step of the comm array
   var $garbage_timeout;
 
+  var $the_end;
+  
   var $tok;
+  var $shm_sz;
 
   function Briskin5 (&$room, $table_idx, $table_token) {
     $this->user = array();
     $this->table = array();
 
-    if (($this->tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
+    $this->the_end = FALSE;
+    $this->shm_sz = BRISKIN5_SHM_MIN;
+    if (($this->tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
       echo "FTOK FAILED";
       exit;
     }
@@ -25,7 +35,7 @@ class Briskin5 {
     $user  =& $room->user;
     $table =& $room->table[$table_idx];
 
-    log_wr("xxx", "Briskin5 constructor");
+    log_wr("Briskin5 constructor");
 
     for ($i = 0 ; $i < $table->player_n ; $i++) {
       $user[$table->player[$i]]->table_token = $table_token;
@@ -36,7 +46,7 @@ class Briskin5 {
     $this->table_token = $table_token;
     $this->garbage_timeout = 0;
     
-    log_wr("xxx", "Briskin5 constructor end");
+    log_wr("Briskin5 constructor end");
   }
 
 
@@ -45,7 +55,7 @@ class Briskin5 {
     GLOBAL $PHP_SELF, $G_false;
 
     if (validate_sess($sess)) {
-      for ($i = 0 ; $i < MAX_BRISKIN5_PLAYERS ; $i++) {
+      for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
        if (strcmp($sess, $this->user[$i]->sess) == 0) {
          // find it
          $idx = $i;
@@ -53,12 +63,12 @@ class Briskin5 {
          return ($ret);
        }
       }
-      log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
-      // for ($i = 0 ; $i < MAX_PLAYERS ; $i++) 
-      // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
+      log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
+      // for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) 
+      // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
     }
     else {
-      log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
+      log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
     }
 
     return ($G_false);
@@ -73,16 +83,17 @@ class Briskin5 {
     $curtime = time();
     if ($force || $this->garbage_timeout < $curtime) {
       
-      for ($i = 0 ; $i < MAX_BRISKIN5_PLAYERS ; $i++) {
+      for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
        $user_cur = &$this->user[$i];
-       if ($user_cur->sess == "")
+       if ($user_cur->sess == "" || 
+           ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
          continue;
        
        if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
-         log_rd2($user_cur->sess, "AUTO LOGOUT.");
+         log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
 
          if ($user_cur->stat == 'table') {
-           log_auth($user_cur->sess, "Autologout session.");
+           log_auth($user_cur->sess," bin5 Autologout session.");
 
            /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE 
 
@@ -94,13 +105,11 @@ class Briskin5 {
            
             */
 
-           log_rd2($user_cur->sess, "AUTO LOGOUT.");
-
            $this->table_wakeup(&$user_cur);
          }
        }
       }
-      log_rd2($user_cur->sess, "GARBAGE UPDATED!");
+      log_rd2($user_cur->sess." GARBAGE UPDATED!");
       
       $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
       $ismod = TRUE;
@@ -118,27 +127,31 @@ class Briskin5 {
   function &load_data($table_idx, $table_token = "") 
   {
     GLOBAL $G_false, $sess;
+    $doexit = FALSE;
     $shm = FALSE;
 
-    log_wr($sess, "TABLE_IDX ".FTOK_PATH."/table".$table_idx);
-    if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
-      echo "FTOK FAILED";
-      exit;
-    }
+    log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
     
     do {
-      //      if (shmchk_exists($tok) == FALSE)
-      if (locshm_exists($tok) == FALSE)
+      if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
+       log_main("ftok failed");
+       $doexit = TRUE;
        break;
+      }
 
-      if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
+      if (($shm_sz = sharedmem_sz($tok)) == -1) {
+       log_main("shmop_open failed");
+       break;
+      }
+       
+      if (($shm = shm_attach($tok, $shm_sz)) == FALSE)
        break;
 
       if (($bri = @shm_get_var($shm, $tok)) == FALSE) 
        break;
 
       if ($table_token != "" && $bri->table_token != $table_token) {
-       log_wr($sess, "bri->table_token: ".$bri->table_token."table_token: ".$table_token);
+       log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
        
        break;
       }
@@ -153,80 +166,79 @@ class Briskin5 {
     if ($shm != FALSE)
       shm_detach($shm);
 
-    log_wr($sess, "briskin5 load_data failed");
+    log_wr("briskin5 load_data failed");
+    if ($doexit)
+      exit();
     
     return ($G_false);
   }
   
 
+
   function save_data(&$bri) 
   {
     GLOBAL $sess;
     
     $ret =   FALSE;
     $shm =   FALSE;
-    $isacq = FALSE;
     
-    log_main("XXX", "SAVE BRISKIN5 DATA");
-    // var_dump($bri);
+    log_main("SAVE BRISKIN5 DATA");
     
     if (!isset($bri->tok))
       return (FALSE);
     
-    do {
-      $isacq = TRUE;
-      
-      if (($shm = shm_attach($bri->tok, SHM_DIMS)) == FALSE)
+    while ($bri->shm_sz < BRISKIN5_SHM_MAX) {
+      if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
        break;
       
-      // log_only($sess, "PUT_VAR DI ".strlen(serialize($bri)));
-      if (shm_put_var($shm, $bri->tok, $bri) == FALSE) {
-       log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($bri)));
-       log_only($sess, serialize($bri));
+      if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
+       shm_detach($shm);
+       return (TRUE);
+      }
+      if (shm_remove($shm) === FALSE) {
+       log_only("REMOVE FALLITA");
        break;
       }
-      // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
-      $ret = TRUE;
-    } while (0);
-    
+      shm_detach($shm);
+      $bri->shm_sz += BRISKIN5_SHM_DLT;
+    } 
+
+    log_crit("save data failed!");
+
     if ($shm)
       shm_detach($shm);
     
     return ($ret);
   }
 
+
+
   function destroy_data($table_idx) 
   {
     GLOBAL $sess;
 
     $ret =   FALSE;
     $shm =   FALSE;
-    $isacq = FALSE;
-
-    log_main("XXX", "DESTROY BRISKIN5 DATA");
-
+    log_main("DESTROY BRISKIN5 DATA");
     
     do {
-      $isacq = TRUE;
-      
-      log_main("XXX", "DESTROY2 BRISKIN5 DATA");
+      log_main("DESTROY2 BRISKIN5 DATA");
 
-      if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) 
+      if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) 
        break;
 
-      if (($shm = shm_attach($tok, SHM_DIMS)) === FALSE)
+      if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
        break;
       
-      if (shm_remove($shm) === FALSE) {
-       log_only($sess, "REMOVE FALLITA ".strlen(serialize($bri)));
-       log_only($sess, serialize($bri));
+      if (shmop_delete($shm) == 0) {
+       log_only("REMOVE FALLITA ");
        break;
       }
    
       $shm = FALSE;
-      log_main("XXX", "DESTROY2 BRISKIN5 DATA SUCCESS");
+      log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
       
-      // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
+      // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
       $ret = TRUE;
     } while (0);
     
@@ -240,31 +252,34 @@ class Briskin5 {
   {
     GLOBAL $sess; 
     
-    log_wr($sess, "LOCK_DATA ".FTOK_PATH."/table".$table_idx);
+    log_wr("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
     //  echo "LOCK: ".FTOK_PATH."/main";
     //  exit;
-    if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
+    // FIXME monitor this step
+    if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
       echo "FTOK FAILED";
       exit;
     }
-    // echo "FTOK ".$tok."<br>";
-    if (($res = sem_get($tok)) == FALSE) {
+    // FIXME monitor this step
+    if (($res = @sem_get($tok)) == FALSE) {
       echo "SEM_GET FAILED";
       exit;
     }
     if (sem_acquire($res)) {   
-      log_only($sess, "LOCK");
+      log_lock("LOCK table ".$table_idx."[res: ".$res."]");
       return ($res);
     }
-    else
+    else {
+      log_lock("LOCK table ".$table_idx.":FAILED");
       return (FALSE);
+    }
   }
   
   function unlock_data($res)
   {
     GLOBAL $sess; 
     
-    log_only($sess, "UNLOCK");
+    log_lock("UNLOCK table [res: ".$res."]");
 
     return (sem_release($res));
   }
@@ -282,7 +297,7 @@ class Briskin5 {
     
     $dt = date("H:i ", $timecur);
     if (strncmp($user_mesg, "/nick ", 6) == 0) {
-      log_main($user->sess, "chatt_send BEGIN");
+      log_main($user->sess." chatt_send BEGIN");
 
       if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
          $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
@@ -296,7 +311,7 @@ class Briskin5 {
       // change
       // update local graph
       // update remote graphs
-      for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
+      for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
        $user_cur = &$this->user[$i];
        //      if ($user_cur->sess == '' || $user_cur->stat != 'room')
        if ($user_cur->sess == '')
@@ -308,14 +323,14 @@ class Briskin5 {
          break;
        }
       }
-      if ($i == MAX_PLAYERS) {
+      if ($i == BRISKIN5_MAX_PLAYERS) {
        $user->name = $name_new;
 
-      log_main($user->sess, "chatt_send start set");
+      log_main($user->sess." chatt_send start set");
        
 
-       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
-         log_main($user->sess, "chatt_send set loop");
+       for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
+         log_main($user->sess." chatt_send set loop");
          
          $user_cur = &$this->user[$i];
          if ($user_cur->sess == '')
@@ -326,11 +341,11 @@ class Briskin5 {
            }
            else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
                     $user->stat == 'table') {
-             log_main($user->sess, "chatt_send pre table update");
+             log_main($user->sess." chatt_send pre table update");
 
              $this->table_update(&$user);
 
-             log_main($user->sess, "chatt_send post table update");
+             log_main($user->sess." chatt_send post table update");
            }
          }
          else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
@@ -338,11 +353,11 @@ class Briskin5 {
            
            $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
            $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
-               xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
-               xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
-               xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
-               (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
-               (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
+               xcape($this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->name),
+               xcape($this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
+               xcape($this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
+               (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
+               (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
            if ($user_cur == $user)
              $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";', 
                                                                   xcape($user->name,ENT_COMPAT,"UTF-8"));
@@ -352,7 +367,7 @@ class Briskin5 {
       }
     }
     else {
-      for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
+      for ($i = 0 ; $i < ($user->stat == 'room' ? BRISKIN5_MAX_PLAYERS : BRISKIN5_PLAYERS_N) ; $i++) {
        if ($user->stat == 'room') {
          $user_cur = &$this->user[$i];
          if ($user_cur->sess == '' || $user_cur->stat != 'room')
@@ -376,15 +391,15 @@ class Briskin5 {
   {
     $table = &$this->table[0];
 
-    log_main("BRISKIN5_WAKEUP", "begin function table  stat: ".$user->stat."  subst: ".$user->subst);
+    log_main("BRISKIN5_WAKEUP begin function table  stat: ".$user->stat."  subst: ".$user->subst);
 
     $curtime = time();
 
-    log_main("BRISKIN5_WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
+    log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
     
     for ($i = 0 ; $i < $table->player_n ; $i++) {
       $user_cur = &$this->user[$i];
-      log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
+      log_main("PREIMPOST INLOOP name: ".$user_cur->name);
       
       if ($user_cur == $user)
        $user_cur->subst = "shutdowner";
@@ -395,10 +410,12 @@ class Briskin5 {
       $ret = "gst.st = ".($user_cur->step+1)."; ";
       $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("../index.php");|';
 
-      log_wr($user_cur->sess, "BRISKIN5_WAKEUP: ".$ret);
+      log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
       $user_cur->step_inc();
     }
+
+    $this->the_end = TRUE;
   }
   
   /*
@@ -422,20 +439,20 @@ class Briskin5 {
 
     return ($is_ab);
   }
-} // end of class Briskin5
+} // end class Briskin5
 
 function locshm_exists($tok)
 {
   // return (TRUE);
 
-  if (($id = shmop_open($tok,"a", 0, 0)) == FALSE) {
-    log_main($tok, "SHM NOT exists");
+  if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
+    log_main($tok." SHM NOT exists");
 
     return (FALSE);
   }
   else {
     shmop_close($id);
-    log_main($tok, "SHM exists");
+    log_main($tok." SHM exists");
 
     return (TRUE);
   }
@@ -443,4 +460,4 @@ function locshm_exists($tok)
 }
 
 
-?>
\ No newline at end of file
+?>