U
    ¹êW[Š  ã                   @   sª   d Z ddlmZmZ ddlZddlmZmZ ddlm	Z	m
Z
mZmZm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ñ
A poll() 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 pollreactor
    pollreactor.install()
é    )ÚdivisionÚabsolute_importN)ÚerrorÚpoll)ÚPOLLINÚPOLLOUTÚPOLLHUPÚPOLLERRÚPOLLNVAL)Úimplementer)Úlog)Ú	posixbase)ÚIReactorFDSetc                   @   s€   e Zd ZdZeeB 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eZdd„ Zdd„ ZdS )ÚPollReactora^  
    A reactor that uses poll(2).

    @ivar _poller: A L{select.poll} which will be used to check for I/O
        readiness.

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

    @ivar _reads: A dictionary mapping integer file descriptors to arbitrary
        values (this is essentially a set).  Keys in this dictionary will be
        registered with C{_poller} for read readiness notifications which will
        be dispatched to the corresponding L{FileDescriptor} instances in
        C{_selectables}.

    @ivar _writes: A dictionary mapping integer file descriptors to arbitrary
        values (this is essentially a set).  Keys in this dictionary will be
        registered with C{_poller} for write readiness notifications which will
        be dispatched to the corresponding L{FileDescriptor} instances in
        C{_selectables}.
    c                 C   s*   t ƒ | _i | _i | _i | _tj | ¡ dS )zo
        Initialize polling object, file descriptor tracking dictionaries, and
        the base class.
        N)r   Ú_pollerÚ_selectablesÚ_readsÚ_writesr   ÚPosixReactorBaseÚ__init__©Úself© r   ú>/usr/lib/python3/dist-packages/twisted/internet/pollreactor.pyr   =   s
    zPollReactor.__init__c                 C   s|   z| j  |¡ W n tk
r$   Y nX d}|| jkr<|tB }|| jkrN|tB }|dkrf| j  ||¡ n|| jkrx| j|= dS )z*Register/unregister an fd with the poller.r   N)	r   Z
unregisterÚKeyErrorr   r   r   r   Úregisterr   )r   ÚfdÚmaskr   r   r   Ú_updateRegistrationI   s    


zPollReactor._updateRegistrationc                 C   sd   z|  ¡ }||  W n2   | j ¡ D ]\}}||kr& qBq&Y d S Y nX ||kr`||= |  |¡ d S )N)Úfilenor   Úitemsr   )r   Ú
selectableZmdictr   Zfdesr   r   r   Ú_dictRemove[   s    zPollReactor._dictRemovec                 C   s4   |  ¡ }|| jkr0|| j|< d| j|< |  |¡ dS )zIAdd a FileDescriptor for notification of data available to read.
        é   N)r   r   r   r   )r   Úreaderr   r   r   r   Ú	addReaderp   s
    


zPollReactor.addReaderc                 C   s4   |  ¡ }|| jkr0|| j|< d| j|< |  |¡ dS )zJAdd a FileDescriptor for notification of data available to write.
        r#   N)r   r   r   r   )r   Úwriterr   r   r   r   Ú	addWritery   s
    


zPollReactor.addWriterc                 C   s   |   || j¡S )zHRemove a Selectable for notification of data available to read.
        )r"   r   )r   r$   r   r   r   ÚremoveReader‚   s    zPollReactor.removeReaderc                 C   s   |   || j¡S )zIRemove a Selectable for notification of data available to write.
        )r"   r   )r   r&   r   r   r   ÚremoveWriter‡   s    zPollReactor.removeWriterc                    s,   ˆ   ‡ fdd„ˆ jD ƒ‡ fdd„ˆ jD ƒ¡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)PollReactor.removeAll.<locals>.<listcomp>c                    s   g | ]}ˆ j | ‘qS r   r*   r+   r   r   r   r-   ’   s     )Z
_removeAllr   r   r   r   r   r   Ú	removeAllŒ   s    þzPollReactor.removeAllc              
   C   s¶   |dk	rt |d ƒ}z| j |¡}W n> tk
rb } z |jd tjkrPW Y ¢
dS ‚ W 5 d}~X Y nX | j}|D ]B\}}z| j| }W n t	k
rœ   Y qnY nX t
 |||||¡ qndS )zPoll the poller for new events.Niè  r   )Úintr   r   ÚSelectErrorÚargsÚerrnoZEINTRZ_doReadOrWriter   r   r   ZcallWithLogger)r   ZtimeoutÚlÚeZ_drdwr   Zeventr!   r   r   r   ÚdoPoll•   s    

zPollReactor.doPollc                    s   ‡ fdd„ˆ j D ƒS )Nc                    s   g | ]}ˆ j | ‘qS r   r*   r+   r   r   r   r-   ®   s     z*PollReactor.getReaders.<locals>.<listcomp>)r   r   r   r   r   Ú
getReaders­   s    zPollReactor.getReadersc                    s   ‡ fdd„ˆ j D ƒS )Nc                    s   g | ]}ˆ j | ‘qS r   r*   r+   r   r   r   r-   ²   s     z*PollReactor.getWriters.<locals>.<listcomp>)r   r   r   r   r   Ú
getWriters±   s    zPollReactor.getWritersN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r
   Z_POLL_DISCONNECTEDr   Z_POLL_INr   Z	_POLL_OUTr   r   r"   r%   r'   r(   r)   r.   r5   ZdoIterationr6   r7   r   r   r   r   r      s    			r   c                  C   s   t ƒ } ddlm} || ƒ dS )zInstall the poll() reactor.r   )ÚinstallReactorN)r   Ztwisted.internet.mainr<   )Úpr<   r   r   r   Úinstall¶   s    r>   )r;   Z
__future__r   r   r2   Zselectr   r0   r   r   r   r   r	   r
   Zzope.interfacer   Ztwisted.pythonr   Ztwisted.internetr   Ztwisted.internet.interfacesr   r   Z_PollLikeMixinr   r>   Ú__all__r   r   r   r   Ú<module>   s   
 