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