from html_streaming to xynt_streaming object name
[brisk.git] / web / briskin5 / index_rd.php
1 <?php
2 /*
3  *  brisk - briskin5/index_rd.php
4  *
5  *  Copyright (C) 2006-2012 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
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.
14  *
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.
22  *
23  */
24
25 $G_base = "../";
26
27 require_once("../Obj/brisk.phh");
28 // require_once("../Obj/proxyscan.phh");
29 require_once("Obj/briskin5.phh");
30
31 $S_load_stat = array( 'rU_heavy'      => 0,
32                       'lL_laccgarb'   => 0,
33                       'wU_lacc_upd'   => 0,
34                       'wR_garbage'    => 0,
35                       'wR_minusone'   => 0,
36                       'wR_the_end'    => 0 );
37
38 // Use of proxies isn't allowed.
39 // if (is_proxy()) {
40 //   sleep(5);
41 //   exit;
42 // }
43
44 log_load("LOAD: bin5/index_rd.php ".$QUERY_STRING);
45
46 // $first_loop = TRUE;
47 $the_end = FALSE;
48
49 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
50   echo "Debugging time!";
51   exit;
52 }
53
54 function shutta()
55 {
56   log_rd2("bin5 SHUTTA [".connection_status()."] !");
57 }
58
59 register_shutdown_function(shutta);
60
61 function blocking_error($is_unrecoverable)
62 {
63   GLOBAL $is_page_streaming;
64
65   $is_page_streaming = TRUE;
66   log_rd2("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
67   return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
68 }
69
70 function page_sync($sess, $page)
71 {
72   GLOBAL $is_page_streaming;
73
74   $is_page_streaming = TRUE;
75   log_rd2("PAGE_SYNC");
76   return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
77 }
78
79 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $table_idx, $table_token)
80 {
81     GLOBAL $is_page_streaming, $S_load_stat;
82     // GLOBAL $first_loop;
83
84     $ret = FALSE;
85     $bri = FALSE;
86     $user = FALSE;
87     $curtime = time();
88
89     if (($proxy_step = Bin5_user::load_step($table_idx, $sess)) == FALSE) {
90         log_only2("R");
91         ignore_user_abort(FALSE);
92         return (blocking_error(TRUE));
93     }
94     
95     // log_rd2("M");
96     /* Sync check (read only without modifications */
97     ignore_user_abort(TRUE);
98
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);|xstm.xhr_abort();");
104     }
105
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));
110     }
111
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 */
114
115     if ((($curtime - $user->lacc) > STREAM_TIMEOUT) || Bin5::garbage_time_is_expired($table_idx, $curtime)) {
116         Bin5::unlock_data($sem);
117         
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);|xstm.xhr_abort();");
123         }
124         $S_load_stat['lL_laccgarb']++;
125
126         unset($user);
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));
132         }
133
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);            
139         }
140
141         if (Bin5::garbage_time_is_expired($table_idx, $curtime)) {
142             log_only("F");
143                 
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));
149             }
150                 
151             $bri->garbage_manager(FALSE);
152                 
153             Bin5::save_data($bri);
154             unset($bri);
155         }
156     }
157     log_main("infolock: U");
158     Bin5::unlock_data($sem);
159     ignore_user_abort(FALSE);
160
161
162 //     if  ($first_loop == TRUE) {
163
164 //         if (($sem = Bin5::lock_data(TRUE, $table_idx)) != FALSE) { 
165 //             // Aggiorna l'expire time lato server
166 //             $S_load_stat['rU_first_loop']++;
167
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));
172 //             }
173 //             $user->lacc = $curtime;
174
175 //             Bin5_user::save_data($user, $table_idx, $user->idx);
176             
177 //             if (Bin5::garbage_time_is_expired($table_idx, $curtime)) {
178 //                 log_only("F");
179                 
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));
185 //                 }
186                 
187 //                 $bri->garbage_manager(FALSE);
188                 
189 //                 Bin5::save_data($bri);
190 //                 unset($bri);
191 //             }
192 //             log_main("infolock: U");
193 //             Bin5::unlock_data($sem);
194 //             ignore_user_abort(FALSE);
195 //         } // if (($sem = Bin5::lock_data(TRUE, $table ...
196 //         else {
197 //             ignore_user_abort(FALSE);
198             
199 //             return ("sleep(gst,20000);|xstm.xhr_abort();");
200 //         }
201         
202 //         $first_loop = FALSE;
203 //     } // if  ($first_loop == TRUE) {
204     
205     if ($cur_step == $proxy_step['s']) {
206         log_main("infolock: P");
207         return (FALSE);
208     }
209     else {
210         log_only2("R");
211     }
212     
213     $S_load_stat['rU_heavy']++;
214     if ($user == FALSE) {
215         do {
216             ignore_user_abort(TRUE);
217             if (($sem = Bin5::lock_data(TRUE, $table_idx)) == FALSE) 
218                 break;
219             
220             log_main("infolock: P");
221             if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
222                 break;
223             }
224         } while (0);
225         
226         if ($sem != FALSE)
227             Bin5::unlock_data($sem);
228         
229         ignore_user_abort(FALSE);
230         if ($user == FALSE) 
231             return (blocking_error(TRUE));
232     }
233     
234     /* Nothing changed, return. */
235     if ($cur_step == $user->step) 
236         return (FALSE);
237     
238     log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
239
240     if ($cur_step == -1) {
241         /*
242          *  if $cur_step == -1 load the current state from the main struct
243          */
244         
245         /* unset the $user var to reload it from main structure */
246         unset($user);
247
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));
254         }
255         $S_load_stat['wR_minusone']++;
256
257         if (($user = $bri->get_user($sess, $idx)) == FALSE) {
258             Bin5::unlock_data($sem);
259             ignore_user_abort(FALSE);
260             return (blocking_error(TRUE));
261         }
262         if ($user->the_end == TRUE) {
263             log_rd2("main_check: the end".var_export(debug_backtrace()));
264             $is_page_streaming = TRUE;
265         }
266         
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;
271             
272             Bin5::save_data($bri);
273             Bin5::unlock_data($sem);
274             ignore_user_abort(FALSE);
275         }
276         else {
277             log_rd2("TRANS NON ATTIVATO");
278             
279             //       ARRAY_POP DISABLED
280             //       while (array_pop($user->comm) != NULL);
281             //       // $user->step_inc(COMM_N + 1);
282             //       Bin5::save_data($bri);
283             
284             Bin5::unlock_data($sem);
285             ignore_user_abort(FALSE);
286         }
287     }
288     
289     if ($cur_step == -1) {
290         log_rd2("PRE-NEWSTAT.");
291         
292         /***************
293          *             *
294          *    TABLE    *
295          *             *
296          ***************/
297         if ($user->stat == "table") {      
298             $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
299             
300             log_rd2("SENDED TO THE STREAM: ".$ret);
301         }
302         log_rd2("NEWSTAT: ".$user->stat);
303         
304         $new_stat =  $user->stat;
305         $new_subst = $user->subst;
306         $new_step =  $user->step;
307     }
308     else {
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));
316         }
317         if ($cur_step < $user->step) {
318             do {
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"));
325                     }
326                     log_rd2("lost history, refresh from scratch");
327                     $new_step = -1;
328                     break;
329                 } 
330                 for ($i = $cur_step ; $i < $user->step ; $i++) {
331                     $ii = $i % COMM_N;
332                     log_wr("TRY RET ".$i."  COMM_N ".COMM_N."  II ".$ii);
333                     $ret .= $user->comm[$ii];
334                 }
335                 $new_stat =  $user->stat;
336                 $new_subst = $user->subst;
337                 $new_step =  $user->step;
338             } while (0);
339             
340             log_rd2($user->step, 'bin::index_rd.php: after ret set');
341             
342             if ($user->the_end == TRUE) {
343                 log_rd2("LOGOUT BYE BYE!!");
344                 log_auth($user->sess, "Explicit logout.");
345
346                 unset($user);
347                 
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));
353                 }
354                 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
355                     Bin5::unlock_data($sem);
356                     ignore_user_abort(FALSE);
357                     return (blocking_error(TRUE));
358                 }
359                 
360                 $tmp_sess = $user->sess;
361                 $user->sess = "";
362                 Bin5_user::unproxy_step($tmp_sess);
363                 $user->name = "";
364                 $user->the_end = FALSE;
365                 
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);
371                 else
372                     log_rd2("LOGOUT FROM WHAT ???");
373                 */
374                 Bin5::save_data($bri);
375             }
376         }
377         
378         Bin5::unlock_data($sem);
379         ignore_user_abort(FALSE);
380   }
381   
382   
383   return ($ret);
384 }
385
386 /*
387  *  MAIN
388  */
389
390 /*
391    FROM THE EXTERN 
392    sess
393    stat
394    step
395 */
396
397 $is_page_streaming =  (webservers_exceeded() || stristr($HTTP_USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; rv:5.0)") || stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
398
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);
404
405 if (!isset($from))
406      $from = "";
407 if (!isset($subst))
408      $subst = "";
409 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." FROM: ".$from. "IS_PAGE:" . $is_page_streaming."USER_AGENT:".$HTTP_USER_AGENT."  TABLE:".$table_idx);
410
411
412 $endtime = time() + STREAM_TIMEOUT;
413 $old_stat =  $stat;
414 $old_subst = $subst;
415 $old_step =  $ext_step = $step;
416
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) {
421     echo '@BEGIN@';
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));
423     echo "$ret";
424     echo ' @END@'; 
425     log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
426     mop_flush();
427     log_rd2(0, 'bin::index_rd.php: after mop_flush (begin: '.sprintf("%f", $pre_main).')');
428     if ($is_page_streaming)
429       break;
430   }
431   $old_stat =  $stat;
432   $old_subst = $subst;
433   $old_step =  $step;
434   // log_rd("POST MAIN ".$step);;
435   usleep(400000);
436   if (($i % 10) == 0) {
437     // log_rd2("TIME: ".time());
438     echo '_';
439     mop_flush();
440   }
441  }
442
443 $s = ""; 
444 $tr = 0;
445 $tw = 0;
446 foreach ($S_load_stat as $key => $value) {
447     $s .= sprintf("%s: %d - ", $key, $value);
448     if (substr($key, 0, 1) == "w")
449         $tw += $value;
450     else if (substr($key, 0, 1) == "r")
451         $tr += $value;
452 }
453 $s = sprintf("briskin5/index_rd.php stats:  R: %d W: %d - %s", $tr, $tw, $s);
454 log_crit($s);
455
456 ?>