aggiunto page_sync per blackout di rete lunghi e cambi pagina nel mentre
[brisk.git] / web / index_rd.php
1 <?php
2 /*
3  *  brisk - index_rd.php
4  *
5  *  Copyright (C) 2006 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   return (sprintf('the_end=true; window.onunload = null; document.location.assign("index.php");'));
50 }
51
52 function page_sync($page)
53 {
54   GLOBAL $is_page_streaming;
55
56   $is_page_streaming = TRUE;
57   return (sprintf('the_end=true; window.onunload = null; document.location.assign("%s");', $page));
58 }
59
60
61
62
63 function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step)
64 {
65   GLOBAL $is_page_streaming, $first_loop;
66   
67   $ret = FALSE;
68   
69   
70   /* Sync check (read only without modifications */
71   ignore_user_abort(TRUE);
72   if (($sem = lock_data()) != FALSE) { 
73     $bri = &load_data();
74     // Aggiorna l'expire time lato server
75     if  ($first_loop == TRUE) {
76       if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
77         unlock_data($sem);
78         ignore_user_abort(FALSE);
79         return (unrecerror());
80       }
81       log_auth($sess, "update lacc");
82       $user->lacc = time();
83
84       $bri->garbage_manager(FALSE);
85       
86       save_data($bri);
87       $first_loop = FALSE;
88     }
89     unlock_data($sem);
90     ignore_user_abort(FALSE);
91   }
92   else {
93     return (FALSE);
94   }
95     
96   if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
97     return (unrecerror());
98   }
99
100   /* Nothing changed, return. */
101   if ($cur_stat == $user->stat && $cur_step == $user->step) 
102     return;
103
104   log_rd2($sess, "do other ++".$cur_stat."++".$user->stat."++".$cur_step."++".$user->step);
105
106   if ($cur_step == -1) {
107     // FUNZIONE from_scratch DA QUI 
108     ignore_user_abort(TRUE);
109     $sem = lock_data();
110     $bri = &load_data();
111     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
112       unlock_data($sem);
113       ignore_user_abort(FALSE);
114       return (unrecerror());
115     }
116     if ($user->the_end) 
117       $is_page_streaming = TRUE;
118
119
120     if ($user->trans_step != -1) {
121       log_rd2($sess, "TRANS USATO ".$user->trans_step);
122       $cur_step = $user->trans_step;
123       $user->trans_step = -1;
124
125
126       save_data($bri);
127       unlock_data($sem);
128       ignore_user_abort(FALSE);
129     }
130     else {
131       log_rd2($sess, "TRANS NON ATTIVATO");
132       unlock_data($sem);
133       ignore_user_abort(FALSE);
134     }
135   }
136       
137   if ($cur_step == -1) {
138     log_rd2($sess, "PRE-NEWSTAT.");
139
140     if ($user->stat == 'room') {
141       log_rd($sess, "roomma");
142       $ret .= show_room(&$bri, &$user);
143     }
144     /***************
145      *             *
146      *    TABLE    *
147      *             *
148      ***************/
149     else if ($user->stat == 'table') {      
150       $ret = show_table(&$bri,&$user,$user->step,FALSE,FALSE);
151
152       log_rd2($sess, "SENDED TO THE STREAM: ".$ret);
153     }
154     log_rd2($sess, "NEWSTAT: ".$user->stat);
155
156     $new_stat =  $user->stat;
157     $new_subst = $user->subst;
158     $new_step =  $user->step;
159   }
160   else {
161     ignore_user_abort(TRUE);
162     $sem = lock_data();
163     $bri = &load_data();
164     if (($user = &$bri->get_user($sess, $idx)) == FALSE) {
165       unlock_data($sem);
166       ignore_user_abort(FALSE);
167       return (unrecerror());
168     }
169     if ($cur_step < $user->step) {
170       do {
171         if ($cur_step + COMM_N < $user->step) {
172           if (($cur_stat != $user->stat)) {
173             $to_stat = $user->stat;
174             unlock_data($sem);
175             ignore_user_abort(FALSE);
176             return (page_sync($to_stat == "table" ? "table.php" : "index.php"));
177           }
178           log_rd2($sess, "lost history, refresh from scratch");
179           $new_step = -1;
180           break;
181         } 
182         for ($i = $cur_step ; $i < $user->step ; $i++) {
183           log_rd2($sess, "ADDED TO THE STREAM: ".$user->comm[$i % COMM_N]);
184           $ret .= $user->comm[$i % COMM_N];
185         }
186         $new_stat =  $user->stat;
187         $new_subst = $user->subst;
188         $new_step =  $user->step;
189       } while (0);
190       
191       if ($user->the_end == TRUE) {
192         log_rd2($sess, "LOGOUT BYE BYE!!");
193         log_auth($user->sess, "Explicit logout.");
194         $user->sess = "";
195         $user->name = "";
196         $user->the_end = FALSE;
197         
198         if ($user->subst == 'sitdown')
199           $bri->room_wakeup(&$user);
200         else if ($user->subst == 'standup')
201           $bri->room_outstandup(&$user);
202         else
203           log_rd2($sess, "LOGOUT FROM WHAT ???");
204           
205         save_data($bri);
206       }
207     }
208           
209     unlock_data($sem);
210     ignore_user_abort(FALSE);
211   }
212
213   
214   return ($ret);
215 }
216
217 /*
218  *  MAIN
219  */
220
221 /*
222    FROM THE EXTERN 
223    sess
224    stat
225    step
226 */
227
228 $is_page_streaming =  ((stristr($HTTP_USER_AGENT, "linux") && 
229                         stristr($HTTP_USER_AGENT, "firefox")) ? FALSE : TRUE);
230
231
232 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
233 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
234
235 log_rd2($sess, "FROM OUTSIDE - STAT: ".$stat." SUBST: ".$subst." STEP: ".$step." MYFROM: ".$myfrom. "IS_PAGE:" . $is_page_streaming);
236
237
238 $endtime = time() + STREAM_TIMEOUT;
239 $old_stat =  $stat;
240 $old_subst = $subst;
241 $old_step =  $ext_step = $step;
242
243 for ($i = 0 ; time() < $endtime ; $i++) {
244   // log_rd($sess, "PRE MAIN ".$step);;
245   if (($ret = maincheck($sess, $old_stat, $old_subst, $old_step, &$stat, &$subst, &$step)) != FALSE) {
246     echo '@BEGIN@';
247     // 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));
248     echo "$ret";
249     echo ' @END@'; 
250     log_send($sess, "EXT_STEP: ".$ext_step." ENDTIME: [".$endtime."] ".$ret);
251     flush();
252     if ($is_page_streaming)
253       break;
254   }
255   $old_stat =  $stat;
256   $old_subst = $subst;
257   $old_step =  $step;
258   // log_rd($sess, "POST MAIN ".$step);;
259   usleep(200000);
260   if (($i % 5) == 0) {
261     // log_rd2($sess, "TIME: ".time());
262     echo '_';
263     flush();
264   }
265 }
266
267 ?>