ó
[³XMc        	   @   sC  d  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	 d „  Z
 d e j f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ i d d 6d d 6Z d d l Z x¿ d j ƒ  D]± Z x¨ d j ƒ  D]š Z e d k re d k rqà n  e ƒ  d e Z i d d 6j e e ƒ Z e e e ƒ  Z e j e e e f i e d 6e d 6ƒ Z e e ƒ  e e <qà WqÍ Wd e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d  e j f d! „  ƒ  YZ d" e j e  f d# „  ƒ  YZ! d d  d$ d" g g  d% j ƒ  D]# Z d j ƒ  D] Z e e ^ q%qZ" d S(&   sÁ  
Reactor-based Services

Here are services to run clients, servers and periodic services using
the reactor.

If you want to run a server service, L{StreamServerEndpointService} defines a
service that can wrap an arbitrary L{IStreamServerEndpoint} as an L{IService}.
See also L{twisted.internet.strports.service} for constructing one of these
directly from a descriptive string.

Additionally, this module (dynamically) defines various Service subclasses that
let you represent clients and servers in a Service hierarchy.  Endpoints APIs
should be preferred for stream server services, but since those APIs do not yet
exist for clients or datagram services, many of these are still useful.

They are as follows::

  TCPServer, TCPClient,
  UNIXServer, UNIXClient,
  SSLServer, SSLClient,
  UDPServer, UDPClient,
  UNIXDatagramServer, UNIXDatagramClient,
  MulticastServer

These classes take arbitrary arguments in their constructors and pass
them straight on to their respective reactor.listenXXX or
reactor.connectXXX calls.

