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