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( 'U_first_loop' => 0,
37 // Use of proxies isn't allowed.
43 log_load("LOAD: bin5/index_rd.php ".$QUERY_STRING);
48 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
49 echo "Debugging time!";
55 log_rd2("bin5 SHUTTA!".connection_status());
59 register_shutdown_function(shutta);
63 GLOBAL $is_page_streaming;
65 $is_page_streaming = TRUE;
66 log_rd2("UNREC_ERROR");
67 return (sprintf('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));
82 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $table_idx, $table_token)
84 GLOBAL $is_page_streaming, $first_loop, $S_load_stat;
91 if (($proxy_step = Bin5_user::step_get($sess)) == FALSE) {
96 // error_log("maincheck: step di i [".$proxy_step['i']."]", 0);
99 /* Sync check (read only without modifications */
100 ignore_user_abort(TRUE);
101 if ($first_loop == TRUE) {
102 if (($sem = Bin5::lock_data($table_idx)) != FALSE) {
103 // Aggiorna l'expire time lato server
104 $S_load_stat['U_first_loop']++;
105 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
107 ignore_user_abort(FALSE);
108 return (unrecerror());
110 $user->lacc = $curtime;
111 Bin5_user::save_data($user, $proxy_step['i'], $user->idx);
113 if (Bin5::garbage_time_is_expired($curtime)) {
116 $S_load_stat['R_garbage']++;
117 if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
118 Bin5::unlock_data($sem);
119 ignore_user_abort(FALSE);
120 return (unrecerror());
123 $bri->garbage_manager(FALSE);
125 Bin5::save_data($bri);
129 Bin5::unlock_data($sem);
130 ignore_user_abort(FALSE);
131 } // if (($sem = Bin5::lock_data($table ...
133 ignore_user_abort(FALSE);
135 return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
139 } // if ($first_loop == TRUE) {
141 if ($cur_step == $proxy_step['s']) {
149 if ($user == FALSE) {
151 ignore_user_abort(TRUE);
152 if (($sem = Bin5::lock_data($table_idx)) == FALSE)
156 $S_load_stat['U_heavy']++;
157 // if (($bri = &Bin5::load_data($table_idx, $table_token)) == FALSE)
158 // if (($user = Bin5_user::load_data($table_idx, $table_token)) == FALSE)
159 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
165 Bin5::unlock_data($sem);
167 ignore_user_abort(FALSE);
169 return (unrecerror());
172 /* Nothing changed, return. */
173 if ($cur_step == $user->step)
176 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
178 if ($cur_step == -1) {
180 * if $cur_step == -1 load the current state from the main struct
182 ignore_user_abort(TRUE);
183 $sem = Bin5::lock_data($table_idx);
184 $bri = Bin5::load_data($table_idx, $table_token);
185 $S_load_stat['R_minusone']++;
187 /* unset the $user var to reload it from main structure */
189 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
190 Bin5::unlock_data($sem);
191 ignore_user_abort(FALSE);
192 return (unrecerror());
194 if ($user->the_end) {
195 log_rd2("main_check: the end".var_export(debug_backtrace()));
196 $is_page_streaming = TRUE;
199 if ($user->trans_step != -1) {
200 log_rd2("TRANS USATO ".$user->trans_step);
201 $cur_step = $user->trans_step;
202 $user->trans_step = -1;
204 Bin5::save_data($bri);
205 Bin5::unlock_data($sem);
206 ignore_user_abort(FALSE);
209 log_rd2("TRANS NON ATTIVATO");
211 // ARRAY_POP DISABLED
212 // while (array_pop($user->comm) != NULL);
213 // // $user->step_inc(COMM_N + 1);
214 // Bin5::save_data($bri);
216 Bin5::unlock_data($sem);
217 ignore_user_abort(FALSE);
221 if ($cur_step == -1) {
222 log_rd2("PRE-NEWSTAT.");
229 if ($user->stat == "table") {
230 $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
232 log_rd2("SENDED TO THE STREAM: ".$ret);
234 log_rd2("NEWSTAT: ".$user->stat);
236 $new_stat = $user->stat;
237 $new_subst = $user->subst;
238 $new_step = $user->step;
241 ignore_user_abort(TRUE);
242 $sem = Bin5::lock_data($table_idx);
243 // if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
244 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
245 Bin5::unlock_data($sem);
246 ignore_user_abort(FALSE);
247 return (unrecerror());
249 if ($cur_step < $user->step) {
251 if ($cur_step + COMM_N < $user->step) {
252 if (($cur_stat != $user->stat)) {
253 $to_stat = $user->stat;
254 Bin5::unlock_data($sem);
255 ignore_user_abort(FALSE);
256 return (page_sync($user->sess, $to_stat == "table" ? "index.php" : "../index.php"));
258 log_rd2("lost history, refresh from scratch");
262 for ($i = $cur_step ; $i < $user->step ; $i++) {
264 log_wr("TRY RET ".$i." COMM_N ".COMM_N." II ".$ii);
265 $ret .= $user->comm[$ii];
267 $new_stat = $user->stat;
268 $new_subst = $user->subst;
269 $new_step = $user->step;
272 log_mop($user->step, 'bin::index_rd.php: after ret set');
274 if ($user->the_end == TRUE) {
275 log_rd2("LOGOUT BYE BYE!!");
276 log_auth($user->sess, "Explicit logout.");
278 $S_load_stat['R_the_end']++;
279 $bri = Bin5::load_data($table_idx, $table_token);
281 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
282 Bin5::unlock_data($sem);
283 ignore_user_abort(FALSE);
284 return (unrecerror());
287 $tmp_sess = $user->sess;
289 step_unproxy($tmp_sess);
291 $user->the_end = FALSE;
293 if ($user->subst == 'sitdown')
294 $bri->room_wakeup($user);
295 else if ($user->subst == 'standup')
296 $bri->room_outstandup($user);
298 log_rd2("LOGOUT FROM WHAT ???");
300 Bin5::save_data($bri);
304 Bin5::unlock_data($sem);
305 ignore_user_abort(FALSE);
323 $is_page_streaming = (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
325 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
326 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
327 header('Content-type: application/xml; charset="utf-8"',true);
328 // header('Content-type: text/plain; charset="utf-8"',true);
329 // header('Content-type: text/html; charset="utf-8"',true);
335 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming."USER_AGENT:".$HTTP_USER_AGENT);
338 $endtime = time() + STREAM_TIMEOUT;
341 $old_step = $ext_step = $step;
343 for ($i = 0 ; time() < $endtime ; $i++) {
344 // log_rd("PRE MAIN ".$step);;
345 $pre_main = gettimeofday(TRUE);
346 if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step, $table_idx, $table_token)) != FALSE) {
348 // 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));
351 log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
353 log_mop(0, 'bin::index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
354 if ($is_page_streaming)
360 // log_rd("POST MAIN ".$step);;
362 if (($i % 10) == 0) {
363 // log_rd2("TIME: ".time());
369 $s = "[".$sess."] briskin5/index_rd.php stats: ";
370 foreach ($S_load_stat as $key => $value) {
371 $s .= sprintf("%s: %d - ", $key, $value);