Lock-meter added in log file.
[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
59 register_shutdown_function(shutta);
60
61 function unrecerror()
62 {
63   GLOBAL $is_page_streaming;
64
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");'));
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('the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
77 }
78
79
80
81
82 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $table_idx, $table_token)
83 {
84     GLOBAL $is_page_streaming, $first_loop, $S_load_stat;
85     
86     $ret = FALSE;
87     $bri = FALSE;
88     $user = FALSE;
89     $curtime = time();
90     
91     if (($proxy_step = Bin5_user::load_step($sess)) == FALSE) {
92         log_only2("R");
93         return (FALSE);
94     }
95     
96     // log_rd2("M");
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
104             if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
105                 Bin5::unlock_data();
106                 ignore_user_abort(FALSE);
107                 return (unrecerror());
108             }
109             $user->lacc = $curtime;
110
111             Bin5_user::save_data($user, $table_idx, $user->idx);
112             
113             if (Bin5::garbage_time_is_expired($curtime)) {
114                 log_only("F");
115                 
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());
121                 }
122                 
123                 $bri->garbage_manager(FALSE);
124                 
125                 Bin5::save_data($bri);
126                 unset($bri);
127             }
128             log_lock("U");
129             Bin5::unlock_data($sem);
130             ignore_user_abort(FALSE);
131         } // if (($sem = Bin5::lock_data($table ...
132         else {
133             ignore_user_abort(FALSE);
134             
135             return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
136         }
137         
138         $first_loop = FALSE;
139     } // if  ($first_loop == TRUE) {
140     
141     if ($cur_step == $proxy_step['s']) {
142         log_lock("P");
143         return (FALSE);
144     }
145     else {
146         log_only2("R");
147     }
148     
149     if ($user == FALSE) {
150         do {
151             ignore_user_abort(TRUE);
152             if (($sem = Bin5::lock_data($table_idx)) == FALSE) 
153                 break;
154             
155             log_lock("P");
156             $S_load_stat['U_heavy']++;
157             if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
158                 break;
159             }
160         } while (0);
161         
162         if ($sem != FALSE)
163             Bin5::unlock_data($sem);
164         
165         ignore_user_abort(FALSE);
166         if ($user == FALSE) 
167             return (unrecerror());
168     }
169     
170     /* Nothing changed, return. */
171     if ($cur_step == $user->step) 
172         return (FALSE);
173     
174     log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
175
176     if ($cur_step == -1) {
177         /*
178          *  if $cur_step == -1 load the current state from the main struct
179          */
180         ignore_user_abort(TRUE);
181         $sem = Bin5::lock_data($table_idx);
182         $bri = Bin5::load_data($table_idx, $table_token);
183         $S_load_stat['R_minusone']++;
184         
185         /* unset the $user var to reload it from main structure */
186         unset($user);
187         if (($user = $bri->get_user($sess, $idx)) == FALSE) {
188             Bin5::unlock_data($sem);
189             ignore_user_abort(FALSE);
190             return (unrecerror());
191         }
192         if ($user->the_end) {
193             log_rd2("main_check: the end".var_export(debug_backtrace()));
194             $is_page_streaming = TRUE;
195         }
196         
197         if ($user->trans_step != -1) {
198             log_rd2("TRANS USATO ".$user->trans_step);
199             $cur_step = $user->trans_step;
200             $user->trans_step = -1;
201             
202             Bin5::save_data($bri);
203             Bin5::unlock_data($sem);
204             ignore_user_abort(FALSE);
205         }
206         else {
207             log_rd2("TRANS NON ATTIVATO");
208             
209             //       ARRAY_POP DISABLED
210             //       while (array_pop($user->comm) != NULL);
211             //       // $user->step_inc(COMM_N + 1);
212             //       Bin5::save_data($bri);
213             
214             Bin5::unlock_data($sem);
215             ignore_user_abort(FALSE);
216         }
217     }
218     
219     if ($cur_step == -1) {
220         log_rd2("PRE-NEWSTAT.");
221         
222         /***************
223          *             *
224          *    TABLE    *
225          *             *
226          ***************/
227         if ($user->stat == "table") {      
228             $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
229             
230             log_rd2("SENDED TO THE STREAM: ".$ret);
231         }
232         log_rd2("NEWSTAT: ".$user->stat);
233         
234         $new_stat =  $user->stat;
235         $new_subst = $user->subst;
236         $new_step =  $user->step;
237     }
238     else {
239         ignore_user_abort(TRUE);
240         $sem = Bin5::lock_data($table_idx);
241         // if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
242         if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
243             Bin5::unlock_data($sem);
244             ignore_user_abort(FALSE);
245             return (unrecerror());
246         }
247         if ($cur_step < $user->step) {
248             do {
249                 if ($cur_step + COMM_N < $user->step) {
250                     if (($cur_stat != $user->stat)) {
251                         $to_stat = $user->stat;
252                         Bin5::unlock_data($sem);
253                         ignore_user_abort(FALSE);
254                         return (page_sync($user->sess, $to_stat == "table" ? "index.php" : "../index.php"));
255                     }
256                     log_rd2("lost history, refresh from scratch");
257                     $new_step = -1;
258                     break;
259                 } 
260                 for ($i = $cur_step ; $i < $user->step ; $i++) {
261                     $ii = $i % COMM_N;
262                     log_wr("TRY RET ".$i."  COMM_N ".COMM_N."  II ".$ii);
263                     $ret .= $user->comm[$ii];
264                 }
265                 $new_stat =  $user->stat;
266                 $new_subst = $user->subst;
267                 $new_step =  $user->step;
268             } while (0);
269             
270             log_mop($user->step, 'bin::index_rd.php: after ret set');
271             
272             if ($user->the_end == TRUE) {
273                 log_rd2("LOGOUT BYE BYE!!");
274                 log_auth($user->sess, "Explicit logout.");
275                 
276                 $S_load_stat['R_the_end']++;
277                 $bri = Bin5::load_data($table_idx, $table_token);
278                 unset($user);
279                 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
280                     Bin5::unlock_data($sem);
281                     ignore_user_abort(FALSE);
282                     return (unrecerror());
283                 }
284                 
285                 $tmp_sess = $user->sess;
286                 $user->sess = "";
287                 step_unproxy($tmp_sess);
288                 $user->name = "";
289                 $user->the_end = FALSE;
290                 
291                 if ($user->subst == 'sitdown')
292                     $bri->room_wakeup($user);
293                 else if ($user->subst == 'standup')
294                     $bri->room_outstandup($user);
295                 else
296                     log_rd2("LOGOUT FROM WHAT ???");
297                 
298                 Bin5::save_data($bri);
299             }
300         }
301         
302         Bin5::unlock_data($sem);
303         ignore_user_abort(FALSE);
304   }
305   
306   
307   return ($ret);
308 }
309
310 /*
311  *  MAIN
312  */
313
314 /*
315    FROM THE EXTERN 
316    sess
317    stat
318    step
319 */
320
321 $is_page_streaming =  (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
322
323 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
324 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
325 header('Content-type: application/xml; charset="utf-8"',true);
326 // header('Content-type: text/plain; charset="utf-8"',true);
327 // header('Content-type: text/html; charset="utf-8"',true);
328
329 if (!isset($myfrom))
330      $myfrom = "";
331 if (!isset($subst))
332      $subst = "";
333 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming."USER_AGENT:".$HTTP_USER_AGENT);
334
335
336 $endtime = time() + STREAM_TIMEOUT;
337 $old_stat =  $stat;
338 $old_subst = $subst;
339 $old_step =  $ext_step = $step;
340
341 for ($i = 0 ; time() < $endtime ; $i++) {
342   // log_rd("PRE MAIN ".$step);;
343   $pre_main = gettimeofday(TRUE);
344   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step, $table_idx, $table_token)) != FALSE) {
345     echo '@BEGIN@';
346     // 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     echo "$ret";
348     echo ' @END@'; 
349     log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
350     flush();
351     log_mop(0, 'bin::index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
352     if ($is_page_streaming)
353       break;
354   }
355   $old_stat =  $stat;
356   $old_subst = $subst;
357   $old_step =  $step;
358   // log_rd("POST MAIN ".$step);;
359   usleep(400000);
360   if (($i % 10) == 0) {
361     // log_rd2("TIME: ".time());
362     echo '_';
363     flush();
364   }
365  }
366
367 $s = "[".$sess."] briskin5/index_rd.php stats: ";
368 foreach ($S_load_stat as $key => $value) {
369     $s .= sprintf("%s: %d - ", $key, $value);
370 }
371 log_crit($s);
372
373 ?>