Exemplo n.º 1
0
 public function send($replicaId = -1, $topics)
 {
     // Add header (ApiKey, ApiVersion, CorrelationId, ClientId)
     $data = $this->buildHeader();
     // Add ReplicaId
     $data .= Packer::packSignedInt32($replicaId);
     // 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 Time
             $data .= Packer::packSignedInt64($partitionParams['Time']);
             // Add MaxNumberOfOffsets
             $data .= Packer::packSignedInt32($partitionParams['MaxNumberOfOffsets']);
         }
     }
     // Concat data length (32 bits) and data
     $data = Packer::packStringSignedInt32($data);
     // Send data
     return $this->client->write($data);
 }
Exemplo n.º 2
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);
 }
Exemplo n.º 3
0
 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);
 }
Exemplo n.º 4
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);
 }
Exemplo n.º 5
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);
 }