コード例 #1
0
ファイル: PackerTest.php プロジェクト: tolejarz/kafka-talker
 public function testMinSignedInt64()
 {
     $minSignedInt64 = -0x7fffffffffffffff;
     // This test shoud be performed with "-0x8000000000000000" but it fails...
     $packedMinSignedInt64 = Packer::packSignedInt64($minSignedInt64);
     $unpackedMinSignedInt64 = Packer::unpackSignedInt64($packedMinSignedInt64);
     $this->assertSame($minSignedInt64, $unpackedMinSignedInt64);
 }
コード例 #2
0
 public function receive()
 {
     // Read response length
     $responseLength = $this->client->read(4);
     $responseLength = Packer::unpackSignedInt32($responseLength);
     Logger::log('Response length: %s', var_export($responseLength, true));
     // Read response
     $response = $this->client->read($responseLength);
     Logger::log('Response (packed): %s', var_export($response, true));
     $cursor = 0;
     // Read CorrelationId
     $correlationId = Packer::unpackSignedInt32(substr($response, $cursor, 4));
     Logger::log('> CorrelationId: %s', var_export($correlationId, true));
     $cursor += 4;
     // Read Topic count
     $topicCount = Packer::unpackSignedInt32(substr($response, $cursor, 4));
     Logger::log('> Topic count: %s', var_export($topicCount, true));
     $cursor += 4;
     // Read Topics
     $topics = [];
     for ($i = 1; $i <= $topicCount; $i++) {
         // Read Topic length
         $topicLength = Packer::unpackSignedInt16(substr($response, $cursor, 2));
         Logger::log('    > Topic length: %s', var_export($topicLength, true));
         $cursor += 2;
         // Read Topic
         $topic = substr($response, $cursor, $topicLength);
         Logger::log('    > Topic: %s', var_export($topic, true));
         $cursor += $topicLength;
         // Read Parition count
         $partitionCount = Packer::unpackSignedInt32(substr($response, $cursor, 4));
         Logger::log('    > Partition count: %s', var_export($partitionCount, true));
         $cursor += 4;
         // Read Partitions
         $partitions = [];
         for ($j = 1; $j <= $partitionCount; $j++) {
             Logger::log('        > [Partition #%d]', $j);
             // Read Partition
             $partitionId = Packer::unpackSignedInt32(substr($response, $cursor, 4));
             Logger::log('            > PartitionId: %s', var_export($partitionId, true));
             $cursor += 4;
             // Read Offset
             $offset = Packer::unpackSignedInt64(substr($response, $cursor, 8));
             Logger::log('            > Offset: %s', var_export($offset, true));
             $cursor += 8;
             // Read Metadata length
             $metadataLength = Packer::unpackSignedInt16(substr($response, $cursor, 2));
             Logger::log('            > Metadata length: %s', var_export($metadataLength, true));
             $cursor += 2;
             // Read Metadata
             $metadata = substr($response, $cursor, $metadataLength);
             Logger::log('            > Metadata: %s', var_export($metadata, true));
             $cursor += $metadataLength;
             // Read ErrorCode
             $errorCode = Packer::unpackSignedInt16(substr($response, $cursor, 2));
             Logger::log('            > ErrorCode: %s', var_export($errorCode, true));
             $cursor += 2;
             $partitions[$partitionId] = ['PartitionId' => $partitionId, 'Offset' => $offset, 'Metadata' => $metadata, 'ErrorCode' => $errorCode];
         }
         $topics[$topic] = ['Topic' => $topic, 'Partitions' => $partitions];
     }
     return ['CorrelationId' => $correlationId, 'Topics' => $topics];
 }
