4 * brisk - spush/brisk-spush.php
6 * Copyright (C) 2012 Matteo Nastasi
7 * mailto: nastasi@alternativeoutput.it
8 * matteo.nastasi@milug.org
9 * web: http://www.alternativeoutput.it
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.
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.
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");
46 function enable($sock, $sess)
63 return ($this->cnt < 0 ? FALSE : TRUE);
71 function sock_set($sock)
97 function user_get_free($user_arr)
99 foreach ($user_arr as $i => $user) {
100 if (!$user->is_enable()) {
107 function user_get_sess($user_arr, $sess)
109 foreach ($user_arr as $i => $user) {
110 printf("SESS: [%s] cur: [%s]\n", $user->sess_get(), $sess);
111 if ($user->sess_get() == $sess) {
121 log_rd2("SHUTTA [".connection_status()."] !");
124 register_shutdown_function(shutta);
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;
149 for ($i = 0 ; $i < 10 ; $i++) {
150 $user_a[$i] = new SPUser($i, 0, NULL);
154 for ($i = 0 ; $i < 4096 ; $i++) {
155 $rndstr .= chr(mt_rand(65, 90));
158 $FILE_SOCKET = "/tmp/test001.sock";
159 $UNIX_SOCKET = "unix://$FILE_SOCKET";
163 $blocking_mode = 0; // 0 for non-blocking
165 if (file_exists($FILE_SOCKET)) {
166 unlink($FILE_SOCKET);
169 $old_umask = umask(0);
170 if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
177 if (($in = fopen("php://stdin", "r")) === FALSE) {
181 stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
185 /* Prepare the read array */
187 $read = array_merge(array("$in" => $in), $socks);
189 $read = array_merge(array("$list" => $list, "$in" => $in), $socks);
192 printf("PRE_SELECT\n");
197 $num_changed_sockets = stream_select($read, $write, $except, 5);
199 if ($num_changed_sockets === FALSE) {
200 printf("No data in 5 secs");
202 else if ($num_changed_sockets > 0) {
203 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
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);
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);
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);
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;
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;
246 fwrite($new_socket, $rndstr);
250 printf("Too many opened users\n");
255 printf("WARNING: ancillary_getstream failed\n");
259 if (($buf = fread($sock, 512)) === FALSE) {
260 printf("error read\n");
263 else if (strlen($buf) === 0) {
264 if ($sock === $list) {
265 printf("Arrivati %d bytes da list\n", strlen($buf));
267 else if ($sock === $in) {
268 printf("Arrivati %d bytes da stdin\n", strlen($buf));
271 unset($socks[intval($sock)]);
272 $user_a[$s2u[intval($sock)]]->disable();
273 unset($s2u[intval($sock)]);
277 printf("post unset\n");
285 if ($sock === $list) {
286 printf("Arrivati %d bytes da list\n", strlen($buf));
288 else if ($sock === $in) {
289 printf("Arrivati %d bytes da stdin\n", strlen($buf));
292 $key = array_search("$sock", $socks);
293 printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
305 if (0 == 1) { // WRITE PART EXAMPLE
307 foreach ($socks as $k => $sock) {
308 fwrite($sock, sprintf("DI QUI CI PASSO [%d]\n", $user_a[$s2u[intval($sock)]]->cnt_inc()));