fddb5a2de2aedcdb46853e71aa7d7cc0b7be644f
[brisk.git] / web / briskin5 / index_rd.php
1 <?php
2 /*
3  *  brisk - index_rd.php
4  *
5  *  Copyright (C) 2006-2007 matteo.nastasi@milug.org
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details. You should have received a
16  * copy of the GNU General Public License along with this program; if
17  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18  * Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * $Id$
21  *
22  */
23
24 require_once("../Obj/brisk.phh");
25 require_once("Obj/briskin5.phh");
26
27 log_load("LOAD: bin5/index_rd.php ".$QUERY_STRING);
28
29 $first_loop = TRUE;
30 $the_end = FALSE;
31
32 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
33   echo "Debugging time!";
34   exit;
35 }
36
37 function shutta()
38 {
39   log_rd2("bin5 SHUTTA!".connection_status());
40 }
41
42
43 register_shutdown_function(shutta);
44
45 function unrecerror()
46 {
47   GLOBAL $is_page_streaming;
48
49   $is_page_streaming = TRUE;
50   log_rd2("UNREC_ERROR");
51   return (sprintf('the_end=true; window.onunload = null; document.location.assign("../index.php");'));
52 }
53
54 function page_sync($sess, $page)
55 {
56   GLOBAL $is_page_streaming;
57
58   $is_page_streaming = TRUE;
59   log_rd2("PAGE_SYNC");
60   return (sprintf('the_end=true; window.onunload = null; document.location.assign("%s");', $page));
61 }
62
63
64
65
66 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $table_idx, $table_token)
67 {
68   GLOBAL $is_page_streaming, $first_loop;
69   
70   $ret = FALSE;
71   $bri = FALSE;
72
73   // log_rd2("M");
74   /* Sync check (read only without modifications */
75   ignore_user_abort(TRUE);
76   if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { 
77     // Aggiorna l'expire time lato server
78     if  ($first_loop == TRUE) {
79       log_only("F");
80
81       // VERIFICARE TUTTE LE LOAD_DATA E PRENDERE CONTROMISURE NEL CASO FALLISCANO //
82
83       if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) {
84         Briskin5::unlock_data($sem);
85         ignore_user_abort(FALSE);
86         return (unrecerror());
87       }
88       if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
89         Briskin5::unlock_data($sem);
90         ignore_user_abort(FALSE);
91         return (unrecerror());
92       }
93       log_auth($sess, "bin5::update lacc");
94       $user->lacc = time();
95
96       $bri->garbage_manager(FALSE);
97       
98       Briskin5::save_data($bri);
99       $first_loop = FALSE;
100     }
101
102     log_lock("U");
103     Briskin5::unlock_data($sem);
104     ignore_user_abort(FALSE);
105   }
106   else {
107     return (FALSE);
108   }
109   
110   if (($proxy_step = step_get($sess)) != FALSE) {
111     // log_rd2("Postget".$proxy_step."zizi");
112     
113     if ($cur_step == $proxy_step) {
114       log_lock("P");
115       return (FALSE);
116     }
117     else {
118       log_only2("R");
119     }
120   }
121   else {
122     log_only2("R");
123   }
124   
125   if ($bri == FALSE) {
126     do {
127       ignore_user_abort(TRUE);
128       if (($sem = Briskin5::lock_data($table_idx)) == FALSE) 
129         break;
130       
131       log_lock("P");
132       if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) 
133         break;
134     } while (0);
135     
136     if ($sem != FALSE)
137       Briskin5::unlock_data($sem);
138     
139     ignore_user_abort(FALSE);
140     if ($bri == FALSE) 
141       return (unrecerror());
142   }
143   
144   if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
145     return (unrecerror());
146   }
147
148   /* Nothing changed, return. */
149   if ($cur_step == $user->step) 
150     return;
151
152   log_rd2("do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
153
154   if ($cur_step == -1) {
155     // FUNZIONE from_scratch DA QUI 
156     ignore_user_abort(TRUE);
157     $sem = Briskin5::lock_data($table_idx);
158     $bri = &Briskin5::load_data($table_idx, $table_token);
159     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
160       Briskin5::unlock_data($sem);
161       ignore_user_abort(FALSE);
162       return (unrecerror());
163     }
164     if ($user->the_end) 
165       $is_page_streaming = TRUE;
166
167
168     if ($user->trans_step != -1) {
169       log_rd2("TRANS USATO ".$user->trans_step);
170       $cur_step = $user->trans_step;
171       $user->trans_step = -1;
172
173
174       Briskin5::save_data($bri);
175       Briskin5::unlock_data($sem);
176       ignore_user_abort(FALSE);
177     }
178     else {
179       log_rd2("TRANS NON ATTIVATO");
180       Briskin5::unlock_data($sem);
181       ignore_user_abort(FALSE);
182     }
183   }
184       
185   if ($cur_step == -1) {
186     log_rd2("PRE-NEWSTAT.");
187
188     /***************
189      *             *
190      *    TABLE    *
191      *             *
192      ***************/
193     if ($user->stat == "table") {      
194       $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
195
196       log_rd2("SENDED TO THE STREAM: ".$ret);
197     }
198     log_rd2("NEWSTAT: ".$user->stat);
199
200     $new_stat =  $user->stat;
201     $new_subst = $user->subst;
202     $new_step =  $user->step;
203   }
204   else {
205     ignore_user_abort(TRUE);
206     $sem = Briskin5::lock_data($table_idx);
207     if (($bri = &Briskin5::load_data($table_idx, $table_token)) == FALSE) {
208       Briskin5::unlock_data($sem);
209       ignore_user_abort(FALSE);
210       return (unrecerror());
211     }
212     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
213       Briskin5::unlock_data($sem);
214       ignore_user_abort(FALSE);
215       return (unrecerror());
216     }
217     if ($cur_step < $user->step) {
218       do {
219         if ($cur_step + COMM_N < $user->step) {
220           if (($cur_stat != $user->stat)) {
221             $to_stat = $user->stat;
222             Briskin5::unlock_data($sem);
223             ignore_user_abort(FALSE);
224             return (page_sync($user->sess, $to_stat == "table" ? "index.php" : "../index.php"));
225           }
226           log_rd2("lost history, refresh from scratch");
227           $new_step = -1;
228           break;
229         } 
230         for ($i = $cur_step ; $i < $user->step ; $i++) {
231           $ii = $i % COMM_N;
232           log_wr("TRY RET ".$i."  COMM_N ".COMM_N."  II ".$ii);
233           $ret .= $user->comm[$ii];
234         }
235         $new_stat =  $user->stat;
236         $new_subst = $user->subst;
237         $new_step =  $user->step;
238       } while (0);
239       
240       if ($user->the_end == TRUE) {
241         log_rd2("LOGOUT BYE BYE!!");
242         log_auth($user->sess, "Explicit logout.");
243         $tmp_sess = $user->sess;
244         $user->sess = "";
245         step_unproxy($tmp_sess);
246         
247         $user->name = "";
248         $user->the_end = FALSE;
249         
250         if ($user->subst == 'sitdown')
251           $bri->room_wakeup(&$user);
252         else if ($user->subst == 'standup')
253           $bri->room_outstandup(&$user);
254         else
255           log_rd2("LOGOUT FROM WHAT ???");
256           
257         Briskin5::save_data($bri);
258       }
259     }
260           
261     Briskin5::unlock_data($sem);
262     ignore_user_abort(FALSE);
263   }
264
265   
266   return ($ret);
267 }
268
269 /*
270  *  MAIN
271  */
272
273 /*
274    FROM THE EXTERN 
275    sess
276    stat
277    step
278 */
279
280 $is_page_streaming =  ((stristr($HTTP_USER_AGENT, "linux") && 
281                         (stristr($HTTP_USER_AGENT, "firefox") || stristr($HTTP_USER_AGENT, "iceweasel"))) ? FALSE : TRUE);
282
283
284 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
285 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
286
287 if (!isset($myfrom))
288      $myfrom = "";
289 if (!isset($subst))
290      $subst = "";
291 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming."USER_AGENT:".$HTTP_USER_AGENT);
292
293
294 $endtime = time() + STREAM_TIMEOUT;
295 $old_stat =  $stat;
296 $old_subst = $subst;
297 $old_step =  $ext_step = $step;
298
299 for ($i = 0 ; time() < $endtime ; $i++) {
300   // log_rd("PRE MAIN ".$step);;
301   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step, $table_idx, $table_token)) != FALSE) {
302     echo '@BEGIN@';
303     // 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));
304     echo "$ret";
305     echo ' @END@'; 
306     log_send("EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
307     flush();
308     if ($is_page_streaming)
309       break;
310   }
311   $old_stat =  $stat;
312   $old_subst = $subst;
313   $old_step =  $step;
314   // log_rd("POST MAIN ".$step);;
315   usleep(400000);
316   if (($i % 5) == 0) {
317     // log_rd2("TIME: ".time());
318     echo '_';
319     flush();
320   }
321 }
322
323 ?>