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