/** * @param array $config */ private function remit(array $config) { if ($config["remit"]["driver"] === "zeromq") { $config["remit"]["server"] += ["host" => "127.0.0.1"]; $config["remit"]["client"] += ["host" => "127.0.0.1"]; $this->server = new ZeroMqServer(new InMemoryLocation($config["remit"]["server"]["host"], $config["remit"]["server"]["port"])); $this->client = new ZeroMqClient(new InMemoryLocation($config["remit"]["client"]["host"], $config["remit"]["client"]["port"])); } $this->server->addListener("r", function ($result, $id) { if (isset($this->deferred[$id])) { $this->deferred[$id]->resolve($result); unset($this->deferred[$id]); } }); $this->server->addListener("e", function ($error, $id) { if (isset($this->deferred[$id])) { $this->deferred[$id]->reject($error); unset($this->deferred[$id]); } }); $this->server->addListener("dd", function () { $this->server->disconnect(); $this->client->disconnect(); }); Loop\periodic(0, function () { $this->server->tick(); }); }
/** * @inheritdoc * * @return bool */ public function tick() { $this->server->tick(); if ($this->manager->tick()) { return true; } $this->server->tick(); if (empty($this->waiting)) { return false; } $next = array_shift($this->waiting); if ($next["type"] === "parallel") { $client = $this->getClient(); $tasks = $next["tasks"]; $tasks = array_map(function ($task) use($client) { if ($task instanceof Task) { return $task; } if ($task instanceof Closure) { $task = new ProcessCallbackTask($task); } if ($task instanceof Task) { return new DoormanTask($task, $client); } return $task; }, $tasks); $tasks = array_filter($tasks, function ($task) { return $task instanceof Task; }); $this->manager->addTaskGroup($tasks); } if ($next["type"] === "synchronous") { foreach ($next["tasks"] as $task) { if ($task instanceof Task) { $handler = $task->getHandler(); $object = new $handler(); if ($object instanceof Handler) { $object = new DoormanHandler($object); $object->handle($task); } } } } return true; }