/**
  * Processes the request by invoking the request handler that attaches the message to the
  * requested queue in a protected context.
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
  *
  * @return void
  * @throws \Exception Is thrown if the requested message queue is not available
  */
 public function invoke(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     // load the application context
     /** @var \AppserverIo\Appserver\Application\Application $application */
     $application = $servletRequest->getContext();
     // unpack the message
     $message = MessageQueueProtocol::unpack($servletRequest->getBodyContent());
     // load message queue name
     $queueName = $message->getDestination()->getName();
     // lookup the message queue manager and attach the message
     $queueManager = $application->search('QueueContextInterface');
     if ($messageQueue = $queueManager->lookup($queueName)) {
         $messageQueue->attach($message);
     } else {
         throw new \Exception("Can\\'t find queue for message queue {$queueName}");
     }
     // finally dispatch this request, because we have finished processing it
     $servletRequest->setDispatched(true);
 }
 /**
  * Parses the message queue response and returns a response instance.
  *
  * @param string $line The response string to parse
  *
  * @return \AppserverIo\Messaging\QueueResponse The queue response instance
  * @throws \AppserverIo\Psr\Pms\MessageQueueException Is thrown if we found an invalid status code
  */
 public function parseResult($line)
 {
     // parse the header line with
     list($protocolVersion, $statusCode, $message, ) = explode(' ', trim($line));
     // check protocol and version
     $this->checkProtocolAndVersion($protocolVersion);
     // prepare the queue response
     $responseMessages = MessageQueueProtocol::getResponseMessages();
     if (isset($responseMessages[$statusCode])) {
         return new QueueResponse($statusCode, $message);
     }
     // we can't prepare the queue response because of an unknown status code
     throw new MessageQueueException(sprintf('Found unknown status code %d', $statusCode));
 }
 /**
  * Sends a Message to the server by writing it to the socket.
  *
  * @param \AppserverIo\Psr\Pms\MessageInterface $message          Holds the message to send
  * @param boolean                               $validateResponse If this flag is TRUE, the queue connection validates the response code
  *
  * @return \AppserverIo\Messaging\QueueResponse The response of the message queue, or null
  *
  * @throws \Guzzle\Http\Exception\CurlException
  * @throws \Exception
  */
 public function send(MessageInterface $message, $validateResponse = false)
 {
     // connect to the server if necessary
     $this->connect();
     // serialize the message and write it to the socket
     $packed = MessageQueueProtocol::pack($message);
     // invoke the RMC with a number of retries
     $maxRetries = 0;
     $retry = true;
     while ($retry) {
         try {
             // send a POST request
             $request = $this->getSocket()->post($this->getPath(), array('timeout' => 5));
             $request->setBody($packed);
             $response = $request->send();
             $retry = false;
         } catch (CurlException $ce) {
             $maxRetries++;
             if ($maxRetries >= 5) {
                 $retry = false;
                 throw $ce;
             }
         }
     }
     // check if we should validate the response
     if ($validateResponse && $response->getStatusCode() !== 200) {
         throw new \Exception($response->getBody());
     }
 }