3 * curl-de-sac - curl-de-sac.phh
5 * Copyright (C) 2014 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@gmail.com
8 * web: http://www.alternativeoutput.it
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.
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.
25 $G_curl_de_sac_version = "0.1";
31 function CDS_cmd($cmd_cls, $ch)
33 $this->cmd_cls = $cmd_cls;
39 // NOTE: cmd_cls must be valid by definition
40 if ($this->cmd_cls->cds == NULL)
42 return $this->cmd_cls->cds->dbg_get();
51 function CDS_cmd_cls($name, $tout)
58 function cds_set($cds)
63 static function pre_create($url)
65 if (($ch = curl_init()) == FALSE)
67 curl_setopt($ch, CURLOPT_URL, $url);
68 curl_setopt($ch, CURLOPT_HEADER, 0);
69 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
74 function create($cds, $ch)
77 printf("CDS_cmd_cls::create - begin\n");
80 if (($ret = curl_multi_add_handle($cds->mh, $ch)) != 0) {
81 // INFO: $ret is a CURLM_XXX errors code
85 printf("CDS_cmd_cls::create - end\n");
90 function cb($cmd, $ret)
93 print "THIS MUST BE IMPLEMENTED";
104 function Curl_de_sac($dbg=0) {
105 $this->mh = curl_multi_init();
106 $this->cmd_cls = array();
107 $this->cmd = array();
111 function dbg_set($dbg)
121 function cmd_cls_register($cmd_cls)
123 if (get_class($cmd_cls) != 'CDS_cmd_cls' && is_subclass_of($cmd_cls, 'CDS_cmd_cls') == FALSE)
126 if (isset($this->cmd_cls[$cmd_cls->name]))
129 $this->cmd_cls[$cmd_cls->name] = $cmd_cls;
130 $cmd_cls->cds_set($this);
135 function cmd_cls_deregister($cmd_cls)
137 if (get_class($cmd_cls) != 'CDS_cmd_cls' && is_subclass_of($cmd_cls, 'CDS_cmd_cls') == FALSE)
139 if (!isset($this->cmd_cls[$cmd_cls->name]))
142 $this->cmd_cls[$cmd_cls->name]->cds_set(NULL);
144 unset($this->cmd_cls[$cmd_cls->name]);
148 function cmd_cls_deregister_all()
150 foreach($this->cmd_cls as $cmd_cls) {
151 $cmd_cls->cds_set(NULL);
154 $this->cmd_cls = array();
159 $args = func_get_args();
161 if ($this->dbg > 1) {
162 printf("CDS_cmd_cls::execute ARGS:\n");
166 if (($name = array_shift($args)) === NULL)
168 array_unshift($args, $this);
170 if (!isset($this->cmd_cls[$name]))
173 $cmd_cls = $this->cmd_cls[$name];
175 if (($inst = call_user_func_array(array($cmd_cls, "create"), $args)) == FALSE)
178 array_push($this->cmd, $inst);
179 if ($this->dbg > 1) {
180 printf("CDS_cmd_cls::process - execute push cmd\n");
191 if ($this->dbg > 1) {
192 printf("CDS_cmd_cls::process - begin\n");
195 $ret = curl_multi_exec($this->mh, $running);
196 $msgs_in_queue = NULL;
199 if ($ret = curl_multi_info_read ($this->mh, $msgs_in_queue)) {
201 printf("Info_read miq: %d\n", $msgs_in_queue);
203 foreach($this->cmd as $cmd) {
204 if ($cmd->ch == $ret['handle']) {
205 $cmd->cmd_cls->cb($cmd, $ret);
209 $info = curl_getinfo($ret['handle']);
210 if ($this->dbg > 1) {
211 printf("Getinfo:\n");
215 } while ($msgs_in_queue > 0);
216 if ($this->dbg > 1) {
217 printf("CDS_cmd_cls::process - end (queue: %d)\n", $msgs_in_queue);