X-Git-Url: https://mop.ddnsfree.com/gitweb/?a=blobdiff_plain;f=web%2FObj%2Fcurl-de-sac.phh;h=8c295bd96d6743932bf2f2d9890f3fea341430d0;hb=cbc55a7b9f2928c7f84a0b52d8e8bab73806bc03;hp=409d1f785dc0d84b34b4dbf5f0be30a42381b075;hpb=de865f1c7ca1c4d0395aa745cf9e4e46744b6706;p=curl-de-sac.git diff --git a/web/Obj/curl-de-sac.phh b/web/Obj/curl-de-sac.phh index 409d1f7..8c295bd 100644 --- a/web/Obj/curl-de-sac.phh +++ b/web/Obj/curl-de-sac.phh @@ -24,18 +24,64 @@ $G_curl_de_sac_version = "0.1"; +class CDS_cmd { + var $cmd_cls; + var $ch; + + function CDS_cmd($cmd_cls, $ch) + { + $this->cmd_cls = $cmd_cls; + $this->ch = $ch; + } +} + class CDS_cmd_cls { + var $cds; var $name; var $tout; - + function CDS_cmd_cls($name, $tout) { + $this->cds = NULL; $this->name = $name; $this->tout = $tout; } - function cb() + function cds_set($cds) + { + $this->cds = $cds; + } + + static function pre_create($url) + { + if (($ch = curl_init()) == FALSE) + return FALSE; + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + + return ($ch); + } + + function create($cds, $ch) + { + if ($cds->dbg > 2) { + printf("CDS_cmd_cls::create - begin\n"); + print_r($ch); + } + if (($ret = curl_multi_add_handle($cds->mh, $ch)) != 0) { + // INFO: $ret is a CURLM_XXX errors code + return (FALSE); + } + if ($cds->dbg > 2) { + printf("CDS_cmd_cls::create - end\n"); + } + return (TRUE); + } + + function cb($cmd, $ret) { + print "THIS MUST BE IMPLEMENTED"; exit(123); } @@ -44,22 +90,119 @@ class CDS_cmd_cls { class Curl_de_sac { var $mh; var $cmd_cls; + var $cmd; + var $dbg; - function Curl_de_sac() { + function Curl_de_sac($dbg=0) { $this->mh = curl_multi_init(); $this->cmd_cls = array(); + $this->cmd = array(); + $this->dbg = $dbg; } - function cmd_register($cmd_cls) + function dbg_set($dbg) { - if (get_class($cmd_cls) != 'CDS_cmd_cls' || is_subclass_of($cmd_cls, 'CDS_cmd_cls') == FALSE) + $this->dbg = $dbg; + } + + function cmd_cls_register($cmd_cls) + { + if (get_class($cmd_cls) != 'CDS_cmd_cls' && is_subclass_of($cmd_cls, 'CDS_cmd_cls') == FALSE) return FALSE; if (isset($this->cmd_cls[$cmd_cls->name])) return FALSE; - $this->cmd[$cmd_cls->name] = $cmd_cls; + $this->cmd_cls[$cmd_cls->name] = $cmd_cls; + $cmd_cls->cds_set($this); + + return TRUE; + } + + function cmd_cls_deregister($cmd_cls) + { + if (get_class($cmd_cls) != 'CDS_cmd_cls' && is_subclass_of($cmd_cls, 'CDS_cmd_cls') == FALSE) + return FALSE; + if (!isset($this->cmd_cls[$cmd_cls->name])) + return FALSE; + + $this->cmd_cls[$cmd_cls->name]->cds_set(NULL); + unset($this->cmd_cls[$cmd_cls->name]); return TRUE; } + + function cmd_cls_deregister_all() + { + foreach($this->cmd_cls as $cmd_cls) { + $cmd_cls->cds_set(NULL); + } + + $this->cmd_cls = array(); + } + + function execute() + { + $args = func_get_args(); + + if ($this->dbg > 1) { + printf("CDS_cmd_cls::execute ARGS:\n"); + print_r($args); + } + do { + if (($name = array_shift($args)) === NULL) + break; + array_unshift($args, $this); + + if (!isset($this->cmd_cls[$name])) + break; + + $cmd_cls = $this->cmd_cls[$name]; + + if (($inst = call_user_func_array(array($cmd_cls, "create"), $args)) == FALSE) + break; + + array_push($this->cmd, $inst); + if ($this->dbg > 1) { + printf("CDS_cmd_cls::process - execute push cmd\n"); + print_r($this->cmd); + } + return TRUE; + } while (FALSE); + + return FALSE; + } + + function process() + { + if ($this->dbg > 1) { + printf("CDS_cmd_cls::process - begin\n"); + } + $running = NULL; + $ret = curl_multi_exec($this->mh, $running); + $msgs_in_queue = NULL; + + do { + if ($ret = curl_multi_info_read ($this->mh, $msgs_in_queue)) { + if ($this->dbg > 1) + printf("Info_read miq: %d\n", $msgs_in_queue); + + foreach($this->cmd as $cmd) { + if ($cmd->ch == $ret['handle']) { + $cmd->cmd_cls->cb($cmd, $ret); + break; + } + } + $info = curl_getinfo($ret['handle']); + if ($this->dbg > 1) { + printf("Getinfo:\n"); + print_r($info); + } + } + } while ($msgs_in_queue > 0); + if ($this->dbg > 1) { + printf("CDS_cmd_cls::process - end (queue: %d)\n", $msgs_in_queue); + } + } + } \ No newline at end of file