/** * @expectedException \Stomp\Exception\StompException */ public function testWaitForReceiptWillThrowExceptionOnIdMissmatch() { $receiptFrame = new Frame('RECEIPT'); $receiptFrame->setHeader('receipt-id', 'not-matching-id'); $stomp = $this->getStompWithInjectedMockedConnectionReadResult($receiptFrame); $waitForReceipt = new ReflectionMethod($stomp, 'waitForReceipt'); $waitForReceipt->setAccessible(true); // expect a receipt for another id $waitForReceipt->invoke($stomp, 'your-id'); }
protected function _throwStompException(Frame $frame) { $headers = $frame->getHeaders(); if (isset($headers['message'])) { $message = $headers['message']; } else { $message = $frame->getCommand(); foreach ($headers->getHeaders() as $key => $value) { $message .= ' ' . $key . ' ' . $value . ';'; } } if ($frame->getBody()) { $message .= ' ' . $frame->getBody(); } throw new StompException($message); }
/** * Constructor * * @param Frame|string $msg * @param array $headers */ public function __construct($msg, array $headers = array()) { if ($msg instanceof Frame) { parent::__construct($msg->command, $msg->headers, $msg->body); $this->map = json_decode($msg->body, true); } else { parent::__construct('SEND', $headers, $msg); $this->headers['transformation'] = 'jms-map-json'; $this->body = json_encode($msg); } }
/** * Acknowledge consumption of a message from a subscription * Note: This operation is always asynchronous * * @param string|Frame $message ID to ack * @param string $transactionId * @return boolean * @throws StompException */ public function ack($message, $transactionId = null) { if ($message instanceof Frame) { return $this->sendFrame($this->protocol->getAckFrame($message->getMessageId(), $transactionId), false); } else { return $this->sendFrame($this->protocol->getAckFrame($message, $transactionId), false); } }
/** * Read frame * @return bool|Frame */ public function read($timeout = 0) { $this->setTimeout($timeout); $this->isConnected(); // as per protocol COMMAND is 1st \n terminated then headers also \n terminated // COMMAND and header block are seperated by a blank line. $headers_rows = array(); // read command and headers while (($line = @fgets($this->_socket)) !== false) { $headers_rows[] = $line; if (rtrim($line) === '') { break; } } if (count($headers_rows) > 0) { $command = trim($headers_rows[0]); unset($headers_rows[0]); $headers = Frame::extractHeaders($headers_rows); $response = ''; if (!isset($headers[Frame::CONTENT_LENGTH])) { // read till we hit the end of frame marker do { $chunk = @fgets($this->_socket); if ($chunk === false || strlen($chunk) === 0) { $this->_checkSocketReadTimeout(); break; } if (substr($chunk, -2) === Frame::END_OF_FRAME) { // add the chunk above to the result before returning $response .= $chunk; break; } $response .= $chunk; } while (feof($this->_socket) === false); } else { // we have a content-length header set $contentLength = $headers[Frame::CONTENT_LENGTH] + 2; $current_pos = ftell($this->_socket); $chunk = ''; for ($read_to = $current_pos + $contentLength; $read_to > $current_pos; $current_pos = ftell($this->_socket)) { $chunk = fread($this->_socket, $read_to - $current_pos); if ($chunk === false || strlen($chunk) === 0) { $this->_checkSocketReadTimeout(); break; } $response .= $chunk; // Break if the connection ended prematurely if (feof($this->_socket)) { break; } } } return new Frame($command, substr($response, 0, -2), $headers); } else { return false; } }
/** * Write frame to server. * * @param Frame $stompFrame * @return boolean * @throws ConnectionException */ public function writeFrame(Frame $stompFrame) { if (!$this->isConnected()) { throw new ConnectionException('Not connected to any server.', $this->activeHost); } $data = $stompFrame->__toString(); if (!@fwrite($this->connection, $data, strlen($data))) { throw new ConnectionException('Was not possible to write frame!', $this->activeHost); } return true; }
/** @test */ public function shouldConvertFrameWithoutBodyToString() { $frame = new Frame('SEND', array('destination' => '/queue/a')); $result = $frame->__toString(); $this->assertEquals("SEND\ndestination:/queue/a\n\n", $result); }
public function testParseHeaders() { $this->assertEquals(array('version' => '1.0'), \Stomp\Frame::extractHeaders(array('version:1.0'))); $this->assertEquals(array('server' => 'apache-apollo/1.5'), \Stomp\Frame::extractHeaders(array('server:apache-apollo/1.5'))); $this->assertEquals(array('destination' => '/queue/a', 'content-type' => 'text/plain'), \Stomp\Frame::extractHeaders(array('destination:/queue/a', 'content-type:text/plain'))); }