From 7edbe80f9797346b9950db411461f306d0e4b15d Mon Sep 17 00:00:00 2001 From: "Matteo Nastasi (mop)" Date: Mon, 8 Sep 2014 08:09:13 +0200 Subject: [PATCH] partial debug system --- web/Obj/curl-de-sac.phh | 133 +++++++++++++++++++++++++++++++++++++++- webtest/cds_test01.php | 112 ++++++++++++++++++++++++++++++++- 2 files changed, 241 insertions(+), 4 deletions(-) diff --git a/web/Obj/curl-de-sac.phh b/web/Obj/curl-de-sac.phh index 0a1b18d..ac7744d 100644 --- a/web/Obj/curl-de-sac.phh +++ b/web/Obj/curl-de-sac.phh @@ -24,6 +24,17 @@ $G_curl_de_sac_version = "0.1"; +class CDS_cmd { + var $ch; + var $cmd_cls; + + function CDS_cmd($ch, $cmd_cls) + { + $this->ch = $ch; + $this->cmd_cls = $cmd_cls; + } +} + class CDS_cmd_cls { var $name; var $tout; @@ -34,8 +45,36 @@ class CDS_cmd_cls { $this->tout = $tout; } - function cb() + 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,10 +83,19 @@ 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 dbg_set($dbg) + { + $this->dbg = $dbg; } function cmd_register($cmd_cls) @@ -62,4 +110,85 @@ class Curl_de_sac { return TRUE; } + + function cmd_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; + + unset($this->cmd_cls[$cmd_cls->name]); + return TRUE; + } + + function cmd_deregister_all() + { + $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 diff --git a/webtest/cds_test01.php b/webtest/cds_test01.php index f959547..ed005fc 100755 --- a/webtest/cds_test01.php +++ b/webtest/cds_test01.php @@ -1,23 +1,84 @@ #!/usr/bin/php short_data = $short_data; + } +} + class short_cmd_cls extends CDS_cmd_cls { function short_cmd_cls() { parent::__construct("short", 10); } + function create($cds, $url) + { + printf("short::create url:[%s]\n", $url); + + do { + if (($ch = parent::pre_create($url)) == FALSE) + break; + + if (parent::create($cds, $ch) == FALSE) + break; + + $cmd = new short_cmd($ch, $this, "none currently"); + + return $cmd; + } while (FALSE); + + return FALSE; + } + function cb() { printf("short_cb:\n"); } } +class long_cmd extends CDS_cmd { + var $long_data; + + function long_cmd($ch, $cmd_cls, $long_data) + { + parent::__construct($ch, $cmd_cls); + $this->long_data = $long_data; + } +} + class long_cmd_cls extends CDS_cmd_cls { function long_cmd_cls() { - parent::__construct("long", 15); + parent::__construct("long", 10); + } + + function create($cds, $url) + { + printf("long::create url:[%s]\n", $url); + + do { + if (($ch = parent::pre_create($url)) == FALSE) + break; + + if (parent::create($cds, $ch) == FALSE) + break; + + $cmd = new long_cmd($ch, $this, "none currently"); + + return $cmd; + } while (FALSE); + + return FALSE; } function cb() @@ -26,15 +87,17 @@ class long_cmd_cls extends CDS_cmd_cls { } } + function main() { // create cds - $cds = new Curl_de_sac(); + $cds = new Curl_de_sac(999); // create cds_cmd 1 $cmd_cls1 = new short_cmd_cls(); // registrer cds_cmd 1 + printf("Register CLS1\n"); if (($cds->cmd_register($cmd_cls1)) == FALSE) { fprintf(STDERR, "cmd_cls1 registration failed\n"); exit(1); @@ -44,20 +107,65 @@ function main() $cmd_cls2 = new long_cmd_cls(); // register cds_cmd 2 + printf("Register CLS2\n"); if (($cds->cmd_register($cmd_cls2)) == FALSE) { fprintf(STDERR, "cmd_cls2 registration failed\n"); exit(2); } // register cds_cmd 2 (retry) + printf("Re-register CLS2 (must go wrong)\n"); if (($cds->cmd_register($cmd_cls2)) != FALSE) { fprintf(STDERR, "cmd_cls2 re-registration success\n"); exit(3); } + print_r($cds); + printf("Deregister CLS2\n"); + if (($cds->cmd_deregister($cmd_cls2)) == FALSE) { + fprintf(STDERR, "cmd_cls2 deregistration failed\n"); + exit(4); + } + print_r($cds); + + // re-re-register cds_cmd 2 + printf("Re-re-register CLS2\n"); + if (($cds->cmd_register($cmd_cls2)) == FALSE) { + fprintf(STDERR, "cmd_cls2 re-re-registration failed\n"); + exit(5); + } + + printf("Deregister all\n"); + $cds->cmd_deregister_all(); + + // registrer cds_cmd 1 + printf("Register CLS1\n"); + if (($cds->cmd_register($cmd_cls1)) == FALSE) { + fprintf(STDERR, "cmd_cls1 registration failed\n"); + exit(1); + } + + // register cds_cmd 2 + printf("Register CLS2\n"); + if (($cds->cmd_register($cmd_cls2)) == FALSE) { + fprintf(STDERR, "cmd_cls2 registration failed\n"); + exit(2); + } print_r($cds); printf("SUCCESS\n"); + for ($i = 0 ; $i < 10 ; $i++) { + if ($i == 2) { + print("load short\n"); + if ($cds->execute("short", WEBURL.'/short.php') == FALSE) { + printf("push command failed\n"); + exit(123); + } + } + printf("Call process\n"); + $cds->process(); + sleep(1); + } // start loop // print status // if input data execute some command -- 2.17.1