public function testWriteTable()
 {
     $this->_writer->write_table(array('x-foo' => array('S', 'bar'), 'x-bar' => array('A', array('baz', 'qux')), 'x-baz' => array('I', 42), 'x-true' => array('t', true), 'x-false' => array('t', false)));
     $out = $this->_writer->getvalue();
     $expected = "Gx-fooSbarx-barASbazSquxx-bazI*x-truetx-falset";
     $this->assertEquals($expected, $out);
 }
 /**
  * Returns a new AMQPWriter or mutates the provided $pkt
  *
  * @param string $channel
  * @param int $class_id
  * @param int $weight
  * @param int $body_size
  * @param string $packed_properties
  * @param string $body
  * @param AMQPWriter $pkt
  * @return AMQPWriter
  */
 public function prepare_content($channel, $class_id, $weight, $body_size, $packed_properties, $body, $pkt = null)
 {
     if (empty($pkt)) {
         $pkt = new AMQPWriter();
     }
     // Content already prepared ?
     $key_cache = sprintf('%s|%s|%s|%s', $channel, $packed_properties, $class_id, $weight);
     if (!isset($this->prepare_content_cache[$key_cache])) {
         $w = new AMQPWriter();
         $w->write_octet(2);
         $w->write_short($channel);
         $w->write_long(mb_strlen($packed_properties, 'ASCII') + 12);
         $w->write_short($class_id);
         $w->write_short($weight);
         $this->prepare_content_cache[$key_cache] = $w->getvalue();
         if (count($this->prepare_content_cache) > $this->prepare_content_cache_max_size) {
             reset($this->prepare_content_cache);
             $old_key = key($this->prepare_content_cache);
             unset($this->prepare_content_cache[$old_key]);
         }
     }
     $pkt->write($this->prepare_content_cache[$key_cache]);
     $pkt->write_longlong($body_size);
     $pkt->write($packed_properties);
     $pkt->write_octet(0xce);
     // memory efficiency: walk the string instead of biting
     // it. good for very large packets (close in size to
     // memory_limit setting)
     $position = 0;
     $bodyLength = mb_strlen($body, 'ASCII');
     while ($position < $bodyLength) {
         $payload = mb_substr($body, $position, $this->frame_max - 8, 'ASCII');
         $position += $this->frame_max - 8;
         $pkt->write_octet(3);
         $pkt->write_short($channel);
         $pkt->write_long(mb_strlen($payload, 'ASCII'));
         $pkt->write($payload);
         $pkt->write_octet(0xce);
     }
     return $pkt;
 }
 /**
  * returns a new AMQPWriter or mutates the provided $pkt
  *
  * @param string $channel
  * @param int $class_id
  * @param int $weight
  * @param int $body_size
  * @param string $packed_properties
  * @param string $body
  * @param AMQPWriter $pkt
  * @return AMQPWriter
  */
 public function prepare_content($channel, $class_id, $weight, $body_size, $packed_properties, $body, $pkt = null)
 {
     if (empty($pkt)) {
         $pkt = new AMQPWriter();
     }
     // Content already prepared ?
     $key_cache = "{$channel}|{$packed_properties}|{$class_id}|{$weight}";
     if (!isset($this->prepare_content_cache[$key_cache])) {
         $w = new AMQPWriter();
         $w->write_octet(2);
         $w->write_short($channel);
         $w->write_long(mb_strlen($packed_properties, 'ASCII') + 12);
         $w->write_short($class_id);
         $w->write_short($weight);
         $this->prepare_content_cache[$key_cache] = $w->getvalue();
         if (count($this->prepare_content_cache) > $this->prepare_content_cache_max_size) {
             reset($this->prepare_content_cache);
             $old_key = key($this->prepare_content_cache);
             unset($this->prepare_content_cache[$old_key]);
         }
     }
     $pkt->write($this->prepare_content_cache[$key_cache]);
     $pkt->write_longlong($body_size);
     $pkt->write($packed_properties);
     $pkt->write_octet(0xce);
     while ($body) {
         $bodyStart = $this->frame_max - 8;
         $payload = mb_substr($body, 0, $bodyStart, 'ASCII');
         $body = mb_substr($body, $bodyStart, mb_strlen($body, 'ASCII') - $bodyStart, 'ASCII');
         $pkt->write_octet(3);
         $pkt->write_short($channel);
         $pkt->write_long(mb_strlen($payload, 'ASCII'));
         $pkt->write($payload);
         $pkt->write_octet(0xce);
     }
     return $pkt;
 }
 /**
  * serialize the 'properties' attribute (a dictionary) into the
  * raw bytes making up a set of property flags and a property
  * list, suitable for putting into a content frame header.
  */
 public function serialize_properties()
 {
     $shift = 15;
     $flag_bits = 0;
     $flags = array();
     $raw_bytes = new AMQPWriter();
     foreach ($this->prop_types as $key => $proptype) {
         if (isset($this->properties[$key])) {
             $val = $this->properties[$key];
         } else {
             $val = null;
         }
         if ($val != null) {
             if ($shift == 0) {
                 $flags[] = $flag_bits;
                 $flag_bits = 0;
                 $shift = 15;
             }
             $flag_bits |= 1 << $shift;
             if ($proptype != "bit") {
                 $raw_bytes->{'write_' . $proptype}($val);
             }
         }
         $shift -= 1;
     }
     $flags[] = $flag_bits;
     $result = new AMQPWriter();
     foreach ($flags as $flag_bits) {
         $result->write_short($flag_bits);
     }
     $result->write($raw_bytes->getvalue());
     return $result->getvalue();
 }
