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()."] !");
58 register_shutdown_function(shutta);
62 GLOBAL $is_page_streaming;
64 $is_page_streaming = TRUE;
65 log_rd2("UNREC_ERROR");
66 return (sprintf('the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
69 function page_sync($sess, $page)
71 GLOBAL $is_page_streaming;
73 $is_page_streaming = TRUE;
75 return (sprintf('the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
78 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $table_idx, $table_token)
80 GLOBAL $is_page_streaming, $first_loop, $S_load_stat;
87 if (($proxy_step = Bin5_user::load_step($sess)) == FALSE) {
93 /* Sync check (read only without modifications */
94 ignore_user_abort(TRUE);
95 if ($first_loop == TRUE) {
96 if (($sem = Bin5::lock_data($table_idx)) != FALSE) {
97 // Aggiorna l'expire time lato server
98 $S_load_stat['U_first_loop']++;
100 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
102 ignore_user_abort(FALSE);
103 return (unrecerror());
105 $user->lacc = $curtime;
107 Bin5_user::save_data($user, $table_idx, $user->idx);
109 if (Bin5::garbage_time_is_expired($curtime)) {
112 $S_load_stat['R_garbage']++;
113 if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
114 Bin5::unlock_data($sem);
115 ignore_user_abort(FALSE);
116 return (unrecerror());
119 $bri->garbage_manager(FALSE);
121 Bin5::save_data($bri);
124 log_main("infolock: U");
125 Bin5::unlock_data($sem);
126 ignore_user_abort(FALSE);
127 } // if (($sem = Bin5::lock_data($table ...
129 ignore_user_abort(FALSE);
131 return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
135 } // if ($first_loop == TRUE) {
137 if ($cur_step == $proxy_step['s']) {
138 log_main("infolock: P");
145 if ($user == FALSE) {
147 ignore_user_abort(TRUE);
148 if (($sem = Bin5::lock_data($table_idx)) == FALSE)
151 log_main("infolock: P");
152 $S_load_stat['U_heavy']++;
153 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
159 Bin5::unlock_data($sem);
161 ignore_user_abort(FALSE);
163 return (unrecerror());
166 /* Nothing changed, return. */
167 if ($cur_step == $user->step)
170 log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
172 if ($cur_step == -1) {
174 * if $cur_step == -1 load the current state from the main struct
176 ignore_user_abort(TRUE);
177 $sem = Bin5::lock_data($table_idx);
178 $bri = Bin5::load_data($table_idx, $table_token);
179 $S_load_stat['R_minusone']++;
181 /* unset the $user var to reload it from main structure */
183 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
184 Bin5::unlock_data($sem);
185 ignore_user_abort(FALSE);
186 return (unrecerror());
188 if ($user->the_end) {
189 log_rd2("main_check: the end".var_export(debug_backtrace()));
190 $is_page_streaming = TRUE;
193 if ($user->trans_step != -1) {
194 log_rd2("TRANS USATO ".$user->trans_step);
195 $cur_step = $user->trans_step;
196 $user->trans_step = -1;
198 Bin5::save_data($bri);
199 Bin5::unlock_data($sem);
200 ignore_user_abort(FALSE);
203 log_rd2("TRANS NON ATTIVATO");
205 // ARRAY_POP DISABLED
206 // while (array_pop($user->comm) != NULL);
207 // // $user->step_inc(COMM_N + 1);
208 // Bin5::save_data($bri);
210 Bin5::unlock_data($sem);
211 ignore_user_abort(FALSE);
215 if ($cur_step == -1) {
216 log_rd2("PRE-NEWSTAT.");
223 if ($user->stat == "table") {
224 $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
226 log_rd2("SENDED TO THE STREAM: ".$ret);
228 log_rd2("NEWSTAT: ".$user->stat);
230 $new_stat = $user->stat;
231 $new_subst = $user->subst;
232 $new_step = $user->step;
235 ignore_user_abort(TRUE);
236 $sem = Bin5::lock_data($table_idx);
237 // if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
238 if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
239 Bin5::unlock_data($sem);
240 ignore_user_abort(FALSE);
241 return (unrecerror());
243 if ($cur_step < $user->step) {
245 if ($cur_step + COMM_N < $user->step) {
246 if (($cur_stat != $user->stat)) {
247 $to_stat = $user->stat;
248 Bin5::unlock_data($sem);
249 ignore_user_abort(FALSE);
250 return (page_sync($user->sess, $to_stat == "table" ? "index.php" : "../index.php"));
252 log_rd2("lost history, refresh from scratch");
256 for ($i = $cur_step ; $i < $user->step ; $i++) {
258 log_wr("TRY RET ".$i." COMM_N ".COMM_N." II ".$ii);
259 $ret .= $user->comm[$ii];
261 $new_stat = $user->stat;
262 $new_subst = $user->subst;
263 $new_step = $user->step;
266 log_mop($user->step, 'bin::index_rd.php: after ret set');
268 if ($user->the_end == TRUE) {
269 log_rd2("LOGOUT BYE BYE!!");
270 log_auth($user->sess, "Explicit logout.");
272 $S_load_stat['R_the_end']++;
273 $bri = Bin5::load_data($table_idx, $table_token);
275 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
276 Bin5::unlock_data($sem);
277 ignore_user_abort(FALSE);
278 return (unrecerror());
281 $tmp_sess = $user->sess;
283 step_unproxy($tmp_sess);
285 $user->the_end = FALSE;
287 if ($user->subst == 'sitdown')
288 $bri->room_wakeup($user);
289 else if ($user->subst == 'standup')
290 $bri->room_outstandup($user);
292 log_rd2("LOGOUT FROM WHAT ???");
294 Bin5::save_data($bri);
298 Bin5::unlock_data($sem);
299 ignore_user_abort(FALSE);
317 $is_page_streaming = (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
319 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
320 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
321 header('Content-type: application/xml; charset="utf-8"',true);
322 // header('Content-type: text/plain; charset="utf-8"',true);
323 // header('Content-type: text/html; charset="utf-8"',true);
329 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming."USER_AGENT:".$HTTP_USER_AGENT);
332 $endtime = time() + STREAM_TIMEOUT;
335 $old_step = $ext_step = $step;
337 for ($i = 0 ; time() < $endtime ; $i++) {
338 // log_rd("PRE MAIN ".$step);;
339 $pre_main = gettimeofday(TRUE);
340 if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step, $table_idx, $table_token)) != FALSE) {
342 // 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));
345 log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
347 log_mop(0, 'bin::index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
348 if ($is_page_streaming)
354 // log_rd("POST MAIN ".$step);;
356 if (($i % 10) == 0) {
357 // log_rd2("TIME: ".time());
363 $s = "[".$sess."] briskin5/index_rd.php stats: ";
364 foreach ($S_load_stat as $key => $value) {
365 $s .= sprintf("%s: %d - ", $key, $value);