コード例 #3
0
 public function receive($yield = false)
 {
     // Read response length
     $responseLength = $this->client->read(4);
     $responseLength = Packer::unpackSignedInt32($responseLength);
     Logger::log('Response length: %s', var_export($responseLength, true));
     // Read response
     $response = $this->client->read($responseLength);
     Logger::log('Response (packed): %s', var_export($response, true));
     $cursor = 0;
     // Read CorrelationId
     $correlationId = Packer::unpackSignedInt32(substr($response, $cursor, 4));
     Logger::log('> CorrelationId: %s', var_export($correlationId, true));
     $cursor += 4;
     // Read Topics count
     $topicCount = Packer::unpackSignedInt32(substr($response, $cursor, 4));
     Logger::log('> Topic count: %s', var_export($topicCount, true));
     $cursor += 4;
     // Read Topics
     $topics = [];
     for ($i = 1; $i <= $topicCount; $i++) {
         Logger::log('    > [Topic #%d]', $i);
         // Read Topic length
         $topicLength = Packer::unpackSignedInt16(substr($response, $cursor, 2));
         Logger::log('    > Topic length: %s', var_export($topicLength, true));
         $cursor += 2;
         // Read Topic
         $topic = substr($response, $cursor, $topicLength);
         Logger::log('    > Topic: %s', var_export($topic, true));
         $cursor += $topicLength;
         // Read Partition count
         $partitionCount = Packer::unpackSignedInt32(substr($response, $cursor, 4));
         Logger::log('    > Partition count: %s', var_export($partitionCount, true));
         $cursor += 4;
         // Read Partitions
         $partitions = [];
         for ($j = 1; $j <= $partitionCount; $j++) {
             Logger::log('        > [Partition #%d]', $j);
             // Read Partition
             $partitionId = Packer::unpackSignedInt32(substr($response, $cursor, 4));
             Logger::log('            > PartitionId: %s', var_export($partitionId, true));
             $cursor += 4;
             // Read ErrorCode
             $errorCode = Packer::unpackSignedInt16(substr($response, $cursor, 2));
             Logger::log('                > ErrorCode: %s', var_export($errorCode, true));
             $cursor += 2;
             // Read HighwaterMarkOffset
             $highwaterMarkOffset = Packer::unpackSignedInt64(substr($response, $cursor, 8));
             Logger::log('                > HighwaterMarkOffset: %s', var_export($highwaterMarkOffset, true));
             $cursor += 8;
             // Read MessageSet length
             $messageSetLength = Packer::unpackSignedInt32(substr($response, $cursor, 4));
             Logger::log('                > MessageSet length: %s', var_export($messageSetLength, true));
             $cursor += 4;
             $read = 0;
             $numMessages = 0;
             $messageSet = [];
             while ($read !== $messageSetLength) {
                 Logger::log('                    > [Message #%d]', $numMessages);
                 // Read Offset
                 $offset = Packer::unpackSignedInt64(substr($response, $cursor, 8));
                 Logger::log('                        > Offset: %s', var_export($offset, true));
                 $cursor += 8;
                 $read += 8;
                 // Read Message size
                 $messageSize = Packer::unpackSignedInt32(substr($response, $cursor, 4));
                 Logger::log('                        > Message size: %s', var_export($messageSize, true));
                 $cursor += 4;
                 $read += 4;
                 // Read CRC
                 $crc = Packer::unpackSignedInt32(substr($response, $cursor, 4));
                 Logger::log('                        > CRC: %s', var_export($crc, true));
                 $cursor += 4;
                 $read += 4;
                 // Read MagicByte
                 $magicByte = Packer::unpackSignedInt8(substr($response, $cursor, 1));
                 Logger::log('                        > MagicByte: %s', var_export($magicByte, true));
                 $cursor += 1;
                 $read += 1;
                 // Read Attributes
                 $attributes = Packer::unpackSignedInt8(substr($response, $cursor, 1));
                 Logger::log('                        > Attributes: %s', var_export($attributes, true));
                 $cursor += 1;
                 $read += 1;
                 // Read Key size
                 $keySize = Packer::unpackSignedInt32(substr($response, $cursor, 4));
                 Logger::log('                        > Key size: %s', var_export($keySize, true));
                 $cursor += 4;
                 $read += 4;
                 // Read Key
                 if ($keySize !== -1) {
                     $key = substr($response, $cursor, $keySize);
                     $cursor += $keySize;
                     $read += $keySize;
                 } else {
                     $key = null;
                 }
                 // Read Value length
                 $valueLength = Packer::unpackSignedInt32(substr($response, $cursor, 4));
                 Logger::log('                        > Value length: %s', var_export($valueLength, true));
                 $cursor += 4;
                 $read += 4;
                 // Read Value
                 $value = substr($response, $cursor, $valueLength);
                 Logger::log('                        > Value: %s', var_export($value, true));
                 $cursor += $valueLength;
                 $read += $valueLength;
                 $numMessages++;
                 if ($yield) {
                     (yield ['Offset' => $offset, 'CRC' => $crc, 'MagicByte' => $magicByte, 'Attributes' => $attributes, 'Key' => $key, 'Value' => $value]);
                 }
                 $messageSet[] = ['Offset' => $offset, 'CRC' => $crc, 'MagicByte' => $magicByte, 'Attributes' => $attributes, 'Key' => $key, 'Value' => $value];
             }
             $partitions[] = ['PartitionId' => $partitionId, 'ErrorCode' => $errorCode, 'HighwaterMarkOffset' => $highwaterMarkOffset, 'MessageSet' => $messageSet];
         }
         $topics[] = ['Topic' => $topic, 'Partitions' => $partitions];
     }
     if (!$yield) {
         (yield ['CorrelationId' => $correlationId, 'Topics' => $topics]);
     }
 }