Kafka\Protocol\Decoder::fetchOffsetResponse PHP Method

fetchOffsetResponse() public method

decode fetch offset response
public fetchOffsetResponse ( ) : array
return array
    public function fetchOffsetResponse()
    {
        $result = array();
        $dataLen = self::unpack(self::BIT_B32, $this->stream->read(4, true));
        $dataLen = array_shift($dataLen);
        if (!$dataLen) {
            throw new \Kafka\Exception\Protocol('fetch offset response invalid.');
        }
        $data = $this->stream->read($dataLen, true);
        $offset = 4;
        $topicCount = self::unpack(self::BIT_B32, substr($data, $offset, 4));
        $offset += 4;
        $topicCount = array_shift($topicCount);
        for ($i = 0; $i < $topicCount; $i++) {
            $topicLen = self::unpack(self::BIT_B16, substr($data, $offset, 2));
            // int16 topic name length
            $topicLen = isset($topicLen[1]) ? $topicLen[1] : 0;
            $offset += 2;
            $topicName = substr($data, $offset, $topicLen);
            $offset += $topicLen;
            $partitionCount = self::unpack(self::BIT_B32, substr($data, $offset, 4));
            $partitionCount = isset($partitionCount[1]) ? $partitionCount[1] : 0;
            $offset += 4;
            $result[$topicName] = array();
            for ($j = 0; $j < $partitionCount; $j++) {
                $partitionId = self::unpack(self::BIT_B32, substr($data, $offset, 4));
                $offset += 4;
                $partitionOffset = self::unpack(self::BIT_B64, substr($data, $offset, 8));
                $offset += 8;
                $metaLen = self::unpack(self::BIT_B16, substr($data, $offset, 2));
                $metaLen = array_shift($metaLen);
                $offset += 2;
                $metaData = '';
                if ($metaLen) {
                    $metaData = substr($data, $offset, $metaLen);
                    $offset += $metaLen;
                }
                $errCode = self::unpack(self::BIT_B16_SIGNED, substr($data, $offset, 2));
                $offset += 2;
                $result[$topicName][$partitionId[1]] = array('offset' => $partitionOffset, 'metadata' => $metaData, 'errCode' => $errCode[1]);
            }
        }
        return $result;
    }

Usage Example

Beispiel #1
0
 /**
  * testFetchOffsetResponseNotData
  *
  * @access public
  * @return void
  */
 public function testFetchOffsetResponseNotData()
 {
     $this->setData(Decoder::Khex2bin('00000000'));
     $decoder = new \Kafka\Protocol\Decoder($this->stream);
     try {
         $actual = $decoder->fetchOffsetResponse();
     } catch (\Kafka\Exception\Protocol $e) {
         $this->assertSame('fetch offset response invalid.', $e->getMessage());
     }
 }