5 * Copyright (C) 2006-2011 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details. You should have received a
19 * copy of the GNU General Public License along with this program; if
20 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21 * Suite 330, Boston, MA 02111-1307, USA.
27 require_once("Obj/brisk.phh");
28 // require_once("Obj/proxyscan.phh");
29 require_once("briskin5/Obj/briskin5.phh");
31 $S_load_stat = array( 'rU_heavy' => 0,
39 'btn_backtotab' => array('it' => ' torna ai tavoli ',
40 'en' => ' back to tables '),
41 'btn_btotabsup' => array('it' => ' grazie della donazione, torna ai tavoli ',
42 'en' => ' thank you for donation, back to tables ')
45 // Use of proxies isn't allowed.
50 log_load("index_rd.php");
52 // $first_loop = TRUE;
55 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
56 echo "Debugging time!";
62 log_rd2("SHUTTA [".connection_status()."] !");
65 register_shutdown_function(shutta);
67 function blocking_error($is_unrecoverable)
69 GLOBAL $is_page_streaming;
71 $is_page_streaming = TRUE;
72 log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
73 return (sprintf(($is_unrecoverable ? 'the_end=true; ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("index.php");'));
76 function page_sync($sess, $page, $table_idx, $table_token)
78 GLOBAL $is_page_streaming;
80 log_rd2("page_sync:".var_export(debug_backtrace()));
82 $is_page_streaming = TRUE;
85 return (sprintf('createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); the_end=true; window.onunload = null; window.onbeforeunload = null; document.location.assign("%s");', $table_idx, $table_token, $page));
91 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
93 GLOBAL $G_lang, $mlang_indrd, $is_page_streaming;
94 // GLOBAL $first_loop;
95 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
96 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
97 $CO_splashdate = "CO_splashdate".$G_splash_idx;
98 GLOBAL $$CO_splashdate;
102 log_rd("maincheck begin");
109 // NOTE: qui forse si potrebbe fallback-are a una User::load_data
110 // anche se non ce ne dovrebbe essere mai la necessità
111 if (($proxy_step = User::load_step($sess)) == FALSE) {
113 ignore_user_abort(FALSE);
114 return (blocking_error(TRUE));
118 /* Sync check (read only without modifications */
119 ignore_user_abort(TRUE);
122 /* shared locking to load info */
123 if (($sem = Room::lock_data(FALSE)) == FALSE) {
124 // wait 20 secs, then restart the xhr
125 ignore_user_abort(FALSE);
126 return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
129 // Verifica l'expire time lato server
130 if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
131 Room::unlock_data($sem);
132 ignore_user_abort(FALSE);
133 return (blocking_error(TRUE));
135 /* if lacc time great than STREAM_TIMEOUT or the room garbage_time is expired
136 switch to exclusive locking and verify again the conditions */
137 if ((($curtime - $user->lacc) > STREAM_TIMEOUT) || Room::garbage_time_is_expired($curtime)) {
138 /* there is some info that require to change data, switch to exclusive locking */
139 Room::unlock_data($sem);
140 if (($sem = Room::lock_data(TRUE)) == FALSE) {
141 // wait 20 secs, then restart the xhr
142 ignore_user_abort(FALSE);
143 return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
145 $S_load_stat['lL_laccgarb']++;
147 // load again the data after locking
149 // Verifica l'expire time lato server
150 if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
151 Room::unlock_data($sem);
152 ignore_user_abort(FALSE);
153 return (blocking_error(TRUE));
156 if (($curtime - $user->lacc) >= STREAM_TIMEOUT) {
157 $S_load_stat['wU_lacc_upd']++;
158 $user->lacc = $curtime;
159 // lacc field updated
160 User::save_data($user, $user->idx);
163 if (Room::garbage_time_is_expired($curtime)) {
166 $S_load_stat['wR_garbage']++;
167 if (($room = Room::load_data()) == FALSE) {
168 Room::unlock_data($sem);
169 ignore_user_abort(FALSE);
170 return (blocking_error(TRUE));
172 log_main("pre garbage_manager TRE");
173 $room->garbage_manager(FALSE);
174 Room::save_data($room);
178 log_main("infolock: U");
179 Room::unlock_data($sem);
180 ignore_user_abort(FALSE);
184 // if ($first_loop == TRUE) {
185 // if (($sem = Room::lock_data(TRUE)) != FALSE) {
186 // // Aggiorna l'expire time lato server
187 // $S_load_stat['U_first_loop']++;
188 // if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
189 // Room::unlock_data($sem);
190 // ignore_user_abort(FALSE);
191 // return (blocking_error(TRUE));
193 // $user->lacc = $curtime;
194 // // lacc field updated
195 // User::save_data($user, $user->idx);
197 // if (Room::garbage_time_is_expired($curtime)) {
200 // $S_load_stat['R_garbage']++;
201 // if (($room = Room::load_data()) == FALSE) {
202 // Room::unlock_data($sem);
203 // ignore_user_abort(FALSE);
204 // return (blocking_error(TRUE));
206 // log_main("pre garbage_manager TRE");
207 // $room->garbage_manager(FALSE);
208 // Room::save_data($room);
211 // log_main("infolock: U");
212 // Room::unlock_data($sem);
213 // ignore_user_abort(FALSE);
214 // } // if (($sem = Room::lock_data(TRUE)) != FALSE) {
216 // // wait 20 secs, then restart the xhr
217 // ignore_user_abort(FALSE);
219 // return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
221 // $first_loop = FALSE;
222 // } // if ($first_loop == TRUE) {
224 if ($cur_step == $proxy_step['s']) {
225 log_main("infolock: P");
232 $S_load_stat['rU_heavy']++;
233 if ($user == FALSE) {
235 ignore_user_abort(TRUE);
236 if (($sem = Room::lock_data(FALSE)) == FALSE)
239 log_main("infolock: P");
240 if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
246 Room::unlock_data($sem);
248 ignore_user_abort(FALSE);
249 if ($user == FALSE) {
250 return (blocking_error(TRUE));
254 /* Nothing changed, return. */
255 if ($cur_step == $user->step)
258 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
260 if ($cur_step == -1) {
262 * if $cur_step == -1 load the current state from the main struct
265 /* unset the $user var to reload it from main structure */
268 ignore_user_abort(TRUE);
269 $sem = Room::lock_data(TRUE);
270 if (($room = Room::load_data()) == FALSE) {
271 Room::unlock_data($sem);
272 ignore_user_abort(FALSE);
273 return (blocking_error(TRUE));
275 $S_load_stat['wR_minusone']++;
277 if (($user = $room->get_user($sess, $idx)) == FALSE) {
278 Room::unlock_data($sem);
279 ignore_user_abort(FALSE);
280 return (blocking_error(TRUE));
283 if ($user->the_end == TRUE) {
284 log_rd2("main_check: the end".var_export(debug_backtrace()));
285 $is_page_streaming = TRUE;
288 if ($user->trans_step != -1) {
289 log_rd2("TRANS USATO ".$user->trans_step);
290 $cur_step = $user->trans_step;
291 $user->trans_step = -1;
293 Room::save_data($room);
294 Room::unlock_data($sem);
295 ignore_user_abort(FALSE);
298 log_rd2("TRANS NON ATTIVATO");
299 // ARRAY_POP DISABLED
300 // log_rd2("TRANS NON ATTIVATO, clean del comm array");
301 // while (($el = array_pop($user->comm)) != NULL) {
302 // log_rd2("clean element [".$el."]");
304 // // $user->step_inc(COMM_N + 1);
305 // Room::save_data($room);
306 // // $new_step = $user->step;
308 Room::unlock_data($sem);
309 ignore_user_abort(FALSE);
314 /* this part I suppose is read only on $room structure */
315 if ($cur_step == -1) {
316 log_rd2("PRE-NEWSTAT: ".$user->stat);
318 if ($user->stat == 'room') {
319 log_rd("roomma ".$user->step);
322 if ($G_with_splash &&
323 ($$CO_splashdate < $curtime - $G_splash_interval ||
324 $$CO_splashdate > $curtime)) {
325 $is_super = $user->flags & USER_FLAG_TY_SUPER;
326 $ret .= show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]),
327 ($is_super ? 0 : $G_splash_timeout),
328 $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang],
329 $G_splash_w, $G_splash_h, true,
330 ($is_super ? 0 : $G_splash_timeout));
331 $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
333 $ret .= $room->show_room($user->step, $user);
335 // TODO uncomment and test
336 /* NOTE the sets went common */
337 $new_stat = $user->stat;
338 $new_subst = $user->subst;
339 $new_step = $user->step;
346 else if ($user->stat == 'table') {
348 return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
350 log_rd2("NEWSTAT: ".$user->stat);
353 ignore_user_abort(TRUE);
354 $sem = Room::lock_data(FALSE);
355 $S_load_stat['rU_heavy']++;
356 if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
357 Room::unlock_data($sem);
358 ignore_user_abort(FALSE);
359 return (blocking_error(TRUE));
362 if ($cur_step < $user->step) {
364 if ($cur_step + COMM_N < $user->step) {
365 if (($cur_stat != $user->stat)) {
366 $to_stat = $user->stat;
367 Room::unlock_data($sem);
368 ignore_user_abort(FALSE);
370 return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
372 log_rd2("lost history, refresh from scratch");
376 for ($i = $cur_step ; $i < $user->step ; $i++) {
378 log_rd2("ADDED TO THE STREAM: ".$user->comm[$ii]);
379 $ret .= $user->comm[$ii];
381 $new_stat = $user->stat;
382 $new_subst = $user->subst;
383 $new_step = $user->step;
386 log_mop($user->step, 'index_rd.php: after ret set');
388 if ($user->the_end == TRUE) {
389 Room::unlock_data($sem);
391 /* Switch to exclusive locking */
392 $sem = Room::lock_data(TRUE);
396 $S_load_stat['wR_the_end']++;
397 if (($room = Room::load_data()) == FALSE) {
398 Room::unlock_data($sem);
399 ignore_user_abort(FALSE);
400 return (blocking_error(TRUE));
403 if (($user = $room->get_user($sess, $idx)) == FALSE) {
404 Room::unlock_data($sem);
405 ignore_user_abort(FALSE);
406 return (blocking_error(TRUE));
409 log_rd2("LOGOUT BYE BYE!!");
410 log_auth($user->sess, "Explicit logout.");
412 if ($user->the_end == TRUE) {
415 if ($user->subst == 'sitdown') {
416 log_load("ROOM WAKEUP");
417 $room->room_wakeup($user);
419 else if ($user->subst == 'standup')
420 $room->room_outstandup($user);
422 log_rd2("LOGOUT FROM WHAT ???");
424 Room::save_data($room);
429 Room::unlock_data($sem);
430 ignore_user_abort(FALSE);
448 $is_page_streaming = (webservers_exceeded() || stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
450 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
451 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
452 header('Content-type: application/xml; charset="utf-8"',true);
458 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
461 $endtime = time() + STREAM_TIMEOUT;
464 $old_step = $ext_step = $step;
466 for ($i = 0 ; time() < $endtime ; $i++) {
467 // log_rd("PRE MAIN ".$step);;
468 $pre_main = gettimeofday(TRUE);
469 if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
471 // log_rd2(sprintf("\nSESS: [%s]\nOLD_STAT: [%s] OLD_SUBST: [%s] OLD_STEP: [%s] \nSTAT: [%s] SUBST: [%s] STEP: [%s] \nCOMM: [%s]\n", $sess, $old_stat, $old_subst, $old_step, $stat, $subst, $step, $ret));
474 log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")."EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
476 log_mop(0, 'index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
477 if ($is_page_streaming)
483 // log_rd("POST MAIN ".$step);;
485 if (($i % 10) == 0) {
486 // log_rd2("TIME: ".time());
495 foreach ($S_load_stat as $key => $value) {
496 $s .= sprintf("%s: %d - ", $key, $value);
497 if (substr($key, 0, 1) == "w")
499 else if (substr($key, 0, 1) == "r")
502 $s = sprintf("index_rd.php stats: R: %d W: %d - %s", $tr, $tw, $s);