ó
[³XMc           @   sº  d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z m Z m Z m	 Z	 m
 Z
 d d l m Z m Z m Z e e d ƒ s˜ e d ƒ ‚ n  d d l m Z m Z m Z m Z m Z m Z m Z d d l m Z d d	 l m Z m Z m Z m Z d
 e j  f d „  ƒ  YZ  d „  Z! d e" f d „  ƒ  YZ# d e# e j$ f d „  ƒ  YZ$ d e j% f d „  ƒ  YZ& d e j' f d „  ƒ  YZ( d e# e j$ f d „  ƒ  YZ) d e) f d „  ƒ  YZ* d S(   s˜   
Various asynchronous TCP/IP classes.

End users shouldn't use this module directly - use the reactor APIs instead.

Maintainer: Itamar Shtull-Trauring
iÿÿÿÿN(   t   EINTRt   EMSGSIZEt   EAGAINt   EWOULDBLOCKt   ECONNREFUSED(   t
   implementst   implementsOnlyt   implementedByt   AF_UNIXs+   UNIX sockets not supported on this platform(   t   baset   tcpt   udpt   errort
   interfacest   protocolt   address(   t   CannotListenError(   t   lockfilet   logt   reflectt   failuret   Serverc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s,   t  j j |  | | | d  f | | | ƒ d  S(   N(   R
   R   t   __init__t   None(   t   selft   sockR   t   clientt   servert	   sessionnot   reactor(    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR      s    c         C   s   t  j |  j j ƒ  ƒ S(   N(   R   t   UNIXAddresst   sockett   getsockname(   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt   getHost!   s    c         C   s   t  j |  j ƒ S(   N(   R   R   t   hostname(   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt   getPeer$   s    (   t   __name__t
   __module__R   R!   R#   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR      s   		c         C   s   |  d  d k S(   s›  
    Determine whether the given unix socket path is in a filesystem namespace.

    While most PF_UNIX sockets are entries in the filesystem, Linux 2.2 and
    above support PF_UNIX sockets in an "abstract namespace" that does not
    correspond to any path. This function returns C{True} if the given socket
    path is stored in the filesystem and C{False} if the path is in this
    abstract namespace.
    i   t    (    (   t   path(    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt   _inFilesystemNamespace)   s    
t	   _UNIXPortc           B   s   e  Z d  „  Z RS(   c         C   sF   t  j d k s t |  j ƒ r0 |  j j ƒ  } n	 |  j } t j | ƒ S(   sM   Returns a UNIXAddress.

        This indicates the server's address.
        i   i   (   i   i   (   t   syst   version_infoR(   t   portR   R    R   R   (   R   R'   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR!   7   s    	(   R$   R%   R!   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR)   6   s   t   Portc           B   sh   e  Z e j Z e j Z e Z d	 Z
 d  d d	 d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(
   i2   i¶  i    c         C   s5   t  j j |  | | | d | ƒ| |  _ | |  _ d  S(   NR   (   R
   R-   R   t   modet   wantPID(   R   t   fileNamet   factoryt   backlogR.   R   R/   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR   M   s    	c         C   sD   t  j |  j j ƒ } t |  d ƒ r5 d | |  j f Sd | f Sd  S(   NR   s
   <%s on %r>s   <%s (not listening)>(   R   t   qualR1   t	   __class__t   hasattrR,   (   R   t   factoryName(    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt   __repr__R   s    c         C   s   t  j | ƒ S(   N(   R   R   (   R   t   name(    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt
   _buildAddrY   s    c         C   s‹  t  j d |  j j t |  j ƒ f ƒ |  j rÀ t j |  j d ƒ |  _	 |  j	 j
 ƒ  so t d |  j d f ‚ qÀ |  j	 j sÀ y5 t j t j |  j ƒ j ƒ r¯ t j |  j ƒ n  Wq½ q½ XqÀ n  |  j j ƒ  y  |  j ƒ  } | j |  j ƒ Wn+ t j k
 r} t d |  j | f ‚ nm Xt |  j ƒ rCt j |  j |  j ƒ n  | j |  j ƒ t |  _ | |  _ |  j j |  _ d |  _  |  j! ƒ  d S(   sÇ   Create and bind my socket, and begin listening on it.

        This is called on unserialization, and must be called after creating a
        server to begin listening on the specified port.
        s   %s starting on %rs   .locks   Cannot acquire lockid   N("   R   t   msgR1   R4   t   reprR,   R/   R   t   FilesystemLockt   lockFilet   lockR   R   t   cleant   statt   S_ISSOCKt   ost   st_modet   removet   doStartt   createInternetSockett   bindR   R   R(   t   chmodR.   t   listenR2   t   Truet	   connectedt   filenot   numberAcceptst   startReading(   R   t   sktt   le(    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt   startListening\   s2    &	
			c         C   s!   t  j d t |  j ƒ f ƒ d S(   s0   
        Log message for closing socket
        s   (UNIX Port %s Closed)N(   R   R:   R;   R,   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt   _logConnectionLostMsg†   s    c         C   sX   t  |  j ƒ r" t j |  j ƒ n  |  j d  k	 rA |  j j ƒ  n  t j j	 |  | ƒ d  S(   N(
   R(   R,   RB   t   unlinkR=   R   t   unlockR
   R-   t   connectionLost(   R   t   reason(    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyRU      s
    N(   R$   R%   R   R   t   addressFamilyt   SOCK_STREAMt
   socketTypeR   t	   transportR   R=   R   R7   R9   RQ   RR   RU   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR-   F   s   					*	t   Clientc           B   sA   e  Z d  Z e j Z e j Z d d d „ Z	 d „  Z
 d „  Z RS(   s   A client for Unix sockets.i    c         C   sx   | |  _  | |  _ |  _ | rU t j | d ƒ rU |  j d  d  t j | ƒ | ƒ n  |  j |  j	 |  j
 ƒ  d  | ƒ d  S(   Ns   .lock(   t	   connectort   realAddresst   addrR   t   isLockedt   _finishInitR   R   t   BadFileErrort	   doConnectRF   (   R   t   filenameR\   R   t   checkPID(    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR   ›   s    	"c         C   s   t  j |  j ƒ S(   N(   R   R   R^   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR#   £   s    c         C   s   t  j d  ƒ S(   N(   R   R   R   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR!   ¦   s    N(   R$   R%   t   __doc__R   R   RW   RX   RY   R   R   R#   R!   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR[   –   s   			t	   Connectorc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s/   t  j j |  | | | ƒ | |  _ | |  _ d  S(   N(   R	   t   BaseConnectorR   R   Rd   (   R   R   R1   t   timeoutR   Rd   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR   «   s    	c         C   s   t  |  j |  |  j |  j ƒ S(   N(   R[   R   R   Rd   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt   _makeTransport°   s    c         C   s   t  j |  j  ƒ S(   N(   R   R   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt   getDestination³   s    (   R$   R%   R   Ri   Rj   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyRf   ª   s   		t   DatagramPortc           B   sf   e  Z d  Z e e j ƒ e j Z d d d	 d „ Z
 d „  Z d „  Z d „  Z d	 d „ Z d „  Z RS(
   s*   Datagram UNIX port, listening for packets.i    i¶  c         C   s/   t  j j |  | | d | d | ƒ| |  _ d S(   s.   Initialize with address to listen on.
        t   maxPacketSizeR   N(   R   R-   R   R.   (   R   R^   t   protoRl   R.   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR   ¾   s    "c         C   sD   t  j |  j j ƒ } t |  d ƒ r5 d | |  j f Sd | f Sd  S(   NR   s
   <%s on %r>s   <%s (not listening)>(   R   R3   R   R4   R5   R,   (   R   t   protocolName(    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR7   Å   s    c         C   sÙ   t  j d |  j j t |  j ƒ f ƒ y, |  j ƒ  } |  j rQ | j |  j ƒ n  Wn. t j	 k
 r‚ } t	 j
 d  |  j | f ‚ n X|  j r´ t |  j ƒ r´ t j |  j |  j ƒ n  d |  _ | |  _ |  j j |  _ d  S(   Ns   %s starting on %si   (   R   R:   R   R4   R;   R,   RF   RG   R   R   R   R   R(   RB   RH   R.   RK   RL   (   R   RO   RP   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt   _bindSocketÍ   s    &			c         C   sŠ   y |  j  j | | ƒ SWnl t  j k
 r… } | j d } | t k rU |  j | | ƒ S| t k rp t j d ‚ q† | t k r q† ‚  n Xd S(   s   Write a datagram.i    s   message too longN(	   R   t   sendtoR   t   argsR    t   writeR   t   MessageLengthErrorR   (   R   t   datagramR   t   set   no(    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyRr   Ü   s    c         C   sš   t  j d t |  j ƒ ƒ t j j |  | ƒ t |  d ƒ rL |  j j	 ƒ  n  d |  _
 |  j j ƒ  |  ` |  ` t |  d ƒ r– |  j j d ƒ |  ` n  d S(   s   Cleans up my socket.
        s   (Port %s Closed)R   i    t   dN(   R   R:   R;   R,   R	   t   BasePortRU   R5   R   t   doStopRK   R   t   closeRL   Rw   t   callbackR   (   R   RV   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyRU   î   s    	c         C   s    t  j |  j j ƒ d |  _ d  S(   Ns    (UDP)(   R   R3   R   R4   t   logstr(   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt	   setLogStrÿ   s    N(   R$   R%   Re   R   R   t   IUNIXDatagramTransportR   R   RW   R   R   R7   Ro   Rr   RU   R}   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyRk   ·   s   				t   ConnectedDatagramPortc           B   si   e  Z d  Z e e j e e j ƒ Œ d d d	 d	 d „ Z
 d „  Z d „  Z d „  Z d „  Z d „  Z RS(
   s+   
    A connected datagram UNIX socket.
    i    i¶  c         C   sA   t  | t j ƒ s t ‚ t j |  | | | | | ƒ | |  _ d  S(   N(   t
   isinstanceR   t   ConnectedDatagramProtocolt   AssertionErrorRk   R   t
   remoteaddr(   R   R^   Rm   Rl   R.   t   bindAddressR   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR     s    c         C   sL   y+ |  j  ƒ  |  j j |  j ƒ |  j ƒ  Wn |  j t j ƒ  ƒ n Xd  S(   N(   Ro   R   t   connectRƒ   t   _connectToProtocolt   connectionFailedR   t   Failure(   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyRQ     s    
c         C   s$   |  j  ƒ  |  j j | ƒ |  ` d S(   s£   
        Called when a connection fails. Stop listening on the socket.

        @type reason: L{Failure}
        @param reason: Why the connection failed.
        N(   t   stopListeningR   R‡   (   R   RV   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR‡     s    
c         C   sÍ   d } xÀ | |  j  k  rÈ y? |  j j |  j ƒ \ } } | t | ƒ 7} |  j j | ƒ Wq	 t j k
 r´ } | j d } | t	 t
 t f k r’ d S| t k r® |  j j ƒ  qÅ ‚  q	 t j ƒ  q	 Xq	 Wd S(   s=   
        Called when my socket is ready for reading.
        i    N(   t   maxThroughputR   t   recvfromRl   t   lenR   t   datagramReceivedR   Rq   R   R    R   R   t   connectionRefusedR   t   deferr(   R   t   readt   dataR^   Ru   Rv   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt   doRead)  s    c         C   s    y |  j  j | ƒ SWn… t  j k
 r› } | j d } | t k rO |  j | ƒ S| t k rj t j d ‚ qœ | t k r† |  j	 j
 ƒ  qœ | t k r• qœ ‚  n Xd S(   s#   
        Write a datagram.
        i    s   message too longN(   R   t   sendR   Rq   R    Rr   R   Rs   R   R   RŽ   R   (   R   R‘   Ru   Rv   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyRr   ?  s    c         C   s   t  j |  j ƒ S(   N(   R   R   Rƒ   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR#   V  s    N(   R$   R%   Re   R   R   t   IUNIXDatagramConnectedTransportR   R	   Rx   R   R   RQ   R‡   R’   Rr   R#   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyR     s   						(+   Re   RB   R*   R@   R   t   errnoR    R   R   R   R   t   zope.interfaceR   R   R   R5   t   ImportErrort   twisted.internetR	   R
   R   R   R   R   R   t   twisted.internet.errorR   t   twisted.pythonR   R   R   R   R   R(   t   objectR)   R-   t
   BaseClientR[   Rg   Rf   Rk   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/internet/unix.pyt   <module>   s    0(4"	PM