For example, the following service starts a web server on port 8080:
C{TCPServer(8080, server.Site(r))}.  See the documentation for the
reactor.listen/connect* methods for more information.
iÿÿÿÿN(   t   log(   t   service(   t   task(   t   CancelledErrorc         C   s(   |  d k r  d d l m } | S|  Sd S(   sR   
    @return: the argument, or the global reactor if the argument is C{None}.
    iÿÿÿÿ(   t   reactorN(   t   Nonet   twisted.internetR   (   t   maybeReactorR   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyt   _maybeGlobalReactor0   s    t   _VolatileDataServicec           B   s   e  Z g  Z d  „  Z RS(   c         C   s@   t  j j |  ƒ } x' |  j D] } | | k r | | =q q W| S(   N(   R   t   Servicet   __getstate__t   volatile(   t   selft   dt   attr(    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   ?   s
    (   t   __name__t
   __module__R   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR	   ;   s   t   _AbstractServerc           B   sV   e  Z d  Z d g Z d Z d Z d Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z RS(   sü  
    @cvar volatile: list of attribute to remove from pickling.
    @type volatile: C{list}

    @ivar method: the type of method to call on the reactor, one of B{TCP},
        B{UDP}, B{SSL} or B{UNIX}.
    @type method: C{str}

    @ivar reactor: the current running reactor.
    @type reactor: a provider of C{IReactorTCP}, C{IReactorUDP},
        C{IReactorSSL} or C{IReactorUnix}.

    @ivar _port: instance of port set when the service is started.
    @type _port: a provider of C{IListeningPort}.
    t   _portc         O   s7   | |  _  d | k r* | j d ƒ |  _ n  | |  _ d  S(   NR   (   t   argst   popR   t   kwargs(   R   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyt   __init___   s    	c         C   s#   t  j j |  ƒ |  j ƒ  |  _ d  S(   N(   R   R
   t   privilegedStartServicet   _getPortR   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   f   s    c         C   s5   t  j j |  ƒ |  j d  k r1 |  j ƒ  |  _ n  d  S(   N(   R   R
   t   startServiceR   R   R   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   k   s    c         C   s<   t  j j |  ƒ |  j d  k	 r8 |  j j ƒ  } |  ` | Sd  S(   N(   R   R
   t   stopServiceR   R   t   stopListening(   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   q   s
    c         C   s/   t  t |  j ƒ d |  j f ƒ |  j |  j Ž  S(   sÆ   
        Wrapper around the appropriate listen method of the reactor.

        @return: the port object returned by the listen method.
        @rtype: an object providing L{IListeningPort}.
        s   listen%s(   t   getattrR   R   t   methodR   R   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   {   s    N(   R   R   t   __doc__R   R   R   R   R   R   R   R   R   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   H   s   					
t   _AbstractClientc           B   sM   e  Z d  Z d g Z d Z d Z d Z d „  Z d „  Z	 d „  Z
 d „  Z RS(   s
  
    @cvar volatile: list of attribute to remove from pickling.
    @type volatile: C{list}

    @ivar method: the type of method to call on the reactor, one of B{TCP},
        B{UDP}, B{SSL} or B{UNIX}.
    @type method: C{str}

    @ivar reactor: the current running reactor.
    @type reactor: a provider of C{IReactorTCP}, C{IReactorUDP},
        C{IReactorSSL} or C{IReactorUnix}.

    @ivar _connection: instance of connection set when the service is started.
    @type _connection: a provider of C{IConnector}.
    t   _connectionc         O   s7   | |  _  d | k r* | j d ƒ |  _ n  | |  _ d  S(   NR   (   R   R   R   R   (   R   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR      s    	c         C   s#   t  j j |  ƒ |  j ƒ  |  _ d  S(   N(   R   R
   R   t   _getConnectionR!   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   ¤   s    c         C   s9   t  j j |  ƒ |  j d  k	 r5 |  j j ƒ  |  ` n  d  S(   N(   R   R
   R   R!   R   t
   disconnect(   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   ©   s    c         C   s/   t  t |  j ƒ d |  j f ƒ |  j |  j Ž  S(   sÄ   
        Wrapper around the appropriate connect method of the reactor.

        @return: the port object returned by the connect method.
        @rtype: an object providing L{IConnector}.
        s	   connect%s(   R   R   R   R   R   R   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR"   °   s    N(   R   R   R   R   R   R   R   R!   R   R   R   R"   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR    ‡   s   				sz   Connect to %(tran)s

Call reactor.connect%(method)s when the service starts, with the
arguments given to the constructor.
t   Clientsú   Serve %(tran)s clients

Call reactor.listen%(method)s when the service starts, with the
arguments given to the constructor. When the service stops,
stop listening. See twisted.internet.interfaces for documentation
on arguments to the reactor method.
t   Servers'   TCP UNIX SSL UDP UNIXDatagram Multicasts   Server Clientt	   Multicastt	   _Abstractt   Witht   GenericR   R   t   GenericServerc           B   s   e  Z d  Z d Z d „  Z RS(   sX  
    Serve Generic clients

    Call reactor.listenWith when the service starts, with the arguments given to
    the constructor. When the service stops, stop listening. See
    twisted.internet.interfaces for documentation on arguments to the reactor
    method.

    This service is deprecated (because reactor.listenWith is deprecated).
    R(   c         O   s0   t  j d d t d d ƒt j |  | | Ž d  S(   Ns-   GenericServer was deprecated in Twisted 10.1.t   categoryt
   stackleveli   (   t   warningst   warnt   DeprecationWarningR   R   (   R   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   è   s
    (   R   R   R   R   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR*   Û   s   
t   GenericClientc           B   s   e  Z d  Z d Z d „  Z RS(   sÒ   
    Connect to Generic.

    Call reactor.connectWith when the service starts, with the arguments given
    to the constructor.

    This service is deprecated (because reactor.connectWith is deprecated).
    R(   c         O   s0   t  j d d t d d ƒt j |  | | Ž d  S(   Ns-   GenericClient was deprecated in Twisted 10.1.R+   R,   i   (   R-   R.   R/   R    R   (   R   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   ü   s
    (   R   R   R   R   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR0   ñ   s   t   TimerServicec           B   s;   e  Z d  Z d g Z d „  Z d „  Z d „  Z d „  Z RS(   sÎ   Service to periodically call a function

    Every C{step} seconds call the given function with the given arguments.
    The service starts the calls when it starts, and cancels them
    when it stops.
    t   _loopc         O   s   | |  _  | | | f |  _ d  S(   N(   t   stept   call(   R   R3   t   callableR   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR     s    	c         C   sc   t  j j |  ƒ |  j \ } } } t j | | | Ž |  _ |  j j |  j d t	 ƒj
 |  j ƒ d  S(   Nt   now(   R   R
   R   R4   R   t   LoopingCallR2   t   startR3   t   Truet
   addErrbackt   _failed(   R   R5   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR     s    c         C   s   t  |  j _ t j | ƒ d  S(   N(   t   FalseR2   t   runningR    t   err(   R   t   why(    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR;     s    c         C   s,   |  j  j r |  j  j ƒ  n  t j j |  ƒ S(   N(   R2   R=   t   stopR   R
   R   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   %  s    (   R   R   R   R   R   R   R;   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR1     s   			
	t   CooperatorServicec           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sb   
    Simple L{service.IService} which starts and stops a L{twisted.internet.task.Cooperator}.
    c         C   s   t  j d t ƒ |  _ d  S(   Nt   started(   R   t
   CooperatorR<   t   coop(   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   0  s    c         C   s   |  j  j | ƒ S(   N(   RD   t	   coiterate(   R   t   iterator(    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyRE   4  s    c         C   s   |  j  j ƒ  d  S(   N(   RD   R8   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   8  s    c         C   s   |  j  j ƒ  d  S(   N(   RD   R@   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   <  s    (   R   R   R   R   RE   R   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyRA   ,  s
   			t   StreamServerEndpointServicec           B   s8   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z RS(   sÜ  
    A L{StreamServerEndpointService} is an L{IService} which runs a server on a
    listening port described by an L{IStreamServerEndpoint}.

    @ivar factory: A server factory which will be used to listen on the
        endpoint.

    @ivar endpoint: An L{IStreamServerEndpoint} provider which will be used to
        listen when the service starts.

    @ivar _waitingForPort: a Deferred, if C{listen} has yet been invoked on the
        endpoint, otherwise None.

    @ivar _raiseSynchronously: Defines error-handling behavior for the case
        where C{listen(...)} raises an exception before C{startService} or
        C{privilegedStartService} have completed.

    @type _raiseSynchronously: C{bool}

    @since: 10.2
    c         C   s   | |  _  | |  _ d  |  _ d  S(   N(   t   endpointt   factoryR   t   _waitingForPort(   R   RH   RI   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   Z  s    		c            sk   t  j j ˆ  ƒ ˆ  j j ˆ  j ƒ ˆ  _ g  ‰ ‡  ‡ f d †  } ˆ  j j | ƒ ˆ rg ˆ d j ƒ  n  d S(   s2   
        Start listening on the endpoint.
        c            s<   ˆ  j  r ˆ j |  ƒ n |  j t ƒ s8 t j |  ƒ n  d  S(   N(   t   _raiseSynchronouslyt   appendt   checkR   R    R>   (   R>   (   R   t	   raisedNow(    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyt   handleItg  s    	i    N(	   R   R
   R   RH   t   listenRI   RJ   R:   t   raiseException(   R   RO   (    (   R   RN   s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   `  s    c         C   s0   t  j j |  ƒ |  j d k r, |  j ƒ  n  d S(   su   
        Start listening on the endpoint, unless L{privilegedStartService} got
        around to it already.
        N(   R   R
   R   RJ   R   R   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   q  s    c            sH   ˆ  j  j ƒ  d „  } ˆ  j  j | ƒ } ‡  f d †  } | j | ƒ | S(   sã   
        Stop listening on the port if it is already listening, otherwise,
        cancel the attempt to listen.

        @return: a L{Deferred} which fires with C{None} when the port has
            stopped listening.
        c         S   s   |  d  k	 r |  j ƒ  Sd  S(   N(   R   R   (   t   port(    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyt   stopIt„  s    c            s   t  ˆ  _ |  S(   N(   R<   R=   (   t   passthrough(   R   (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR@   ˆ  s    	(   RJ   t   cancelt   addCallbackt   addBoth(   R   RS   R   R@   (    (   R   s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyR   {  s    	N(	   R   R   R   R   RK   R   R   R   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyRG   A  s   			
t   MulticastServers%   Generic TCP UNIX SSL UDP UNIXDatagram(#   R   R-   t   twisted.pythonR    t   twisted.applicationR   R   R   t   twisted.internet.deferR   R   R
   R	   R   R    t   _doct   newt   splitt   trant   sidet   globalst   baset   getR   t   varst   doct   classobjt   klassR*   R0   R1   RA   t   objectRG   t   __all__(    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/application/internet.pyt   <module>%   sB   	?5
'O	