-use http\request\Pool as HttpRequestPool;
-use http\Request as HttpRequest;
-
-$factory = new http\request\Factory("curl", array("requestPoolClass" => "pool", "requestClass" => "request"));
-
-class pool extends HttpRequestPool {
- private $url;
- private $cnt;
-
- private $factory;
-
- static function fetch($factory, $url, $n, $c, $e, $p) {
- $pool = $factory->createPool();
- $pool->factory = $factory;
- $pool->url = $url;
- $pool->cnt = $n;
-
- $pool->enablePipelining($p);
- $pool->enableEvents($e);
-
- for ($i = 0; $i < $c; ++$i) {
- $pool->push();
- }
- try {
- $pool->send();
- } catch (Exception $ex) {
- echo $ex, "\n";
- }
- }
-
- function push() {
- if ($this->cnt > 0) {
- $this->factory->createRequest()->init($this, $this->url)->id = $this->cnt--;
- }
- }
-}
-
-class request extends HttpRequest implements SplObserver {
- static $counter = 0;
-
- public $id;
- private $pool;
-
- function init(pool $pool, $url) {
- $this->setUrl($url);
- $this->pool = $pool;
- $this->attach($this);
- $pool->attach($this);
- return $this;
- }
-
- function update(SplSubject $r) {
- if ($r->getProgress()->finished) {
- ++self::$counter;
- $this->pool->detach($this);
- $this->detach($this);
- $this->pool->push();
- }
- }
-}
-