define constant with enclosed into single quotes
[brisk.git] / web / spush / brisk-spush.php
1 #!/usr/bin/php
2 <?php
3 /*
4  *  brisk - spush/brisk-spush.php
5  *
6  *  Copyright (C) 2012 Matteo Nastasi
7  *                          mailto: nastasi@alternativeoutput.it 
8  *                                  matteo.nastasi@milug.org
9  *                          web: http://www.alternativeoutput.it
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details. You should have received a
20  * copy of the GNU General Public License along with this program; if
21  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
22  * Suite 330, Boston, MA 02111-1307, USA.
23  *
24  */
25
26 $G_base = "../";
27
28 require_once("./brisk-spush.phh");
29 require_once("../Obj/brisk.phh");
30 // require_once("../Obj/proxyscan.phh");
31 require_once("./sac-a-push.phh");
32
33 define('SITE_PREFIX', '/brisk/');
34
35 class SPUser {
36     var $id;
37     var $sess;
38     var $cnt;
39     var $sock;
40     
41     function SPUser($id)
42     {
43         $this->id = $id;
44         $this->cnt = -1;
45         $this->sock = NULL;
46     }
47
48     function enable($sock, $sess)
49     {
50         $this->sess = $sess;
51         $this->cnt = 0;
52         $this->sock = $sock;
53
54         return ($this->id);
55     }
56
57     function disable()
58     {
59         $this->cnt = -1;
60         $this->sock = NULL;
61     }
62
63     function is_enable()
64     {
65         return ($this->cnt < 0 ? FALSE : TRUE);
66     }
67
68     function sock_get()
69     {
70         return $this->sock;
71     }
72
73     function sock_set($sock)
74     {
75         $this->sock = $sock;
76     }
77
78     function id_get()
79     {
80         return $this->id;
81     }
82     
83     function sess_get()
84     {
85         return $this->sess;
86     }
87
88     function cnt_get()
89     {
90         return $this->cnt;
91     }
92
93     function cnt_inc()
94     {
95         return $this->cnt++;
96     }
97 }
98
99 function user_get_free($user_arr)
100 {
101     foreach ($user_arr as $i => $user) {
102         if (!$user->is_enable()) {
103             return ($user);
104         }
105     }
106     return FALSE;
107 }
108
109 function user_get_sess($user_arr, $sess)
110 {
111     foreach ($user_arr as $i => $user) {
112         printf("SESS: [%s]  cur: [%s]\n", $user->sess_get(), $sess);
113         if ($user->sess_get() == $sess) {
114             return ($user);
115         }
116     }
117     return FALSE;
118 }
119
120
121 function shutta()
122 {
123   log_rd2("SHUTTA [".connection_status()."] !");
124 }
125
126 register_shutdown_function(shutta);
127
128 /*
129  *  MAIN
130  */
131 $shutdown = FALSE;
132
133 function main()
134 {
135     GLOBAL $G_lang, $mlang_indrd, $is_page_streaming;
136     // GLOBAL $first_loop;
137     GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
138     GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
139     $CO_splashdate = "CO_splashdate".$G_splash_idx;
140     GLOBAL $$CO_splashdate;
141     
142     GLOBAL $S_load_stat;
143
144
145
146     GLOBAL $shutdown;
147     $main_loop = TRUE;
148
149     $user_a = array();
150     $s2u  = array();
151     for ($i = 0 ; $i < 10 ; $i++) {
152         $user_a[$i] = new SPUser($i, 0, NULL);
153     }
154
155     $rndstr = "";
156     for ($i = 0 ; $i < 4096 ; $i++) {
157         $rndstr .= chr(mt_rand(65, 90));
158     }
159
160     $FILE_SOCKET = "/tmp/test001.sock";
161     $UNIX_SOCKET = "unix://$FILE_SOCKET";
162     $debug = 0;
163     $fixed_fd = 2;
164
165     $blocking_mode = 0; // 0 for non-blocking
166
167     if (file_exists($FILE_SOCKET)) {
168         unlink($FILE_SOCKET);
169     }
170     
171     $old_umask = umask(0);
172     if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
173         exit(11);
174     }
175     umask($old_umask);
176
177     $socks = array();
178
179     if (($in = fopen("php://stdin", "r")) === FALSE) {
180         exit(11);
181     }
182
183     stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
184
185     while ($main_loop) {
186         echo "IN LOOP\n";
187         /* Prepare the read array */
188         if ($shutdown) 
189             $read   = array_merge(array("$in" => $in), $socks);
190         else
191             $read   = array_merge(array("$list" => $list, "$in" => $in), $socks);
192
193         if ($debug > 1) {
194             printf("PRE_SELECT\n");
195             print_r($read);
196         }
197         $write  = NULL;
198         $except = NULL;
199         $num_changed_sockets = stream_select($read, $write, $except, 5);
200         
201         if ($num_changed_sockets === FALSE) {
202             printf("No data in 5 secs");
203         } 
204         else if ($num_changed_sockets > 0) {
205             printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
206             if ($debug > 1) {
207                 print_r($read);
208             }
209             /* At least at one of the sockets something interesting happened */
210             foreach ($read as $i => $sock) {
211                 if ($sock === $list) {
212                     printf("NUOVA CONNEX\n");
213                     $new_unix = stream_socket_accept($list);
214                     $stream_info = "";
215                     if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
216                         printf("RECEIVED HEADER:\n%s", $stream_info);
217                         $m = spu_process_info($stream_info, $header, $get, $post);
218                         printf("M: %s\nHEADER:\n", $m);
219                         print_r($header);
220                         printf("GET:\n");
221                         print_r($get);
222                         printf("POST:\n");
223                         print_r($post);
224
225                         /* TODO: here stuff to decide if it is old or new user */
226                         if (($user_cur = user_get_sess($user_a, $get['sess'])) != FALSE) {
227                             /* close the previous socket */
228                             unset($s2u[intval($user_cur->sock_get())]);
229                             unset($socks[intval($user_cur->sock_get())]);
230                             fclose($user_cur->sock_get());
231                             /* assign the new socket */
232                             $user_cur->sock_set($new_socket);
233                             $id = $user_cur->id_get();
234                             $s2u[intval($new_socket)] = $id;
235                             $socks[intval($new_socket)] = $new_socket;
236                             fwrite($new_socket, $rndstr);
237                             fflush($new_socket);
238                         }
239                         else if (($user_cur = user_get_free($user_a)) != FALSE) {
240                             stream_set_blocking($new_socket, $blocking_mode); // Set the stream to non-blocking
241                             $socks[intval($new_socket)] = $new_socket;
242                             
243                             $id = $user_cur->id_get();
244                             $user_a[$id]->enable($new_socket, $get['sess']);
245                             printf("s2u: ci passo %d\n", intval($new_socket));
246                             $s2u[intval($new_socket)] = $id;
247                             
248                             fwrite($new_socket, $rndstr);
249                             fflush($new_socket);
250                         }
251                         else {
252                             printf("Too many opened users\n");
253                             fclose($new_socket);
254                         }
255                     }
256                     else {
257                         printf("WARNING: ancillary_getstream failed\n");
258                     }
259                 }
260                 else {
261                     if (($buf = fread($sock, 512)) === FALSE) {
262                         printf("error read\n");
263                         exit(123);
264                     }
265                     else if (strlen($buf) === 0) {
266                         if ($sock === $list) {
267                             printf("Arrivati %d bytes da list\n", strlen($buf));
268                         }
269                         else if ($sock === $in) {
270                             printf("Arrivati %d bytes da stdin\n", strlen($buf));
271                         }
272                         else {
273                             unset($socks[intval($sock)]);
274                             $user_a[$s2u[intval($sock)]]->disable();
275                             unset($s2u[intval($sock)]);
276                             fclose($sock);
277                         }
278                         if ($debug > 1) {
279                             printf("post unset\n");
280                             print_r($socks);
281                         }
282                     }
283                     else {
284                         if ($debug > 1) {
285                             print_r($read);
286                         }
287                         if ($sock === $list) {
288                             printf("Arrivati %d bytes da list\n", strlen($buf));
289                         }
290                         else if ($sock === $in) {
291                             printf("Arrivati %d bytes da stdin\n", strlen($buf));
292                         }
293                         else {
294                             $key = array_search("$sock", $socks);
295                             printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
296                         }
297                     }
298                 }
299             }
300         }
301
302
303
304
305
306
307         if (0 == 1) { // WRITE PART EXAMPLE 
308             // sleep(3);
309             foreach ($socks as $k => $sock) {
310                 fwrite($sock, sprintf("DI QUI CI PASSO [%d]\n", $user_a[$s2u[intval($sock)]]->cnt_inc()));
311                 fflush($sock);
312             }
313         }
314     }
315     
316     exit(0);
317 }
318
319 main();
320 ?>