cc30e3ec5dbc39cf3475b1ce2077150c73e75610
[brisk.git] / web / 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("briskin5/Obj/briskin5.phh");
26
27 log_load("index_rd.php");
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("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:".var_export(debug_backtrace()));
51   return (sprintf('the_end=true; window.onunload = null; document.location.assign("index.php");'));
52 }
53
54 function page_sync($sess, $page, $table_idx, $table_token)
55 {
56   GLOBAL $is_page_streaming;
57
58   log_rd2("page_sync:".var_export(debug_backtrace()));
59
60   $is_page_streaming = TRUE;
61
62   log_rd2("PAGE_SYNC");
63   return (sprintf('createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); the_end=true; window.onunload = null; document.location.assign("%s");', $table_idx, $table_token, $page));
64 }
65
66
67
68
69 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
70 {
71   GLOBAL $is_page_streaming, $first_loop;
72   
73   $ret = FALSE;
74   $room = FALSE;
75
76   // log_rd2("M");
77   /* Sync check (read only without modifications */
78   ignore_user_abort(TRUE);
79   if (($sem = Room::lock_data()) != FALSE) { 
80     // Aggiorna l'expire time lato server
81     if  ($first_loop == TRUE) {
82       log_only("F");
83       $room = &Room::load_data();
84       if (($user = &$room->get_user($sess, $idx)) == FALSE) {
85         Room::unlock_data($sem);
86         ignore_user_abort(FALSE);
87         return (unrecerror());
88       }
89       log_auth($sess, "update lacc");
90       $user->lacc = time();
91
92       log_main("pre garbage_manager TRE");
93       $room->garbage_manager(FALSE);
94       
95       Room::save_data($room);
96       $first_loop = FALSE;
97     }
98
99     log_lock("U");
100     Room::unlock_data($sem);
101     ignore_user_abort(FALSE);
102   }
103   else {
104     return (FALSE);
105   }
106     
107   if (($proxy_step = step_get($sess)) != FALSE) {
108     // log_rd2("Postget".$proxy_step."zizi");
109
110     if ($cur_step == $proxy_step) {
111       log_lock("P");
112       return (FALSE);
113     }
114     else {
115       log_only2("R");
116     }
117   }
118   else {
119       log_only2("R");
120   }
121
122   if ($room == FALSE) {
123     do {
124       ignore_user_abort(TRUE);
125       if (($sem = Room::lock_data()) == FALSE) 
126         break;
127       
128       log_lock("P");
129       if (($room = &Room::load_data()) == FALSE) 
130         break;
131     } while (0);
132     
133     if ($sem != FALSE)
134       Room::unlock_data($sem);
135     
136     ignore_user_abort(FALSE);
137     if ($room == FALSE) 
138       return (FALSE);
139   }
140   
141   if (($user = &$room->get_user($sess, $idx)) == FALSE) {
142     return (unrecerror());
143   }
144
145   /* Nothing changed, return. */
146   if ($cur_step == $user->step) 
147     return (FALSE);
148
149   log_rd2("do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
150
151   if ($cur_step == -1) {
152     // FUNZIONE from_scratch DA QUI 
153     ignore_user_abort(TRUE);
154     $sem = Room::lock_data();
155     $room = &Room::load_data();
156     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
157       Room::unlock_data($sem);
158       ignore_user_abort(FALSE);
159       return (unrecerror());
160     }
161     if ($user->the_end) { 
162       log_rd2("main_check: the end".var_export(debug_backtrace()));
163       $is_page_streaming = TRUE;
164     }
165
166     if ($user->trans_step != -1) {
167       log_rd2("TRANS USATO ".$user->trans_step);
168       $cur_step = $user->trans_step;
169       $user->trans_step = -1;
170
171
172       Room::save_data($room);
173       Room::unlock_data($sem);
174       ignore_user_abort(FALSE);
175     }
176     else {
177        log_rd2("TRANS NON ATTIVATO");
178 //        ARRAY_POP DISABLED
179 //        log_rd2("TRANS NON ATTIVATO, clean del comm array");
180 //        while (($el = array_pop($user->comm)) != NULL) { 
181 //          log_rd2("clean element [".$el."]");
182 //        }
183 //        //        $user->step_inc(COMM_N + 1);
184 //        Room::save_data($room);
185 //        //        $new_step = $user->step;
186          
187        Room::unlock_data($sem);
188        ignore_user_abort(FALSE);
189     }
190   }
191       
192   if ($cur_step == -1) {
193     log_rd2("PRE-NEWSTAT: ".$user->stat);
194
195     if ($user->stat == 'room') {
196       log_rd("roomma ".$user->step);
197       $ret .= show_room(&$room, $user->step, &$user);
198
199       // TODO uncomment and test
200       /* NOTE the sets went common */
201       $new_stat =  $user->stat;
202       $new_subst = $user->subst;
203       $new_step =  $user->step;
204     }
205     /***************
206      *             *
207      *    TABLE    *
208      *             *
209      ***************/
210     else if ($user->stat == 'table') {
211       log_load("RESYNC");
212       return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
213     }
214     log_rd2("NEWSTAT: ".$user->stat);
215   }
216   else {
217     ignore_user_abort(TRUE);
218     $sem = Room::lock_data();
219     $room = &Room::load_data();
220     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
221       Room::unlock_data($sem);
222       ignore_user_abort(FALSE);
223       return (unrecerror());
224     }
225     if ($cur_step < $user->step) {
226       do {
227         if ($cur_step + COMM_N < $user->step) {
228           if (($cur_stat != $user->stat)) {
229             $to_stat = $user->stat;
230             Room::unlock_data($sem);
231             ignore_user_abort(FALSE);
232             log_load("RESYNC");
233             return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
234           }
235           log_rd2("lost history, refresh from scratch");
236           $new_step = -1;
237           break;
238         } 
239         for ($i = $cur_step ; $i < $user->step ; $i++) {
240           log_rd2("ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
241           $ret .= $user->comm[$i % COMM_N];
242         }
243         $new_stat =  $user->stat;
244         $new_subst = $user->subst;
245         $new_step =  $user->step;
246       } while (0);
247       
248       if ($user->the_end == TRUE) {
249         log_rd2("LOGOUT BYE BYE!!");
250         log_auth($user->sess, "Explicit logout.");
251
252         $user->reset();
253         /* factorized with ->reset()
254         $tmp_sess = $user->sess;
255         $user->sess = "";
256         step_unproxy($tmp_sess);
257         $user->name = "";
258         while (array_pop($user->comm) != NULL); 
259         $user->step = 0;
260         $user->the_end = FALSE;
261         */
262
263         if ($user->subst == 'sitdown') {
264           log_load("ROOM WAKEUP");
265           $room->room_wakeup(&$user);
266         }
267         else if ($user->subst == 'standup')
268           $room->room_outstandup(&$user);
269         else
270           log_rd2("LOGOUT FROM WHAT ???");
271           
272         Room::save_data($room);
273       }
274     }
275           
276     Room::unlock_data($sem);
277     ignore_user_abort(FALSE);
278   }
279
280   
281   return ($ret);
282 }
283
284 /*
285  *  MAIN
286  */
287
288 /*
289    FROM THE EXTERN 
290    sess
291    stat
292    step
293 */
294
295 $is_page_streaming =  ((stristr($HTTP_USER_AGENT, "linux") && 
296                         (stristr($HTTP_USER_AGENT, "firefox") || stristr($HTTP_USER_AGENT, "iceweasel"))) ? FALSE : TRUE);
297
298
299 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
300 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
301
302 if (!isset($myfrom))
303      $myfrom = "";
304 if (!isset($subst))
305      $subst = "";
306 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
307
308
309 $endtime = time() + STREAM_TIMEOUT;
310 $old_stat =  $stat;
311 $old_subst = $subst;
312 $old_step =  $ext_step = $step;
313
314 for ($i = 0 ; time() < $endtime ; $i++) {
315   // log_rd("PRE MAIN ".$step);;
316   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
317     echo '@BEGIN@';
318     // 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));
319     echo "$ret";
320     echo ' @END@'; 
321     log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")."EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
322     flush();
323     if ($is_page_streaming)
324       break;
325   }
326   $old_stat =  $stat;
327   $old_subst = $subst;
328   $old_step =  $step;
329   // log_rd("POST MAIN ".$step);;
330   usleep(200000);
331   if (($i % 10) == 0) {
332     // log_rd2("TIME: ".time());
333     echo '_';
334     flush();
335   }
336 }
337
338 ?>