U
    
W[-!  ã                   @   sš   d Z ddlmZmZ ddlmZmZmZmZm	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 eeƒG d	d
„ d
ejejƒƒZdd„ Zd
dgZdS )zõ
An epoll() based implementation of the twisted main loop.

To install the event loop (and you should do this before any connections,
listeners or connectors are added)::

    from twisted.internet import epollreactor
    epollreactor.install()
é    )ÚdivisionÚabsolute_import)ÚepollÚEPOLLHUPÚEPOLLERRÚEPOLLINÚEPOLLOUTN)Úimplementer)ÚIReactorFDSet)Úlog)Ú	posixbasec                   @   s|   e Zd ZdZeeB ZeZe	Z
dd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZeZdS )ÚEPollReactora‡  
    A reactor that uses epoll(7).

    @ivar _poller: A C{epoll} which will be used to check for I/O
        readiness.

    @ivar _selectables: A dictionary mapping integer file descriptors to
        instances of C{FileDescriptor} which have been registered with the
        reactor.  All C{FileDescriptors} which are currently receiving read or
        write readiness notifications will be present as values in this
        dictionary.

    @ivar _reads: A set containing integer file descriptors.  Values in this
        set will be registered with C{_poller} for read readiness notifications
        which will be dispatched to the corresponding C{FileDescriptor}
        instances in C{_selectables}.

    @ivar _writes: A set containing integer file descriptors.  Values in this
        set will be registered with C{_poller} for write readiness
        notifications which will be dispatched to the corresponding
        C{FileDescriptor} instances in C{_selectables}.

    @ivar _continuousPolling: A L{_ContinuousPolling} instance, used to handle
        file descriptors (e.g. filesystem files) that are not supported by
        C{epoll(7)}.
    c                 C   s<   t dƒ| _tƒ | _tƒ | _i | _t | ¡| _tj	 
| ¡ dS )zm
        Initialize epoll object, file descriptor tracking dictionaries, and the
        base class.
        i   N)r   Ú_pollerÚsetÚ_readsÚ_writesÚ_selectablesr   Z_ContinuousPollingÚ_continuousPollingÚPosixReactorBaseÚ__init__©Úself© r   ú?/usr/lib/python3/dist-packages/twisted/internet/epollreactor.pyr   =   s    
zEPollReactor.__init__c           	      C   sX   |  ¡ }||krT|}||kr4||O }| j ||¡ n| j ||¡ | |¡ |||< dS )zÖ
        Private method for adding a descriptor from the event loop.

        It takes care of adding it if  new or modifying it if already added
        for another state (read -> read/write for example).
        N)Úfilenor   ÚmodifyÚregisterÚadd)	r   ÚxerÚprimaryÚotherÚselectablesÚeventÚ	antieventÚfdÚflagsr   r   r   Ú_addM   s    
zEPollReactor._addc              
   C   sb   z|   || j| j| jtt¡ W n> tk
r\ } z |jtjkrJ| j	 
|¡ n‚ W 5 d}~X Y nX dS )zR
        Add a FileDescriptor for notification of data available to read.
        N)r&   r   r   r   r   r   ÚIOErrorÚerrnoÚEPERMr   Ú	addReader)r   ÚreaderÚer   r   r   r*   h   s     ÿzEPollReactor.addReaderc              
   C   sb   z|   || j| j| jtt¡ W n> tk
r\ } z |jtjkrJ| j	 
|¡ n‚ W 5 d}~X Y nX dS )zS
        Add a FileDescriptor for notification of data available to write.
        N)r&   r   r   r   r   r   r'   r(   r)   r   Ú	addWriter)r   Úwriterr,   r   r   r   r-   y   s     ÿzEPollReactor.addWriterc           
      C   sv   |  ¡ }|dkr2| ¡ D ]\}}||kr q2qdS ||krr||krV|}	| j ||	¡ n||= | j |¡ | |¡ dS )z·
        Private method for removing a descriptor from the event loop.

        It does the inverse job of _add, and also add a check in case of the fd
        has gone away.
        éÿÿÿÿN)r   Úitemsr   r   Z
