/** * @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->getOrigin()); }