/** * {@inheritDoc} */ public function sendResponse(Response $response) { $message = array($response->getType(), (string) $response->getHeaders()); if (MessageTypes::isResponseTypeWithResult($response->getType())) { $message[2] = $response->getResultBody(); } try { $this->socket->sendMulti($message); $this->waitingForResponse = false; } catch (\Exception $e) { $this->waitingForResponse = false; throw new TransportException('Unable to send request', null, $e); } }
protected function useResponse(Request $request, Response $response, $filter = null) { if ($request->isExpectingResult()) { $this->serializer->expects($this->once())->method($response->getType() === MessageTypes::ERROR ? 'serializeError' : 'serializeResult')->with($request->getMethodName(), $this->equalTo($response->getResultBody(), 0, 0), $response->getHeaders()->get('content-type'))->will($this->returnValue($response->getResultBody())); } else { $this->serializer->expects($this->never())->method('serializeResult'); } if (!$filter) { $filter = function () { }; } $this->transport->expects($this->once())->method('sendResponse')->with($response)->will($this->returnCallback($filter)); }
/** * @param Response $response * @param Request $request * @return mixed|null * @throws \Wookieb\ZorroRPC\Exception\FormatException */ private function obtainResponse(Response $response, Request $request) { // woah, time to handle error if ($response->getType() === MessageTypes::ERROR) { $this->handleError($response, $request); } // check whether response type is suitable for request type if ($response->getType() !== self::$validResponseType[$request->getType()]) { $msg = sprintf('Invalid response type "%s" for request type "%s"', $response->getType(), $request->getType()); throw new FormatException($msg, $response); } if (!MessageTypes::isResponseTypeWithResult($response->getType())) { return; } return $this->serializer->unserializeResult($request->getMethodName(), $response->getResultBody(), $response->getHeaders()->get('Content-Type')); }
/** * {@inheritDoc} */ public function receiveResponse() { try { $result = $this->socket->recvMulti(); } catch (\ZMQSocketException $e) { throw new TransportException('Cannot receive response', 0, $e); } if ($result === false) { throw new TimeoutException('Timeout (' . $this->getTimeout() . 's) reached'); } if (!isset($result[0])) { throw new FormatException('Invalid response - no response type', $result); } if (!isset($result[1])) { throw new FormatException('Invalid response - no headers', $result); } $response = new Response($result[0]); $response->setHeaders(new Headers(Parser::parseHeaders($result[1]))); if (!MessageTypes::isResponseTypeWithResult($response->getType())) { return $response; } if (!isset($result[2])) { throw new FormatException('Invalid response - no response body', $result); } return $response->setResultBody($result[2]); }