c66c58bad4baef9851bd1b41f2a6dbde39e4ff14
[curl-de-sac.git] / web / Obj / curl-de-sac.phh
1 <?php
2 /*
3  *  curl-de-sac - curl-de-sac.phh
4  *
5  *  Copyright (C)      2014 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it
7  *                                  matteo.nastasi@gmail.com
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 $G_curl_de_sac_version = "0.1";
26
27 class CDS_cmd {
28     var $cmd_cls;
29     var $ch;
30     var $tlimit;
31
32     function CDS_cmd($cmd_cls, $ch)
33     {
34         $this->cmd_cls = $cmd_cls;
35         $this->ch = $ch;
36         $this->tlimit = time() + $cmd_cls->tout;
37     }
38
39     function ch_get()
40     {
41         return ($this->ch);
42     }
43
44     function dbg_get()
45     {
46         // NOTE: cmd_cls must be valid by definition
47         if ($this->cmd_cls->cds == NULL)
48             return -1;
49         return $this->cmd_cls->cds->dbg_get();
50     }
51 }
52
53 class CDS_cmd_cls {
54     var $cds;
55     var $name;
56     var $tout;
57
58     function CDS_cmd_cls($name, $tout)
59     {
60         $this->cds = NULL;
61         $this->name = $name;
62         $this->tout = $tout;
63     }
64
65     function cds_set($cds)
66     {
67         $this->cds = $cds;
68     }
69
70     static function pre_create($cds, $url, $opts=NULL)
71     {
72         if ($cds->dbg_get() > 2) { printf("CURL: curl_init\n"); }
73         if (($ch = curl_init()) == FALSE)
74             return FALSE;
75         curl_setopt($ch, CURLOPT_URL, $url);
76         if ($opts == NULL) {
77             curl_setopt($ch, CURLOPT_HEADER, 0);
78             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
79             curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
80             curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
81         }
82         else {
83             foreach($opts as $opt => $value) {
84                 curl_setopt($ch, $opt, $value);
85             }
86         }
87         return ($ch);
88     }
89
90     function create($cds, $ch)
91     {
92         if ($cds->dbg > 2) {
93             printf("CDS_cmd_cls::create - begin\n");
94             printf("CURL: curl_multi_add_handle\n");
95         }
96         if (($ret = curl_multi_add_handle($cds->mh, $ch)) != 0) {
97             // INFO: $ret is a CURLM_XXX errors code
98             return (FALSE);
99         }
100         if ($cds->dbg > 2) { printf("CDS_cmd_cls::create - end\n"); }
101         return (TRUE);
102     }
103
104     function process($cmd, $ret)
105     {
106         
107         fprintf(STDERR, "process MUST BE IMPLEMENTED");
108         exit(123);
109     }
110
111     function timeout($cmd)
112     {
113         fprintf(STDERR, "timeout MUST BE IMPLEMENTED");
114         exit(123);
115     }
116
117     function dbg_get()
118     {
119         return $this->cds->dbg;
120     }
121 }
122
123 class Curl_de_sac {
124     var $mh;
125     var $cmd_cls;
126     var $cmd;
127     var $dbg;
128
129     function Curl_de_sac($dbg=0) {
130         if ($dbg > 2) { printf("CURL: curl_multi_init\n"); }
131         $this->mh = curl_multi_init();
132         $this->cmd_cls = array();
133         $this->cmd = array();
134         $this->dbg = $dbg;
135     }
136
137     function dbg_set($dbg)
138     {
139         $this->dbg = $dbg;
140     }
141
142     function dbg_get()
143     {
144         return($this->dbg);
145     }
146
147     function cmd_cls_register($cmd_cls)
148     {
149         if (get_class($cmd_cls) != 'CDS_cmd_cls' && is_subclass_of($cmd_cls, 'CDS_cmd_cls') == FALSE)
150             return FALSE;
151
152         if (isset($this->cmd_cls[$cmd_cls->name]))
153             return FALSE;
154
155         $this->cmd_cls[$cmd_cls->name] = $cmd_cls;
156         $cmd_cls->cds_set($this);
157
158         return TRUE;
159     }
160
161     function cmd_cls_deregister($cmd_cls)
162     {
163         if (get_class($cmd_cls) != 'CDS_cmd_cls' && is_subclass_of($cmd_cls, 'CDS_cmd_cls') == FALSE)
164             return FALSE;
165         if (!isset($this->cmd_cls[$cmd_cls->name]))
166             return FALSE;
167
168         $this->cmd_cls[$cmd_cls->name]->cds_set(NULL);
169
170         unset($this->cmd_cls[$cmd_cls->name]);
171         return TRUE;
172     }
173
174     function cmd_cls_deregister_all()
175     {
176         foreach($this->cmd_cls as $cmd_cls) {
177             $cmd_cls->cds_set(NULL);
178         }
179
180         $this->cmd_cls = array();
181     }
182
183
184     function cleanup($key)
185     {
186         $cmd = $this->cmd[$key];
187
188         if ($this->dbg > 2) {
189             printf("cleanup\n");
190             printf("CURL: curl_multi_remove_handle:\n");
191             print_r($cmd->ch_get());
192             printf("\n");
193         }
194         // return 0 on SUCCESS or CURLM_XXX in other cases
195         if (($ret = curl_multi_remove_handle($this->mh, $cmd->ch_get())) != 0) {
196             fprintf(STDERR, "CURL: curl_multi_remove_handle FAILED (%d)\n", $ret);
197         }
198         if ($this->dbg > 2) { printf("CURL: curl_close\n"); }
199         curl_close($cmd->ch_get());
200         unset($this->cmd[$key]);
201     }
202
203     function execute()
204     {
205         $args = func_get_args();
206
207         if ($this->dbg > 1) {
208              printf("CDS_cmd_cls::execute  ARGS:\n");
209              print_r($args);
210         }
211         do {
212             if (($name = array_shift($args)) === NULL)
213                 break;
214             array_unshift($args, $this);
215             
216             if (!isset($this->cmd_cls[$name]))
217                 break;
218             
219             $cmd_cls = $this->cmd_cls[$name];
220             
221             if (($inst = call_user_func_array(array($cmd_cls, "create"), $args)) == FALSE)
222                 break;
223
224             array_push($this->cmd, $inst);
225             if ($this->dbg > 1) { printf("CDS_cmd_cls::process - execute  push cmd\n"); }
226             if (($this->dbg & 1) == 1) { print_r($this); }
227
228             return TRUE;
229         } while (FALSE);
230
231         return FALSE;
232     }
233
234     function process($curtime=0)
235     {
236         if ($curtime  == 0) {
237             $curtime = time();
238         }
239         if ($this->dbg > 1) { printf("CDS_cmd_cls::process - begin\n"); }
240         $running = NULL;
241
242         if ($this->dbg > 2) { printf("CURL: curl_multi_exec\n"); }
243         $ret = curl_multi_exec($this->mh, $running);
244         $msgs_in_queue = NULL;
245
246         do {
247             if ($this->dbg > 2) { printf("CURL: curl_multi_info_read\n"); }
248
249             if ($ret = curl_multi_info_read ($this->mh, $msgs_in_queue)) {
250                 if ($this->dbg > 1) { printf("Info_read miq: %d\n", $msgs_in_queue); }
251                 if ($this->dbg > 2) { printf("CURL: curl_getinfo\n"); }
252
253                 $info = curl_getinfo($ret['handle']);
254                 if ($this->dbg > 1) {
255                     printf("Getinfo:\n");
256                     print_r($info);
257                 }
258
259                 foreach($this->cmd as $key => $cmd) {
260                     if ($cmd->ch == $ret['handle']) {
261                         if ($cmd->cmd_cls->process($cmd, $ret) == TRUE) {
262                             $this->cleanup($key);
263                         }
264                         break;
265                     }
266                 }
267             }
268         } while ($msgs_in_queue > 0);
269         foreach ($this->cmd as $key => $cmd) {
270             if ($this->dbg > 2) { printf("Check tout, curr: %d tlimit %d\n", $curtime, $cmd->tlimit); }
271             if ($curtime > $cmd->tlimit) {
272                 if ($this->dbg > 2) { printf("TIMEOUT REACHED!\n"); }
273                 $cmd->cmd_cls->timeout($cmd);
274                 $this->cleanup($key);
275             }
276         }
277         if ($this->dbg > 1) { printf("CDS_cmd_cls::process - end (queue: %d)\n", $msgs_in_queue); }
278     }
279
280 }