U
     T‚gí"  ã                
   @   sØ   d 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
 z6eedƒZeedƒZeed	ƒZeed
ƒZeedƒZW n* ek
r¤ Z zeeƒ‚W 5 dZ[X Y nX 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()
é    N)Úimplementer)Ú	posixbase)ÚIReactorFDSet)ÚlogÚepollÚEPOLLHUPÚEPOLLERRÚEPOLLINÚEPOLLOUTc                   @   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   úA/tmp/pip-unpacked-wheel-1_7di8ic/twisted/internet/epollreactor.pyr   G   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   Ú_addV   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
   ÚOSErrorÚerrnoÚEPERMr   Ú	addReader)r   ÚreaderÚer   r   r   r(   p   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+   �   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   Ú
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ÚremoveReaderr1   r   r   r   r	   r
   )r   r)   r   r   r   r2   «   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ÚremoveWriterr1   r   r   r   r
   r	   )r   r,   r   r   r   r3   ¶   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   r4   r5   r   r   r   r7   È   s     )Z
_removeAllr   r   r   Ú	removeAllr   r   r   r   r8   Á   s    þüÿzEPollReactor.removeAllc                    s   ‡ fdd„ˆ j D ƒˆ j ¡  S )Nc                    s   g | ]}ˆ j | ‘qS r   r4   r5   r   r   r   r7   Î   s    z+EPollReactor.getReaders.<locals>.<listcomp>)r   r   Ú
getReadersr   r   r   r   r9   Í   s
    
ÿþzEPollReactor.getReadersc                    s   ‡ fdd„ˆ j D ƒˆ j ¡  S )Nc                    s   g | ]}ˆ j | ‘qS r   r4   r5   r   r   r   r7   Ó   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   ÚpollÚlenr   r%   r&   ZEINTRZ_doReadOrWriteÚKeyErrorr   ZcallWithLogger)r   Ú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+   r1   r2   r3   r8   r9   r:   rA   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.mainrF   )ÚprF   r   r   r   Úinstallù   s    rH   )rE   r&   ÚselectZzope.interfacer   Ztwisted.internetr   Ztwisted.internet.interfacesr   Ztwisted.pythonr   Úgetattrr   r   r   r	   r
   ÚAttributeErrorr*   ÚImportErrorr   Z_PollLikeMixinr   rH   Ú__all__r   r   r   r   Ú<module>   s&   




 T
