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