81c05f98a3a0848103a3cc1fe810dcf2693954f9
[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::step_get($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             if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
104                 Bin5::unlock_data();
105                 ignore_user_abort(FALSE);
106                 return (unrecerror());
107             }
108             $user->lacc = $curtime;
109             Bin5_user::save_data($user, $proxy_step['i'], $user->idx);
110             
111             if (Bin5::garbage_time_is_expired($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 (unrecerror());
119                 }
120                 
121                 $bri->garbage_manager(FALSE);
122                 
123                 Bin5::save_data($bri);
124                 unset($bri);
125             }
126             log_lock("U");
127             Bin5::unlock_data($sem);
128             ignore_user_abort(FALSE);
129         } // if (($sem = Bin5::lock_data($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_lock("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($table_idx)) == FALSE) 
151                 break;
152             
153             log_lock("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 (unrecerror());
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($table_idx);
180         $bri = Bin5::load_data($table_idx, $table_token);
181         $S_load_stat['R_minusone']++;
182         
183         /* unset the $user var to reload it from main structure */
184         unset($user);
185         if (($user = $bri->get_user($sess, $idx)) == FALSE) {
186             Bin5::unlock_data($sem);
187             ignore_user_abort(FALSE);
188             return (unrecerror());
189         }
190         if ($user->the_end) {
191             log_rd2("main_check: the end".var_export(debug_backtrace()));
192             $is_page_streaming = TRUE;
193         }
194         
195         if ($user->trans_step != -1) {
196             log_rd2("TRANS USATO ".$user->trans_step);
197             $cur_step = $user->trans_step;
198             $user->trans_step = -1;
199             
200             Bin5::save_data($bri);
201             Bin5::unlock_data($sem);
202             ignore_user_abort(FALSE);
203         }
204         else {
205             log_rd2("TRANS NON ATTIVATO");
206             
207             //       ARRAY_POP DISABLED
208             //       while (array_pop($user->comm) != NULL);
209             //       // $user->step_inc(COMM_N + 1);
210             //       Bin5::save_data($bri);
211             
212             Bin5::unlock_data($sem);
213             ignore_user_abort(FALSE);
214         }
215     }
216     
217     if ($cur_step == -1) {
218         log_rd2("PRE-NEWSTAT.");
219         
220         /***************
221          *             *
222          *    TABLE    *
223          *             *
224          ***************/
225         if ($user->stat == "table") {      
226             $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
227             
228             log_rd2("SENDED TO THE STREAM: ".$ret);
229         }
230         log_rd2("NEWSTAT: ".$user->stat);
231         
232         $new_stat =  $user->stat;
233         $new_subst = $user->subst;
234         $new_step =  $user->step;
235     }
236     else {
237         ignore_user_abort(TRUE);
238         $sem = Bin5::lock_data($table_idx);
239         // if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
240         if (($user = Bin5_user::load_data($table_idx, $proxy_step['i'], $sess)) == FALSE) {
241             Bin5::unlock_data($sem);
242             ignore_user_abort(FALSE);
243             return (unrecerror());
244         }
245         if ($cur_step < $user->step) {
246             do {
247                 if ($cur_step + COMM_N < $user->step) {
248                     if (($cur_stat != $user->stat)) {
249                         $to_stat = $user->stat;
250                         Bin5::unlock_data($sem);
251                         ignore_user_abort(FALSE);
252                         return (page_sync($user->sess, $to_stat == "table" ? "index.php" : "../index.php"));
253                     }
254                     log_rd2("lost history, refresh from scratch");
255                     $new_step = -1;
256                     break;
257                 } 
258                 for ($i = $cur_step ; $i < $user->step ; $i++) {
259                     $ii = $i % COMM_N;
260                     log_wr("TRY RET ".$i."  COMM_N ".COMM_N."  II ".$ii);
261                     $ret .= $user->comm[$ii];
262                 }
263                 $new_stat =  $user->stat;
264                 $new_subst = $user->subst;
265                 $new_step =  $user->step;
266             } while (0);
267             
268             log_mop($user->step, 'bin::index_rd.php: after ret set');
269             
270             if ($user->the_end == TRUE) {
271                 log_rd2("LOGOUT BYE BYE!!");
272                 log_auth($user->sess, "Explicit logout.");
273                 
274                 $S_load_stat['R_the_end']++;
275                 $bri = Bin5::load_data($table_idx, $table_token);
276                 unset($user);
277                 if (($user = $bri->get_user($sess, $idx)) == FALSE) {
278                     Bin5::unlock_data($sem);
279                     ignore_user_abort(FALSE);
280                     return (unrecerror());
281                 }
282                 
283                 $tmp_sess = $user->sess;
284                 $user->sess = "";
285                 step_unproxy($tmp_sess);
286                 $user->name = "";
287                 $user->the_end = FALSE;
288                 
289                 if ($user->subst == 'sitdown')
290                     $bri->room_wakeup($user);
291                 else if ($user->subst == 'standup')
292                     $bri->room_outstandup($user);
293                 else
294                     log_rd2("LOGOUT FROM WHAT ???");
295                 
296                 Bin5::save_data($bri);
297             }
298         }
299         
300         Bin5::unlock_data($sem);
301         ignore_user_abort(FALSE);
302   }
303   
304   
305   return ($ret);
306 }
307
308 /*
309  *  MAIN
310  */
311
312 /*
313    FROM THE EXTERN 
314    sess
315    stat
316    step
317 */
318
319 $is_page_streaming =  (stristr($HTTP_USER_AGENT, "MSIE") || stristr($HTTP_USER_AGENT, "CHROME") ? TRUE : FALSE);
320
321 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
322 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
323 header('Content-type: application/xml; charset="utf-8"',true);
324 // header('Content-type: text/plain; charset="utf-8"',true);
325 // header('Content-type: text/html; charset="utf-8"',true);
326
327 if (!isset($myfrom))
328      $myfrom = "";
329 if (!isset($subst))
330      $subst = "";
331 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming."USER_AGENT:".$HTTP_USER_AGENT);
332
333
334 $endtime = time() + STREAM_TIMEOUT;
335 $old_stat =  $stat;
336 $old_subst = $subst;
337 $old_step =  $ext_step = $step;
338
339 for ($i = 0 ; time() < $endtime ; $i++) {
340   // log_rd("PRE MAIN ".$step);;
341   $pre_main = gettimeofday(TRUE);
342   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step, $table_idx, $table_token)) != FALSE) {
343     echo '@BEGIN@';
344     // 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     echo "$ret";
346     echo ' @END@'; 
347     log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
348     flush();
349     log_mop(0, 'bin::index_rd.php: after flush (begin: '.sprintf("%f", $pre_main).')');
350     if ($is_page_streaming)
351       break;
352   }
353   $old_stat =  $stat;
354   $old_subst = $subst;
355   $old_step =  $step;
356   // log_rd("POST MAIN ".$step);;
357   usleep(400000);
358   if (($i % 10) == 0) {
359     // log_rd2("TIME: ".time());
360     echo '_';
361     flush();
362   }
363  }
364
365 $s = "[".$sess."] briskin5/index_rd.php stats: ";
366 foreach ($S_load_stat as $key => $value) {
367     $s .= sprintf("%s: %d - ", $key, $value);
368 }
369 log_crit($s);
370
371 ?>