X-Git-Url: http://mop.ddnsfree.com/gitweb/?a=blobdiff_plain;f=web%2FObj%2Fcurl-de-sac.phh;h=38fcc8ce8a17433bb823cbd379540b43a19e9785;hb=c7d496408f733c5cac096cd7ebebdeaeaf3d051c;hp=4ef36444d6f155a818d963b85de68dfcb7e638cc;hpb=2630bbd10b83238863e371681cc7d272e73a3f39;p=curl-de-sac.git diff --git a/web/Obj/curl-de-sac.phh b/web/Obj/curl-de-sac.phh index 4ef3644..38fcc8c 100644 --- a/web/Obj/curl-de-sac.phh +++ b/web/Obj/curl-de-sac.phh @@ -22,16 +22,24 @@ * */ -$G_curl_de_sac_version = "0.1"; +define('CURL_DE_SAC_VERS', '0.1'); +$G_curl_de_sac_vers = CURL_DE_SAC_VERS; class CDS_cmd { var $cmd_cls; var $ch; + var $tlimit; function CDS_cmd($cmd_cls, $ch) { $this->cmd_cls = $cmd_cls; $this->ch = $ch; + $this->tlimit = time() + $cmd_cls->tout; + } + + function ch_get() + { + return ($this->ch); } function dbg_get() @@ -60,14 +68,23 @@ class CDS_cmd_cls { $this->cds = $cds; } - static function pre_create($url) + static function pre_create($cds, $url, $opts=NULL) { + if ($cds->dbg_get() > 2) { printf("CURL: curl_init\n"); } 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); - + if ($opts == NULL) { + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_FORBID_REUSE, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close')); + } + else { + foreach($opts as $opt => $value) { + curl_setopt($ch, $opt, $value); + } + } return ($ch); } @@ -75,24 +92,33 @@ class CDS_cmd_cls { { if ($cds->dbg > 2) { printf("CDS_cmd_cls::create - begin\n"); - print_r($ch); + printf("CURL: curl_multi_add_handle\n"); } 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"); - } + if ($cds->dbg > 2) { printf("CDS_cmd_cls::create - end\n"); } return (TRUE); } - function cb($cmd, $ret) + function process($cmd, $ret) { - print "THIS MUST BE IMPLEMENTED"; + fprintf(STDERR, "process MUST BE IMPLEMENTED"); exit(123); } + + function timeout($cmd) + { + fprintf(STDERR, "timeout MUST BE IMPLEMENTED"); + exit(123); + } + + function dbg_get() + { + return $this->cds->dbg; + } } class Curl_de_sac { @@ -102,6 +128,7 @@ class Curl_de_sac { var $dbg; function Curl_de_sac($dbg=0) { + if ($dbg > 2) { printf("CURL: curl_multi_init\n"); } $this->mh = curl_multi_init(); $this->cmd_cls = array(); $this->cmd = array(); @@ -154,6 +181,26 @@ class Curl_de_sac { $this->cmd_cls = array(); } + + function cleanup($key) + { + $cmd = $this->cmd[$key]; + + if ($this->dbg > 2) { + printf("cleanup\n"); + printf("CURL: curl_multi_remove_handle:\n"); + print_r($cmd->ch_get()); + printf("\n"); + } + // return 0 on SUCCESS or CURLM_XXX in other cases + if (($ret = curl_multi_remove_handle($this->mh, $cmd->ch_get())) != 0) { + fprintf(STDERR, "CURL: curl_multi_remove_handle FAILED (%d)\n", $ret); + } + if ($this->dbg > 2) { printf("CURL: curl_close\n"); } + curl_close($cmd->ch_get()); + unset($this->cmd[$key]); + } + function execute() { $args = func_get_args(); @@ -172,50 +219,65 @@ class Curl_de_sac { $cmd_cls = $this->cmd_cls[$name]; - if (($inst = call_user_func_array(array($cmd_cls, "create"), $args)) == FALSE) + // custom create now can return synchronously returning true instead of a + // "command instance class" instance or false if any error occurs + $inst = call_user_func_array(array($cmd_cls, "create"), $args); + if (is_bool($inst)) break; - array_push($this->cmd, $inst); - if ($this->dbg > 1) { - printf("CDS_cmd_cls::process - execute push cmd\n"); - print_r($this->cmd); - } + if ($this->dbg > 1) { printf("CDS_cmd_cls::process - execute push cmd\n"); } + if (($this->dbg & 1) == 1) { print_r($this); } + return TRUE; } while (FALSE); - return FALSE; + return $inst; } - function process() + function process($curtime=0) { - if ($this->dbg > 1) { - printf("CDS_cmd_cls::process - begin\n"); + if ($curtime == 0) { + $curtime = time(); } + if ($this->dbg > 1) { printf("CDS_cmd_cls::process - begin\n"); } $running = NULL; + + if ($this->dbg > 2) { printf("CURL: curl_multi_exec\n"); } $ret = curl_multi_exec($this->mh, $running); $msgs_in_queue = NULL; do { + if ($this->dbg > 2) { printf("CURL: curl_multi_info_read\n"); } + 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; - } - } + if ($this->dbg > 1) { printf("Info_read miq: %d\n", $msgs_in_queue); } + if ($this->dbg > 2) { printf("CURL: curl_getinfo\n"); } + $info = curl_getinfo($ret['handle']); if ($this->dbg > 1) { printf("Getinfo:\n"); print_r($info); } + + foreach($this->cmd as $key => $cmd) { + if ($cmd->ch == $ret['handle']) { + if ($cmd->cmd_cls->process($cmd, $ret) == TRUE) { + $this->cleanup($key); + } + break; + } + } } } while ($msgs_in_queue > 0); - if ($this->dbg > 1) { - printf("CDS_cmd_cls::process - end (queue: %d)\n", $msgs_in_queue); + foreach ($this->cmd as $key => $cmd) { + if ($this->dbg > 2) { printf("Check tout, curr: %d tlimit %d\n", $curtime, $cmd->tlimit); } + if ($curtime > $cmd->tlimit) { + if ($this->dbg > 2) { printf("TIMEOUT REACHED!\n"); } + $cmd->cmd_cls->timeout($cmd); + $this->cleanup($key); + } } + if ($this->dbg > 1) { printf("CDS_cmd_cls::process - end (queue: %d)\n", $msgs_in_queue); } } } \ No newline at end of file