
    MhS*                        d Z ddlZddlZddlZddlZddlmZ ddl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 ed         Z G d
 de          Z G d de
          Z ej        e            G d d          Z G d de          ZdS )zCBase classes to manage a Client's interaction with a running kernel    N)Empty)EventThread)ensure_async   )protocol_version_info)HBChannelABC)Sessionc                       e Zd ZdZdS )InvalidPortNumberz/An exception raised for an invalid port number.N)__name__
__module____qualname____doc__     W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyter_client/channels.pyr   r      s        99Dr   r   c            
       X    e Zd ZU dZdZdZdZdZdZe	e
d<   dZdZdZ	 	 	 ddej        ej                 dej        e         d	ej        ej        eef         ef         d
df fdZeej        dd                        ZddZddZddZddZddZ d
e!fdZ"ddZ#ddZ$de	d
dfdZ% xZ&S )	HBChannelzThe heartbeat channel which monitors the kernel heartbeat.

    Note that the heartbeat channel is paused by default. As long as you start
    this channel, the kernel manager will ensure that it is paused and un-paused
    as appropriate.
    NFg      ?time_to_dead contextsessionaddressreturnc                 f   t                                                       d| _        || _        || _        t          |t                    r#|d         dk    rd}t          |          d|z  }n|}|| _        d| _	        t                      | _        d| _        t          j                    | _        dS )aK  Create the heartbeat monitor thread.

        Parameters
        ----------
        context : :class:`zmq.Context`
            The ZMQ context to use.
        session : :class:`session.Session`
            The session to use.
        address : zmq url
            Standard (ip, port) tuple that the kernel is listening on.
        Tr   r   z*The port number for a channel cannot be 0.ztcp://%s:%iFN)super__init__daemonr   r   
isinstancetupler   r   _runningr   _exit_pausezmqPollerpoller)selfr   r   r   messageaddress_str	__class__s         r   r   zHBChannel.__init__4   s    " 	gu%% 	"qzQF'000''1KK!K" WW
jllr   c                  0    t           dt           _        d S d S )NT)r   _exitingr   r   r   _notice_exitzHBChannel._notice_exitZ   s     
  !%I ! r   c                    | j         8| j                            | j                    | j                                          | j        J | j                             t
          j                  | _         d| j         _        | j        J | j         	                    | j                   | j        
                    | j         t
          j                   d S )N  )socketr'   
unregistercloser   r%   REQlingerr   connectregisterPOLLINr(   s    r   _create_socketzHBChannel._create_socketb   s    ;"K""4;///K|'''l))#'22!|'''DL)))T[#*55555r   c                   K   |                                   d| _        d| _        | j        J | j        r:| j        r | j                            | j                   /d}t          | j        	                    d                     d{V  t          j
                    }| j                            | j                   t          | j                            d                    | _        | j        r-t          | j                                                   d{V  | j        rAt          j
                    |z
  }|                     |           |                                   8| j        8dS dS )z2The thread's main activity.  Call start() instead.TNg        s   pingr   )r:   r"   _beatingr1   r$   r#   waitr   r   sendtimeboolr'   pollrecvcall_handlers)r(   since_last_heartbeatrequest_times      r   
_async_runzHBChannel._async_runo   s{     {&&&m 	{ 
 1222#&  t{//889999999999;;LJOOD-... !1!1!!4!455DM} 
"4;#3#3#5#5666666666 '+y{{\'A$""#7888##%%%3 m 	 	 	 	 	r   c                     t          j                    }t          j        |           |                    |                                            |                                 dS )zRun the heartbeat thread.N)asyncionew_event_loopset_event_looprun_until_completerF   r3   )r(   loops     r   runzHBChannel.run   sP    %''t$$$ 1 1222

r   c                     d| _         dS )zPause the heartbeat.TNr$   r9   s    r   pausezHBChannel.pause   s    r   c                     d| _         dS )zUnpause the heartbeat.FNrO   r9   s    r   unpausezHBChannel.unpause   s    r   c                 N    |                                  r| j        s	| j        rdS dS )z9Is the heartbeat running and responsive (and not paused).TF)is_aliver$   r<   r9   s    r   
is_beatingzHBChannel.is_beating   s.    ==?? 	4; 	4= 	45r   c                     d| _         | j                                         |                                  |                                  dS )z2Stop the channel's event loop and join its thread.FN)r"   r#   setjoinr3   r9   s    r   stopzHBChannel.stop   s9    
		

