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