f7911871fd485e47847c42ef59be60256658603e
[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
31     function CDS_cmd($cmd_cls, $ch)
32     {
33         $this->cmd_cls = $cmd_cls;
34         $this->ch = $ch;
35     }
36 }
37
38 class CDS_cmd_cls {
39     var $cds;
40     var $name;
41     var $tout;
42
43     function CDS_cmd_cls($name, $tout)
44     {
45         $this->cds = NULL;
46         $this->name = $name;
47         $this->tout = $tout;
48     }
49
50     function cds_set($cds)
51     {
52         $this->cds = $cds;
53     }
54
55     static function pre_create($url)
56     {
57         if (($ch = curl_init()) == FALSE)
58             return FALSE;
59         curl_setopt($ch, CURLOPT_URL, $url);
60         curl_setopt($ch, CURLOPT_HEADER, 0);
61         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
62         
63         return ($ch);
64     }
65
66     function create($cds, $ch)
67     {
68         if ($cds->dbg > 2) {
69             printf("CDS_cmd_cls::create - begin\n");
70             print_r($ch);
71         }
72         if (($ret = curl_multi_add_handle($cds->mh, $ch)) != 0) {
73             // INFO: $ret is a CURLM_XXX errors code
74             return (FALSE);
75         }
76         if ($cds->dbg > 2) {
77             printf("CDS_cmd_cls::create - end\n");
78         }
79         return (TRUE);
80     }
81
82     function cb($cmd, $ret)
83     {
84         
85         print "THIS MUST BE IMPLEMENTED";
86         exit(123);
87     }
88 }
89
90 class Curl_de_sac {
91     var $mh;
92     var $cmd_cls;
93     var $cmd;
94     var $dbg;
95
96     function Curl_de_sac($dbg=0) {
97         $this->mh = curl_multi_init();
98         $this->cmd_cls = array();
99         $this->cmd = array();
100         $this->dbg = $dbg;
101     }
102
103     function dbg_set($dbg)
104     {
105         $this->dbg = $dbg;
106     }
107
108     function cmd_register($cmd_cls)
109     {
110         if (get_class($cmd_cls) != 'CDS_cmd_cls' && is_subclass_of($cmd_cls, 'CDS_cmd_cls') == FALSE)
111             return FALSE;
112
113         if (isset($this->cmd_cls[$cmd_cls->name]))
114             return FALSE;
115
116         $this->cmd_cls[$cmd_cls->name] = $cmd_cls;
117         $cmd_cls->cds_set($this);
118
119         return TRUE;
120     }
121
122     function cmd_deregister($cmd_cls)
123     {
124         if (get_class($cmd_cls) != 'CDS_cmd_cls' && is_subclass_of($cmd_cls, 'CDS_cmd_cls') == FALSE)
125             return FALSE;
126         if (!isset($this->cmd_cls[$cmd_cls->name]))
127             return FALSE;
128
129         $this->cmd_cls[$cmd_cls->name]->cds_set(NULL);
130
131         unset($this->cmd_cls[$cmd_cls->name]);
132         return TRUE;
133     }
134
135     function cmd_deregister_all()
136     {
137         foreach($this->cmd_cls as $cmd_cls) {
138             $cmd_cls->cds_set(NULL);
139         }
140
141         $this->cmd_cls = array();
142     }
143
144     function execute()
145     {
146         $args = func_get_args();
147
148         if ($this->dbg > 1) {
149              printf("CDS_cmd_cls::execute  ARGS:\n");
150              print_r($args);
151         }
152         do {
153             if (($name = array_shift($args)) === NULL)
154                 break;
155             array_unshift($args, $this);
156             
157             if (!isset($this->cmd_cls[$name]))
158                 break;
159             
160             $cmd_cls = $this->cmd_cls[$name];
161             
162             if (($inst = call_user_func_array(array($cmd_cls, "create"), $args)) == FALSE)
163                 break;
164
165             array_push($this->cmd, $inst);
166             if ($this->dbg > 1) {
167                 printf("CDS_cmd_cls::process - execute  push cmd\n");
168                 print_r($this->cmd);
169             }
170             return TRUE;
171         } while (FALSE);
172
173         return FALSE;
174     }
175
176     function process()
177     {
178         if ($this->dbg > 1) {
179              printf("CDS_cmd_cls::process - begin\n");
180         }
181         $running = NULL;
182         $ret = curl_multi_exec($this->mh, $running);
183         $msgs_in_queue = NULL;
184
185         do {
186             if ($ret = curl_multi_info_read ($this->mh, $msgs_in_queue)) {
187                 if ($this->dbg > 1)
188                     printf("Info_read miq: %d\n", $msgs_in_queue);
189                 
190                 foreach($this->cmd as $cmd) {
191                     if ($cmd->ch == $ret['handle']) {
192                         $cmd->cmd_cls->cb($cmd, $ret);
193                         break;
194                     }
195                 }
196                 $info = curl_getinfo($ret['handle']);
197                 if ($this->dbg > 1) {
198                     printf("Getinfo:\n");
199                     print_r($info);
200                 }
201             }
202         } while ($msgs_in_queue > 0);
203         if ($this->dbg > 1) {
204             printf("CDS_cmd_cls::process - end (queue: %d)\n", $msgs_in_queue);
205         }
206     }
207
208 }