From 971fb704f6cb6091d0c597209f5f878c04336d51 Mon Sep 17 00:00:00 2001 From: "Matteo Nastasi (mop)" Date: Thu, 7 Feb 2008 06:39:59 +0000 Subject: [PATCH] garbage_manager update --- TODO.txt | 11 ++-- web/Obj/brisk.phh | 88 ++++++++++++++++++++++++++--- web/briskin5/Obj/briskin5.phh | 103 +++++++++++++++++++++++----------- web/index.php | 62 ++++++++++++-------- web/index_rd.php | 2 +- web/index_wr.php | 9 +-- 6 files changed, 202 insertions(+), 73 deletions(-) diff --git a/TODO.txt b/TODO.txt index 21836b5..ede5dd7 100644 --- a/TODO.txt +++ b/TODO.txt @@ -15,13 +15,16 @@ DONE - recover of corrupted table data DONE - closure of a table - HALF - flush of the table (remove the shm ??) + DONE - flush of the table - garbage collector management + DONE - security checks for right scope access - HALF - security checks for right scope access + garbage collector management + recursive garbage - howto ? + remove unused parts + move images optimizations - inheritance + inheritances NORM - pubblic announcements diff --git a/web/Obj/brisk.phh b/web/Obj/brisk.phh index f0f3d75..8136f8f 100644 --- a/web/Obj/brisk.phh +++ b/web/Obj/brisk.phh @@ -32,7 +32,8 @@ define(COMM_N, 12); define(COMM_GEN_N, 50); define(SESS_LEN, 13); define(STREAM_TIMEOUT, 20); -define(EXPIRE_TIME_RD, 180); +// FIXME original: define(EXPIRE_TIME_RD, 180); +define(EXPIRE_TIME_RD, 60); define(EXPIRE_TIME_SMAMMA, 360); // BAN_TIME da allineare anche in commons.js define(BAN_TIME, 900); @@ -44,7 +45,7 @@ define(BRISK_DEBUG, FALSE); $G_false = FALSE; $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 ); -$G_brisk_version = "0.8.2"; +$G_brisk_version = "spawn-0.0.3"; $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), NOVITA\': nuovo layout che permette più tavoli, più tavoli.', 'Se vuoi iscriverti alla Mailing List, cliccala!' ); @@ -658,6 +659,7 @@ class Room { function Room () { $this->user = array(); + $this->table = array(); for ($i = 0 ; $i < MAX_PLAYERS ; $i++) { $this->user[$i] =& User::create("", ""); @@ -670,11 +672,74 @@ class Room { function garbage_manager($force) { - + $ismod = FALSE; + /* Garbage collector degli utenti in timeout */ $curtime = time(); if ($force || $this->garbage_timeout < $curtime) { + // Before all align times with table timeout + for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) { + $table_cur =& $this->table[$table_idx]; + // if the table is complete and exists its shared mem we get the info about users lacc + if ($table_cur->player_n == PLAYERS_N) { + if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { + if (($bri = &Briskin5::load_data($table_idx)) != FALSE) { + if ($bri->garbage_manager(TRUE) == TRUE) { + // if garbaged you need to save data + Briskin5::save_data(&$bri); + if ($bri->is_abandoned()) { + // FIXME recovery of room and clean of the table + /* + + # + # KILL USERS + # + + if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente + log_rd2($user_cur->sess, "AUTO LOGOUT."); + + if ($user_cur->stat == 'table' || $user_cur->stat == 'room') { + log_auth($user_cur->sess, "Autologout session."); + + $tmp_sess = $user_cur->sess; + $user_cur->sess = ""; + step_unproxy($tmp_sess); + $user_cur->name = ""; + $user_cur->step = 0; + $user_cur->the_end = FALSE; + + log_rd2($user_cur->sess, "AUTO LOGOUT."); + if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table') + $this->room_wakeup(&$user_cur); + else if ($user_cur->subst == 'standup') + $this->room_outstandup(&$user_cur); + else + log_rd2($sess, "LOGOUT FROM WHAT ???"); + } + } + + # + # destroy shared + # + + Briskin5::destroy_data($user->table); + + + */ + } + } + $table_bri = &$bri->table[0]; + for ($i = 0 ; $i < PLAYERS_N ; $i++) { + $this->user[$table_cur->player[$i]]->lacc = + $bri->user[$table_bri->player[$i]]->lacc; + } + } + Briskin5::unlock_data($sem); + } + } + } + for ($i = 0 ; $i < MAX_PLAYERS ; $i++) { $user_cur = &$this->user[$i]; if ($user_cur->sess == "") @@ -690,6 +755,7 @@ class Room { $user_cur->sess = ""; step_unproxy($tmp_sess); $user_cur->name = ""; + $user_cur->step = 0; $user_cur->the_end = FALSE; log_rd2($user_cur->sess, "AUTO LOGOUT."); @@ -714,10 +780,10 @@ class Room { log_rd2($user_cur->sess, "GARBAGE UPDATED!"); $this->garbage_timeout = time() + GARBAGE_TIMEOUT; + $ismod = TRUE; } - // BAN_IP_CLEAN - + return ($ismod); } @@ -802,11 +868,11 @@ class Room { } } - function room_join_wakeup(&$user) + function room_join_wakeup(&$user, $update_lacc = FALSE) { $table_idx = $user->table; $table = &$this->table[$table_idx]; - + log_main($user->sess, "JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst); $curtime = time(); @@ -818,7 +884,9 @@ class Room { $user_cur = &$this->user[$table->player[$i]]; log_main($user->sess, "PREIMPOST INLOOP name: ".$user_cur->name); if ($user_cur->sess != "") { - $user_cur->laccwr = $curtime; + if ($update_lacc == TRUE) { + $user_cur->laccwr = $curtime; + } log_load($user->sess, "cur: ".$user_cur->name." subst: ".$user_cur->subst); if ($user_cur->subst == "shutdowned") { $user_cur->stat_set("room"); @@ -1766,6 +1834,10 @@ function show_table(&$room, &$user, $sendstep, $is_transition, $is_again) /* GENERAL STATUS */ $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos); + + log_rd($user->sess, sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', + $sendstep, $user->stat, $user->subst, $table_pos)); + /* BACKGROUND */ $ret .= "background_set();"; diff --git a/web/briskin5/Obj/briskin5.phh b/web/briskin5/Obj/briskin5.phh index 9e12289..ecd4b41 100644 --- a/web/briskin5/Obj/briskin5.phh +++ b/web/briskin5/Obj/briskin5.phh @@ -69,10 +69,11 @@ class Briskin5 { { /* Garbage collector degli utenti in timeout */ + $ismod = FALSE; $curtime = time(); if ($force || $this->garbage_timeout < $curtime) { - for ($i = 0 ; $i < MAX_PLAYERS ; $i++) { + for ($i = 0 ; $i < MAX_BRISKIN5_PLAYERS ; $i++) { $user_cur = &$this->user[$i]; if ($user_cur->sess == "") continue; @@ -80,41 +81,32 @@ class Briskin5 { if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente log_rd2($user_cur->sess, "AUTO LOGOUT."); - if ($user_cur->stat == 'table' || $user_cur->stat == 'room') { + if ($user_cur->stat == 'table') { log_auth($user_cur->sess, "Autologout session."); - + + /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE + $tmp_sess = $user_cur->sess; $user_cur->sess = ""; step_unproxy($tmp_sess); $user_cur->name = ""; $user_cur->the_end = FALSE; + */ + log_rd2($user_cur->sess, "AUTO LOGOUT."); - if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table') - $this->room_wakeup(&$user_cur); - else if ($user_cur->subst == 'standup') - $this->room_outstandup(&$user_cur); - else - log_rd2($sess, "LOGOUT FROM WHAT ???"); - } - } - if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi - if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') { - $this->room_wakeup(&$user_cur); - $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; "; - $user_cur->comm[$user_cur->step % COMM_N] .= show_notify("
Sei stato inattivo per ".(EXPIRE_TIME_SMAMMA/60.0)." minuti.

