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