working version with timeout and cleanup of exausted handles
[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)
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         curl_setopt($ch, CURLOPT_HEADER, 0);
77         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
78         curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
79         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
80         return ($ch);
81     }
82
83     function create($cds, $ch)
84     {
85         if ($cds->dbg > 2) {
86             printf("CDS_cmd_cls::create - begin\n");
87             printf("CURL: curl_multi_add_handle\n");
88         }
89         if (($ret = curl_multi_add_handle($cds->mh, $ch)) != 0) {
90             // INFO: $ret is a CURLM_XXX errors code
91             return (FALSE);
92         }
93         if ($cds->dbg > 2) { printf("CDS_cmd_cls::create - end\n"); }
94         return (TRUE);
95     }
96
97     function process($cmd, $ret)
98     {
99         
100         fprintf(STDERR, "process MUST BE IMPLEMENTED");
101         exit(123);
102     }
103
104     function timeout($cmd)
105     {
106         fprintf(STDERR, "timeout MUST BE IMPLEMENTED");
107         exit(123);
108     }
109
110     function dbg_get()
111     {
112         return $this->cds->dbg;
113     }
114 }
115
116 class Curl_de_sac {
117     var $mh;
118     var $cmd_cls;
119     var $cmd;
120     var $dbg;
121
122     function Curl_de_sac($dbg=0) {
123         if ($dbg > 2) { printf("CURL: curl_multi_init\n"); }
124         $this->mh = curl_multi_init();
125         $this->cmd_cls = array();
126         $this->cmd = array();
127         $this->dbg = $dbg;
128     }
129
130     function dbg_set($dbg)
131     {
132         $this->dbg = $dbg;
133     }
134
135     function dbg_get()
136     {
137         return($this->dbg);
138     }
139
140     function cmd_cls_register($cmd_cls)
141     {
142         if (get_class($cmd_cls) != 'CDS_cmd_cls' && is_subclass_of($cmd_cls, 'CDS_cmd_cls') == FALSE)
143             return FALSE;
144
145         if (isset($this->cmd_cls[$cmd_cls->name]))
146             return FALSE;
147
148         $this->cmd_cls[$cmd_cls->name] = $cmd_cls;
149         $cmd_cls->cds_set($this);
150
151         return TRUE;
152     }
153
154     function cmd_cls_deregister($cmd_cls)
155     {
156         if (get_class($cmd_cls) != 'CDS_cmd_cls' && is_subclass_of($cmd_cls, 'CDS_cmd_cls') == FALSE)
157             return FALSE;
158         if (!isset($this->cmd_cls[$cmd_cls->name]))
159             return FALSE;
160
161         $this->cmd_cls[$cmd_cls->name]->cds_set(NULL);
162
163         unset($this->cmd_cls[$cmd_cls->name]);
164         return TRUE;
165     }
166
167     function cmd_cls_deregister_all()
168     {
169         foreach($this->cmd_cls as $cmd_cls) {
170             $cmd_cls->cds_set(NULL);
171         }
172
173         $this->cmd_cls = array();
174     }
175
176
177     function cleanup($key)
178     {
179         $cmd = $this->cmd[$key];
180
181         if ($this->dbg > 2) {
182             printf("cleanup\n");
183             printf("CURL: curl_multi_remove_handle:\n");
184             print_r($cmd->ch_get());
185             printf("\n");
186         }
187         // return 0 on SUCCESS or CURLM_XXX in other cases
188         if (($ret = curl_multi_remove_handle($this->mh, $cmd->ch_get())) != 0) {
189             fprintf(STDERR, "CURL: curl_multi_remove_handle FAILED (%d)\n", $ret);
190         }
191         if ($this->dbg > 2) { printf("CURL: curl_close\n"); }
192         curl_close($cmd->ch_get());
193         unset($this->cmd[$key]);
194     }
195
196     function execute()
197     {
198         $args = func_get_args();
199
200         if ($this->dbg > 1) {
201              printf("CDS_cmd_cls::execute  ARGS:\n");
202              print_r($args);
203         }
204         do {
205             if (($name = array_shift($args)) === NULL)
206                 break;
207             array_unshift($args, $this);
208             
209             if (!isset($this->cmd_cls[$name]))
210                 break;
211             
212             $cmd_cls = $this->cmd_cls[$name];
213             
214             if (($inst = call_user_func_array(array($cmd_cls, "create"), $args)) == FALSE)
215                 break;
216
217             array_push($this->cmd, $inst);
218             if ($this->dbg > 1) { printf("CDS_cmd_cls::process - execute  push cmd\n"); }
219             if (($this->dbg & 1) == 1) { print_r($this); }
220
221             return TRUE;
222         } while (FALSE);
223
224         return FALSE;
225     }
226
227     function process($curtime=0)
228     {
229         if ($curtime  == 0) {
230             $curtime = time();
231         }
232         if ($this->dbg > 1) { printf("CDS_cmd_cls::process - begin\n"); }
233         $running = NULL;
234
235         if ($this->dbg > 2) { printf("CURL: curl_multi_exec\n"); }
236         $ret = curl_multi_exec($this->mh, $running);
237         $msgs_in_queue = NULL;
238
239         do {
240             if ($this->dbg > 2) { printf("CURL: curl_multi_info_read\n"); }
241
242             if ($ret = curl_multi_info_read ($this->mh, $msgs_in_queue)) {
243                 if ($this->dbg > 1) { printf("Info_read miq: %d\n", $msgs_in_queue); }
244                 if ($this->dbg > 2) { printf("CURL: curl_getinfo\n"); }
245
246                 $info = curl_getinfo($ret['handle']);
247                 if ($this->dbg > 1) {
248                     printf("Getinfo:\n");
249                     print_r($info);
250                 }
251
252                 foreach($this->cmd as $key => $cmd) {
253                     if ($cmd->ch == $ret['handle']) {
254                         if ($cmd->cmd_cls->process($cmd, $ret) == TRUE) {
255                             $this->cleanup($key);
256                         }
257                         break;
258                     }
259                 }
260             }
261         } while ($msgs_in_queue > 0);
262         foreach ($this->cmd as $key => $cmd) {
263             if ($this->dbg > 2) { printf("Check tout, curr: %d tlimit %d\n", $curtime, $cmd->tlimit); }
264             if ($curtime > $cmd->tlimit) {
265                 if ($this->dbg > 2) { printf("TIMEOUT REACHED!\n"); }
266                 $cmd->cmd_cls->timeout($cmd);
267                 $this->cleanup($key);
268             }
269         }
270         if ($this->dbg > 1) { printf("CDS_cmd_cls::process - end (queue: %d)\n", $msgs_in_queue); }
271     }
272
273 }