U
    lufì  ã                   @   sð   d Z ddlZddlmZmZmZmZ ddlZddlZddl	m
Z
 ddlmZ G dd„ de
ƒZee edœd	d
„Zeee  edœdd„Zeddeee dœdd„ƒZedeje eedœdd„ƒZdejeeeee f dœdd„Zd
dgZdS )zJModule holding utility and convenience functions for zmq event monitoring.é    N)Ú	AwaitableÚListÚUnionÚoverload)Ú	TypedDict)Ú_check_versionc                   @   s&   e Zd ZU eed< eed< eed< dS )Ú_MonitorMessageÚeventÚvalueÚendpointN)Ú__name__Ú
__module__Ú__qualname__ÚintÚ__annotations__Úbytes© r   r   ú5/tmp/pip-unpacked-wheel-h6ekxre8/zmq/utils/monitor.pyr      s   
r   )ÚmsgÚreturnc                 C   s\   t | ƒdkst | d ƒdkr(td|  ƒ‚t d| d ¡\}}t |¡t |¡| d dœ}|S )aÄ  decode zmq_monitor event messages.

    Parameters
    ----------
    msg : list(bytes)
        zmq multipart message that has arrived on a monitor PAIR socket.

        First frame is::

            16 bit event id
            32 bit event value
            no padding

        Second frame is the endpoint as a bytestring

    Returns
    -------
    event : dict
        event description as dict with the keys `event`, `value`, and `endpoint`.
    é   r   é   z Invalid event message format: %sz=hié   )r	   r
   r   )ÚlenÚRuntimeErrorÚstructÚunpackÚzmqÚEvent)r   Zevent_idr
   r	   r   r   r   Úparse_monitor_message   s    ýr   )Úawaitable_msgr   c                 Ã   s   | I dH }t |ƒS )zyLike parse_monitor_msg, but awaitable

    Given awaitable message, return awaitable for the parsed monitor message.
    N)r   )r    r   r   r   r   Ú_parse_monitor_msg_async5   s    
r!   zzmq.asyncio.Socket)ÚsocketÚflagsr   c                 C   s   d S ©Nr   ©r"   r#   r   r   r   Úrecv_monitor_messageB   s    r&   c                 C   s   d S r$   r   r%   r   r   r   r&   J   s    c                 C   s.   t ddƒ |  |¡}t|tƒr&t|ƒS t|ƒS )u„  Receive and decode the given raw message from the monitoring socket and return a dict.

    Requires libzmq â‰¥ 4.0

    The returned dict will have the following entries:
      event     : int, the event id as described in libzmq.zmq_socket_monitor
      value     : int, the event value associated with the event, see libzmq.zmq_socket_monitor
      endpoint  : string, the affected endpoint

    .. versionchanged:: 23.1
        Support for async sockets added.
        When called with a async socket,
        returns an awaitable for the monitor message.

    Parameters
    ----------
    socket : zmq PAIR socket
        The PAIR socket (created by other.get_monitor_socket()) on which to recv the message
    flags : bitfield (int)
        standard zmq recv flags

    Returns
    -------
    event : dict
        event description as dict with the keys `event`, `value`, and `endpoint`.
    )é   r   zlibzmq event API)r   Zrecv_multipartÚ
isinstancer   r!   r   )r"   r#   r   r   r   r   r&   R   s
    


)r   )r   )r   )Ú__doc__r   Útypingr   r   r   r   r   Zzmq.asyncioZzmq._typingr   Z	zmq.errorr   r   r   r   r!   r   r&   ZSocketÚ__all__r   r   r   r   Ú<module>   s@   !
þ þý þý	 þý,