corretta serializzazione all'accesso della shmem
[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("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   $bri = FALSE;
71
72   log_rd2($sess, "M");
73   /* Sync check (read only without modifications */
74   ignore_user_abort(TRUE);
75   if (($sem = lock_data()) != FALSE) { 
76     // Aggiorna l'expire time lato server
77     if  ($first_loop == TRUE) {
78       log_only($sess, "F");
79       $bri = &load_data();
80       if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
81         unlock_data($sem);
82         ignore_user_abort(FALSE);
83         return (unrecerror());
84       }
85       log_auth($sess, "update lacc");
86       $user->lacc = time();
87
88       $bri->garbage_manager(FALSE);
89       
90       save_data($bri);
91       $first_loop = FALSE;
92     }
93
94     log_only($sess, "U");
95     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_rd2($sess, "P");
107       return (FALSE);
108     }
109   }
110
111   if ($bri == FALSE) {
112     ignore_user_abort(TRUE);
113     if (($sem = lock_data()) != FALSE) { 
114       log_only($sess, "P");
115       $bri = &load_data();
116       unlock_data($sem);
117       ignore_user_abort(FALSE);
118     }
119     else {
120       return (FALSE);
121     }
122   }
123   
124   if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
125     return (unrecerror());
126   }
127
128   /* Nothing changed, return. */
129   if ($cur_step == $user->step) 
130     return;
131
132   log_rd2($sess, "do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
133
134   if ($cur_step == -1) {
135     // FUNZIONE from_scratch DA QUI 
136     ignore_user_abort(TRUE);
137     $sem = lock_data();
138     $bri = &load_data();
139     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
140       unlock_data($sem);
141       ignore_user_abort(FALSE);
142       return (unrecerror());
143     }
144     if ($user->the_end) 
145       $is_page_streaming = TRUE;
146
147
148     if ($user->trans_step != -1) {
149       log_rd2($sess, "TRANS USATO ".$user->trans_step);
150       $cur_step = $user->trans_step;
151       $user->trans_step = -1;
152
153
154       save_data($bri);
155       unlock_data($sem);
156       ignore_user_abort(FALSE);
157     }
158     else {
159       log_rd2($sess, "TRANS NON ATTIVATO");
160       unlock_data($sem);
161       ignore_user_abort(FALSE);
162     }
163   }
164       
165   if ($cur_step == -1) {
166     log_rd2($sess, "PRE-NEWSTAT.");
167
168     if ($user->stat == 'room') {
169       log_rd($sess, "roomma");
170       $ret .= show_room(&$bri, &$user);
171     }
172     /***************
173      *             *
174      *    TABLE    *
175      *             *
176      ***************/
177     else if ($user->stat == 'table') {      
178       $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
179
180       log_rd2($sess, "SENDED TO THE STREAM: ".$ret);
181     }
182     log_rd2($sess, "NEWSTAT: ".$user->stat);
183
184     $new_stat =  $user->stat;
185     $new_subst = $user->subst;
186     $new_step =  $user->step;
187   }
188   else {
189     ignore_user_abort(TRUE);
190     $sem = lock_data();
191     $bri = &load_data();
192     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
193       unlock_data($sem);
194       ignore_user_abort(FALSE);
195       return (unrecerror());
196     }
197     if ($cur_step < $user->step) {
198       do {
199         if ($cur_step + COMM_N < $user->step) {
200           if (($cur_stat != $user->stat)) {
201             $to_stat = $user->stat;
202             unlock_data($sem);
203             ignore_user_abort(FALSE);
204             return (page_sync($user->sess, $to_stat == "table" ? "table.php" : "index.php"));
205           }
206           log_rd2($sess, "lost history, refresh from scratch");
207           $new_step = -1;
208           break;
209         } 
210         for ($i = $cur_step ; $i < $user->step ; $i++) {
211           log_rd2($sess, "ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
212           $ret .= $user->comm[$i % COMM_N];
213         }
214         $new_stat =  $user->stat;
215         $new_subst = $user->subst;
216         $new_step =  $user->step;
217       } while (0);
218       
219       if ($user->the_end == TRUE) {
220         log_rd2($sess, "LOGOUT BYE BYE!!");
221         log_auth($user->sess, "Explicit logout.");
222         $tmp_sess = $user->sess;
223         $user->sess = "";
224         step_unproxy($tmp_sess);
225         
226         $user->name = "";
227         $user->the_end = FALSE;
228         
229         if ($user->subst == 'sitdown')
230           $bri->room_wakeup(&$user);
231         else if ($user->subst == 'standup')
232           $bri->room_outstandup(&$user);
233         else
234           log_rd2($sess, "LOGOUT FROM WHAT ???");
235           
236         save_data($bri);
237       }
238     }
239           
240     unlock_data($sem);
241     ignore_user_abort(FALSE);
242   }
243
244   
245   return ($ret);
246 }
247
248 /*
249  *  MAIN
250  */
251
252 /*
253    FROM THE EXTERN 
254    sess
255    stat
256    step
257 */
258
259 $is_page_streaming =  ((stristr($HTTP_USER_AGENT, "linux") && 
260                         stristr($HTTP_USER_AGENT, "firefox")) ? FALSE : TRUE);
261
262
263 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
264 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
265
266 if (!isset($myfrom))
267      $myfrom = "";
268 if (!isset($subst))
269      $subst = "";
270 log_rd2($sess, "FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
271
272
273 $endtime = time() + STREAM_TIMEOUT;
274 $old_stat =  $stat;
275 $old_subst = $subst;
276 $old_step =  $ext_step = $step;
277
278 for ($i = 0 ; time() < $endtime ; $i++) {
279   // log_rd($sess, "PRE MAIN ".$step);;
280   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
281     echo '@BEGIN@';
282     // 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));
283     echo "$ret";
284     echo ' @END@'; 
285     log_send($sess, "EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
286     flush();
287     if ($is_page_streaming)
288       break;
289   }
290   $old_stat =  $stat;
291   $old_subst = $subst;
292   $old_step =  $step;
293   // log_rd($sess, "POST MAIN ".$step);;
294   usleep(200000);
295   if (($i % 10) == 0) {
296     // log_rd2($sess, "TIME: ".time());
297     echo '_';
298     flush();
299   }
300 }
301
302 ?>