def _encode_message(cls, message): """ Encode a single message. The magic number of a message is a format version number. The only supported magic number right now is zero Format ====== Message => Crc MagicByte Attributes Key Value Crc => int32 MagicByte => int8 Attributes => int8 Key => bytes Value => bytes """ if message.magic == 0: msg = struct.pack('>BB', message.magic, message.attributes) msg += write_int_string(message.key) msg += write_int_string(message.value) crc = zlib.crc32(msg) msg = struct.pack('>i%ds' % len(msg), crc, msg) else: raise Exception("Unexpected magic number: %d" % message.magic) return msg
def _encode_message(cls, message): """ Encode a single message. The magic number of a message is a format version number. The only supported magic number right now is zero Format ====== Message => Crc MagicByte Attributes Key Value Crc => int32 MagicByte => int8 Attributes => int8 Key => bytes Value => bytes """ if message.magic == 0: msg = b"".join([ struct.pack(">BB", message.magic, message.attributes), write_int_string(message.key), write_int_string(message.value), ]) crc = crc32(msg) msg = struct.pack(">i%ds" % len(msg), crc, msg) else: raise ProtocolError("Unexpected magic number: %d" % message.magic) return msg
def _encode_message(cls, message): """ Encode a single message. The magic number of a message is a format version number. The only supported magic number right now is zero Format ====== Message => Crc MagicByte Attributes Key Value Crc => int32 MagicByte => int8 Attributes => int8 Key => bytes Value => bytes """ if message.magic == 0: msg = b"".join( [ struct.pack(">BB", message.magic, message.attributes), write_int_string(message.key), write_int_string(message.value), ] ) crc = crc32(msg) msg = struct.pack(">I%ds" % len(msg), crc, msg) else: raise ProtocolError("Unexpected magic number: %d" % message.magic) return msg
def encode_metadata_request(cls, client_id, correlation_id, topics=None, payloads=None): """ Encode a MetadataRequest Arguments: client_id: string correlation_id: int topics: list of strings """ if payloads is None: topics = [] if topics is None else topics else: topics = payloads message = [] message.append(cls._encode_message_header(client_id, correlation_id, KafkaProtocol.METADATA_KEY)) message.append(struct.pack('>i', len(topics))) for topic in topics: message.append(struct.pack('>h%ds' % len(topic), len(topic), topic)) msg = b''.join(message) return write_int_string(msg)
def encode_metadata_request(cls, client_id, correlation_id, topics=[]): """ Encode a MetadataRequest Params ====== client_id: string correlation_id: string topics: list of strings """ message = cls._encode_message_header(client_id, correlation_id, KafkaProtocol.METADATA_KEY) message += struct.pack('>i', len(topics)) for topic in topics: message += struct.pack('>h%ds' % len(topic), len(topic), topic) return write_int_string(message)
def encode_consumer_metadata_request(cls, client_id, correlation_id, payloads): """ Encode a ConsumerMetadataRequest Arguments: client_id: string correlation_id: int payloads: string (consumer group) """ message = [] message.append(cls._encode_message_header(client_id, correlation_id, KafkaProtocol.CONSUMER_METADATA_KEY)) message.append(struct.pack('>h%ds' % len(payloads), len(payloads), payloads)) msg = b''.join(message) return write_int_string(msg)
def encode_metadata_request(cls, client_id, correlation_id, topics=None): """ Encode a MetadataRequest Params ====== client_id: string correlation_id: int topics: list of strings """ topics = [] if topics is None else topics message = cls._encode_message_header(client_id, correlation_id, KafkaProtocol.METADATA_KEY) message += struct.pack('>i', len(topics)) for topic in topics: message += struct.pack('>h%ds' % len(topic), len(topic), topic) return write_int_string(message)