o
    `šjö  ã                   @   sp   d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ e e¡ZG dd„ dƒZdS )é    N)ÚFindCoordinatorResponse)Ú
KafkaBytes)ÚInt32©Ú__version__c                   @   sZ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚKafkaProtocola“  Manage the kafka network protocol

    Use an instance of KafkaProtocol to manage bytes send/recv'd
    from a network socket to a broker.

    Arguments:
        client_id (str): identifier string to be included in each request
        ident (str): Optional log-prefix identifier.
        receive_message_max_bytes (int): Maximum allowed message frame size.
            Default: 100000000 (100MB).
    c                 K   s^   |  dd¡| _|  d|  ¡ ¡| _|  dd¡| _d| _tdƒ| _d | _d| _	t
 ¡ | _g | _d S )	NÚidentÚ Ú	client_idÚreceive_message_max_bytesi áõr   é   F)ÚgetÚ_identÚ_gen_client_idÚ
_client_idÚ_max_frame_sizeÚ_correlation_idr   Ú_headerÚ_rbufferÚ
_receivingÚcollectionsÚdequeÚin_flight_requestsÚbytes_to_send)ÚselfÚconfig© r   úS/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/protocol/parser.pyÚ__init__   s   


zKafkaProtocol.__init__c                 C   s   | j d d | _ | j S )Né   l        )r   ©r   r   r   r   Ú_next_correlation_id%   s   z"KafkaProtocol._next_correlation_idc                 C   s   dt  S )Nzkafka-pythonr   r    r   r   r   r   )   s   zKafkaProtocol._gen_client_idNc                 C   sf   |du r|   ¡ }t d| j||¡ |j|| jd |jddd}| j |¡ | 	¡ r1| j
 |j¡ |S )aq  Encode and queue a kafka api request for sending.

        Arguments:
            request : An un-encoded kafka request.
            correlation_id (int, optional): Optionally specify an ID to
                correlate requests with responses. If not provided, an ID will
                be generated automatically.

        Returns:
            correlation_id
        Nz%s Sending request %d %s)Úcorrelation_idr
   T)ÚframedÚheader)r!   ÚlogÚdebugr   Úwith_headerr   Úencoder   ÚappendÚexpect_responser   r$   )r   Úrequestr"   Údatar   r   r   Úsend_request,   s   zKafkaProtocol.send_requestc                 C   sV   t | jƒ}|dkrdS |dkr| jd }g | _n	d | j¡}g | _t d| j|¡ |S )z1Retrieve all pending bytes to send on the networkr   ó    r   z%s Send: %r)Úlenr   Újoinr%   r&   r   )r   Únr,   r   r   r   Ú
send_bytesC   s   

zKafkaProtocol.send_bytesc           
      C   sv  d}t |ƒ}g }|rt d| j|¡ ||k r¹| jsbtd| j ¡  || ƒ}| j |||| … ¡ ||7 }| j ¡ dkrV| j 	d¡ t
 | j¡}|  |¡ t|ƒ| _d| _n| j ¡ dkrbt d¡‚| jrµt | jƒ}| j ¡ }t|| || ƒ}| j |||| … ¡ ||7 }| j ¡ }||kr–t d¡‚||kr	 |S d| _| j 	d¡ |  | j¡}	| |	¡ |  ¡  ||k s|S )a"  Process bytes received from the network.

        Arguments:
            data (bytes): any length bytes received from a network connection
                to a kafka broker.

        Returns:
            responses (list of (correlation_id, response)): any/all completed
                responses, decoded from bytes to python objects.

        Raises:
             KafkaProtocolError: if the bytes received could not be decoded.
             CorrelationIdError: if the response does not match the request
                 correlation id.
        r   z%s Recv: %rr   Tz+this should not happen - are you threading?z,Receive buffer has more bytes than expected?F)r/   r%   r&   r   r   Úminr   ÚtellÚwriteÚseekr   ÚdecodeÚ_validate_frame_sizer   r   ÚErrorsÚ
KafkaErrorÚ_process_responser)   Ú_reset_buffer)
r   r,   Úir1   Ú	responsesÚbytes_to_readÚnbytesÚtotal_bytesÚstaged_bytesÚrespr   r   r   Úreceive_bytesS   sJ   






û
Ü%zKafkaProtocol.receive_bytesc                 C   s$   |dk s	|| j krt d| ¡‚d S )Nr   zInvalid frame length: %d)r   r9   ÚInvalidReceiveError)r   r@   r   r   r   r8      s   ÿz"KafkaProtocol._validate_frame_sizec           	      C   s"  | j st d¡‚| j  ¡ }|j}| ¡ }|d u r,t d|j|j	¡ t 
d|j|j	f ¡‚| |¡}|j}|dkrK|dkrK|tu rK|j	dkrKt d¡ n||krXt d||f ¡‚z| |¡}W n$ ttjfyƒ   | d¡ | ¡ }t d|||t|ƒ|¡ t 
d¡‚w t d	| j||¡ ||fS )
Nz.No in-flight-request found for server responsez1Unable to find ResponseType for api=%d version=%dz+Unable to find response type for api %d v%dr   z£Kafka 0.8.2 quirk -- FindCoordinatorResponse Correlation ID does not match request. This should go away once at least one topic has been initialized on the broker.z.Correlation IDs do not match: sent %d, recv %dzUResponse %d [ResponseType: %s RequestHeader: %s]: Unable to decode %d-byte buffer: %rzUnable to decode responsez%s Received response %d %s)r   r9   ÚCorrelationIdErrorÚpopleftr"   Úget_response_classr%   ÚerrorÚapi_keyÚapi_versionÚKafkaProtocolErrorÚparse_headerr   Úwarningr7   Ú
ValueErrorÚstructr6   Úreadr/   r&   r   )	r   Úread_bufferr$   r"   Úresponse_typeÚresponse_headerÚrecv_correlation_idÚresponseÚbufr   r   r   r;   “   sD   

ÿ
ÿÿ

ý
ù	zKafkaProtocol._process_responsec                 C   s   d| _ | j d¡ d | _d S )NFr   )r   r   r6   r   r    r   r   r   r<   ¼   s   
zKafkaProtocol._reset_buffer)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r   r-   r2   rD   r8   r;   r<   r   r   r   r   r      s    
<)r   )r   ÚloggingrP   Úkafka.errorsÚerrorsr9   Úkafka.protocol.metadatar   Úkafka.protocol.framer   Ú$kafka.protocol.schemas.fields.codecsr   Úkafka.versionr   Ú	getLoggerrX   r%   r   r   r   r   r   Ú<module>   s    
