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");
33 define('SITE_PREFIX', '/brisk/');
48 function enable($sock, $sess)
65 return ($this->cnt < 0 ? FALSE : TRUE);
73 function sock_set($sock)
99 function user_get_free($user_arr)
101 foreach ($user_arr as $i => $user) {
102 if (!$user->is_enable()) {
109 function user_get_sess($user_arr, $sess)
111 foreach ($user_arr as $i => $user) {
112 printf("SESS: [%s] cur: [%s]\n", $user->sess_get(), $sess);
113 if ($user->sess_get() == $sess) {
123 log_rd2("SHUTTA [".connection_status()."] !");
126 register_shutdown_function(shutta);
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;
151 for ($i = 0 ; $i < 10 ; $i++) {
152 $user_a[$i] = new SPUser($i, 0, NULL);
156 for ($i = 0 ; $i < 4096 ; $i++) {
157 $rndstr .= chr(mt_rand(65, 90));
160 $FILE_SOCKET = "/tmp/test001.sock";
161 $UNIX_SOCKET = "unix://$FILE_SOCKET";
165 $blocking_mode = 0; // 0 for non-blocking
167 if (file_exists($FILE_SOCKET)) {
168 unlink($FILE_SOCKET);
171 $old_umask = umask(0);
172 if (($list = stream_socket_server($UNIX_SOCKET, $err, $errs)) === FALSE) {
179 if (($in = fopen("php://stdin", "r")) === FALSE) {
183 stream_set_blocking($list, $blocking_mode); # Set the stream to non-blocking
187 /* Prepare the read array */
189 $read = array_merge(array("$in" => $in), $socks);
191 $read = array_merge(array("$list" => $list, "$in" => $in), $socks);
194 printf("PRE_SELECT\n");
199 $num_changed_sockets = stream_select($read, $write, $except, 5);
201 if ($num_changed_sockets === FALSE) {
202 printf("No data in 5 secs");
204 else if ($num_changed_sockets > 0) {
205 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
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);
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);
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);
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;
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;
248 fwrite($new_socket, $rndstr);
252 printf("Too many opened users\n");
257 printf("WARNING: ancillary_getstream failed\n");
261 if (($buf = fread($sock, 512)) === FALSE) {
262 printf("error read\n");
265 else if (strlen($buf) === 0) {
266 if ($sock === $list) {
267 printf("Arrivati %d bytes da list\n", strlen($buf));
269 else if ($sock === $in) {
270 printf("Arrivati %d bytes da stdin\n", strlen($buf));
273 unset($socks[intval($sock)]);
274 $user_a[$s2u[intval($sock)]]->disable();
275 unset($s2u[intval($sock)]);
279 printf("post unset\n");
287 if ($sock === $list) {
288 printf("Arrivati %d bytes da list\n", strlen($buf));
290 else if ($sock === $in) {
291 printf("Arrivati %d bytes da stdin\n", strlen($buf));
294 $key = array_search("$sock", $socks);
295 printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
307 if (0 == 1) { // WRITE PART EXAMPLE
309 foreach ($socks as $k => $sock) {
310 fwrite($sock, sprintf("DI QUI CI PASSO [%d]\n", $user_a[$s2u[intval($sock)]]->cnt_inc()));