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() {
- }
+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);
+ }
- /* CREATE NOT USED
- function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
- 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->asta_card = -2;
- $thiz->asta_pnt = -1;
- $thiz->handpt = -1;
- $thiz->exitislock = TRUE;
- $thiz->privflags = 0;
+ 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."/table".$tab_id."/".$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 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."/table".$this->table_orig."/".$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);
+ }
+
+ 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;
- function parentcopy(&$from)
- {
- parent::copy($from);
- }
+ @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
+ }
+
+ static function load_data($tab_id, $id, $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) {
+ 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)) {
+ if (($user = @shm_get_var($shm, $tok)) == FALSE) {
+ break;
+ }
+
+ 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_shme("Bin5_user::save_data2");
+
+ 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($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);
+ }
+
+ }
+ }
+
+ $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);
+ }
- function copy(&$from)
+ static function save_data($user, $tab_id, $id)
{
- $this->parentcopy($from);
+ 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.");
- $this->asta_card = $from->asta_card;
- $this->asta_pnt = $from->asta_pnt;
- $this->handpt = $from->handpt;
- $this->exitislock = $from->exitislock;
- $this->privflags = $from->privflags;
- }
+ log_shme("Bin5_user::save_data");
- /* CLONE NOT USED
- function myclone(&$from)
- {
- if (($thiz =& new User()) == FALSE)
+ 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->copy($from);
-
- return ($thiz);
- }
- */
-
- function spawn(&$from, $table, $table_pos)
+ function destroy_data($tab_id)
{
- GLOBAL $CO_bin5_pref_ring_endauct;
-
- 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");
+ 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;
- $this->privflags = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
+ 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);
+ }
- $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;
$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");
}
$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 == "" ||
$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;
}
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($this->table_idx, $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;
}
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);
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;
- if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
- shm_detach($shm);
- return (TRUE);
- }
- if (shm_remove($shm) === FALSE) {
- log_only("REMOVE FALLITA");
- break;
+ $ret = FALSE;
+ $shm = FALSE;
+
+ 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;
}
- shm_detach($shm);
- $bri->shm_sz += BRISKIN5_SHM_DLT;
- }
+
+ 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) {
- log_crit("save data failed!");
+ log_shme("Bin5::save_data");
- if ($shm)
- shm_detach($shm);
-
- return ($ret);
+ $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];
+ }
+ log_load("FINISH: ".($ret == TRUE ? "TRUE" : "FALSE"));
+
+ return ($ret);
}
+
-
- function destroy_data($table_idx)
+ function destroy_data()
{
GLOBAL $sess;
log_main("DESTROY BRISKIN5 DATA");
do {
- log_main("DESTROY2 BRISKIN5 DATA");
-
- if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "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)
{
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
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));
}
return ($is_ab);
}
-} // end class Briskin5
+
+
+ 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."/table".$tab_id."/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($tab_id, $tm)
+ {
+ do {
+ if (file_exists(BIN5_PROXY_PATH) == FALSE)
+ mkdir(BIN5_PROXY_PATH, 0775, TRUE);
+ if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/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)
{