Quindi ritorni tra i Giocatori in piedi.", 0, "torna ai tavoli", 400, 100); - $user_cur->step_inc(); + $this->table_wakeup(&$user_cur); } } } log_rd2($user_cur->sess, "GARBAGE UPDATED!"); - $this->garbage_timeout = time() + GARBAGE_TIMEOUT; + $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT; + $ismod = TRUE; } - // BAN_IP_CLEAN - + return ($ismod); } @@ -123,7 +115,7 @@ class Briskin5 { // // static functions // - function &load_data($table_idx,$table_token) + function &load_data($table_idx, $table_token = "") { GLOBAL $G_false, $sess; $shm = FALSE; @@ -135,13 +127,17 @@ class Briskin5 { } do { + // if (shmchk_exists($tok) == FALSE) + if (locshm_exists($tok) == FALSE) + break; + if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE) break; if (($bri = @shm_get_var($shm, $tok)) == FALSE) break; - if ($bri->table_token != $table_token) { + if ($table_token != "" && $bri->table_token != $table_token) { log_wr($sess, "bri->table_token: ".$bri->table_token."table_token: ".$table_token); break; @@ -199,26 +195,26 @@ class Briskin5 { return ($ret); } - function destroy_data(&$bri) + function destroy_data($table_idx) { GLOBAL $sess; - + $ret = FALSE; $shm = FALSE; $isacq = FALSE; - - // var_dump($bri); - + log_main("XXX", "DESTROY BRISKIN5 DATA"); - if (!isset($bri->tok)) - return (FALSE); do { $isacq = TRUE; log_main("XXX", "DESTROY2 BRISKIN5 DATA"); - if (($shm = shm_attach($bri->tok, SHM_DIMS)) === FALSE) + + if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) + break; + + if (($shm = shm_attach($tok, SHM_DIMS)) === FALSE) break; if (shm_remove($shm) === FALSE) { @@ -226,10 +222,10 @@ class Briskin5 { log_only($sess, serialize($bri)); break; } - else { - $shm = FALSE; - log_main("XXX", "DESTROY2 BRISKIN5 DATA SUCCESS"); - } + + $shm = FALSE; + log_main("XXX", "DESTROY2 BRISKIN5 DATA SUCCESS"); + // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]"); $ret = TRUE; } while (0); @@ -404,8 +400,47 @@ class Briskin5 { $user_cur->step_inc(); } } + + /* + * If all players are freezed the room garbage_manager clean up table and users. + */ + function is_abandoned() + { + $is_ab = TRUE; + $curtime = time(); + $table = &$this->table[0]; + + for ($i = 0 ; $i < $table->player_n ; $i++) { + $user_cur = &$this->user[$i]; + if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) { + $is_ab = FALSE; + break; + } + } + + return ($is_ab); + } } // end of class Briskin5 +function locshm_exists($tok) +{ + // return (TRUE); + + 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"); + + return (TRUE); + } + +} + + ?> \ No newline at end of file diff --git a/web/index.php b/web/index.php index 475d3cf..bee1463 100644 --- a/web/index.php +++ b/web/index.php @@ -44,10 +44,10 @@ function main() $sem = Room::lock_data(); log_load($sess, "lock Room"); $room = &Room::load_data(); - + $curtime = time(); /* Actions */ if (validate_sess($sess)) { - // FIXME uncomment $room->garbage_manager(TRUE); + $room->garbage_manager(TRUE); if (($user = &$room->get_user($sess, &$idx)) != FALSE) { if ($user->stat == "table") { $change_page = TRUE; @@ -57,12 +57,13 @@ function main() log_load($sess, "SET TABLE_IDX GOOD VALUE"); $bri_sem = Briskin5::lock_data($user->table); - if (($bri = &Briskin5::load_data($user->table, $table_token)) == FALSE) { - // table data error: recovery - + if (($bri = &Briskin5::load_data($user->table, $user->table_token)) == FALSE) { + /* + * CASE: TABLE DATA ERROR - RECOVERY + */ log_load($sess, "table data error: recovery".$user->table); - $table = &$room->table[$user->table]; + $table = &$room->table[$user->table]; for ($i = 0 ; $i < $table->player_n ; $i++) { $user_cur = &$room->user[$table->player[$i]]; $user_cur->subst = "shutdowner"; @@ -76,18 +77,22 @@ function main() $user_cur->step_inc(); } - $room->room_join_wakeup(&$user); + $room->room_join_wakeup(&$user, TRUE); + $table->table_token = ""; +// if (Room::save_data(&$room) == FALSE) { +// echo "ERRORE SALVATAGGIO\n"; +// exit; +// } - if (Room::save_data(&$room) == FALSE) { - echo "ERRORE SALVATAGGIO\n"; - exit; - } + Briskin5::destroy_data($user->table); $change_page = FALSE; - } + } // if (($bri = &Briskin5::load_data($user->table, ... else if (($bri_user = &$bri->get_user($sess, &$bri_idx)) != FALSE) { if ($bri_user->subst == "shutdowned" || $bri_user->subst == "shutdowner") { - // QUI WAKEUP + /* + * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN + */ $table = &$room->table[$user->table]; $bri_table = &$bri->table[0]; @@ -95,37 +100,50 @@ function main() $room->user[$table->player[$i]]->subst = $bri->user[$i]->subst; $room->user[$table->player[$i]]->step = $bri->user[$i]->step; $room->user[$table->player[$i]]->trans_step = $bri->user[$i]->step+1; + $room->user[$table->player[$i]]->lacc = $bri->user[$i]->lacc; + $room->user[$table->player[$i]]->laccwr = $bri->user[$i]->laccwr; + log_load($sess, "from table bri subst[".$i."]: ".$bri->user[$i]->subst); log_load($sess, "from table roo subst[".$i."]: ".$room->user[$table->player[$i]]->subst); } $room->room_join_wakeup(&$user); - - if (Room::save_data(&$room) == FALSE) { - echo "ERRORE SALVATAGGIO\n"; - exit; - } + $table->table_token = ""; +// if (Room::save_data(&$room) == FALSE) { +// echo "ERRORE SALVATAGGIO\n"; +// exit; +// } $change_page = FALSE; - Briskin5::destroy_data(&$bri); + Briskin5::destroy_data($user->table); } - log_load($sess, "from table subst: ".$bri_user->subst); } Briskin5::unlock_data($bri_sem); - log_load($sess, "unlock Room"); if ($change_page) { + if (Room::save_data(&$room) == FALSE) { + echo "ERRORE SALVATAGGIO\n"; + exit; + } + log_load($sess, "unlock Room"); Room::unlock_data($sem); + setcookie("table_token", $user->table_token, $curtime + 31536000); + setcookie("table_idx", $user->table_idx, $curtime + 31536000); header ("Location: briskin5/briskin5.php"); exit; } } $ACTION = "room"; } + + if (Room::save_data(&$room) == FALSE) { + echo "ERRORE SALVATAGGIO\n"; + exit; + } } if ($ACTION == "login" && isset($name)) { - // FIXME uncomment $room->garbage_manager(TRUE); + $room->garbage_manager(TRUE); /* try login */ if (($user = &$room->add_user(&$sess, &$idx, $name, $_SERVER['REMOTE_ADDR'])) != FALSE) { $ACTION = "room"; diff --git a/web/index_rd.php b/web/index_rd.php index d9031f5..38a660e 100644 --- a/web/index_rd.php +++ b/web/index_rd.php @@ -85,7 +85,7 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su log_auth($sess, "update lacc"); $user->lacc = time(); - // FIXME uncomment $room->garbage_manager(FALSE); + $room->garbage_manager(FALSE); Room::save_data($room); $first_loop = FALSE; diff --git a/web/index_wr.php b/web/index_wr.php index 54c0c3c..2d93e22 100644 --- a/web/index_wr.php +++ b/web/index_wr.php @@ -151,6 +151,7 @@ else if ($user->stat == 'room') { // Create new spawned table $bri_sem = Briskin5::lock_data($table_idx); $table_token = uniqid(""); + $room->table[$table_idx]->table_token = $table_token; if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE) log_wr($sess, "bri create: FALSE"); else @@ -169,15 +170,15 @@ else if ($user->stat == 'room') { $bri_user_cur = &$bri->user[$i]; $user_cur = &$room->user[$table->player[$i]]; + $bri_user_cur->stat_set('table'); + $bri_user_cur->subst = 'asta'; + $bri_user_cur->laccwr = $curtime; + $bri_user_cur->trans_step = $user_cur->step + 1; $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = ""; $bri_user_cur->step_inc(); $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE); - $bri_user_cur->stat_set('table'); - $bri_user_cur->subst = 'asta'; - $bri_user_cur->laccwr = $curtime; - $bri_user_cur->step_inc(); log_wr($bri_user_cur->sess, "TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step); -- 2.17.1