Exemple #5
0
 /**
  * Sends a heartbeat message
  */
 protected function write_heartbeat()
 {
     $pkt = new AMQPWriter();
     $pkt->write_octet(8);
     $pkt->write_short(0);
     $pkt->write_long(0);
     $pkt->write_octet(0xce);
     $this->write($pkt->getvalue());
 }
Exemple #6
0
 /**
  * Serializes the 'properties' attribute (a dictionary) into the
  * raw bytes making up a set of property flags and a property
  * list, suitable for putting into a content frame header.
  *
  * @return string
  * @todo Inject the AMQPWriter to make the method easier to test
  */
 public function serialize_properties()
 {
     if (!empty($this->serialized_properties)) {
         return $this->serialized_properties;
     }
     $shift = 15;
     $flag_bits = 0;
     $flags = array();
     $raw_bytes = new AMQPWriter();
     foreach ($this->prop_types as $key => $prototype) {
         $val = isset($this->properties[$key]) ? $this->properties[$key] : null;
         // Very important: PHP type eval is weak, use the === to test the
         // value content. Zero or false value should not be removed
         if ($val === null) {
             $shift -= 1;
             continue;
         }
         if ($shift === 0) {
             $flags[] = $flag_bits;
             $flag_bits = 0;
             $shift = 15;
         }
         $flag_bits |= 1 << $shift;
         if ($prototype != 'bit') {
             $raw_bytes->{'write_' . $prototype}($val);
         }
         $shift -= 1;
     }
     $flags[] = $flag_bits;
     $result = new AMQPWriter();
     foreach ($flags as $flag_bits) {
         $result->write_short($flag_bits);
     }
     $result->write($raw_bytes->getvalue());
     $this->serialized_properties = $result->getvalue();
     return $this->serialized_properties;
 }
Exemple #7
0
 protected function writeAndRead($v, $write_method, $read_method)
 {
     $w = new AMQPWriter();
     $w->{$write_method}($v);
     $r = new AMQPReader($w->getvalue());
     $this->assertEquals($v, $r->{$read_method}());
 }
Exemple #8
0
 public function testTableWriteReadCollection()
 {
     $w = new AMQPWriter();
     $w->write_table(new AMQPTable(array('long' => 12345, 'long_neg' => -12345, 'longlong' => 3000000000, 'longlong_neg' => -3000000000, 'float_low' => 9.2233720368548, 'float_high' => (double) 9.2233720368548E+18, 'bool_true' => true, 'bool_false' => false, 'array' => array(1, 2, 3, 'foo', array('bar' => 'baz'), array('boo', false, 5), true), 'array_empty' => array(), 'table' => array('foo' => 'bar', 'baz' => 'boo', 'bool' => true, 'tbl' => array('bar' => 'baz'), 'arr' => array('boo', false, 5)), 'table_num' => array(1 => 5, 3 => 'foo', 786 => 674), 'array_nested' => array(1, array(2, array(3, array(4)))), 'table_nested' => array('i' => 1, 'n' => array('i' => 2, 'n' => array('i' => 3, 'n' => array('i' => 4)))))));
     $r = new AMQPReader($w->getvalue());
     //type casting - thanks to ancient phpunit on travis
     $this->assertEquals(array('long' => 12345, 'long_neg' => -12345, 'longlong' => (string) 3000000000, 'longlong_neg' => (string) -3000000000, 'float_low' => (string) (double) 9.2233720368548, 'float_high' => (string) (double) 9.2233720368548E+18, 'bool_true' => true, 'bool_false' => false, 'array' => array(1, 2, 3, 'foo', array('bar' => 'baz'), array('boo', false, 5), true), 'array_empty' => array(), 'table' => array('foo' => 'bar', 'baz' => 'boo', 'bool' => true, 'tbl' => array('bar' => 'baz'), 'arr' => array('boo', false, 5)), 'table_num' => array(1 => 5, 3 => 'foo', 786 => 674), 'array_nested' => array(1, array(2, array(3, array(4)))), 'table_nested' => array('i' => 1, 'n' => array('i' => 2, 'n' => array('i' => 3, 'n' => array('i' => 4))))), $r->read_table(true)->getNativeData());
 }
 /**
  * serialize the 'properties' attribute (a dictionary) into the
  * raw bytes making up a set of property flags and a property
  * list, suitable for putting into a content frame header.
  */
 public function serialize_properties()
 {
     if (!empty($this->serialized_properties)) {
         return $this->serialized_properties;
     }
     $shift = 15;
     $flag_bits = 0;
     $flags = array();
     $raw_bytes = new AMQPWriter();
     foreach ($this->prop_types as $key => $prototype) {
         $val = isset($this->properties[$key]) ? $this->properties[$key] : null;
         if ($val === null) {
             $shift -= 1;
             continue;
         }
         if ($shift === 0) {
             $flags[] = $flag_bits;
             $flag_bits = 0;
             $shift = 15;
         }
         $flag_bits |= 1 << $shift;
         if ($prototype != 'bit') {
             $raw_bytes->{'write_' . $prototype}($val);
         }
         $shift -= 1;
     }
     $flags[] = $flag_bits;
     $result = new AMQPWriter();
     foreach ($flags as $flag_bits) {
         $result->write_short($flag_bits);
     }
     $result->write($raw_bytes->getvalue());
     $this->serialized_properties = $result->getvalue();
     return $this->serialized_properties;
 }