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
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);
$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.'), <b>NOVITA\'</b>: nuovo layout che permette più tavoli, più tavoli.',
'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
function Room () {
$this->user = array();
+ $this->table = array();
for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
$this->user[$i] =& User::create("", "");
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 == "")
$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.");
log_rd2($user_cur->sess, "GARBAGE UPDATED!");
$this->garbage_timeout = time() + GARBAGE_TIMEOUT;
+ $ismod = TRUE;
}
- // BAN_IP_CLEAN
-
+ return ($ismod);
}
}
}
- 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();
$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");
/* 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();";
{
/* 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;
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("<br>Sei stato inattivo per ".(EXPIRE_TIME_SMAMMA/60.0)." minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.", 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);
}
//
// static functions
//
- function &load_data($table_idx,$table_token)
+ function &load_data($table_idx, $table_token = "")
{
GLOBAL $G_false, $sess;
$shm = FALSE;
}
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;
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) {
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);
$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
$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;
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";
$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];
$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";
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;
// 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
$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);