/** * @param ChannelBaseInterface $channel * @param string $name * @param string|ChannelProtocolInterface $message * @param mixed[] $params */ public function __construct($channel, $name, $message, $params = []) { $this->channel = $channel; $this->name = $name; $this->message = $message instanceof ChannelProtocolInterface ? $message : $this->channel->createProtocol($message); $this->params = ['timeout' => isset($params['timeout']) ? $params['timeout'] : 3.0, 'retriesLimit' => isset($params['retriesLimit']) ? $params['retriesLimit'] : 6, 'retriesInterval' => isset($params['retriesInterval']) ? $params['retriesInterval'] : 2.0]; $this->counter = 1; $this->message->setTimestamp(TimeSupport::now() + ($this->params['retriesInterval'] + $this->params['timeout']) * 1000.0 * $this->params['retriesLimit'], true); }
/** * @param PromiseInterface $promise * @return PromiseInterface */ protected function send(PromiseInterface $promise) { $pid = $this->protocol->getPid(); $origin = $this->protocol->getOrigin(); $message = $this->message; $channel = $this->channel; if ($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 ChannelCompositeInterface $composite * @param ChannelProtocolInterface $protocol */ private function executeProtocol(ChannelCompositeInterface $composite, ChannelProtocolInterface $protocol) { /** * If the json_decode fails, it means the received message is leftover of request response, * hence it should be dropped. */ try { $params = json_decode($protocol->getMessage(), true); $command = array_shift($params); $params['origin'] = $protocol->getOrigin(); $promise = $this->executeCommand($command, $params); } catch (Error $ex) { return; } catch (Exception $ex) { return; } if ($protocol->getType() === Channel::TYPE_REQ) { $promise->then(function ($response) use($composite, $protocol, $command) { return (new Response($composite, $protocol, $response))->call(); }, function ($reason) use($composite, $protocol) { return (new Response($composite, $protocol, $reason))->call(); }, function ($reason) use($composite, $protocol) { return (new Response($composite, $protocol, $reason))->call(); }); } }
/** * @param ChannelCompositeInterface $composite * @param ChannelProtocolInterface $protocol */ private function executeProtocol(ChannelCompositeInterface $composite, ChannelProtocolInterface $protocol) { $params = json_decode($protocol->getMessage(), true); $command = array_shift($params); $params['origin'] = $protocol->getOrigin(); $promise = $this->executeCommand($command, $params); if ($protocol->getType() === Channel::TYPE_REQ) { $promise->then(function ($response) use($composite, $protocol, $command) { return (new Response($composite, $protocol, $response))->call(); }, function ($reason) use($composite, $protocol) { return (new Response($composite, $protocol, $reason))->call(); }, function ($reason) use($composite, $protocol) { return (new Response($composite, $protocol, $reason))->call(); }); } }
/** * @param string $name * @param ChannelProtocolInterface $protocol * @return bool */ public function __invoke($name, ChannelProtocolInterface $protocol) { return StringSupport::match($this->name, $protocol->getDestination()); }
/** * @param $str * @return ChannelProtocolInterface */ public function decode($str) { return $this->protocol->setAll($this->parser->decode($str)); }
/** * @param string $name * @param ChannelProtocolInterface $protocol * @return bool */ public function __invoke($name, ChannelProtocolInterface $protocol) { return $protocol->getPid() === $this->pid; }
/** * @param string $exception * @param ChannelProtocolInterface $protocol * @return bool */ public function __invoke($exception, ChannelProtocolInterface $protocol) { return StringSupport::match($this->exception, $protocol->getException()); }
/** * @param string $name * @param string|ChannelProtocolInterface $message * @param int $flags * @return bool */ protected function handleSendAsync($name, $message, $flags = Channel::MODE_DEFAULT) { if ($message->getType() === '') { $message->setType(Channel::TYPE_SND); } if ($message->getDestination() === '') { $message->setDestination($name); } return $this->output()->handle($name, $message, $flags); }