/** * @param ChannelInterface $channel * @param string $name * @param string|ProtocolInterface $message * @param mixed[] $params */ public function __construct($channel, $name, $message, $params = []) { $this->channel = $channel; $this->name = $name; $this->message = $message instanceof ProtocolInterface ? $message : $this->channel->createProtocol($message); $this->params = ['timeout' => isset($params['timeout']) ? $params['timeout'] : 2.0, 'retriesLimit' => isset($params['retriesLimit']) ? $params['retriesLimit'] : 10, 'retriesInterval' => isset($params['retriesInterval']) ? $params['retriesInterval'] : 0.25]; $this->counter = 1; $this->message->setTimestamp(TimeSupport::now() + ($this->params['retriesInterval'] + $this->params['timeout']) * 1000.0 * $this->params['retriesLimit'], true); }
/** * 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 string $name * @param ProtocolInterface $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->getOutput()->handle($name, $message, $flags); }
/** * @param RuntimeContainerInterface $runtime * @param ChannelCompositeInterface $composite * @param ProtocolInterface $protocol */ private function executeProtocol(RuntimeContainerInterface $runtime, ChannelCompositeInterface $composite, ProtocolInterface $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(); if (!$runtime->isFailed() || isset($params['hash']) && $runtime->getHash() === $params['hash']) { $promise = $this->executeCommand($command, $params); } else { $promise = Promise::doReject(new RejectionException('Container is currently in failed state and cannot execute any tasks.')); } 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(); }); } } catch (Error $ex) { return; } catch (Exception $ex) { return; } }
/** * @param string $name * @param ProtocolInterface $protocol * @return bool */ public function __invoke($name, ProtocolInterface $protocol) { return StringSupport::match($this->name, $protocol->getOrigin()); }
/** * @override * @inheritDoc */ public function decode($str) { return $this->protocol->setAll($this->parser->decode($str)); }
/** * @param string $exception * @param ProtocolInterface $protocol * @return bool */ public function __invoke($exception, ProtocolInterface $protocol) { return StringSupport::match($this->exception, $protocol->getException()); }
/** * @param ChannelCompositeInterface $composite * @param ProtocolInterface $protocol */ private function executeProtocol(ChannelCompositeInterface $composite, ProtocolInterface $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 ProtocolInterface $protocol * @return bool */ protected function handleReceiveResponse(ProtocolInterface $protocol) { $pid = $protocol->getPid(); if (!$this->existsRequest($pid)) { return false; } $message = $protocol->getMessage(); $exception = $protocol->getException(); if ($exception === '') { $this->resolveRequest($pid, $message); } else { if ($exception === TaskIncompleteException::class) { $this->cancelRequest($pid, new ThrowableProxy([$exception, $message])); } else { $this->rejectRequest($pid, new ThrowableProxy([$exception, $message])); } } return true; }
/** * @param string $name * @param ProtocolInterface $protocol * @return bool */ public function __invoke($name, ProtocolInterface $protocol) { return $protocol->getPid() === $this->pid; }