コード例 #1
0
ファイル: PackerTest.php プロジェクト: tolejarz/kafka-talker
 public function testMinSignedInt32()
 {
     $minSignedInt32 = -0x80000000;
     $packedMinSignedInt32 = Packer::packSignedInt32($minSignedInt32);
     $unpackedMinSignedInt32 = Packer::unpackSignedInt32($packedMinSignedInt32);
     $this->assertSame($minSignedInt32, $unpackedMinSignedInt32);
 }
コード例 #2
0
 public function send($groupId, $sessionTimeout, $memberId, $protocolType, $groupProtocols)
 {
     // Add header (ApiKey, ApiVersion, CorrelationId, ClientId)
     $data = $this->buildHeader();
     // Add GroupId
     $data .= Packer::packStringSignedInt16($groupId);
     // Add SessionTimeout
     $data .= Packer::packSignedInt32($sessionTimeout);
     // Add MemberId
     $data .= Packer::packStringSignedInt16($memberId);
     // Add ProtocolType
     $data .= Packer::packStringSignedInt16($protocolType);
     // Add GroupProtocol count
     $data .= Packer::packSignedInt32(count($groupProtocols));
     // Add GroupProtocols
     foreach ($groupProtocols as $protocolName => $protocolMetadata) {
         // Add ProtocolName
         $data .= Packer::packStringSignedInt16($protocolName);
         // Add ProtocolMetadata
         $data .= Packer::packStringSignedInt32($protocolMetadata);
     }
     // Concat data length (32 bits) and data
     $data = Packer::packStringSignedInt32($data);
     // Sent data
     return $this->client->write($data);
 }
コード例 #3
0
 public function send($consumerGroup, $topics)
 {
     // Add header (ApiKey, ApiVersion, CorrelationId, ClientId)
     $data = $this->buildHeader();
     // Add ConsumerGroup
     $data .= Packer::packStringSignedInt16($consumerGroup);
     // Add Topic count
     $data .= Packer::packSignedInt32(count($topics));
     // Add Topics
     foreach ($topics as $topic => $partitions) {
         // Add Topic
         $data .= Packer::packStringSignedInt16($topic);
         // Add Partition count
         $data .= Packer::packSignedInt32(count($partitions));
         // Add Partitions
         foreach ($partitions as $partition) {
             // Add Partition
             $data .= Packer::packSignedInt32($partition);
         }
     }
     // Concat data length (32 bits) and data
     $data = Packer::packStringSignedInt32($data);
     // Send data
     return $this->client->write($data);
 }
コード例 #4
0
 public function send($replicaId = -1, $maxWaitTime = 100, $minBytes = 1024, $topics)
 {
     // Add header (ApiKey, ApiVersion, CorrelationId, ClientId)
     $data = $this->buildHeader();
     // Add ReplicaId
     $data .= Packer::packSignedInt32($replicaId);
     // Add MaxWaitTime
     $data .= Packer::packSignedInt32($maxWaitTime);
     // Add MinBytes
     $data .= Packer::packSignedInt32($minBytes);
     // Add Topic count
     $data .= Packer::packSignedInt32(count($topics));
     // Add Topics
     foreach ($topics as $topic => $partitions) {
         // Add Topic
         $data .= Packer::packStringSignedInt16($topic);
         // Add Partition count
         $data .= Packer::packSignedInt32(count($partitions));
         // Add Partitions
         foreach ($partitions as $partition => $partitionParams) {
             // Add Partition
             $data .= Packer::packSignedInt32($partition);
             // Add Partition offset
             $data .= Packer::packSignedInt64($partitionParams['offset']);
             // Add PartitionMaxBytes
             $data .= Packer::packSignedInt32($partitionParams['max_bytes']);
         }
     }
     // Concat data length (32 bits) and data
     $data = Packer::packStringSignedInt32($data);
     // Send data
     return $this->client->write($data);
 }
