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