/** * @param $name * @param InputInterface $input * @param OutputInterface $output * * @return Client */ protected function createClient($name, InputInterface $input, OutputInterface $output) { $that = $this; $client = new Client($name); $client->registerLogger(function ($text, $args) use($that, $output) { $that->log($output, array_merge([$text], $args)); }); $client->onError(function (\Exception $e, Client $client) { $client->logException($e); }); unset($input); return $client; }
/** * @param Client $client * * @return $this */ public function run(Client $client) { $client->init(); $context = $client->getContext(); $context->zmqContext = $this->getZmqService()->createContext(); $context->zmqOutgoingSockets = $this->getZmqService()->createSockets($client->getOutgoingSockets(), $context->zmqContext); $client->onMessageSent(function ($socketName, $msg, Client $client) { $sockets = $client->getContext()->zmqOutgoingSockets; if (!isset($sockets[$socketName])) { throw new \RuntimeException(sprintf("Unable to send message: unknown outgoing socket '%s'", $socketName), 500); } /** @var NamedSocket $socket */ $socket = $sockets[$socketName]; $socket->send(json_encode($msg)); $response = null; if ($socket->getSocketType() === \ZMQ::SOCKET_REQ) { $rawMsg = $socket->recv(); $response = @json_decode($rawMsg, true); if (!$response) { throw new \RuntimeException(sprintf("Unable to parse incoming message: %s", $rawMsg), 412); } } return $response; }); $client->execute(); return $this; }