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( 'U_first_loop' => 0,
38 'btn_backtotab' => array('it' => ' torna ai tavoli ',
39 'en' => ' back to tables '),
40 'btn_btotabsup' => array('it' => ' grazie della donazione, torna ai tavoli ',
41 'en' => ' thank you for donation, back to tables ')
44 // Use of proxies isn't allowed.
49 log_load("index_rd.php");
54 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
55 echo "Debugging time!";
61 log_rd2("SHUTTA [".connection_status()."] !");
64 register_shutdown_function(shutta);
66 function blocking_error($is_unrecoverable)
68 GLOBAL $is_page_streaming;
70 $is_page_streaming = TRUE;
71 log_rd2("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
72 return (sprintf(($is_unrecoverable ? 'the_end=true; ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("index.php");'));
75 function page_sync($sess, $page, $table_idx, $table_token)
77 GLOBAL $is_page_streaming;
79 log_rd2("page_sync:".var_export(debug_backtrace()));
81 $is_page_streaming = TRUE;
84 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));
90 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
92 GLOBAL $G_lang, $mlang_indrd, $is_page_streaming, $first_loop;
93 GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
94 GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
95 $CO_splashdate = "CO_splashdate".$G_splash_idx;
96 GLOBAL $$CO_splashdate;
100 log_rd("maincheck begin");
107 // NOTE: qui forse si potrebbe fallback-are a una User::load_data
108 // anche se non ce ne dovrebbe essere mai la necessità
109 if (($proxy_step = User::load_step($sess)) == FALSE) {
111 ignore_user_abort(FALSE);
112 return (blocking_error(TRUE));
116 /* Sync check (read only without modifications */
117 ignore_user_abort(TRUE);
118 if ($first_loop == TRUE) {
119 if (($sem = Room::lock_data(TRUE)) != FALSE) {
120 // Aggiorna l'expire time lato server
121 $S_load_stat['U_first_loop']++;
122 if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
123 Room::unlock_data($sem);
124 ignore_user_abort(FALSE);
125 return (blocking_error(TRUE));
127 $user->lacc = $curtime;
128 User::save_data($user, $user->idx);
130 if (Room::garbage_time_is_expired($curtime)) {
133 $S_load_stat['R_garbage']++;
134 if (($room = Room::load_data()) == FALSE) {
135 Room::unlock_data($sem);
136 ignore_user_abort(FALSE);
137 return (blocking_error(TRUE));
139 log_main("pre garbage_manager TRE");
140 $room->garbage_manager(FALSE);
141 Room::save_data($room);
144 log_main("infolock: U");
145 Room::unlock_data($sem);
146 ignore_user_abort(FALSE);
147 } // if (($sem = Room::lock_data(TRUE)) != FALSE) {
149 // wait 20 secs, then restart the xhr
150 ignore_user_abort(FALSE);
152 return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
155 } // if ($first_loop == TRUE) {
157 if ($cur_step == $proxy_step['s']) {
158 log_main("infolock: P");
166 if ($user == FALSE) {
168 ignore_user_abort(TRUE);
169 if (($sem = Room::lock_data(TRUE)) == FALSE)
172 log_main("infolock: P");
173 $S_load_stat['U_heavy']++;
174 if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
180 Room::unlock_data($sem);
182 ignore_user_abort(FALSE);
183 if ($user == FALSE) {
184 return (blocking_error(TRUE));
188 /* Nothing changed, return. */
189 if ($cur_step == $user->step)
192 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
194 if ($cur_step == -1) {
196 * if $cur_step == -1 load the current state from the main struct
198 ignore_user_abort(TRUE);
199 $sem = Room::lock_data(TRUE);
200 if (($room = Room::load_data()) == FALSE) {
201 Room::unlock_data($sem);
202 ignore_user_abort(FALSE);
203 return (blocking_error(TRUE));
205 $S_load_stat['R_minusone']++;
207 /* unset the $user var to reload it from main structure */
209 if (($user = $room->get_user($sess, $idx)) == FALSE) {
210 Room::unlock_data($sem);
211 ignore_user_abort(FALSE);
212 return (blocking_error(TRUE));
215 if ($user->the_end) {
216 log_rd2("main_check: the end".var_export(debug_backtrace()));
217 $is_page_streaming = TRUE;
220 if ($user->trans_step != -1) {
221 log_rd2("TRANS USATO ".$user->trans_step);
222 $cur_step = $user->trans_step;
223 $user->trans_step = -1;
225 Room::save_data($room);
226 Room::unlock_data($sem);
227 ignore_user_abort(FALSE);
230 log_rd2("TRANS NON ATTIVATO");
231 // ARRAY_POP DISABLED
232 // log_rd2("TRANS NON ATTIVATO, clean del comm array");
233 // while (($el = array_pop($user->comm)) != NULL) {
234 // log_rd2("clean element [".$el."]");
236 // // $user->step_inc(COMM_N + 1);
237 // Room::save_data($room);
238 // // $new_step = $user->step;
240 Room::unlock_data($sem);
241 ignore_user_abort(FALSE);
246 /* this part I suppose is read only on $room structure */
247 if ($cur_step == -1) {
248 log_rd2("PRE-NEWSTAT: ".$user->stat);
250 if ($user->stat == 'room') {
251 log_rd("roomma ".$user->step);
254 if ($G_with_splash &&
255 ($$CO_splashdate < $curtime - $G_splash_interval ||
256 $$CO_splashdate > $curtime)) {
257 $is_super = $user->flags & USER_FLAG_TY_SUPER;
258 $ret .= show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]),
259 ($is_super ? 0 : $G_splash_timeout),
260 $mlang_indrd[($is_super ? 'btn_btotabsup' : 'btn_backtotab')][$G_lang],
261 $G_splash_w, $G_splash_h, true,
262 ($is_super ? 0 : $G_splash_timeout));
263 $ret .= sprintf('|createCookie("CO_splashdate%d", %d, 24*365, cookiepath);', $G_splash_idx, $curtime);
265 $ret .= $room->show_room($user->step, $user);
267 // TODO uncomment and test
268 /* NOTE the sets went common */
269 $new_stat = $user->stat;
270 $new_subst = $user->subst;
271 $new_step = $user->step;
278 else if ($user->stat == 'table') {
280 return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
282 log_rd2("NEWSTAT: ".$user->stat);
285 // TODO: verify if we can use only $user struct
286 ignore_user_abort(TRUE);
287 $sem = Room::lock_data(TRUE);
288 $S_load_stat['U_heavy']++;
289 if (($user = User::load_data($proxy_step['i'], $sess)) == FALSE) {
290 Room::unlock_data($sem);
291 ignore_user_abort(FALSE);
292 return (blocking_error(TRUE));
295 if ($cur_step < $user->step) {
297 if ($cur_step + COMM_N < $user->step) {
298 if (($cur_stat != $user->stat)) {
299 $to_stat = $user->stat;
300 Room::unlock_data($sem);
301 ignore_user_abort(FALSE);
303 return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
305 log_rd2("lost history, refresh from scratch");
309 for ($i = $cur_step ; $i < $user->step ; $i++) {
311 log_rd2("ADDED TO THE STREAM: ".$user->comm[$ii]);
312 $ret .= $user->comm[$ii];
314 $new_stat = $user->stat;
315 $new_subst = $user->subst;
316 $new_step = $user->step;
319 log_mop($user->step, 'index_rd.php: after ret set');
321 if ($user->the_end == TRUE) {
322 log_rd2("LOGOUT BYE BYE!!");
323 log_auth($user->sess, "Explicit logout.");
325 $S_load_stat['R_the_end']++;
326 if (($room = Room::load_data()) == FALSE) {
327 Room::unlock_data($sem);
328 ignore_user_abort(FALSE);
329 return (blocking_error(TRUE));
333 if (($user = $room->get_user($sess, $idx)) == FALSE) {
334 Room::unlock_data($sem);
335 ignore_user_abort(FALSE);
336 return (blocking_error(TRUE));
340 if ($user->subst == 'sitdown') {
341 log_load("ROOM WAKEUP");
342 $room->room_wakeup($user);
344 else if ($user->subst == 'standup')
345 $room->room_outstandup($user);
347 log_rd2("LOGOUT FROM WHAT ???");
349 Room::save_data($room);
353 Room::unlock_data($sem);
354 ignore_user_abort(FALSE);
372 $is_page_streaming = (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
374 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
375 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
376 header('Content-type: application/xml; charset="utf-8"',true);
382 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
385 $endtime = time() + STREAM_TIMEOUT;
388 $old_step = $ext_step = $step;
390 for ($i = 0 ; time() < $endtime ; $i++) {
391 // log_rd("PRE MAIN ".$step);;
392 $pre_main = gettimeofday(TRUE);
393 if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
395 // 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));
398 log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")."EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
400 log_mop(0, 'index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
401 if ($is_page_streaming)
407 // log_rd("POST MAIN ".$step);;
409 if (($i % 10) == 0) {
410 // log_rd2("TIME: ".time());
416 $s = "[".$sess."] index_rd.php stats: ";
417 foreach ($S_load_stat as $key => $value) {
418 $s .= sprintf("%s: %d - ", $key, $value);