o
    |j6h"                     @   s   d Z ddlmZ ddlmZmZmZm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d
lmZ ddlmZ dedeej fddZG dd dejZ						ddee deeej  deee  dee dee deej fddZdS )z2Server-side implementation of gRPC Asyncio Python.    )Executor)AnyDictOptionalSequenceN)_common)_compression)cygrpc   )_base_server)ServerInterceptor)ChannelArgumentTypebase_optionscompressionc                 C   s   t |}t| | S N)r   create_channel_optiontuple)r   r   compression_option r   _/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/grpc/aio/_server.py_augment_channel_arguments   s   
r   c                   @   s   e Zd ZdZdee deeej  deee	  de
dee deej fdd	Zd
eej ddfddZdedeeejf ddfddZdedefddZdedejdefddZd#ddZdee ddfddZ	d$dee defdd Zd!d" ZdS )%ServerzServes RPCs.thread_poolgeneric_handlersinterceptorsoptionsmaximum_concurrent_rpcsr   c              	   C   sP   t  | _|rdd |D }|rtd| t | j|||t|||| _d S )Nc                 S   s   g | ]	}t |ts|qS r   )
isinstancer   ).0interceptorr   r   r   
<listcomp>2   s    z#Server.__init__.<locals>.<listcomp>zBInterceptor must be ServerInterceptor, the following are invalid: )r	   get_working_loop_loop
ValueError	AioServerr   _server)selfr   r   r   r   r   r   invalid_interceptorsr   r   r   __init__'   s&   
	
zServer.__init__generic_rpc_handlersreturnNc                 C   s   | j | dS )zRegisters GenericRpcHandlers with this Server.

        This method is only safe to call before the server is started.

        Args:
          generic_rpc_handlers: A sequence of GenericRpcHandlers that will be
          used to service RPCs.
        N)r%   add_generic_rpc_handlers)r&   r)   r   r   r   r+   E   s   zServer.add_generic_rpc_handlersservice_namemethod_handlersc                 C   s   d S r   r   )r&   r,   r-   r   r   r   add_registered_method_handlersR   s   z%Server.add_registered_method_handlersaddressc                 C   s   t || jt |S )a  Opens an insecure port for accepting RPCs.

        This method may only be called before starting the server.

        Args:
          address: The address for which to open a port. If the port is 0,
            or not specified in the address, then the gRPC runtime will choose a port.

        Returns:
          An integer port on which the server will accept RPC requests.
        )r   validate_port_binding_resultr%   add_insecure_portencode)r&   r/   r   r   r   r1   Z   s   zServer.add_insecure_portserver_credentialsc                 C   s   t || jt ||S )a  Opens a secure port for accepting RPCs.

        This method may only be called before starting the server.

        Args:
          address: The address for which to open a port.
            if the port is 0, or not specified in the address, then the gRPC
            runtime will choose a port.
          server_credentials: A ServerCredentials object.

        Returns:
          An integer port on which the server will accept RPC requests.
        )r   r0   r%   add_secure_portr2   )r&   r/   r3   r   r   r   r4   j   s   
zServer.add_secure_portc                    s   | j  I dH  dS )zgStarts this Server.

        This method may only be called once. (i.e. it is not idempotent).
        N)r%   startr&   r   r   r   r5      s   zServer.startgracec                    s   | j |I dH  dS )a  Stops this Server.

        This method immediately stops the server from servicing new RPCs in
        all cases.

        If a grace period is specified, this method waits until all active
        RPCs are finished or until the grace period is reached. RPCs that haven't
        been terminated within the grace period are aborted.
        If a grace period is not specified (by passing None for grace), all
        existing RPCs are aborted immediately and this method blocks until
        the last RPC handler terminates.

        This method is idempotent and may be called at any time. Passing a
        smaller grace value in a subsequent call will have the effect of
        stopping the Server sooner (passing None will have the effect of
        stopping the server immediately). Passing a larger grace value in a
        subsequent call will not have the effect of stopping the server later
        (i.e. the most restrictive grace value is used).

        Args:
          grace: A duration of time in seconds or None.
        N)r%   shutdown)r&   r7   r   r   r   stop   s   zServer.stoptimeoutc                    s   | j |I dH S )a  Block current coroutine until the server stops.

        This is an EXPERIMENTAL API.

        The wait will not consume computational resources during blocking, and
        it will block until one of the two following conditions are met:

        1) The server is stopped or terminated;
        2) A timeout occurs if timeout is not `None`.

        The timeout argument works in the same way as `threading.Event.wait()`.
        https://docs.python.org/3/library/threading.html#threading.Event.wait

        Args:
          timeout: A floating point number specifying a timeout for the
            operation in seconds.

        Returns:
          A bool indicates if the operation times out.
        N)r%   wait_for_termination)r&   r:   r   r   r   r;      s   zServer.wait_for_terminationc                 C   s6   t | dr| j rt| jd| j dS dS dS )zSchedules a graceful shutdown in current event loop.

        The Cython AioServer doesn't hold a ref-count to this class. It should
        be safe to slightly extend the underlying Cython object's life span.
        r%   N)hasattrr%   
is_runningr	   schedule_coro_threadsafer8   r"   r6   r   r   r   __del__   s   


zServer.__del__)r*   Nr   )__name__
__module____qualname____doc__r   r   r   grpcGenericRpcHandlerr   r   intCompressionr(   r+   strr   RpcMethodHandlerr.   r1   ServerCredentialsr4   r5   floatr9   boolr;   r?   r   r   r   r   r   $   sX    






r   migration_thread_poolhandlersr   r   r   c                 C   s<   t | |du rdn||du rdn||du rd||S |||S )a  Creates a Server with which RPCs can be serviced.

    Args:
      migration_thread_pool: A futures.ThreadPoolExecutor to be used by the
        Server to execute non-AsyncIO RPC handlers for migration purpose.
      handlers: An optional list of GenericRpcHandlers used for executing RPCs.
        More handlers may be added by calling add_generic_rpc_handlers any time
        before the server is started.
      interceptors: An optional list of ServerInterceptor objects that observe
        and optionally manipulate the incoming RPCs before handing them over to
        handlers. The interceptors are given control in the order they are
        specified. This is an EXPERIMENTAL API.
      options: An optional list of key-value pairs (:term:`channel_arguments` in gRPC runtime)
        to configure the channel.
      maximum_concurrent_rpcs: The maximum number of concurrent RPCs this server
        will service before returning RESOURCE_EXHAUSTED status, or None to
        indicate no limit.
      compression: An element of grpc.compression, e.g.
        grpc.compression.Gzip. This compression algorithm will be used for the
        lifetime of the server unless overridden by set_compression.

    Returns:
      A Server object.
    Nr   )r   )rM   rN   r   r   r   r   r   r   r   server   s    
rO   )NNNNNN)rC   concurrent.futuresr   typingr   r   r   r   rD   r   r   grpc._cythonr	    r   _interceptorr   _typingr   rG   r   r   rE   rF   rO   r   r   r   r   <module>   sH   
 &
