
    Vh2                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZ  G d	 d
ej                        Z G d de      Z G d de      ZdZdZ G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d e      Z G d! d"e      Z G d# d$e      Z G d% d&e      Z G d' d(ej                        Z  G d) d*ej                        Z!g d+Z"y,)-a  
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
<twisted.internet.interfaces.IStreamServerEndpoint>}
as an L{IService}. See also L{twisted.application.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,
  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.
    )List)service)task)CancelledError)log   )ClientService_maybeGlobalReactorbackoffPolicyc                   (    e Zd ZU g Zee   ed<   d Zy)_VolatileDataServicevolatilec                 v    t         j                  j                  |       }| j                  D ]
  }||v s||=  |S N)r   Service__getstate__r   )selfdattrs      L/home/dcms/DCMS/lib/python3.12/site-packages/twisted/application/internet.pyr   z!_VolatileDataService.__getstate__5   s>    OO((.MM 	DqydG	     N)__name__
__module____qualname__r   r   str__annotations__r    r   r   r   r   2   s    Hd3ir   r   c                   L    e Zd ZU dZdgZdZeed<   dZdZ	d Z
d Zd Zd	 Zd
 Zy)_AbstractServera  
    @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 L{twisted.internet.interfaces.IListeningPort}.
    _port methodNc                 T    || _         d|v r|j                  d      | _        || _        y Nreactorargspopr%   kwargsr   r'   r)   s      r   __init__z_AbstractServer.__init__T   (    	!::i0DLr   c                 l    t         j                  j                  |        | j                         | _        y r   )r   r   privilegedStartService_getPortr    r   s    r   r.   z&_AbstractServer.privilegedStartServiceZ   s!    ..t4]]_
r   c                     t         j                  j                  |        | j                  | j	                         | _        y y r   )r   r   startServicer    r/   r0   s    r   r2   z_AbstractServer.startService^   s0    $$T*::DJ r   c                     t         j                  j                  |        | j                  | j                  j	                         }| `|S y r   )r   r   stopServicer    stopListening)r   r   s     r   r4   z_AbstractServer.stopServicec   sA    ##D) ::!

((*A
H "r   c                      t        t        | j                        dj                  | j                              | j
                  i | j                  S )z
        Wrapper around the appropriate listen method of the reactor.

        @return: the port object returned by the listen method.
        @rtype: an object providing
            L{twisted.internet.interfaces.IListeningPort}.
        zlisten{})getattrr
   r%   formatr"   r'   r)   r0   s    r   r/   z_AbstractServer._getPortl   sP    
w-

 99%
 % 	%r   )r   r   r   __doc__r   r"   r   r   r%   r    r+   r.   r2   r4   r/   r   r   r   r   r   =   s=      yHFCGE%)
%r   r   c                   F    e Zd ZU dZdgZdZeed<   dZdZ	d Z
d Zd Zd	 Zy)
_AbstractClienta&  
    @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 L{twisted.internet.interfaces.IConnector}.
    _connectionr!   r"   Nc                 T    || _         d|v r|j                  d      | _        || _        y r$   r&   r*   s      r   r+   z_AbstractClient.__init__   r,   r   c                 l    t         j                  j                  |        | j                         | _        y r   )r   r   r2   _getConnectionr<   r0   s    r   r2   z_AbstractClient.startService   s%    $$T*..0r   c                     t         j                  j                  |        | j                  | j                  j	                          | `y y r   )r   r   r4   r<   
disconnectr0   s    r   r4   z_AbstractClient.stopService   s=    ##D)''')  (r   c                      t        t        | j                        d| j                         | j                  i | j
                  S )z
        Wrapper around the appropriate connect method of the reactor.

        @return: the port object returned by the connect method.
        @rtype: an object providing L{twisted.internet.interfaces.IConnector}.
        connect)r7   r
   r%   r"   r'   r)   r0   s    r   r?   z_AbstractClient._getConnection   sC     Sw*4<<8GDKK=:QRYY
++
 	
r   )r   r   r   r9   r   r"   r   r   r%   r<   r+   r2   r4   r?   r   r   r   r;   r;   |   s8      HFCGK1!	
r   r;   ztConnect to {tran}

