ó
H!¯Nc           @   sÈ   d  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 d	 e j 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	   posixbase(   t   IReactorFDSett   PollReactorc           B   s›   e  Z d  Z e e ƒ e e Be BZ e	 Z
 e Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z e 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   ;   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   _updateRegistrationG   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   _dictRemoveY   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	   addReadern   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	   addWriterw   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 | ƒ } Wn0 t k
 rg } | j d t j k ra d S‚  n X|  j } xU | D]M \ } } y |  j	 | } Wn t
 k
 r« qx n Xt j | | | | | ƒ qx Wd S(   s   Poll the poller for new events.iè  i    N(   t   Nonet   intR   R   t   SelectErrort   argst   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!   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   t   _POLL_DISCONNECTEDR   t   _POLL_INR   t	   _POLL_OUTR   R   R   R    R"   R#   R$   R&   R4   t   doIterationR5   R6   (    (    (    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.mainR>   (   t   pR>   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt   install´   s    	RA   (   R9   R+   t   selectR    R)   R   R   R   R   R   R   t   zope.interfaceR   t   twisted.pythonR   t   twisted.internetR	   t   twisted.internet.interfacesR
   R   t   _PollLikeMixinR   RA   t   __all__(    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.pyt   <module>   s   (˜	