Frame.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. *
  4. * Copyright 2005-2006 The Apache Software Foundation
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /* vim: set expandtab tabstop=3 shiftwidth=3: */
  19. /**
  20. * Stomp Frames are messages that are sent and received on a stomp connection.
  21. *
  22. * @package Stomp
  23. */
  24. class StompFrame
  25. {
  26. public $command;
  27. public $headers = array();
  28. public $body;
  29. /**
  30. * Constructor
  31. *
  32. * @param string $command
  33. * @param array $headers
  34. * @param string $body
  35. */
  36. public function __construct ($command = null, $headers = null, $body = null)
  37. {
  38. $this->_init($command, $headers, $body);
  39. }
  40. protected function _init ($command = null, $headers = null, $body = null)
  41. {
  42. $this->command = $command;
  43. if ($headers != null) {
  44. $this->headers = $headers;
  45. }
  46. $this->body = $body;
  47. if ($this->command == 'ERROR') {
  48. require_once 'Exception.php';
  49. throw new StompException($this->headers['message'], 0, $this->body);
  50. }
  51. }
  52. /**
  53. * Convert frame to transportable string
  54. *
  55. * @return string
  56. */
  57. public function __toString()
  58. {
  59. $data = $this->command . "\n";
  60. foreach ($this->headers as $name => $value) {
  61. $data .= $name . ": " . $value . "\n";
  62. }
  63. $data .= "\n";
  64. $data .= $this->body;
  65. return $data .= "\x00";
  66. }
  67. }
  68. ?>