garbage management refactored
[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 unrecerror()
61 {
62   GLOBAL $is_page_streaming;
63
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");'));
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         return (FALSE);
90     }
91     
92     // log_rd2("M");
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']++;
99
100             if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
101                 Bin5::unlock_data($sem);
102                 ignore_user_abort(FALSE);
103                 return (unrecerror());
104             }
105             $user->lacc = $curtime;
106
107             Bin5_user::save_data($user, $table_idx, $user->idx);
108             
109             if (Bin5::garbage_time_is_expired($table_idx, $curtime)) {
110                 log_only("F");
111                 
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());
117                 }
118                 
119                 $bri->garbage_manager(FALSE);
120                 
121                 Bin5::save_data($bri);
122                 unset($bri);
123             }
124             log_main("infolock: U");
125             Bin5::unlock_data($sem);
126             ignore_user_abort(FALSE);
127         } // if (($sem = Bin5::lock_data($table ...
128         else {
129             ignore_user_abort(FALSE);
130             
131             return ("sleep(gst,20000);|xhr_rd_abort(xhr_rd);");
132         }
133         
134         $first_loop = FALSE;
135     } // if  ($first_loop == TRUE) {
136     
137     if ($cur_step == $proxy_step['s']) {
138         log_main("infolock: P");
139         return (FALSE);
140     }
141     else {
142         log_only2("R");
143     }
144     
145     if ($user == FALSE) {
146         do {
147             ignore_user_abort(TRUE);
148             if (($sem = Bin5::lock_data($table_idx)) == FALSE) 
149                 break;
150             
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) {
154                 break;
155             }
156         } while (0);
157         
158         if ($sem != FALSE)
159             Bin5::unlock_data($sem);
160         
161         ignore_user_abort(FALSE);
162         if ($user == FALSE) 
163             return (unrecerror());
164     }
165     
166     /* Nothing changed, return. */
167     if ($cur_step == $user->step) 
168         return (FALSE);
169     
170     log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$user->stat."] cur_step[".$cur_step."] user_step[".$user->step."]");
171
172     if ($cur_step == -1) {
173         /*
174          *  if $cur_step == -1 load the current state from the main struct
175          */
176         ignore_user_abort(TRUE);
177         $sem = Bin5::lock_data($table_idx);
178         if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
179             Bin5::unlock_data($sem);
180             ignore_user_abort(FALSE);
181             return (unrecerror());
182         }
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                 if (($bri = Bin5::load_data($table_idx, $table_token)) == FALSE) {
278                     Bin5::unlock_data($sem);
279                     ignore_user_abort(FALSE);
280                     return (unrecerror());
281                 }
282                 unset($user);
283                 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
284                     Bin5::unlock_data($sem);
285                     ignore_user_abort(FALSE);
286                     return (unrecerror());
287                 }
288                 
289                 $tmp_sess = $user->sess;
290                 $user->sess = "";
291                 Bin5_user::unproxy_step($tmp_sess);
292                 $user->name = "";
293                 $user->the_end = FALSE;
294                 
295                 if ($user->subst == 'sitdown')
296                     $bri->room_wakeup($user);
297                 else if ($user->subst == 'standup')
298                     $bri->room_outstandup($user);
299                 else
300                     log_rd2("LOGOUT FROM WHAT ???");
301                 
302                 Bin5::save_data($bri);
303             }
304         }
305         
306         Bin5::unlock_data($sem);
307         ignore_user_abort(FALSE);
308   }
309   
310   
311   return ($ret);
312 }
313
314 /*
315  *  MAIN
316  */
317
318 /*
319    FROM THE EXTERN 
320    sess
321    stat
322    step
323 */
324
325 $is_page_streaming =  (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
326
327 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
328 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
329 header('Content-type: application/xml; charset="utf-8"',true);
330 // header('Content-type: text/plain; charset="utf-8"',true);
331 // header('Content-type: text/html; charset="utf-8"',true);
332
333 if (!isset($myfrom))
334      $myfrom = "";
335 if (!isset($subst))
336      $subst = "";
337 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);
338
339
340 $endtime = time() + STREAM_TIMEOUT;
341 $old_stat =  $stat;
342 $old_subst = $subst;
343 $old_step =  $ext_step = $step;
344
345 for ($i = 0 ; time() < $endtime ; $i++) {
346   // log_rd("PRE MAIN ".$step);;
347   $pre_main = gettimeofday(TRUE);
348   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step, $table_idx, $table_token)) != FALSE) {
349     echo '@BEGIN@';
350     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));
351     echo "$ret";
352     echo ' @END@'; 
353     log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
354     flush();
355     log_mop(0, 'bin::index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
356     if ($is_page_streaming)
357       break;
358   }
359   $old_stat =  $stat;
360   $old_subst = $subst;
361   $old_step =  $step;
362   // log_rd("POST MAIN ".$step);;
363   usleep(400000);
364   if (($i % 10) == 0) {
365     // log_rd2("TIME: ".time());
366     echo '_';
367     flush();
368   }
369  }
370
371 $s = "[".$sess."] briskin5/index_rd.php stats: ";
372 foreach ($S_load_stat as $key => $value) {
373     $s .= sprintf("%s: %d - ", $key, $value);
374 }
375 log_crit($s);
376
377 ?>