r   c                     | j         6	 | j                             d           n# t          $ r Y nw xY wd| _         dS dS )zClose the heartbeat thread.Nr   r5   r1   r3   	Exceptionr9   s    r   r3   zHBChannel.close   ^    ;"!!!++++   DKKK #"   % 
22rD   c                     dS )ai  This method is called in the ioloop thread when a message arrives.

        Subclasses should override this method to handle incoming messages.
        It is important to remember that this method is called in the thread
        so that some logic must be done to ensure that the application level
        handlers are called in the application thread.
        Nr   )r(   rD   s     r   rC   zHBChannel.call_handlers   s	     	r   )NNr   r   N)'r   r   r   r   r   r1   r   r-   r   float__annotations__r"   r$   r<   tOptionalr%   Contextr
   UnionTuplestrintr   staticmethodatexitr7   r.   r:   rF   rM   rP   rR   r@   rU   rY   r3   rC   __classcell__r+   s   @r   r   r   "   s          GFGHL%HFH ,0'+35	$# $#CK($# G$$# c*C/0	$#
 
$# $# $# $# $# $#L _& & & _ \&6 6 6 6       D         D          % D        r   r   c            	       z    e Zd ZdZddej        dedej        ddf fdZ	dej        dej
        eej        f         fd	Zdd
ej        e         dej
        eej        f         fdZdej        ej
        eej        f                  fdZdefdZddZeZdefdZdej
        eej        f         ddfdZddZ xZS )ZMQSocketChannelzA ZMQ socket wrapperNr1   r   rL   r   c                 d    t                                                       || _        || _        dS )a  Create a channel.

        Parameters
        ----------
        socket : :class:`zmq.Socket`
            The ZMQ socket to use.
        session : :class:`session.Session`
            The session to use.
        loop
            Unused here, for other implementations
        N)r   r   r1   r   )r(   r1   r   rL   r+   s       r   r   zZMQSocketChannel.__init__   s-     	.4r   kwargsc                     | j         J  | j         j        di |}| j                            |          \  }}| j                            |          S Nr   r1   recv_multipartr   feed_identitiesdeserialize)r(   rr   msgidentsmsgs        r   _recvzZMQSocketChannel._recv   sY    {&&&(dk(22622l22377t|''---r   timeoutc                     | j         J |dnt          |dz            }| j                             |          }|r|                                 }|S t          z-Gets a message if there is one that is ready.Nr0   r1   rj   rA   r|   r   r(   r}   
timeout_msreadyress        r   get_msgzZMQSocketChannel.get_msg   s]    {&&&$_TT#gn2E2E
  ,, 	**,,CJKr   c                     g }	 	 |                     |                                            n# t          $ r Y nw xY w:|S )*Get all messages that are currently ready.appendr   r   r(   msgss     r   get_msgszZMQSocketChannel.get_msgs   sZ    	DLLNN++++   	
 s   '- 
