is_exclusive argument added to all lock_data() functions, (exclusivity is always...
[brisk.git] / web / briskin5 / index_rd.php
1 <?php
2 /*
3  *  brisk - briskin5/index_rd.php
4  *
5  *  Copyright (C) 2006-2011 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( 'U_first_loop' => 0,
32                       'U_heavy'      => 0,
33                       'R_garbage'    => 0,
34                       'R_minusone'   => 0,
35                       'R_the_end'    => 0 );
36
37 // Use of proxies isn't allowed.
38 // if (is_proxy()) {
39 //   sleep(5);
40 //   exit;
41 // }
42
43 log_load("LOAD: bin5/index_rd.php ".$QUERY_STRING);
44
45 $first_loop = TRUE;
46 $the_end = FALSE;
47
48 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
49   echo "Debugging time!";
50   exit;
51 }
52
53 function shutta()
54 {
55   log_rd2("bin5 SHUTTA [".connection_status()."] !");
56 }
57
58 register_shutdown_function(shutta);
59
60 function blocking_error($is_unrecoverable)
61 {
62   GLOBAL $is_page_streaming;
63
64   $is_page_streaming = TRUE;
65   log_rd2("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
66   return (sprintf(($is_unrecoverable ? 'the_end=true; ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
67 }
68
69 function page_sync($sess, $page)
70 {
71   GLOBAL $is_page_streaming;
72
73   $is_page_streaming = TRUE;
74   log_rd2("PAGE_SYNC");
75   return (sprintf('the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
76 }
77
78 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $table_idx, $table_token)
79 {
80     GLOBAL $is_page_streaming, $first_loop, $S_load_stat;
81     
82     $ret = FALSE;
83     $bri = FALSE;
84     $user = FALSE;
85     $curtime = time();
86
87     if (($proxy_step = Bin5_user::load_step($table_idx, $sess)) == FALSE) {
88         log_only2("R");
89         ignore_user_abort(FALSE);
90         return (blocking_error(TRUE));
91     }
92     
93     // log_rd2("M");
94     /* Sync check (read only without modifications */
95     ignore_user_abort(TRUE);
96     if  ($first_loop == TRUE) {
97
98         if (($sem = Bin5::lock_data(TRUE, $table_idx)) != FALSE) { 
99             // Aggiorna l'expire time lato server
100             $S_load_stat['U_first_loop']++;
101
102             if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
103                 Bin5::unlock_data($sem);
104                 ignore_user_abort(FALSE);
105                 return (blocking_error(TRUE));
106             }
107             $user->lacc = $curtime;
108
109             Bin5_user::save_data($user, $table_idx, $user->idx);
110             
111             if (Bin5::garbage_time_is_expired($table_idx, $curtime)) {
112                 log_only("F");
113                 
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 (blocking_error(TRUE));
119                 }
120                 
121                 $bri->garbage_manager(FALSE);
122                 
123                 Bin5::save_data($bri);
124                 unset($bri);
125             }
126             log_main("infolock: U");
127             Bin5::unlock_data($sem);
128             ignore_user_abort(FALSE);
129         } // if (($sem = Bin5::lock_data(TRUE, $table ...
130         else {
131             ignore_user_abort(FALSE);
132             
133             return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
134         }
135         
136         $first_loop = FALSE;
137     } // if  ($first_loop == TRUE) {
138     
139     if ($cur_step == $proxy_step['s']) {
140         log_main("infolock: P");
141         return (FALSE);
142     }
143     else {
144         log_only2("R");
145     }
146     
147     if ($user == FALSE) {
148         do {
149             ignore_user_abort(TRUE);
150             if (($sem = Bin5::lock_data(TRUE, $table_idx)) == FALSE) 
151                 break;
152             
153             log_main("infolock: P");
154             $S_load_stat['U_heavy']++;
155             if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
156                 break;
157             }
158         } while (0);
159         
160         if ($sem != FALSE)
161             Bin5::unlock_data($sem);
162         
163         ignore_user_abort(FALSE);
164         if ($user == FALSE) 
165             return (blocking_error(TRUE));
166     }
167     
168     /* Nothing changed, return. */
169     if ($cur_step == $user->step) 
170         return (FALSE);
171     
172     log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
173
174     if ($cur_step == -1) {
175         /*
176          *  if $cur_step == -1 load the current state from the main struct
177          */
178         ignore_user_abort(TRUE);
179         $sem = Bin5::lock_data(TRUE, $table_idx);
180         if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
181             Bin5::unlock_data($sem);
182             ignore_user_abort(FALSE);
183             return (blocking_error(TRUE));
184         }
185         $S_load_stat['R_minusone']++;
186         
187         /* unset the $user var to reload it from main structure */
188         unset($user);
189         if (($user = $bri->get_user($sess, $idx)) == FALSE) {
190             Bin5::unlock_data($sem);
191             ignore_user_abort(FALSE);
192             return (blocking_error(TRUE));
193         }
194         if ($user->the_end) {
195             log_rd2("main_check: the end".var_export(debug_backtrace()));
196             $is_page_streaming = TRUE;
197         }
198         
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;
203             
204             Bin5::save_data($bri);
205             Bin5::unlock_data($sem);
206             ignore_user_abort(FALSE);
207         }
208         else {
209             log_rd2("TRANS NON ATTIVATO");
210             
211             //       ARRAY_POP DISABLED
212             //       while (array_pop($user->comm) != NULL);
213             //       // $user->step_inc(COMM_N + 1);
214             //       Bin5::save_data($bri);
215             
216             Bin5::unlock_data($sem);
217             ignore_user_abort(FALSE);
218         }
219     }
220     
221     if ($cur_step == -1) {
222         log_rd2("PRE-NEWSTAT.");
223         
224         /***************
225          *             *
226          *    TABLE    *
227          *             *
228          ***************/
229         if ($user->stat == "table") {      
230             $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
231             
232             log_rd2("SENDED TO THE STREAM: ".$ret);
233         }
234         log_rd2("NEWSTAT: ".$user->stat);
235         
236         $new_stat =  $user->stat;
237         $new_subst = $user->subst;
238         $new_step =  $user->step;
239     }
240     else {
241         ignore_user_abort(TRUE);
242         $sem = Bin5::lock_data(TRUE, $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 (blocking_error(TRUE));
248         }
249         if ($cur_step < $user->step) {
250             do {
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"));
257                     }
258                     log_rd2("lost history, refresh from scratch");
259                     $new_step = -1;
260                     break;
261                 } 
262                 for ($i = $cur_step ; $i < $user->step ; $i++) {
263                     $ii = $i % COMM_N;
264                     log_wr("TRY RET ".$i."  COMM_N ".COMM_N."  II ".$ii);
265                     $ret .= $user->comm[$ii];
266                 }
267                 $new_stat =  $user->stat;
268                 $new_subst = $user->subst;
269                 $new_step =  $user->step;
270             } while (0);
271             
272             log_mop($user->step, 'bin::index_rd.php: after ret set');
273             
274             if ($user->the_end == TRUE) {
275                 log_rd2("LOGOUT BYE BYE!!");
276                 log_auth($user->sess, "Explicit logout.");
277                 
278                 $S_load_stat['R_the_end']++;
279                 if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
280                     Bin5::unlock_data($sem);
281                     ignore_user_abort(FALSE);
282                     return (blocking_error(TRUE));
283                 }
284                 unset($user);
285                 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
286                     Bin5::unlock_data($sem);
287                     ignore_user_abort(FALSE);
288                     return (blocking_error(TRUE));
289                 }
290                 
291                 $tmp_sess = $user->sess;
292                 $user->sess = "";
293                 Bin5_user::unproxy_step($tmp_sess);
294                 $user->name = "";
295                 $user->the_end = FALSE;
296                 
297                 /* FIXME - and now ?? 
298                 if ($user->subst == 'sitdown')
299                     $bri->room_wakeup($user);
300                 else if ($user->subst == 'standup')
301                     $bri->room_outstandup($user);
302                 else
303                     log_rd2("LOGOUT FROM WHAT ???");
304                 */
305                 Bin5::save_data($bri);
306             }
307         }
308         
309         Bin5::unlock_data($sem);
310         ignore_user_abort(FALSE);
311   }
312   
313   
314   return ($ret);
315 }
316
317 /*
318  *  MAIN
319  */
320
321 /*
322    FROM THE EXTERN 
323    sess
324    stat
325    step
326 */
327
328 $is_page_streaming =  (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
329
330 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
331 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
332 header('Content-type: application/xml; charset="utf-8"',true);
333 // header('Content-type: text/plain; charset="utf-8"',true);
334 // header('Content-type: text/html; charset="utf-8"',true);
335
336 if (!isset($myfrom))
337      $myfrom = "";
338 if (!isset($subst))
339      $subst = "";
340 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);
341
342
343 $endtime = time() + STREAM_TIMEOUT;
344 $old_stat =  $stat;
345 $old_subst = $subst;
346 $old_step =  $ext_step = $step;
347
348 for ($i = 0 ; time() < $endtime ; $i++) {
349   // log_rd("PRE MAIN ".$step);;
350   $pre_main = gettimeofday(TRUE);
351   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step, $table_idx, $table_token)) != FALSE) {
352     echo '@BEGIN@';
353     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));
354     echo "$ret";
355     echo ' @END@'; 
356     log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
357     flush();
358     log_mop(0, 'bin::index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
359     if ($is_page_streaming)
360       break;
361   }
362   $old_stat =  $stat;
363   $old_subst = $subst;
364   $old_step =  $step;
365   // log_rd("POST MAIN ".$step);;
366   usleep(400000);
367   if (($i % 10) == 0) {
368     // log_rd2("TIME: ".time());
369     echo '_';
370     flush();
371   }
372  }
373
374 $s = "[".$sess."] briskin5/index_rd.php stats: ";
375 foreach ($S_load_stat as $key => $value) {
376     $s .= sprintf("%s: %d - ", $key, $value);
377 }
378 log_crit($s);
379
380 ?>