Call reactor.connect{tran} when the service starts, with the
arguments given to the constructor.
zServe {tran} clients

Call reactor.listen{tran} 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.
c                   4    e Zd Zej	                  d      ZdZy)	TCPServerTCPtranNr   r   r   
_serverDocr8   r9   r"   r   r   r   rE   rE          U+GFr   rE   c                   4    e Zd Zej	                  d      ZdZy)	TCPClientrF   rG   Nr   r   r   
_clientDocr8   r9   r"   r   r   r   rM   rM      rK   r   rM   c                   4    e Zd Zej	                  d      ZdZy)
UNIXServerUNIXrG   NrI   r   r   r   rQ   rQ          V,GFr   rQ   c                   4    e Zd Zej	                  d      ZdZy)
UNIXClientrR   rG   NrN   r   r   r   rU   rU      rS   r   rU   c                   4    e Zd Zej	                  d      ZdZy)	SSLServerSSLrG   NrI   r   r   r   rW   rW      rK   r   rW   c                   4    e Zd Zej	                  d      ZdZy)	SSLClientrX   rG   NrN   r   r   r   rZ   rZ      rK   r   rZ   c                   4    e Zd Zej	                  d      ZdZy)	UDPServerUDPrG   NrI   r   r   r   r\   r\      rK   r   r\   c                   4    e Zd Zej	                  d      ZdZy)UNIXDatagramServerUNIXDatagramrG   NrI   r   r   r   r_   r_          ^4GFr   r_   c                   4    e Zd Zej	                  d      ZdZy)UNIXDatagramClientr`   rG   NrN   r   r   r   rc   rc      ra   r   rc   c                   4    e Zd Zej	                  d      ZdZy)MulticastServer	MulticastrG   NrI   r   r   r   re   re      s    [1GFr   re   c                   0    e Zd ZdZddgZd Zd Zd Zd Zy)	TimerServicea  
    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.

    @ivar clock: Source of time. This defaults to L{None} which is
        causes L{twisted.internet.reactor} to be used.
        Feel free to set this to something else, but it probably ought to be
        set *before* calling L{startService}.
    @type clock: L{IReactorTime<twisted.internet.interfaces.IReactorTime>}

    @ivar call: Function and arguments to call periodically.
    @type call: L{tuple} of C{(callable, args, kwargs)}
    _loop_loopFinishedc                 4    || _         |||f| _        d| _        y)a+  
        @param step: The number of seconds between calls.
        @type step: L{float}

        @param callable: Function to call
        @type callable: L{callable}

        @param args: Positional arguments to pass to function
        @param kwargs: Keyword arguments to pass to function
        N)stepcallclock)r   rl   callabler'   r)   s        r   r+   zTimerService.__init__  s      	tV,	
r   c                    t         j                  j                  |        | j                  \  }}}t	        j
                  |g|i || _        t        | j                        | j                  _        | j                  j                  | j                  d      | _        | j                  j                  | j                         y )NT)now)r   r   r2   rm   r   LoopingCallri   r
   rn   startrl   rj   
addErrback_failed)r   ro   r'   r)   s       r   r2   zTimerService.startService  s    $$T*!%$
 %%h@@@
.tzz:

!ZZ--diiT-B%%dll3r   c                 P    d| j                   _        t        j                  |       y NF)ri   runningr   err)r   whys     r   ru   zTimerService._failed  s     #

r   c                       j                   j                  r j                   j                           j                  j	                   fd        j                  S )z
        Stop the service.

        @rtype: L{Deferred<defer.Deferred>}
        @return: a L{Deferred<defer.Deferred>} which is fired when the
            currently running call (if any) is finished.
        c                 B    t         j                  j                        S r   )r   r   r4   )_r   s    r   <lambda>z*TimerService.stopService.<locals>.<lambda>0  s    1L1LT1R r   )ri   rx   stoprj   addCallbackr0   s   `r   r4   zTimerService.stopService&  sA     ::JJOO&&'RS!!!r   N)	r   r   r   r9   r   r+   r2   ru   r4   r   r   r   rh   rh      s'    " )H
