/** * @param ChannelInterface $channel */ protected function applyConsoleController(ChannelInterface $channel) { $router = $channel->getInput(); $router->addDefault(new RuleHandler(function ($params) { })); $router = $channel->getOutput(); $router->addDefault(new RuleHandler(function ($params) use($channel) { $channel->push($params['alias'], $params['protocol'], $params['flags'], $params['success'], $params['failure'], $params['cancel'], $params['timeout']); })); }
/** * @param ChannelCompositeInterface $channel * @param ChannelInterface $console */ private function applyConsoleRouting(ChannelCompositeInterface $channel, ChannelInterface $console) { $master = $channel->getBus('master'); $router = $console->getInput(); $router->addDefault(new RuleHandler(function ($params) use($master) { $master->receive($params['alias'], $params['protocol']); })); $router = $console->getOutput(); $router->addDefault(new RuleHandler(function ($params) use($channel) { $channel->push($params['alias'], $params['protocol'], $params['flags'], $params['success'], $params['failure'], $params['cancel'], $params['timeout']); })); }
/** * @override * @inheritDoc */ protected function informServer($commandParent, $commandName, $commandParams = []) { $protocol = $this->channel->createProtocol(new RuntimeCommand($commandName, $commandParams)); if ($commandParent !== null) { $protocol->setDestination($commandParent); } $req = new Request($this->channel, $this->receiver, $protocol, ['timeout' => 2, 'retriesLimit' => 10, 'retriesInterval' => 1]); return $req->call(); }
/** * Send the request using passed Promise. * * @param PromiseInterface $promise * @return PromiseInterface * @resolves mixed * @rejects Error|Exception|string|null * @cancels Error|Exception|string|null */ protected function send(PromiseInterface $promise) { $pid = $this->protocol->getPid(); $origin = $this->protocol->getOrigin(); $message = $this->message; $channel = $this->channel; if ($message instanceof Error || $message instanceof Exception) { $answer = $channel->createProtocol($message->getMessage())->setPid($pid, true)->setException(get_class($message), true); } else { $answer = $channel->createProtocol($message)->setPid($pid, true); } $this->channel->send($origin, $answer, Channel::MODE_BUFFER_ONLINE); return $promise->resolve(); }
/** * @param PromiseInterface $promise */ private function retry(PromiseInterface $promise) { if ($this->counter >= $this->params['retriesLimit']) { $promise->reject(new ThrowableProxy(new TimeoutException('No response was received during specified timeout.'))); } else { if ($this->params['retriesInterval'] > 0) { $this->counter++; $this->channel->getLoop()->addTimer($this->params['retriesInterval'], function () use($promise) { $this->send($promise); }); } else { $this->counter++; $this->channel->getLoop()->onTick(function () use($promise) { $this->send($promise); }); } } }
/** * @param RuntimeContainerInterface $runtime * @param ChannelCompositeInterface $composite * @param ChannelInterface $console */ private function applyRootRouting(RuntimeContainerInterface $runtime, ChannelCompositeInterface $composite, ChannelInterface $console) { $master = $composite->getBus('master'); $slave = $composite->getBus('slave'); $router = $composite->getInput(); $router->addDefault(new RuleHandler(function ($params) { return true; })); $router = $composite->getOutput(); $router->addDefault(function ($receiver, ProtocolInterface $protocol, $flags, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) use($runtime, $slave, $console) { if ($receiver === Runtime::RESERVED_CONSOLE_CLIENT || $protocol->getDestination() === Runtime::RESERVED_CONSOLE_CLIENT) { $console->push(Runtime::RESERVED_CONSOLE_CLIENT, $protocol, $flags, $success, $failure, $cancel, $timeout); } else { if ($runtime->getManager()->existsRuntime($receiver) || $slave->isConnected($receiver)) { $slave->push($receiver, $protocol, $flags, $success, $failure, $cancel, $timeout); } else { $slave->push($slave->getConnected(), $protocol, $flags, $success, $failure, $cancel, $timeout); } } }); $router = $master->getInput(); $router->addRule(new RuleMatchDestination($master->getName()), new RuleHandler(function ($params) use($runtime, $composite) { $this->executeProtocol($runtime, $composite, $params['protocol']); })); $router->addDefault(new RuleHandler(function ($params) use($slave) { $slave->push($slave->getConnected(), $params['protocol'], $params['flags']); })); $router = $slave->getInput(); $router->addRule(new RuleMatchDestination($slave->getName()), new RuleHandler(function ($params) use($runtime, $composite) { $this->executeProtocol($runtime, $composite, $params['protocol']); })); $router->addDefault(new RuleHandler(function ($params) use($runtime, $slave, $console) { $receiver = $params['alias']; $protocol = $params['protocol']; if ($receiver === Runtime::RESERVED_CONSOLE_CLIENT || $protocol->getDestination() === Runtime::RESERVED_CONSOLE_CLIENT) { $console->push(Runtime::RESERVED_CONSOLE_CLIENT, $protocol, $params['flags']); } else { $slave->push($slave->getConnected(), $params['protocol'], $params['flags']); } })); $router = $master->getOutput(); $router->addDefault(function ($sender, ProtocolInterface $protocol, $flags, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) use($master) { $master->push($sender, $protocol, $flags, $success, $failure, $cancel, $timeout); }); $router = $slave->getOutput(); $router->addDefault(function ($sender, ProtocolInterface $protocol, $flags, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) use($slave) { $slave->push($sender, $protocol, $flags, $success, $failure, $cancel, $timeout); }); }
/** * @override * @inheritDoc */ public function sendMessage($alias, $message, $flags = Channel::MODE_DEFAULT) { $result = $this->channel->send($alias, $message, $flags); return Promise::doResolve($result); }
/** * @param Error|Exception $ex * @param mixed[] $params * @return mixed */ protected function solver($ex, $params = []) { $hash = isset($params['hash']) ? $params['hash'] : ''; return $this->channel->send($params['origin'], new RuntimeCommand('container:continue', ['hash' => $hash])); }