X-Git-Url: http://mop.ddnsfree.com/gitweb/?a=blobdiff_plain;f=web%2Fbriskin5%2FObj%2Fbriskin5.phh;h=3da15451732e63b85bf1304502167c5f396f7144;hb=8fe1e432c4661ebf186026f920d778d37dbe0953;hp=90cc082162b4c60b8e3f602e85965abab8158cd0;hpb=1532532dd4d3670d6d55560bbc3cbb0b238f74cd;p=brisk.git diff --git a/web/briskin5/Obj/briskin5.phh b/web/briskin5/Obj/briskin5.phh index 90cc082..3da1545 100644 --- a/web/briskin5/Obj/briskin5.phh +++ b/web/briskin5/Obj/briskin5.phh @@ -415,100 +415,273 @@ 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; - } +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 + + 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; + + 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); + } - /* CLONE NOT USED - function myclone(&$from) - { - if (($thiz =& new User()) == FALSE) - return (FALSE); + function step_set($step) + { + $this->step = $step & 0x7fffffff; + + return TRUE; + } + + function step_inc($delta = 1) { + $this->step += $delta; + /* modularization because unpack() not manage unsigned 32bit int correctly */ + $this->step &= 0x7fffffff; + + return (TRUE); + } - $thiz->copy($from); + static function load_step($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) + break; + if (($s = fread($fp, 8)) == FALSE) + break; + if (mb_strlen($s, "ASCII") != 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); + } - return ($thiz); - } - */ - - function spawn(&$from, $table, $table_pos) - { - GLOBAL $CO_bin5_pref_ring_endauct; + function save_step() + { + 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); + } - if (($thiz =& new User_briskin5()) == FALSE) - return (FALSE); - $thiz->parentcopy($from); - - $thiz->asta_card = -2; - $thiz->asta_pnt = -1; - $thiz->handpt = -1; - $thiz->exitislock = TRUE; - - log_wr("Briskin5 constructor"); + 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)."] "); + + do { + if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$id, "B")) == -1) { + log_main("ftok failed"); + 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) { + break; + } + log_only("user == ".($user == FALSE ? "FALSE" : "TRUE")." user === ".($user === FALSE ? "FALSE" : "TRUE")." user isset ".(isset($user) ? "TRUE" : "FALSE")); + + 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)); + } + } + else { + if ($sess != FALSE) { + // 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); + $user->step = $arr['s']; + if ($old_step != $user->step) { + log_crit("Bin5:: steps are diffetents User->step ".$user->step." Old_step: ".$old_step); + } + + } + } + + $user->shm_sz = $shm_sz; + + shm_detach($shm); + } + + // + // SHSPLIT: load users from the shared memory + // + return ($user); + } while (0); + + log_crit("Bin5_user::load_data:ret FALSE"); + + return (FALSE); + } - $this->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0; + static function save_data($user, $tab_id, $id) + { + GLOBAL $sess; + + $shm = FALSE; + + 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); + $user->save_step(); + 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); + } - $thiz->table_orig = $table; - $thiz->table = 0; - $thiz->table_pos = $table_pos; +} // end class Bin5_user - return ($thiz); - } -} // end class User_briskin5 +class Bin5 { + static $delta_t = array(); -class Briskin5 { var $user; var $table; var $comm; // commands for many people var $step; // current step of the comm array - var $garbage_timeout; + // externalized var $garbage_timeout; var $shm_sz; var $table_idx; @@ -620,31 +793,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_FTOK ".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; } @@ -668,7 +835,13 @@ class Briskin5 { shm_detach($shm); for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) { - $bri->user[$i] = Bin5_user::load_data($table_idx, $i, FALSE); + if (($bri->user[$i] = Bin5_user::load_data($table_idx, $i, FALSE)) == FALSE) { + log_crit("Bin5_user::load_data failed"); + break; + } + } + if ($i < BIN5_MAX_PLAYERS) { + break; } return ($bri); @@ -678,50 +851,59 @@ class Briskin5 { shm_detach($shm); log_wr("briskin5 load_data failed"); - if ($doexit) - 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_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."/table".$table_idx, "B")) == -1) { + if (($tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) { return (FALSE); } // WARNING monitor this step @@ -775,20 +957,25 @@ class Briskin5 { return (FALSE); } if (sem_acquire($res)) { - log_lock("LOCK table ".$table_idx."[res: ".$res."]"); - return ($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); + log_lock("LOCK table ".$table_idx.":FAILED"); + return (FALSE); } } - function unlock_data($res) + function unlock_data($res_vect) { GLOBAL $sess; - - log_lock("UNLOCK table [res: ".$res."]"); + + $res = $res_vect->getbyid('res'); + $tab = $res_vect->getbyid('tab'); + + log_lock("UNLOCK table [".$tab."] [".(microtime(TRUE) - (self::$delta_t[$tab]))."]"); return (sem_release($res)); } @@ -963,7 +1150,51 @@ class Briskin5 { return ($is_ab); } -} // end class Briskin5 + + + static function garbage_time_is_expired($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) + break; + if (($s = fread($fp, 4)) == FALSE) + break; + if (mb_strlen($s, "ASCII") != 4) + break; + $arr = unpack('Le', $s); + if ($arr['e'] > $tm) + $ret = FALSE; + } while (0); + + if ($fp != FALSE) + fclose($fp); + + log_rd2("END: return ".($ret ? "TRUE" : "FALSE")); + + return ($ret); + } + + static function garbage_time_expire_set($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) + break; + fwrite($fp, pack("L",$tm)); + fclose($fp); + + return (TRUE); + } while (0); + + return (FALSE); + } + +} // end class Bin5 function locshm_exists($tok) {