4"r   rh   c                   (    e Zd ZdZd Zd Zd Zd Zy)CooperatorServicezb
    Simple L{service.IService} which starts and stops a L{twisted.internet.task.Cooperator}.
    c                 :    t        j                  d      | _        y )NF)started)r   
Cooperatorcoopr0   s    r   r+   zCooperatorService.__init__9  s    OOE2	r   c                 8    | j                   j                  |      S r   )r   	coiterate)r   iterators     r   r   zCooperatorService.coiterate<  s    yy""8,,r   c                 8    | j                   j                          y r   )r   rs   r0   s    r   r2   zCooperatorService.startService?  s    		r   c                 8    | j                   j                          y r   )r   r   r0   s    r   r4   zCooperatorService.stopServiceB  s    		r   N)r   r   r   r9   r+   r   r2   r4   r   r   r   r   r   4  s    3-r   r   c                   ,    e Zd ZdZdZd Zd Zd Zd Zy)StreamServerEndpointServiceaP  
    A L{StreamServerEndpointService} is an L{IService} which runs a server on a
    listening port described by an L{IStreamServerEndpoint
    <twisted.internet.interfaces.IStreamServerEndpoint>}.

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

    @ivar endpoint: An L{IStreamServerEndpoint
        <twisted.internet.interfaces.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
    Fc                 .    || _         || _        d | _        y r   )endpointfactory_waitingForPort)r   r   r   s      r   r+   z$StreamServerEndpointService.__init__a  s     #r   c                     t         j                  j                           j                  j	                   j
                         _        g  fd} j                  j                  |       rd   j                          d _	        y)z2
        Start listening on the endpoint.
        c                     j                   rj                  |        y | j                  t              st	        j
                  |        y y r   )_raiseSynchronouslyappendcheckr   r   ry   )ry   	raisedNowr   s    r   handleItzDStreamServerEndpointService.privilegedStartService.<locals>.handleItn  s6    ''  %YY~. /r   r   FN)
r   r   r.   r   listenr   r   rt   raiseExceptionr   )r   r   r   s   ` @r   r.   z2StreamServerEndpointService.privilegedStartServicef  sm     	..t4#}}33DLLA		 	''1aL'')#( r   c                 |    t         j                  j                  |        | j                  | j	                          yy)zu
        Start listening on the endpoint, unless L{privilegedStartService} got
        around to it already.
        N)r   r   r2   r   r.   r0   s    r   r2   z(StreamServerEndpointService.startServicey  s3    
 	$$T*''') (r   c                       j                   j                          d } j                   j                  |      } fd}|j                  |       |S )a  
        Stop listening on the port if it is already listening, otherwise,
        cancel the attempt to listen.

        @return: a L{Deferred<twisted.internet.defer.Deferred>} which fires
            with L{None} when the port has stopped listening.
        c                 (    | | j                         S y r   )r5   )ports    r   stopItz7StreamServerEndpointService.stopService.<locals>.stopIt  s    ))++  r   c                     d_         | S rw   )rx   )passthroughr   s    r   r   z5StreamServerEndpointService.stopService.<locals>.stop  s     DLr   )r   cancelr   addBoth)r   r   r   r   s   `   r   r4   z'StreamServerEndpointService.stopService  sK     	##%	,   ,,V4	 	
		$r   N)	r   r   r   r9   r   r+   r.   r2   r4   r   r   r   r   r   F  s#    0  $
)&*r   r   )rh   r   re   r   r\   r	   rE   rM   rQ   rU   rW   rZ   r_   rc   r	   r   N)#r9   typingr   twisted.applicationr   twisted.internetr   twisted.internet.deferr   twisted.pythonr   _client_servicer	   r
   r   r   r   r   r;   rO   rJ   rE   rM   rQ   rU   rW   rZ   r\   r_   rc   re   rh   r   r   __all__r   r   r   <module>r      s  
!H  ' ! 1  N N7?? <%* <%~0
* 0
f

 
 
 
 
 
 
 
 
 
o 
A"' A"H $Q'// Qhr   