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