upgrade install parametrization and fix typo into builder.sh
[brisk.git] / web / spush / brisk-spush.phh
1 <?php
2 /*
3  *  brisk - spush/brisk-spush.phh
4  *
5  *  Copyright (C) 2012 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 $DOCUMENT_ROOT="";
26 $HTTP_HOST="dodo.birds.lan";
27 define('USOCK_PATH', "/tmp/brisk.sock");
28
29 class PageFlush {
30
31   var $socket; // socket handler of page stream
32   var $kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
33   var $msg;    // place where store failed fwrite data
34   var $msg_sz; // size of content
35
36   function PageFlush($socket, $enc, $curtime, $kalive, $header_out, $body)
37   {
38       printf("TRY FLUSH CREATE\n");
39       $body_out = ZLibStream::compress($enc, $body);
40       if ($enc != 'plain')
41           $header_out['Content-Encoding'] = $enc;
42       $body_out_sz = mb_strlen($body_out, "ASCII");
43       $hea = headers_render($header_out, $body_out_sz);
44       $hea_sz = mb_strlen($hea, "ASCII");
45
46       $this->socket = $socket;
47       $this->kalive = $curtime + $kalive;
48       $this->msg    = $hea.$body_out;
49       $this->msg_sz = $hea_sz + $body_out_sz;
50   }
51
52   /* return TRUE if is removable from it's list */
53   function try_flush($curtime)
54   {
55       printf("TRY FLUSH IN\n");
56       if ($this->kalive < $curtime) {
57           printf("TRY FLUSH CLOSE 1\n");
58           fclose($this->socket);
59           return TRUE;
60       }   
61
62       $wret = @fwrite($this->socket, $this->msg);
63       if ($wret == FALSE && $wret !== FALSE) {
64           printf("TRY FLUSH PageFlush::try_flush: wret 0 but not FALSE\n");
65       }
66       if ($wret == $this->msg_sz) {
67           printf("TRY FLUSH CLOSE 2\n");
68           fclose($this->socket);
69           return TRUE;
70       }
71       $this->msg_sz -= $wret;
72       $this->msg    = mb_substr($this->msg, $wret, $this->msg_sz, "ASCII");
73
74       printf("TRY FLUSH RETURN FALSE\n");
75
76       return FALSE;
77   }
78
79 }
80
81 ?>