コード例 #5
0
 /**
  * Build request header
  * @param  array  $options [description]
  * @return [type]          [description]
  */
 protected function buildHeader($options = [])
 {
     // Set API version if passed, else default to DEFAULT_API_VERSION
     $apiVersion = isset($options['api_version']) ? $options['api_version'] : self::DEFAULT_API_VERSION;
     // Set correlation ID if passed, else generate a random one
     $correlationId = isset($this->correlationId) ? $this->correlationId : mt_rand();
     // Set client if passed, else default to DEFAULT_CLIENT
     $clientId = isset($options['client_id']) ? $options['client_id'] : self::DEFAULT_CLIENT;
     $header = Packer::packSignedInt16(static::API_KEY) . Packer::packSignedInt16($apiVersion) . Packer::packSignedInt32($correlationId) . Packer::packStringSignedInt16($clientId);
     return $header;
 }
コード例 #6
0
 public function send($requiredAcks, $timeout, $topics)
 {
     // Add header (ApiKey, ApiVersion, CorrelationId, ClientId)
     $data = $this->buildHeader();
     // Add RequiredAcks
     $data .= Packer::packSignedInt16($requiredAcks);
     // Add Timeout
     $data .= Packer::packSignedInt32($timeout);
     // Add Topic count
     $data .= Packer::packSignedInt32(count($topics));
     // Add Topics
     foreach ($topics as $topic => $partitions) {
         // Add Topic
         $data .= Packer::packStringSignedInt16($topic);
         // Add Partition count
         $data .= Packer::packSignedInt32(count($partitions));
         // Add Partitions
         foreach ($partitions as $partition => $messages) {
             $data .= Packer::packSignedInt32($partition);
             $packedMessageSet = '';
             foreach ($messages as $message) {
                 if (!is_array($message)) {
                     $offset = 0;
                     // Producer does not know message offset, so we can fill with any value (we choose 0)
                     $magicByte = 0;
                     // Used to ensure backwards compatibility. Currently set to 0;
                     $attributes = 0;
                     $key = null;
                 } else {
                     $offset = array_key_exists('Offset', $message) ? $message['Offset'] : 0;
                     $magicByte = array_key_exists('MagicByte', $message) ? $message['MagicByte'] : 0;
                     $attributes = array_key_exists('Attributes', $message) ? $message['Attributes'] : 0;
                     $attributes = 0;
                     // TODO: handle compression
                     $key = array_key_exists('Key', $message) ? $message['Key'] : null;
                 }
                 $packedMessage = Packer::packSignedInt8($magicByte);
                 $packedMessage .= Packer::packSignedInt8($attributes);
                 $packedMessage .= Packer::packStringSignedInt32($key);
                 $packedMessage .= Packer::packStringSignedInt32($message);
                 $packedMessage = Packer::packSignedInt32(crc32($packedMessage)) . $packedMessage;
                 $packedMessageSet .= Packer::packSignedInt64($offset) . Packer::packStringSignedInt32($packedMessage);
             }
             // Add MessageSet
             $data .= Packer::packStringSignedInt32($packedMessageSet);
         }
     }
     // Concat data length (32 bits) and data
     $data = Packer::packStringSignedInt32($data);
     // Send data
     return $this->client->write($data);
 }
コード例 #7
0
 public function send($groupId, $generationId, $memberId)
 {
     // Add header (ApiKey, ApiVersion, CorrelationId, ClientId)
     $data = $this->buildHeader();
     // Add GroupId
     $data .= Packer::packStringSignedInt16($groupId);
     // Add GenerationId
     $data .= Packer::packSignedInt32($generationId);
     // Add MemberId
     $data .= Packer::packStringSignedInt16($memberId);
     // Concat data length (32 bits) and data
     $data = Packer::packStringSignedInt32($data);
     // Send data
     return $this->client->write($data);
 }
コード例 #8
0
 public function send($groupIds)
 {
     // Add header (ApiKey, ApiVersion, CorrelationId, ClientId)
     $data = $this->buildHeader();
     // Add GroupIds
     $data .= Packer::packSignedInt32(count($groupIds));
     foreach ($groupIds as $groupId) {
         // Add GroupId
         $data .= Packer::packStringSignedInt16($groupId);
     }
     // Concat data length (32 bits) and data
     $data = Packer::packStringSignedInt32($data);
     // Send data
     return $this->client->write($data);
 }
