from PageFlush to more generale PendingPage class
[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 define('PENDINGPAGE_WAITDATA', 0);
30 define('PENDINGPAGE_FLUSHING', 1);
31
32 class PendingPage {
33
34   var $socket; // socket handler of page stream
35   var $status; // status can be 0: waiting for data, 1: flush phase
36
37   var $kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
38   var $msg;    // place where store failed fwrite data
39   var $msg_sz; // size of content
40
41   
42
43   function PendingPage($socket)
44   {
45       $this->socket = $socket;
46   }
47
48   static function pendingpage_waiting($socket, $method, $header, $get, $post, $cookie, $path, $addr)
49   {
50       $thiz = new PendingPage($socket);
51
52       // TODO
53   }
54
55   function try_wait($curtime)
56   {
57       // if completed return TRUE to allow data to be processed, 
58       // if timeout or max content dimension is exceeded move to flushing
59   }
60
61   static function pendingpage_flushing($socket, $enc, $curtime, $kalive, $header_out, $body)
62   {
63       $thiz = new PendingPage($socket);
64
65       $thiz->to_flushing($enc, $curtime, $kalive, $header_out, $body);
66
67       return ($thiz);
68   }
69
70   function to_flushing($enc, $curtime, $kalive, $header_out, $body)
71   {
72       printf("TRY FLUSH CREATE\n");
73       $body_out = ZLibStream::compress($enc, $body);
74       if ($enc != 'plain')
75           $header_out['Content-Encoding'] = $enc;
76       $body_out_sz = mb_strlen($body_out, "ASCII");
77       $hea = headers_render($header_out, $body_out_sz);
78       $hea_sz = mb_strlen($hea, "ASCII");
79
80       $this->status = PENDINGPAGE_FLUSHING;
81       $this->kalive = $curtime + $kalive;
82       $this->msg    = $hea.$body_out;
83       $this->msg_sz = $hea_sz + $body_out_sz;
84   }
85
86   /* return TRUE if is removable from it's list */
87   function try_flush($curtime)
88   {
89       if ($this->status != PENDINGPAGE_FLUSHING)
90           return (FALSE);
91
92       printf("TRY FLUSH IN\n");
93       if ($this->kalive < $curtime) {
94           printf("TRY FLUSH CLOSE 1\n");
95           fclose($this->socket);
96           return TRUE;
97       }   
98
99       $wret = @fwrite($this->socket, $this->msg);
100       if ($wret == FALSE && $wret !== FALSE) {
101           printf("TRY FLUSH PendingPage::try_flush: wret 0 but not FALSE\n");
102       }
103       if ($wret == $this->msg_sz) {
104           printf("TRY FLUSH CLOSE 2\n");
105           fclose($this->socket);
106           return TRUE;
107       }
108       $this->msg_sz -= $wret;
109       $this->msg    = mb_substr($this->msg, $wret, $this->msg_sz, "ASCII");
110
111       printf("TRY FLUSH RETURN FALSE\n");
112
113       return FALSE;
114   }
115
116 }
117
118 ?>