sac-a-push daemon added to the current brisk tree
[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 class SPUser {
34     var $id;
35     var $sess;
36     var $cnt;
37     var $sock;
38     
39     function SPUser($id)
40     {
41         $this->id = $id;
42         $this->cnt = -1;
43         $this->sock = NULL;
44     }
45
46     function enable($sock, $sess)
47     {
48         $this->sess = $sess;
49         $this->cnt = 0;
50         $this->sock = $sock;
51
52         return ($this->id);
53     }
54
55     function disable()
56     {
57         $this->cnt = -1;
58         $this->sock = NULL;
59     }
60
61     function is_enable()
62     {
63         return ($this->cnt < 0 ? FALSE : TRUE);
64     }
65
66     function sock_get()
67     {
68         return $this->sock;
69     }
70
71     function sock_set($sock)
72     {
73         $this->sock = $sock;
74     }
75
76     function id_get()
77     {
78         return $this->id;
79     }
80     
81     function sess_get()
82     {
83         return $this->sess;
84     }
85
86     function cnt_get()
87     {
88         return $this->cnt;
89     }
90
91     function cnt_inc()
92     {
93         return $this->cnt++;
94     }
95 }
96
97 function user_get_free($user_arr)
98 {
99     foreach ($user_arr as $i => $user) {
100         if (!$user->is_enable()) {
101             return ($user);
102         }
103     }
104     return FALSE;
105 }
106
107 function user_get_sess($user_arr, $sess)
108 {
109     foreach ($user_arr as $i => $user) {
110         printf("SESS: [%s]  cur: [%s]\n", $user->sess_get(), $sess);
111         if ($user->sess_get() == $sess) {
112             return ($user);
113         }
114     }
115     return FALSE;
116 }
117
118
119 function shutta()
120 {
121   log_rd2("SHUTTA [".connection_status()."] !");
122 }
123
124 register_shutdown_function(shutta);
125
126 /*
127  *  MAIN
128  */
129 $shutdown = FALSE;
130
131 function main()
132 {
133     GLOBAL $G_lang, $mlang_indrd, $is_page_streaming;
134     // GLOBAL $first_loop;
135     GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
136     GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
137     $CO_splashdate = "CO_splashdate".$G_splash_idx;
138     GLOBAL $$CO_splashdate;
139     
140     GLOBAL $S_load_stat;
141
142
143
144     GLOBAL $shutdown;
145     $main_loop = TRUE;
146
147     $user_a = array();
148     $s2u  = array();
149     for ($i = 0 ; $i < 10 ; $i++) {
150         $user_a[$i] = new SPUser($i, 0, NULL);
151     }
152
153     $rndstr = "";
154     for ($i = 0 ; $i < 4096 ; $i++) {
155         $rndstr .= chr(mt_rand(65, 90));
156     }
157
158     $FILE_SOCKET = "/tmp/test001.sock";
159     $UNIX_SOCKET = "unix://$FILE_SOCKET";
160     $debug = 0;
161     $fixed_fd = 2;
162
163     $blocking_mode = 0; // 0 for non-blocking
164
165     if (file_exists($FILE_SOCKET)) {
166         unlink($FILE_SOCKET);
167     }
168     
169     $old_umask = umask(0);
170     if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
171         exit(11);
172     }
173     umask($old_umask);
174
175     $socks = array();
176
177     if (($in = fopen("php://stdin", "r")) === FALSE) {
178         exit(11);
179     }
180
181     stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
182
183     while ($main_loop) {
184         echo "IN LOOP\n";
185         /* Prepare the read array */
186         if ($shutdown) 
187             $read   = array_merge(array("$in" => $in), $socks);
188         else
189             $read   = array_merge(array("$list" => $list, "$in" => $in), $socks);
190
191         if ($debug > 1) {
192             printf("PRE_SELECT\n");
193             print_r($read);
194         }
195         $write  = NULL;
196         $except = NULL;
197         $num_changed_sockets = stream_select($read, $write, $except, 5);
198         
199         if ($num_changed_sockets === FALSE) {
200             printf("No data in 5 secs");
201         } 
202         else if ($num_changed_sockets > 0) {
203             printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
204             if ($debug > 1) {
205                 print_r($read);
206             }
207             /* At least at one of the sockets something interesting happened */
208             foreach ($read as $i => $sock) {
209                 if ($sock === $list) {
210                     printf("NUOVA CONNEX\n");
211                     $new_unix = stream_socket_accept($list);
212                     $stream_info = "";
213                     if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
214                         printf("RECEIVED HEADER:\n%s", $stream_info);
215                         $m = spu_process_info($stream_info, $header, $get, $post);
216                         printf("M: %s\nHEADER:\n", $m);
217                         print_r($header);
218                         printf("GET:\n");
219                         print_r($get);
220                         printf("POST:\n");
221                         print_r($post);
222
223                         /* TODO: here stuff to decide if it is old or new user */
224                         if (($user_cur = user_get_sess($user_a, $get['sess'])) != FALSE) {
225                             /* close the previous socket */
226                             unset($s2u[intval($user_cur->sock_get())]);
227                             unset($socks[intval($user_cur->sock_get())]);
228                             fclose($user_cur->sock_get());
229                             /* assign the new socket */
230                             $user_cur->sock_set($new_socket);
231                             $id = $user_cur->id_get();
232                             $s2u[intval($new_socket)] = $id;
233                             $socks[intval($new_socket)] = $new_socket;
234                             fwrite($new_socket, $rndstr);
235                             fflush($new_socket);
236                         }
237                         else if (($user_cur = user_get_free($user_a)) != FALSE) {
238                             stream_set_blocking($new_socket, $blocking_mode); // Set the stream to non-blocking
239                             $socks[intval($new_socket)] = $new_socket;
240                             
241                             $id = $user_cur->id_get();
242                             $user_a[$id]->enable($new_socket, $get['sess']);
243                             printf("s2u: ci passo %d\n", intval($new_socket));
244                             $s2u[intval($new_socket)] = $id;
245                             
246                             fwrite($new_socket, $rndstr);
247                             fflush($new_socket);
248                         }
249                         else {
250                             printf("Too many opened users\n");
251                             fclose($new_socket);
252                         }
253                     }
254                     else {
255                         printf("WARNING: ancillary_getstream failed\n");
256                     }
257                 }
258                 else {
259                     if (($buf = fread($sock, 512)) === FALSE) {
260                         printf("error read\n");
261                         exit(123);
262                     }
263                     else if (strlen($buf) === 0) {
264                         if ($sock === $list) {
265                             printf("Arrivati %d bytes da list\n", strlen($buf));
266                         }
267                         else if ($sock === $in) {
268                             printf("Arrivati %d bytes da stdin\n", strlen($buf));
269                         }
270                         else {
271                             unset($socks[intval($sock)]);
272                             $user_a[$s2u[intval($sock)]]->disable();
273                             unset($s2u[intval($sock)]);
274                             fclose($sock);
275                         }
276                         if ($debug > 1) {
277                             printf("post unset\n");
278                             print_r($socks);
279                         }
280                     }
281                     else {
282                         if ($debug > 1) {
283                             print_r($read);
284                         }
285                         if ($sock === $list) {
286                             printf("Arrivati %d bytes da list\n", strlen($buf));
287                         }
288                         else if ($sock === $in) {
289                             printf("Arrivati %d bytes da stdin\n", strlen($buf));
290                         }
291                         else {
292                             $key = array_search("$sock", $socks);
293                             printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
294                         }
295                     }
296                 }
297             }
298         }
299
300
301
302
303
304
305         if (0 == 1) { // WRITE PART EXAMPLE 
306             // sleep(3);
307             foreach ($socks as $k => $sock) {
308                 fwrite($sock, sprintf("DI QUI CI PASSO [%d]\n", $user_a[$s2u[intval($sock)]]->cnt_inc()));
309                 fflush($sock);
310             }
311         }
312     }
313     
314     exit(0);
315 }
316
317 main();
318 ?>