コード例 #9
0
 public function send($topics)
 {
     // Add header (ApiKey, ApiVersion, CorrelationId, ClientId)
     $data = $this->buildHeader();
     // Add Topic count
     $data .= Packer::packSignedInt32(count($topics));
     // Add Topics
     foreach ($topics as $topic) {
         // Add Topic
         $data .= Packer::packStringSignedInt16($topic);
     }
     // Concat data length (32 bits) and data
     $data = Packer::packStringSignedInt32($data);
     // Send the message
     return $this->client->write($data);
 }
コード例 #10
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::packSignedInt32(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($partition, true));
             $cursor += 4;
             // Read ErrorCode
             $errorCode = Packer::unpackSignedInt16(substr($response, $cursor, 2));
             Logger::log('                > ErrorCode: %s', var_export($errorCode, true));
             $cursor += 2;
             // Read Offset count
             $offsetCount = Packer::unpackSignedInt32(substr($response, $cursor, 4));
             Logger::log('                > Offset count: %s', var_export($offsetCount, true));
             $cursor += 4;
             // Read offsets
             $offsets = [];
             for ($k = 1; $k <= $offsetCount; $k++) {
                 Logger::log('                    > [Offset #%d]', $k);
                 $offset = Packer::unpackSignedInt64(substr($response, $cursor, 8));
                 Logger::log('                        > Offset: %s', var_export($offset, true));
                 $cursor += 8;
                 $offsets[] = $offset;
             }
             $partitions[] = ['PartitionId' => $partitionId, 'ErrorCode' => $errorCode, 'Offsets' => $offsets];
         }
         $topics[] = ['Topic' => $topic, 'Partitions' => $partitions];
     }
     return ['CorrelationId' => $correlationId, 'Topics' => $topics];
 }
コード例 #11
0
 public function sendV2($consumerGroup, $consumerGroupGenerationId, $consumerId, $retentionTime, $topics)
 {
     // Add header (ApiKey, ApiVersion, CorrelationId, ClientId)
     $data = $this->buildHeader(['api_version' => 2]);
     // Add ConsumerGroup
     Logger::log('> Packing ConsumerGroup: %s', var_export($consumerGroup, true));
     $data .= Packer::packSignedInt16(strlen($consumerGroup)) . $consumerGroup;
     // Add ConsumerGroupGenerationId
     Logger::log('> Packing ConsumerGroupGenerationId: %s', var_export($consumerGroupGenerationId, true));
     $data .= Packer::packSignedInt32($consumerGroupGenerationId);
     // Add ConsumerId
     Logger::log('> Packing ConsumerId: %s', var_export($consumerId, true));
     $data .= Packer::packSignedInt16(strlen($consumerId)) . $consumerId;
     // Add RetentionTime
     Logger::log('> Packing RetentionTime: %s', var_export($retentionTime, true));
     $data .= Packer::packSignedInt64($retentionTime);
     // Add Topic count
     Logger::log('> Packing Topic count: %s', var_export(count($topics), true));
     $data .= Packer::packSignedInt32(count($topics));
     // Add Topics
     foreach ($topics as $topic => $partitions) {
         // Add Topic
         Logger::log('> Packing Topic: %s', var_export($topic, true));
         $data .= Packer::packSignedInt16(strlen($topic)) . $topic;
         // Add Partition count
         Logger::log('> Packing Partition count: %s', var_export(count($partitions), true));
         $data .= Packer::packSignedInt32(count($partitions));
         // Add Partitions
         foreach ($partitions as $partition => $partitionParams) {
             // Add Partition
             Logger::log('> Packing Partition: %s', var_export(count($partition), true));
             $data .= Packer::packSignedInt32($partition);
             // Add Offset
             Logger::log('> Packing Offset: %s', var_export($partitionParams['Offset'], true));
             $data .= Packer::packSignedInt64($partitionParams['Offset']);
             // Add Metadata
             Logger::log('> Packing Metadata: %s', var_export($partitionParams['Metadata'], true));
             $data .= Packer::packSignedInt16(strlen($partitionParams['Metadata'])) . $partitionParams['Metadata'];
         }
     }
     // Concat data length (32 bits) and data
     $data = Packer::packSignedInt32(strlen($data)) . $data;
     // Send data
     return $this->client->write($data);
 }