unregisterÚremove)
r   r   r   r    r!   r"   r#   r$   Zfdesr%   r   r   r   Ú_removeŠ   s    zEPollReactor._removec                 C   s:   | j  |¡r| j  |¡ dS |  || j| j| jtt¡ dS )zQ
        Remove a Selectable for notification of data available to read.
        N)	r   Z	isReadingÚremoveReaderr2   r   r   r   r   r   )r   r+   r   r   r   r3   ¤   s     ÿzEPollReactor.removeReaderc                 C   s:   | j  |¡r| j  |¡ dS |  || j| j| jtt¡ dS )zR
        Remove a Selectable for notification of data available to write.
        N)	r   Z	isWritingÚremoveWriterr2   r   r   r   r   r   )r   r.   r   r   r   r4   ¯   s     ÿzEPollReactor.removeWriterc                    s6   ˆ   ‡ fdd„ˆ jD ƒ‡ fdd„ˆ jD ƒ¡ˆ j ¡  S )zD
        Remove all selectables, and return a list of them.
        c                    s   g | ]}ˆ j | ‘qS r   ©r   ©Ú.0r$   r   r   r   Ú
<listcomp>¿   s     z*EPollReactor.removeAll.<locals>.<listcomp>c                    s   g | ]}ˆ j | ‘qS r   r5   r6   r   r   r   r8   À   s     )Z
_removeAllr   r   r   Ú	removeAllr   r   r   r   r9   º   s    þýzEPollReactor.removeAllc                    s   ‡ fdd„ˆ j D ƒˆ j ¡  S )Nc                    s   g | ]}ˆ j | ‘qS r   r5   r6   r   r   r   r8   Å   s     z+EPollReactor.getReaders.<locals>.<listcomp>)r   r   Ú
getReadersr   r   r   r   r:   Ä   s    ÿzEPollReactor.getReadersc                    s   ‡ fdd„ˆ j D ƒˆ j ¡  S )Nc                    s   g | ]}ˆ j | ‘qS r   r5   r6   r   r   r   r8   Ê   s     z+EPollReactor.getWriters.<locals>.<listcomp>)r   r   Ú
getWritersr   r   r   r   r;   É   s    ÿzEPollReactor.getWritersc              
   C   s®   |dkrd}z| j  |t| jƒ¡}W n: tk
r^ } z|jtjkrLW Y ¢
dS ‚ W 5 d}~X Y nX | j}|D ]>\}}z| j| }W n tk
r”   Y qjX t	 
|||||¡ qjdS )z1
        Poll the poller for new events.
        Nr/   )r   ZpollÚlenr   r'   r(   ZEINTRZ_doReadOrWriteÚKeyErrorr   ZcallWithLogger)r   ZtimeoutÚlÚerrZ_drdwr$   r"   Z
selectabler   r   r   ÚdoPollÎ   s    
zEPollReactor.doPollN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Z_POLL_DISCONNECTEDr   Z_POLL_INr   Z	_POLL_OUTr   r&   r*   r-   r2   r3   r4   r9   r:   r;   r@   ZdoIterationr   r   r   r   r      s    
r   c                  C   s   t ƒ } ddlm} || ƒ dS )z&
    Install the epoll() reactor.
    r   )ÚinstallReactorN)r   Ztwisted.internet.mainrE   )ÚprE   r   r   r   Úinstallð   s    rG   )rD   Z
__future__r   r   Zselectr   r   r   r   r   r(   Zzope.interfacer	   Ztwisted.internet.interfacesr
   Ztwisted.pythonr   Ztwisted.internetr   r   Z_PollLikeMixinr   rG   Ú__all__r   r   r   r   Ú<module>   s   
 U	