/** * Constructor. * @return void */ public function init() { $req = $this; $job = $this->job = new ComplexJob(function () use($req) { // called when job is done $req->wakeup(); // wake up the request immediately }); $job('query', function ($name, $job) { // registering job named 'showvar' DNSClient::getInstance()->get('phpdaemon.net', function ($response) use($name, $job) { $job->setResult($name, $response); }); }); $job('resolve', function ($name, $job) { // registering job named 'showvar' DNSClient::getInstance()->resolve('phpdaemon.net', function ($ip) use($name, $job) { $job->setResult($name, array('phpdaemon.net resolved to' => $ip)); }); }); $job(); // let the fun begin $this->sleep(5, true); // setting timeout }
public function connect($url, $cb) { $u = $this->parseUrl($url); if (isset($u['user'])) { $this->user = $u['user']; } $this->url = $url; $this->scheme = $u['scheme']; $this->host = $u['host']; $this->port = $u['port']; if (isset($u['pass'])) { $this->password = $u['pass']; } if (isset($u['path'])) { $this->path = ltrim($u['path'], '/'); } if ($cb !== null) { $this->onConnected($cb); } $conn = $this; if ($this->port !== 0 && @inet_pton($this->host) === false) { // dirty condition check DNSClient::getInstance()->resolve($this->host, function ($real) use($conn) { if ($real === false) { Daemon::log(get_class($conn) . '->connectTo: enable to resolve hostname: ' . $conn->host); return; } $conn->hostReal = $real; $conn->connectTo($conn->hostReal, $conn->port); }); } else { $conn->hostReal = $conn->host; $conn->connectTo($conn->hostReal, $conn->port); } }