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) {
97 /* Sync check (read only without modifications */
98 ignore_user_abort(TRUE);
99 if ($first_loop == TRUE) {
100 if (($sem = Bin5::lock_data($table_idx)) != FALSE) {
101 // Aggiorna l'expire time lato server
102 $S_load_stat['U_first_loop']++;
103 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
105 ignore_user_abort(FALSE);
106 return (unrecerror());
108 $user->lacc = $curtime;
109 Bin5_user::save_data($user, $proxy_step['i'], $user->idx);
111 if (Bin5::garbage_time_is_expired($curtime)) {
114 $S_load_stat['R_garbage']++;
115 if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
116 Bin5::unlock_data($sem);
117 ignore_user_abort(FALSE);
118 return (unrecerror());
121 $bri->garbage_manager(FALSE);
123 Bin5::save_data($bri);
127 Bin5::unlock_data($sem);
128 ignore_user_abort(FALSE);
129 } // if (($sem = Bin5::lock_data($table ...
131 ignore_user_abort(FALSE);
133 return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
137 } // if ($first_loop == TRUE) {
139 if ($cur_step == $proxy_step['s']) {
147 if ($user == FALSE) {
149 ignore_user_abort(TRUE);
150 if (($sem = Bin5::lock_data($table_idx)) == FALSE)
154 $S_load_stat['U_heavy']++;
155 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
161 Bin5::unlock_data($sem);
163 ignore_user_abort(FALSE);
165 return (unrecerror());
168 /* Nothing changed, return. */
169 if ($cur_step == $user->step)
172 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
174 if ($cur_step == -1) {
176 * if $cur_step == -1 load the current state from the main struct
178 ignore_user_abort(TRUE);
179 $sem = Bin5::lock_data($table_idx);
180 $bri = Bin5::load_data($table_idx, $table_token);
181 $S_load_stat['R_minusone']++;
183 /* unset the $user var to reload it from main structure */
185 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
186 Bin5::unlock_data($sem);
187 ignore_user_abort(FALSE);
188 return (unrecerror());
190 if ($user->the_end) {
191 log_rd2("main_check: the end".var_export(debug_backtrace()));
192 $is_page_streaming = TRUE;
195 if ($user->trans_step != -1) {
196 log_rd2("TRANS USATO ".$user->trans_step);
197 $cur_step = $user->trans_step;
198 $user->trans_step = -1;
200 Bin5::save_data($bri);
201 Bin5::unlock_data($sem);
202 ignore_user_abort(FALSE);
205 log_rd2("TRANS NON ATTIVATO");
207 // ARRAY_POP DISABLED
208 // while (array_pop($user->comm) != NULL);
209 // // $user->step_inc(COMM_N + 1);
210 // Bin5::save_data($bri);
212 Bin5::unlock_data($sem);
213 ignore_user_abort(FALSE);
217 if ($cur_step == -1) {
218 log_rd2("PRE-NEWSTAT.");
225 if ($user->stat == "table") {
226 $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
228 log_rd2("SENDED TO THE STREAM: ".$ret);
230 log_rd2("NEWSTAT: ".$user->stat);
232 $new_stat = $user->stat;
233 $new_subst = $user->subst;
234 $new_step = $user->step;
237 ignore_user_abort(TRUE);
238 $sem = Bin5::lock_data($table_idx);
239 // if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
240 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
241 Bin5::unlock_data($sem);
242 ignore_user_abort(FALSE);
243 return (unrecerror());
245 if ($cur_step < $user->step) {
247 if ($cur_step + COMM_N < $user->step) {
248 if (($cur_stat != $user->stat)) {
249 $to_stat = $user->stat;
250 Bin5::unlock_data($sem);
251 ignore_user_abort(FALSE);
252 return (page_sync($user->sess, $to_stat == "table" ? "index.php" : "../index.php"));
254 log_rd2("lost history, refresh from scratch");
258 for ($i = $cur_step ; $i < $user->step ; $i++) {
260 log_wr("TRY RET ".$i." COMM_N ".COMM_N." II ".$ii);
261 $ret .= $user->comm[$ii];
263 $new_stat = $user->stat;
264 $new_subst = $user->subst;
265 $new_step = $user->step;
268 log_mop($user->step, 'bin::index_rd.php: after ret set');
270 if ($user->the_end == TRUE) {
271 log_rd2("LOGOUT BYE BYE!!");
272 log_auth($user->sess, "Explicit logout.");
274 $S_load_stat['R_the_end']++;
275 $bri = Bin5::load_data($table_idx, $table_token);
277 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
278 Bin5::unlock_data($sem);
279 ignore_user_abort(FALSE);
280 return (unrecerror());
283 $tmp_sess = $user->sess;
285 step_unproxy($tmp_sess);
287 $user->the_end = FALSE;
289 if ($user->subst == 'sitdown')
290 $bri->room_wakeup($user);
291 else if ($user->subst == 'standup')
292 $bri->room_outstandup($user);
294 log_rd2("LOGOUT FROM WHAT ???");
296 Bin5::save_data($bri);
300 Bin5::unlock_data($sem);
301 ignore_user_abort(FALSE);
319 $is_page_streaming = (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
321 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
322 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
323 header('Content-type: application/xml; charset="utf-8"',true);
324 // header('Content-type: text/plain; charset="utf-8"',true);
325 // header('Content-type: text/html; charset="utf-8"',true);
331 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming."USER_AGENT:".$HTTP_USER_AGENT);
334 $endtime = time() + STREAM_TIMEOUT;
337 $old_step = $ext_step = $step;
339 for ($i = 0 ; time() < $endtime ; $i++) {
340 // log_rd("PRE MAIN ".$step);;
341 $pre_main = gettimeofday(TRUE);
342 if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step, $table_idx, $table_token)) != FALSE) {
344 // 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));
347 log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
349 log_mop(0, 'bin::index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
350 if ($is_page_streaming)
356 // log_rd("POST MAIN ".$step);;
358 if (($i % 10) == 0) {
359 // log_rd2("TIME: ".time());
365 $s = "[".$sess."] briskin5/index_rd.php stats: ";
366 foreach ($S_load_stat as $key => $value) {
367 $s .= sprintf("%s: %d - ", $key, $value);