::c                 d    | j         J t          | j                             d                    S z*Is there a message that has been received?Nr   )r}   r1   r@   rA   r9   s    r   	msg_readyzZMQSocketChannel.msg_ready   s1    {&&&DK$$Q$//000r   c                     | j         6	 | j                             d           n# t          $ r Y nw xY wd| _         dS dS )zClose the socket channel.Nr   r[   r\   r9   s    r   r3   zZMQSocketChannel.close   r^   r_   c                     | j         duS )z"Test whether the channel is alive.N)r1   r9   s    r   rT   zZMQSocketChannel.is_alive  s    {$&&r   ry   c                 X    | j         J | j                            | j         |           dS )z(Pass a message to the ZMQ socket to sendN)r1   r   r>   )r(   ry   s     r   r>   zZMQSocketChannel.send  s0    {&&&$+s+++++r   c                     dS )zStart the socket channel.Nr   r9   s    r   startzZMQSocketChannel.start  s    r   Nra   )r   r   r   r   r%   Socketr
   rd   Anyr   Dictri   r|   re   rb   r   Listr   r@   r   r3   rY   rT   r>   r   rm   rn   s   @r   rp   rp      s        sz G 15 TX      ".ae .sAEz(: . . . .	 	qz%0 	AF3:<N 	 	 	 	!&QU
!34    14 1 1 1 1
    D'$ ' ' ' ',sAEz* ,t , , , ,
       r   rp   c            	       R    e Zd ZU dZej        j        ed<   ddej        j        dede	j
        ddf fdZde	j
        de	j        ee	j
        f         fd	Z	 dd
e	j        e         de	j        ee	j
        f         fdZde	j        e	j        ee	j
        f                  fdZdefdZ xZS )AsyncZMQSocketChannelzA ZMQ socket in an async APIr1   Nr   rL   r   c                     t          |t          j        j                  sd}t	          |          t                                          ||           dS )a  Create a channel.

        Parameters
        ----------
        socket : :class:`zmq.asyncio.Socket`
            The ZMQ socket to use.
        session : :class:`session.Session`
            The session to use.
        loop
            Unused here, for other implementations
        zSocket must be asyncioN)r    r%   rH   r   
ValueErrorr   r   )r(   r1   r   rL   ry   r+   s        r   r   zAsyncZMQSocketChannel.__init__  sM     &#+"455 	"*CS//!)))))r   rr   c                    K   | j         J  | j         j        di | d {V }| j                            |          \  }}| j                            |          S rt   ru   )r(   rr   ry   _r{   s        r   r|   zAsyncZMQSocketChannel._recv(  so      {&&&.DK.8888888888,..s334|''---r   r}   c                    K   | j         J |dnt          |dz            }| j                             |           d{V }|r|                                  d{V }|S t          r   r   r   s        r   r   zAsyncZMQSocketChannel.get_msg.  s       {&&&$_TT#gn2E2E
k&&z22222222 	

$$$$$$CJKr   c                    K   g }	 	 |                     |                                  d{V            n# t          $ r Y nw xY w@|S )r   TNr   r   s     r   r   zAsyncZMQSocketChannel.get_msgs;  sp      	$,,..0000001111   	
 s   -5 
AAc                 t   K   | j         J t          | j                             d           d{V           S r   r   r9   s    r   r   zAsyncZMQSocketChannel.msg_readyE  sG      {&&&$+**1*55555555666r   r   )r   r   r   r   r%   rH   r   rc   r
   rd   r   r   r   ri   r|   re   rb   r   r   r   r@   r   rm   rn   s   @r   r   r     s<        &&K* *s{1 *G *15 *\` * * * * * *".AE .afS!%Z.@ . . . . ,0 z%(	
QU
	   qvc15j'9 :    7 7 7 7 7 7 7 7 7r   r   )r   rH   rl   r?   typingrd   queuer   	threadingr   r   zmq.asyncior%   jupyter_core.utilsr   _versionr   channelsabcr	   r   r
   major_protocol_versionr]   r   r   r7   rp   r   r   r   r   <module>r      s   I I              # # # # # # # #     + + + + + + + + + + + + % % % % % %       /q1 	 	 	 	 		 	 	 	] ] ] ] ] ] ] ]@  i      J J J J J J J JZ67 67 67 67 67, 67 67 67 67 67r   