3 * brisk - briskin5/index_rd.php
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("Obj/briskin5.phh");
31 $S_load_stat = array( 'rU_heavy' => 0,
38 // Use of proxies isn't allowed.
44 log_load("LOAD: bin5/index_rd.php ".$QUERY_STRING);
46 // $first_loop = TRUE;
49 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
50 echo "Debugging time!";
56 log_rd2("bin5 SHUTTA [".connection_status()."] !");
59 register_shutdown_function(shutta);
61 function blocking_error($is_unrecoverable)
63 GLOBAL $is_page_streaming;
65 $is_page_streaming = TRUE;
66 log_rd2("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
67 return (sprintf(($is_unrecoverable ? 'the_end=true; ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
70 function page_sync($sess, $page)
72 GLOBAL $is_page_streaming;
74 $is_page_streaming = TRUE;
76 return (sprintf('the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
79 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $table_idx, $table_token)
81 GLOBAL $is_page_streaming, $S_load_stat;
82 // GLOBAL $first_loop;
89 if (($proxy_step = Bin5_user::load_step($table_idx, $sess)) == FALSE) {
91 ignore_user_abort(FALSE);
92 return (blocking_error(TRUE));
96 /* Sync check (read only without modifications */
97 ignore_user_abort(TRUE);
99 // shared locking to load info
100 if (($sem = Bin5::lock_data(FALSE, $table_idx)) == FALSE) {
101 // wait 20 secs, then restart the xhr
102 ignore_user_abort(FALSE);
103 return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
106 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
107 Bin5::unlock_data($sem);
108 ignore_user_abort(FALSE);
109 return (blocking_error(TRUE));
112 /* if lacc time great than STREAM_TIMEOUT or the room garbage_time is expired
113 switch to exclusive locking and verify again the conditions */
115 if ((($curtime - $user->lacc) > STREAM_TIMEOUT) || Bin5::garbage_time_is_expired($table_idx, $curtime)) {
116 Bin5::unlock_data($sem);
118 // exclusive locking to modify info
119 if (($sem = Bin5::lock_data(TRUE, $table_idx)) == FALSE) {
120 // wait 20 secs, then restart the xhr
121 ignore_user_abort(FALSE);
122 return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
124 $S_load_stat['wL_laccgarb']++;
127 // load again the user data after new lock
128 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
129 Bin5::unlock_data($sem);
130 ignore_user_abort(FALSE);
131 return (blocking_error(TRUE));
134 if (($curtime - $user->lacc) > STREAM_TIMEOUT) {
135 $S_load_stat['wU_lacc_upd']++;
136 $user->lacc = $curtime;
137 // lacc field updated
138 Bin5_user::save_data($user, $table_idx, $user->idx);
141 if (Bin5::garbage_time_is_expired($table_idx, $curtime)) {
144 $S_load_stat['wR_garbage']++;
145 if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
146 Bin5::unlock_data($sem);
147 ignore_user_abort(FALSE);
148 return (blocking_error(TRUE));
151 $bri->garbage_manager(FALSE);
153 Bin5::save_data($bri);
157 log_main("infolock: U");
158 Bin5::unlock_data($sem);
159 ignore_user_abort(FALSE);
162 // if ($first_loop == TRUE) {
164 // if (($sem = Bin5::lock_data(TRUE, $table_idx)) != FALSE) {
165 // // Aggiorna l'expire time lato server
166 // $S_load_stat['rU_first_loop']++;
168 // if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
169 // Bin5::unlock_data($sem);
170 // ignore_user_abort(FALSE);
171 // return (blocking_error(TRUE));
173 // $user->lacc = $curtime;
175 // Bin5_user::save_data($user, $table_idx, $user->idx);
177 // if (Bin5::garbage_time_is_expired($table_idx, $curtime)) {
180 // $S_load_stat['wR_garbage']++;
181 // if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
182 // Bin5::unlock_data($sem);
183 // ignore_user_abort(FALSE);
184 // return (blocking_error(TRUE));
187 // $bri->garbage_manager(FALSE);
189 // Bin5::save_data($bri);
192 // log_main("infolock: U");
193 // Bin5::unlock_data($sem);
194 // ignore_user_abort(FALSE);
195 // } // if (($sem = Bin5::lock_data(TRUE, $table ...
197 // ignore_user_abort(FALSE);
199 // return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
202 // $first_loop = FALSE;
203 // } // if ($first_loop == TRUE) {
205 if ($cur_step == $proxy_step['s']) {
206 log_main("infolock: P");
213 if ($user == FALSE) {
215 ignore_user_abort(TRUE);
216 if (($sem = Bin5::lock_data(TRUE, $table_idx)) == FALSE)
219 log_main("infolock: P");
220 $S_load_stat['rU_heavy']++;
221 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
227 Bin5::unlock_data($sem);
229 ignore_user_abort(FALSE);
231 return (blocking_error(TRUE));
234 /* Nothing changed, return. */
235 if ($cur_step == $user->step)
238 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
240 if ($cur_step == -1) {
242 * if $cur_step == -1 load the current state from the main struct
245 /* unset the $user var to reload it from main structure */
248 ignore_user_abort(TRUE);
249 $sem = Bin5::lock_data(TRUE, $table_idx);
250 if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
251 Bin5::unlock_data($sem);
252 ignore_user_abort(FALSE);
253 return (blocking_error(TRUE));
255 $S_load_stat['wR_minusone']++;
257 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
258 Bin5::unlock_data($sem);
259 ignore_user_abort(FALSE);
260 return (blocking_error(TRUE));
262 if ($user->the_end) {
263 log_rd2("main_check: the end".var_export(debug_backtrace()));
264 $is_page_streaming = TRUE;
267 if ($user->trans_step != -1) {
268 log_rd2("TRANS USATO ".$user->trans_step);
269 $cur_step = $user->trans_step;
270 $user->trans_step = -1;
272 Bin5::save_data($bri);
273 Bin5::unlock_data($sem);
274 ignore_user_abort(FALSE);
277 log_rd2("TRANS NON ATTIVATO");
279 // ARRAY_POP DISABLED
280 // while (array_pop($user->comm) != NULL);
281 // // $user->step_inc(COMM_N + 1);
282 // Bin5::save_data($bri);
284 Bin5::unlock_data($sem);
285 ignore_user_abort(FALSE);
289 if ($cur_step == -1) {
290 log_rd2("PRE-NEWSTAT.");
297 if ($user->stat == "table") {
298 $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
300 log_rd2("SENDED TO THE STREAM: ".$ret);
302 log_rd2("NEWSTAT: ".$user->stat);
304 $new_stat = $user->stat;
305 $new_subst = $user->subst;
306 $new_step = $user->step;
309 ignore_user_abort(TRUE);
310 $sem = Bin5::lock_data(TRUE, $table_idx);
311 // if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
312 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
313 Bin5::unlock_data($sem);
314 ignore_user_abort(FALSE);
315 return (blocking_error(TRUE));
317 if ($cur_step < $user->step) {
319 if ($cur_step + COMM_N < $user->step) {
320 if (($cur_stat != $user->stat)) {
321 $to_stat = $user->stat;
322 Bin5::unlock_data($sem);
323 ignore_user_abort(FALSE);
324 return (page_sync($user->sess, $to_stat == "table" ? "index.php" : "../index.php"));
326 log_rd2("lost history, refresh from scratch");
330 for ($i = $cur_step ; $i < $user->step ; $i++) {
332 log_wr("TRY RET ".$i." COMM_N ".COMM_N." II ".$ii);
333 $ret .= $user->comm[$ii];
335 $new_stat = $user->stat;
336 $new_subst = $user->subst;
337 $new_step = $user->step;
340 log_mop($user->step, 'bin::index_rd.php: after ret set');
342 if ($user->the_end == TRUE) {
343 log_rd2("LOGOUT BYE BYE!!");
344 log_auth($user->sess, "Explicit logout.");
348 $S_load_stat['wR_the_end']++;
349 if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
350 Bin5::unlock_data($sem);
351 ignore_user_abort(FALSE);
352 return (blocking_error(TRUE));
354 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
355 Bin5::unlock_data($sem);
356 ignore_user_abort(FALSE);
357 return (blocking_error(TRUE));
360 $tmp_sess = $user->sess;
362 Bin5_user::unproxy_step($tmp_sess);
364 $user->the_end = FALSE;
366 /* FIXME - and now ??
367 if ($user->subst == 'sitdown')
368 $bri->room_wakeup($user);
369 else if ($user->subst == 'standup')
370 $bri->room_outstandup($user);
372 log_rd2("LOGOUT FROM WHAT ???");
374 Bin5::save_data($bri);
378 Bin5::unlock_data($sem);
379 ignore_user_abort(FALSE);
397 $is_page_streaming = (webservers_exceeded() || stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
399 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
400 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
401 header('Content-type: application/xml; charset="utf-8"',true);
402 // header('Content-type: text/plain; charset="utf-8"',true);
403 // header('Content-type: text/html; charset="utf-8"',true);
409 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming."USER_AGENT:".$HTTP_USER_AGENT." TABLE:".$table_idx);
412 $endtime = time() + STREAM_TIMEOUT;
415 $old_step = $ext_step = $step;
417 for ($i = 0 ; time() < $endtime ; $i++) {
418 // log_rd("PRE MAIN ".$step);;
419 $pre_main = gettimeofday(TRUE);
420 if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step, $table_idx, $table_token)) != FALSE) {
422 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));
425 log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
427 log_mop(0, 'bin::index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
428 if ($is_page_streaming)
434 // log_rd("POST MAIN ".$step);;
436 if (($i % 10) == 0) {
437 // log_rd2("TIME: ".time());
446 foreach ($S_load_stat as $key => $value) {
447 $s .= sprintf("%s: %d - ", $key, $value);
448 if (substr($key, 0, 1) == "w")
453 $s = sprintf("briskin5/index_rd.php stats: R: %d W: %d - %s", $tr, $tw, $s);