fix missing bold for your account item
[brisk.git] / test / nonblocking.php
1 #!/usr/bin/php
2 <?php
3 /*
4  *  brisk - test/nonblocking.php
5  *
6  *  Copyright (C) 2014 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 $blocking_mode = 0; // 0 is non-blocking
27
28 function cmd_serialize($attrs)
29 {
30     $ret = "";
31
32     $sep = "";
33     foreach ($attrs as $key => $value) {
34         $ret .= $sep . $key . '=' . urlencode($value);
35         $sep = "&";
36     }
37     return $ret;
38 }
39
40 function cmd_deserialize($cmd)
41 {
42     $ret = array();
43     $a = explode('&', $cmd);
44     $i = 0;
45     while ($i < count($a)) {
46         $b = split('=', $a[$i]);
47         $ret[urldecode($b[0])] = urldecode($b[1]);
48         $i++;
49     }
50
51     return $ret;
52 }
53
54
55 function sock_mgmt($sock)
56 {
57     $buf_all = "";
58     $output = "bib  bob  bub  beb";
59
60     $st = 1;
61     while(1) {
62         if ($st == 1) {
63             printf("\n  LOOP SOCK BEGIN\n");
64             $buf = fread($sock, 4096);
65             if ($buf == FALSE) {
66                 printf("  BUF == FALSE\n");
67             }
68             if ($buf === FALSE) {
69                 printf("  BUF === FALSE\n");
70             }
71             if (!($buf == FALSE) && !($buf === FALSE)) {
72                 if (strlen($buf) > 0) {
73                     printf("  REC: [%s]\n", $buf);
74                     $buf_all .= $buf;
75                     if (substr(trim($buf_all), -13) == "&the_end=true") {
76                         $st = 2;
77                         $ct = 0;
78                     }
79                 }
80                 else {
81                     printf("  LEN(BUF) == 0\n");
82                 }
83             }
84             else {
85                 printf("  FEOF RETURN %d\n", feof($sock));
86                 if (feof($sock)) {
87                     fclose($sock);
88                     return(TRUE);
89                 }
90             }
91         }
92         else if ($st == 2) {
93             fwrite($sock, substr($output, $ct, 9), 9);
94             fflush($sock);
95             $ct += 9;
96         }
97         usleep(500000);
98     }
99 }
100
101 function main()
102 {
103     GLOBAL $blocking_mode;
104
105     // $cmd1 = array ("pippo" => "pl'&uto", "minnie" => 'mic"=key', "the_end" => "true" );
106     $cmd1 = array ("cmd" => "userauth", "login" => 'mop', 'private' => 'it_must_be_correct',
107                    'the_end' => 'true' );
108
109     $ret1 = cmd_serialize($cmd1);
110     $ret2 = cmd_deserialize($ret1);
111
112     print_r($cmd1);
113     printf("SER: %s\n", $ret1);
114     printf("DESER\n");
115     print_r($ret2);
116
117     $file_socket = "/tmp/brisk.sock";
118     $unix_socket = "unix://$file_socket";
119
120     if (file_exists($file_socket)) {
121         unlink($file_socket);
122         }
123
124     $old_umask = umask(0);
125     if (($list = stream_socket_server($unix_socket, $err, $errs)) === FALSE) {
126         return (FALSE);
127     }
128     umask($old_umask);
129     printf("Blocking mode (listen): %d\n", $blocking_mode);
130     stream_set_blocking($list, $blocking_mode); // Set the stream to non-blocking
131
132     while(1) {
133         printf("\nLOOP BEGIN\n");
134         if (($new_unix = stream_socket_accept($list)) == FALSE) {
135             printf("SOCKET_ACCEPT FAILED\n");
136             usleep(500000);
137             continue;
138         }
139         else {
140             stream_set_blocking($new_unix, $blocking_mode); // Set the stream to non-blocking
141             sock_mgmt($new_unix);
142         }
143         usleep(500000);
144     }
145 }
146
147 main();
148 ?>