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