
    MhF%                         d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZ ddlZddlmZmZmZmZmZmZmZmZ  G d d          Z G d	 d
e          Z G d de          Z G d de          Zg dZdS )z2Classes for running 0MQ Devices in the background.    N)Process)Thread)AnyCallableListOptionalTuple)ENOTSOCKETERMPUSHQUEUEContextZMQBindErrorZMQErrorproxyc            	       ~   e Zd ZU dZej        Zeg ej        f         e	d<   	 e
e	d<   ee	d<   ee	d<   ee	d<   ee         e	d<   ee         e	d<   eeeef                  e	d	<   ee         e	d
<   ee         e	d<   eeeef                  e	d<   ee         e	d<   eej                 e	d<   eddfdedee         dee         ddfdZdeddfdZdedefdZdeddfdZdededdfdZdeddfdZdedefdZdefdZdedefdZdedefdZdeej        ej        f         fdZd%dZd  Zd%d!Z d%d"Z!d&d#ee"         ddfd$Z#dS )'Deviceaa  A 0MQ Device to be run in the background.

    You do not pass Socket instances to this, but rather Socket types::

        Device(device_type, in_socket_type, out_socket_type)

    For instance::

        dev = Device(zmq.QUEUE, zmq.DEALER, zmq.ROUTER)

    Similar to zmq.device, but socket types instead of sockets themselves are
    passed, and the sockets are created in the work thread, to avoid issues
    with thread safety. As a result, additional bind_{in|out} and
    connect_{in|out} methods and setsockopt_{in|out} allow users to specify
    connections for the sockets.

    Parameters
    ----------
    device_type : int
        The 0MQ Device type
    {in|out}_type : int
        zmq socket types, to be passed later to context.socket(). e.g.
        zmq.PUB, zmq.SUB, zmq.REQ. If out_type is < 0, then in_socket is used
        for both in_socket and out_socket.

    Methods
    -------
    bind_{in_out}(iface)
        passthrough for ``{in|out}_socket.bind(iface)``, to be called in the thread
    connect_{in_out}(iface)
        passthrough for ``{in|out}_socket.connect(iface)``, to be called in the
        thread
    setsockopt_{in_out}(opt,value)
        passthrough for ``{in|out}_socket.setsockopt(opt, value)``, to be called in
        the thread

    Attributes
    ----------
    daemon : bool
        sets whether the thread should be run as a daemon
        Default is true, because if it is false, the thread will not
        exit unless it is killed
    context_factory : callable
        This is a class attribute.
        Function for creating the Context. This will be Context.instance
        in ThreadDevices, and Context in ProcessDevices.  The only reason
        it is not instance() in ProcessDevices is that there may be a stale
        Context instance already initialized, and the forked environment
        should *never* try to use it.
    context_factorydaemondevice_typein_typeout_type	_in_binds_in_connects_in_sockopts
_out_binds_out_connects_out_sockopts_random_addrs_socketsNreturnc                     || _         |t          d          |t          d          || _        || _        g | _        g | _        g | _        g | _        g | _        g | _	        g | _
        d| _        d| _        g | _        d S )Nzin_type must be specifiedzout_type must be specifiedTF)r   	TypeErrorr   r   r   r   r   r   r   r   r   r   doner    )selfr   r   r   s       V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/zmq/devices/basedevice.py__init__zDevice.__init__V   s     '?78888999 	    addrc                 :    | j                             |           dS )z`Enqueue ZMQ address for binding on in_socket.

        See zmq.Socket.bind for details.
        N)r   appendr%   r)   s     r&   bind_inzDevice.bind_inn   s     
 	d#####r(   c                 \     | j         |g|R i |}|                     | d|            |S )zEnqueue a random port on the given interface for binding on
        in_socket.

        See zmq.Socket.bind_to_random_port for details.

        .. versionadded:: 18.0
        :)_reserve_random_portr-   r%   r)   argskwargsports        r&   bind_in_to_random_portzDevice.bind_in_to_random_portu   sK     )t(??????%%t%%&&&r(   c                 :    | j                             |           dS )zfEnqueue ZMQ address for connecting on in_socket.

        See zmq.Socket.connect for details.
        N)r   r+   r,   s     r&   
connect_inzDevice.connect_in   s!    
 	  &&&&&r(   optvaluec                 >    | j                             ||f           dS )zeEnqueue setsockopt(opt, value) for in_socket

        See zmq.Socket.setsockopt for details.
        N)r   r+   r%   r8   r9   s      r&   setsockopt_inzDevice.setsockopt_in   s%    
 	  #u.....r(   c                 :    | j                             |           dS )zaEnqueue ZMQ address for binding on out_socket.

        See zmq.Socket.bind for details.
        N)r   r+   r,   s     r&   bind_outzDevice.bind_out   s     
 	t$$$$$r(   c                 \     | j         |g|R i |}|                     | d|            |S )zEnqueue a random port on the given interface for binding on
        out_socket.

        See zmq.Socket.bind_to_random_port for details.

        .. versionadded:: 18.0
        r/   )r0   r>   r1   s        r&   bind_out_to_random_portzDevice.bind_out_to_random_port   sK     )t(??????&&&&'''r(   c                 :    | j                             |           dS )zgEnqueue ZMQ address for connecting on out_socket.

        See zmq.Socket.connect for details.
        N)r   r+   r,   s     r&   connect_outzDevice.connect_out   s!    
 	!!$'''''r(   c                 >    | j                             ||f           dS )zfEnqueue setsockopt(opt, value) for out_socket

        See zmq.Socket.setsockopt for details.
        N)r   r+   r;   s      r&   setsockopt_outzDevice.setsockopt_out   s%    
 	!!3,/////r(   c                 r   t                      5 }|                    t                    5 }t          d          D ]%} |j        |g|R i |}| d| }|| j        v r$ nt          d          | j                            |           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |S )N   r/   zCould not reserve random port.)r   socketr   rangebind_to_random_portr   r   r+   )	r%   r)   r2   r3   ctxbinderir4   new_addrs	            r&   r0   zDevice._reserve_random_port   sQ   YY 	4#D!! 4Vq 
