ó
[³XMc           @   sè   d  Z  d d l 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 m Z m Z d d l m Z e	 e
 Be BZ d	 e j f d
 „  ƒ  YZ d „  Z d	 d g Z d S(   sñ   
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()
iÿÿÿÿN(   t   errort   poll(   t   POLLINt   POLLOUTt   POLLHUPt   POLLERRt   POLLNVAL(   t
   implements(   t   log(   t   maint	   posixbaseR    (   t   IReactorFDSett   PollReactorc           B   sŠ   e  Z d  Z e e ƒ d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z e Z d
 „  Z d „  Z d „  Z RS(   sW  
    A reactor that uses poll(2).

    @ivar _poller: A L{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{FileDescriptors} 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 j |  ƒ d S(   so   
        Initialize polling object, file descriptor tracking dictionaries, and
        the base class.
        N(   R   t   _pollert   _selectablest   _readst   _writesR
   t   PosixReactorBaset   __init__(   t   self(    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyR   9   s
    			c         C   s¨   y |  j  j | ƒ Wn t k
 r' n Xd } | |  j k rJ | t B} n  | |  j k rf | t B} n  | d k rˆ |  j  j | | ƒ n | |  j k r¤ |  j | =n  d S(   s*   Register/unregister an fd with the poller.i    N(	   R   t
   unregistert   KeyErrorR   R   R   R   t   registerR   (   R   t   fdt   mask(    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt   _updateRegistrationE   s    c         C   s}   y | j  ƒ  } | | Wn; x5 |  j j ƒ  D] \ } } | | k r. Pq. q. Wd  Sn X| | k ry | | =|  j | ƒ n  d  S(   N(   t   filenoR   t   itemsR   (   R   t
   selectablet   mdictR   t   fdes(    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt   _dictRemoveW   s    c         C   sI   | j  ƒ  } | |  j k rE | |  j | <d |  j | <|  j | ƒ n  d S(   sI   Add a FileDescriptor for notification of data available to read.
        i   N(   R   R   R   R   (   R   t   readerR   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt	   addReaderl   s
    c         C   sI   | j  ƒ  } | |  j k rE | |  j | <d |  j | <|  j | ƒ n  d S(   sJ   Add a FileDescriptor for notification of data available to write.
        i   N(   R   R   R   R   (   R   t   writerR   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt	   addWriteru   s
    c         C   s   |  j  | |  j ƒ S(   sH   Remove a Selectable for notification of data available to read.
        (   R   R   (   R   R    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt   removeReader~   s    c         C   s   |  j  | |  j ƒ S(   sI   Remove a Selectable for notification of data available to write.
        (   R   R   (   R   R"   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt   removeWriterƒ   s    c         C   sJ   |  j  g  |  j D] } |  j | ^ q g  |  j D] } |  j | ^ q0 ƒ S(   sD   
        Remove all selectables, and return a list of them.
        (   t
   _removeAllR   R   R   (   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt	   removeAllˆ   s     c         C   sÊ   | d k	 r t | d ƒ } n  y |  j j | ƒ } Wn- t k
 rd } | d t j k r^ d S‚  n X|  j } xU | D]M \ } } y |  j | } Wn t	 k
 r¨ qu n Xt
 j | | | | | ƒ qu Wd S(   s   Poll the poller for new events.iè  i    N(   t   Nonet   intR   R   t   SelectErrort   errnot   EINTRt   _doReadOrWriteR   R   R   t   callWithLogger(   R   t   timeoutt   lt   et   _drdwR   t   eventR   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt   doPoll‘   s    	c         C   s  d  } t } | t @rN | t @rN | |  j k rB t j } t } qå t j } n— ys | t @rp | j	 ƒ  } t } n  | r– | t
 @r– | j ƒ  } t } n  | j ƒ  | k sÀ t j d ƒ } t } n  Wn! t j ƒ  t j ƒ  d } n X| r|  j | | | ƒ n  d  S(   Ns   Filedescriptor went awayi   (   R(   t   Falset   POLL_DISCONNECTEDR   R   R	   t   CONNECTION_DONEt   Truet   CONNECTION_LOSTt   doReadR   t   doWriteR   R    t   ConnectionFdescWentAwayR   t   deferrt   syst   exc_infot   _disconnectSelectable(   R   R   R   R3   t   whyt   inRead(    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyR-   ©   s,    		
		
c         C   s!   g  |  j  D] } |  j | ^ q
 S(   N(   R   R   (   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt
   getReadersÄ   s    c         C   s!   g  |  j  D] } |  j | ^ q
 S(   N(   R   R   (   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt
   getWritersÈ   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   R   R!   R#   R$   R%   R'   R4   t   doIterationR-   RC   RD   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyR      s   
														c          C   s'   t  ƒ  }  d d l m } | |  ƒ d S(   s   Install the poll() reactor.iÿÿÿÿ(   t   installReactorN(   R   t   twisted.internet.mainRI   (   t   pRI   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt   installÍ   s    	RL   (   RG   R+   R>   t   selectR    R*   R   R   R   R   R   R   t   zope.interfaceR   t   twisted.pythonR   t   twisted.internetR	   R
   t   twisted.internet.interfacesR   R6   R   R   RL   t   __all__(    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt   <module>   s   (¯	