sharding on sockets with input data
[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_PFX', "/tmp/brisk");
28 define('USOCK_POOL_N', 10);
29 define('SOCK_SHARD_N', 2);
30
31 define('PENDINGPAGE_CONTINUE', 0);
32 define('PENDINGPAGE_WAITDATA', 1);
33 define('PENDINGPAGE_FLUSH',    2);
34
35 class PendingPage {
36   var $socket; // socket handler of page stream
37   var $status; // status can be 0: waiting for data, 1: flush phase
38
39   var $kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
40   var $msg;    // place where store failed fwrite data
41   var $msg_sz; // size of content
42
43   var $method; // method used to request the page
44   var $header; // array of header fields
45   var $get;    // array of get args
46   var $post;   // array of post args
47   var $cookie; // array of cookie args
48   var $path;   // requested path
49   var $addr;   // source address
50   var $contsz; // expected content size
51   var $rest;   // number of missing bytes
52   var $cont;   // content of unfinished POST
53
54   function PendingPage($socket, $curtime, $kalive)
55   {
56       $this->socket = $socket;
57       // fprintf(STDERR, "SOCKET ADD: %s\n", $this->socket);
58       $this->kalive = $curtime + $kalive;
59   }
60
61   static function pendingpage_continue($socket, $curtime, $kalive, $method,
62                                        $header,     $get,   $post, $cookie,
63                                          $path,    $addr,   $rest, $cont)
64   {
65       $thiz = static::pendingpage_staminal($socket, PENDINGPAGE_CONTINUE, $curtime, $kalive, $method,
66                                            $header, $get, $post, $cookie, $path, $addr, $rest, $cont);
67       $thiz->to_continuing();
68
69       return $thiz;
70   }
71
72   function context_get(&$header, &$socket, &$path, &$addr, &$get, &$post, &$cookie)
73   {
74       $header = $this->header;
75       $socket = $this->socket;
76       $path   = $this->path;
77       $addr   = $this->addr;
78       $get    = $this->get;
79       post_manage($post, $this->cont);
80       $cookie = $this->cookie;
81       fprintf(STDERR, "SOCKET GET: %s\n", $this->socket);
82   }
83
84   function to_continuing()
85   {
86       // printf("TRY FLUSH CREATE\n");
87       $header = array();
88       $header['HTTP-Response'] = "100 Continue";
89       $hea = headers_render($header, 0);
90       $hea_sz = mb_strlen($hea, "ASCII");
91
92       $this->status = PENDINGPAGE_CONTINUE;
93       $this->msg    = $hea;
94       $this->msg_sz = $hea_sz;
95   }
96
97   static function pendingpage_waiting($socket, $curtime, $kalive, $method, $header,
98                                       $get, $post, $cookie, $path, $addr, $rest, $cont)
99   {
100       return (static::pendingpage_staminal($socket, PENDINGPAGE_WAITDATA, $curtime, $kalive, $method,
101                                            $header, $get, $post, $cookie, $path, $addr, $rest, $cont));
102   }
103
104   static function pendingpage_staminal($socket, $status, $curtime, $kalive, $method, $header,
105                                        $get, $post, $cookie, $path, $addr, $rest, $cont)
106   {
107       $thiz = new PendingPage($socket, $curtime, $kalive);
108       $thiz->status = $status;
109
110       $thiz->method = $method;
111       $thiz->header = $header;
112       $thiz->get    = $get;
113       $thiz->post   = $post;
114       $thiz->cookie = $cookie;
115       $thiz->path   = $path;
116       $thiz->addr   = $addr;
117       $thiz->contsz = $header['Content-Length'];
118       $thiz->rest   = $rest;
119       $thiz->cont   = $cont;
120
121       return ($thiz);
122   }
123
124   function try_wait($curtime)
125   {
126       // if completed return TRUE to allow data to be processed, 
127       // if timeout or max content dimension is exceeded move to flushing
128   }
129
130   static function pendingpage_flushing($socket, $curtime, $kalive, $enc, $header_out, $body)
131   {
132       $thiz = new PendingPage($socket, $curtime, $kalive);
133
134       $thiz->to_flushing($enc, $header_out, $body);
135
136       return ($thiz);
137   }
138
139   function to_flushing($enc, &$header_out, $body)
140   {
141       // printf("TRY FLUSH CREATE: enc[%s]\n", $enc);
142       $body_out = ZLibStream::compress($enc, $body);
143       if ($enc != 'plain')
144           $header_out['Content-Encoding'] = $enc;
145       $body_out_sz = mb_strlen($body_out, "ASCII");
146       $hea = headers_render($header_out, $body_out_sz);
147       $hea_sz = mb_strlen($hea, "ASCII");
148
149       $this->status = PENDINGPAGE_FLUSH;
150       $this->msg    = $hea.$body_out;
151       $this->msg_sz = $hea_sz + $body_out_sz;
152       // printf("TRY FLUSH CREATE: enc[%s]\n", $enc);
153   }
154
155   /* return TRUE if is removable from it's list */
156   function try_flush($curtime)
157   {
158       // fprintf(STDERR, "IMPORTANT: TRY_FLUSH: start %d\n", $this->status);
159       if ($this->status != PENDINGPAGE_FLUSH &&
160           $this->status != PENDINGPAGE_CONTINUE)
161           return (FALSE);
162
163       if ($this->kalive < $curtime) {
164           // printf("TRY FLUSH CLOSE 1\n");
165           @fclose($this->socket);
166           return TRUE;
167       }   
168
169       $wret = @fwrite($this->socket, $this->msg, mb_strlen($this->msg, "ASCII"));
170       if ($wret == FALSE && $wret !== FALSE) {
171           // printf("TRY FLUSH PendingPage::try_flush: wret 0 but not FALSE [%d]\n", mb_strlen($this->msg, "ASCII"));
172       }
173       if ($wret == $this->msg_sz) {
174           if ($this->status == PENDINGPAGE_CONTINUE) {
175               $this->status = PENDINGPAGE_WAITDATA;
176               return FALSE;
177           }
178           else {
179               // printf("TRY FLUSH CLOSE 2\n");
180               fclose($this->socket);
181               return TRUE;
182           }
183       }
184       $this->msg_sz -= $wret;
185       $this->msg    = mb_substr($this->msg, $wret, $this->msg_sz, "ASCII");
186
187       // printf("TRY FLUSH RETURN FALSE\n");
188
189       return FALSE;
190   }
191
192   function socket_get()
193   {
194       return ($this->socket);
195   }
196
197 } // class PendingPage {
198
199 ?>