I 
IA565dLTLLLVLLD"&////H4#555 &'GHHH"))(3334 4 4 4 4 4 4 4 4 4 4 4 4 4 4	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4  s5   B,AB	B,B	B,B	B,,B03B0c                    |                                  }|| _        |                    | j                  }| j                            |           | j        dk     r|}n4|                    | j                  }| j                            |           | j        D ]\  }}|                    ||           | j	        D ]\  }}|                    ||           | j
        D ]}|                    |           | j        D ]}|                    |           | j        D ]}|                    |           | j        D ]}|                    |           ||fS )Nr   )r   _contextrG   r   r    r+   r   r   
setsockoptr   r   bindr   r   connectr   )r%   rJ   insoutsr8   r9   ifaces          r&   _setup_socketszDevice._setup_sockets   s~   '+';';'='= jj&&S!!!=1DD::dm,,DM  &&& + 	' 	'JCNN3&&&&, 	( 	(JCOOC''''^ 	 	EHHUOOOO_ 	 	EIIe& 	 	EKK' 	  	 ELLDyr(   c                 T    |                                  \  }}t          ||           dS )zpThe runner method.

        Do not call me directly, instead call ``self.start()``, just like a Thread.
        N)rV   r   )r%   rS   rT   s      r&   
run_devicezDevice.run_device   s/    
 ''))	Tc4r(   c                 T    | j         D ]}|r|j        s|                                  dS )zCleanup sockets we createdN)r    closedclose)r%   ss     r&   _close_socketszDevice._close_sockets   s<     	 	A  				 	r(   c                    	 |                                   n.# t          $ r!}|j        t          t          hv rn Y d}~nd}~ww xY wd| _        |                                  dS # d| _        |                                  w xY w)z"wrap run_device in try/catch ETERMNT)rX   r   errnor   r
   r$   r]   )r%   es     r&   runz
Device.run   s    
	"OO 	 	 	w5(+++ 	 DI!!!!! DI!!!!s(    A" 
A=A" AA" "A?c                 *    |                                  S )z>Start the device. Override me in subclass for other launchers.)ra   r%   s    r&   startzDevice.start  s    xxzzr(   timeoutc                     t          j                    }|}| j        sF|	||z
  |k    s?t          j        d           t          j                    }| j        s|0||z
  |k    9dS dS dS dS )z\wait for me to finish, like Thread.join.

        Reimplemented appropriately by subclasses.NgMbP?)time	monotonicr$   sleep)r%   re   tictocs       r&   joinzDevice.join  s     n) 	#W%8S3Y=P=PJu.""C ) 	#W%8S3Y=P=P=P=P 	# 	# 	# 	#=P=Pr(   r!   NN)$__name__
__module____qualname____doc__r   instancer   r   zmq__annotations__boolintr   strr	   r   Socketr   r   r'   r-   r5   r7   r<   r>   r@   rB   rD   r0   rV   rX   r]   ra   rd   floatrl    r(   r&   r   r      s'        1 1f 291AOXb#+o.AAA LLLLLLMMMCys)uS#X''''S	9c3h((((93: !!%"&	  # 3-	
 
   0$C $D $ $ $ $3 C    's 't ' ' ' '/ /S /T / / / /%S %T % % % %C S    ( ( ( ( (0# 0c 0 0 0 0 #    &cj#*&< =    >     " " " "   # #HUO #t # # # # # #r(   r   c                   V    e Zd ZU dZdZeed<   dZeed<   d	dZd
de	e
         ddfdZdS )BackgroundDevicezEBase class for launching Devices in background processes and threads.Nlauncher_launch_classr!   c                     |                      | j                  | _        | j        | j        _        | j                                        S )N)target)r   ra   r~   r   rd   rc   s    r&   rd   zBackgroundDevice.start  s<    **$(*;;#{}""$$$r(   re   c                 8    | j                             |          S )N)re   )r~   rl   )r%   re   s     r&   rl   zBackgroundDevice.join  s    }!!'!222r(   rm   rn   )ro   rp   rq   rr   r~   r   ru   r   rd   r   rz   rl   r{   r(   r&   r}   r}     s{         OOHcM3% % % %
3 3HUO 3t 3 3 3 3 3 3r(   r}   c                       e Zd ZdZeZdS )ThreadDevicezSA Device that will be run in a background Thread.

    See Device for details.
    N)ro   rp   rq   rr   r   r   r{   r(   r&   r   r      s         
 MMMr(   r   c                       e Zd ZdZeZeZdS )ProcessDevicezTA Device that will be run in a background Process.

    See Device for details.
    N)ro   rp   rq   rr   r   r   r   r   r{   r(   r&   r   r   )  s*         
 MO r(   r   )r   r   r   )rr   rg   multiprocessingr   	threadingr   typingr   r   r   r   r	   rt   r
   r   r   r   r   r   r   r   r   r}   r   r   __all__r{   r(   r&   <module>r      sk   8 8
  # # # # # #       7 7 7 7 7 7 7 7 7 7 7 7 7 7 



 T T T T T T T T T T T T T T T T T T T T# # # # # # # #D3 3 3 3 3v 3 3 3    #   
 
 
 
 
$ 
 
 
 6
5
5r(   