merge with spawn branch
[brisk.git] / web / index_rd.php
1 <?php
2 /*
3  *  brisk - index_rd.php
4  *
5  *  Copyright (C) 2006-2008 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * $Id$
24  *
25  */
26
27 require_once("Obj/brisk.phh");
28 // require_once("Obj/proxyscan.phh");
29 require_once("briskin5/Obj/briskin5.phh");
30
31 // Use of proxies isn't allowed.
32 // if (is_proxy()) {
33 //   sleep(5);
34 //   exit;
35 //}
36 log_load("index_rd.php");
37
38 $first_loop = TRUE;
39 $the_end = FALSE;
40
41 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
42   echo "Debugging time!";
43   exit;
44 }
45
46 function shutta()
47 {
48   log_rd2("SHUTTA!".connection_status());
49 }
50
51
52 register_shutdown_function(shutta);
53
54 function unrecerror()
55 {
56   GLOBAL $is_page_streaming;
57
58   $is_page_streaming = TRUE;
59   log_rd2("UNREC_ERROR:".var_export(debug_backtrace()));
60   return (sprintf('the_end=true; window.onunload = null; document.location.assign("index.php");'));
61 }
62
63 function page_sync($sess, $page, $table_idx, $table_token)
64 {
65   GLOBAL $is_page_streaming;
66
67   log_rd2("page_sync:".var_export(debug_backtrace()));
68
69   $is_page_streaming = TRUE;
70
71   log_rd2("PAGE_SYNC");
72   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));
73 }
74
75
76
77
78 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
79 {
80   GLOBAL $is_page_streaming, $first_loop;
81   
82   $ret = FALSE;
83   $room = FALSE;
84
85   // log_rd2("M");
86   /* Sync check (read only without modifications */
87   ignore_user_abort(TRUE);
88   if (($sem = Room::lock_data()) != FALSE) { 
89     // Aggiorna l'expire time lato server
90     if  ($first_loop == TRUE) {
91       log_only("F");
92       $room = &Room::load_data();
93       if (($user = &$room->get_user($sess, $idx)) == FALSE) {
94         Room::unlock_data($sem);
95         ignore_user_abort(FALSE);
96         return (unrecerror());
97       }
98       log_auth($sess, "update lacc");
99       $user->lacc = time();
100
101       log_main("pre garbage_manager TRE");
102       $room->garbage_manager(FALSE);
103       
104       Room::save_data($room);
105       $first_loop = FALSE;
106     }
107
108     log_lock("U");
109     Room::unlock_data($sem);
110     ignore_user_abort(FALSE);
111   }
112   else {
113     return (FALSE);
114   }
115     
116   if (($proxy_step = step_get($sess)) != FALSE) {
117     // log_rd2("Postget".$proxy_step."zizi");
118
119     if ($cur_step == $proxy_step) {
120       log_lock("P");
121       return (FALSE);
122     }
123     else {
124       log_only2("R");
125     }
126   }
127   else {
128       log_only2("R");
129   }
130
131   if ($room == FALSE) {
132     do {
133       ignore_user_abort(TRUE);
134       if (($sem = Room::lock_data()) == FALSE) 
135         break;
136       
137       log_lock("P");
138       if (($room = &Room::load_data()) == FALSE) 
139         break;
140     } while (0);
141     
142     if ($sem != FALSE)
143       Room::unlock_data($sem);
144     
145     ignore_user_abort(FALSE);
146     if ($room == FALSE) 
147       return (FALSE);
148   }
149   
150   if (($user = &$room->get_user($sess, $idx)) == FALSE) {
151     return (unrecerror());
152   }
153
154   /* Nothing changed, return. */
155   if ($cur_step == $user->step) 
156     return (FALSE);
157
158   log_rd2("do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
159
160   if ($cur_step == -1) {
161     // FUNZIONE from_scratch DA QUI 
162     ignore_user_abort(TRUE);
163     $sem = Room::lock_data();
164     $room = &Room::load_data();
165     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
166       Room::unlock_data($sem);
167       ignore_user_abort(FALSE);
168       return (unrecerror());
169     }
170     if ($user->the_end) { 
171       log_rd2("main_check: the end".var_export(debug_backtrace()));
172       $is_page_streaming = TRUE;
173     }
174
175     if ($user->trans_step != -1) {
176       log_rd2("TRANS USATO ".$user->trans_step);
177       $cur_step = $user->trans_step;
178       $user->trans_step = -1;
179
180
181       Room::save_data($room);
182       Room::unlock_data($sem);
183       ignore_user_abort(FALSE);
184     }
185     else {
186        log_rd2("TRANS NON ATTIVATO");
187 //        ARRAY_POP DISABLED
188 //        log_rd2("TRANS NON ATTIVATO, clean del comm array");
189 //        while (($el = array_pop($user->comm)) != NULL) { 
190 //          log_rd2("clean element [".$el."]");
191 //        }
192 //        //        $user->step_inc(COMM_N + 1);
193 //        Room::save_data($room);
194 //        //        $new_step = $user->step;
195          
196        Room::unlock_data($sem);
197        ignore_user_abort(FALSE);
198     }
199   }
200       
201   if ($cur_step == -1) {
202     log_rd2("PRE-NEWSTAT: ".$user->stat);
203
204     if ($user->stat == 'room') {
205       log_rd("roomma ".$user->step);
206       $ret .= $room->show_room($user->step, &$user);
207
208       // TODO uncomment and test
209       /* NOTE the sets went common */
210       $new_stat =  $user->stat;
211       $new_subst = $user->subst;
212       $new_step =  $user->step;
213     }
214     /***************
215      *             *
216      *    TABLE    *
217      *             *
218      ***************/
219     else if ($user->stat == 'table') {
220       log_load("RESYNC");
221       return (page_sync($user->sess, "briskin5/index.php", $user->table, $user->table_token));
222     }
223     log_rd2("NEWSTAT: ".$user->stat);
224   }
225   else {
226     ignore_user_abort(TRUE);
227     $sem = Room::lock_data();
228     $room = &Room::load_data();
229     if (($user = &$room->get_user($sess, $idx)) == FALSE) {
230       Room::unlock_data($sem);
231       ignore_user_abort(FALSE);
232       return (unrecerror());
233     }
234     if ($cur_step < $user->step) {
235       do {
236         if ($cur_step + COMM_N < $user->step) {
237           if (($cur_stat != $user->stat)) {
238             $to_stat = $user->stat;
239             Room::unlock_data($sem);
240             ignore_user_abort(FALSE);
241             log_load("RESYNC");
242             return (page_sync($user->sess, ($to_stat == "table" ? "briskin5/index.php" : "index.php"), $user->table, $user->table_token));
243           }
244           log_rd2("lost history, refresh from scratch");
245           $new_step = -1;
246           break;
247         } 
248         for ($i = $cur_step ; $i < $user->step ; $i++) {
249           log_rd2("ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
250           $ret .= $user->comm[$i % COMM_N];
251         }
252         $new_stat =  $user->stat;
253         $new_subst = $user->subst;
254         $new_step =  $user->step;
255       } while (0);
256       
257       if ($user->the_end == TRUE) {
258         log_rd2("LOGOUT BYE BYE!!");
259         log_auth($user->sess, "Explicit logout.");
260
261         $user->reset();
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 header('Content-type: application/xml; charset="utf-8"',true);
302 // header('Content-type: text/plain; charset="utf-8"',true);
303 // header('Content-type: text/html; charset="utf-8"',true);
304
305 if (!isset($myfrom))
306      $myfrom = "";
307 if (!isset($subst))
308      $subst = "";
309 log_rd2("FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
310
311
312 $endtime = time() + STREAM_TIMEOUT;
313 $old_stat =  $stat;
314 $old_subst = $subst;
315 $old_step =  $ext_step = $step;
316
317 for ($i = 0 ; time() < $endtime ; $i++) {
318   // log_rd("PRE MAIN ".$step);;
319   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
320     echo '@BEGIN@';
321     // 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));
322     echo "$ret";
323     echo ' @END@'; 
324     log_send("IS_PAGE: ".($is_page_streaming == TRUE ? "TRUE" : "FALSE")."EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
325     flush();
326     if ($is_page_streaming)
327       break;
328   }
329   $old_stat =  $stat;
330   $old_subst = $subst;
331   $old_step =  $step;
332   // log_rd("POST MAIN ".$step);;
333   usleep(400000);
334   if (($i % 5) == 0) {
335     // log_rd2("TIME: ".time());
336     echo '_';
337     flush();
338   }
339 }
340
341 ?>