
    h((                         S SK r S SKrS SKrS SKJrJrJr  SSKJrJ	r	  SSK
JrJrJr  \R                  " \5      r " S S5      r " S S	5      r " S
 S\5      r " S S5      r " S S\5      rg)    N)DictListOptional   )
JsonObject	JsonValue)CockpitProblemCockpitProtocolErrorCockpitProtocolServerc                   *    \ rS rSrSrS rS rS rSrg)ExecutionQueue   a_  Temporarily delay calls to a given set of class methods.

Functions by replacing the named function at the instance __dict__
level, effectively providing an override for exactly one instance
of `method`'s object.
Queues the invocations.  Run them later with .run(), which also reverses
the redirection by deleting the named methods from the instance.
c                     [         R                  " 5       U l        Xl        U R                   H  nU R	                  U5        M     g N)collectionsdequequeuemethods_wrap)selfr   methods      0/usr/lib/python3/dist-packages/cockpit/router.py__init__ExecutionQueue.__init__&   s1     &&(
llFJJv #    c                 h   ^ ^ [        TR                  TR                  R                  UU 4S j5        g )Nc                  >   > TR                   R                  TU 45      $ r   )r   append)argsr   r   s    r   <lambda>&ExecutionQueue._wrap.<locals>.<lambda>1   s    IZIZ\bdh[iIjr   )setattr__self____func____name__)r   r   s   ``r   r   ExecutionQueue._wrap-   s      	!9!9;jkr   c                    [         R                  S[        U R                  5      5        U R                   H
  u  pU" U6   M     U R                   H-  n[        UR                  UR                  R                  5        M/     g )Nz.ExecutionQueue: Running %d queued method calls)	loggerdebuglenr   r   delattrr#   r$   r%   )r   r   r   s      r   runExecutionQueue.run3   sX    Es4::W JJLFDM ' llFFOOV__%=%=> #r   )r   r   N)	r%   
__module____qualname____firstlineno____doc__r   r   r,   __static_attributes__ r   r   r   r      s    l?r   r   c            
           \ rS rSr% S\S'   Sr\\   \S'   SS jrS r	S r
S	 rS
\S\S\SS4S jrS
\S\SS4S jrSSSSS\SS4S jrS
\S\SS4S jrS
\S\SSS\SS4
S jrSSSS\SS4S jjrSrg)Endpoint<   RouterrouterN _Endpoint__endpoint_frozen_queuec                 2    UR                  U 5        Xl        g r   )add_endpointr8   r   r8   s     r   r   Endpoint.__init__@   s    D!r   c                     U R                   b   e[        R                  SU 5        [        U R                  U R
                  U R                  15      U l         g )NzFreezing endpoint %s)r9   r(   r)   r   do_channel_controldo_channel_datado_killr   s    r   freeze_endpointEndpoint.freeze_endpointD   sK    ++333+T2'5t7N7NPTPdPdfjfrfr6s't$r   c                     U R                   c   e[        R                  SU 5        U R                   R                  5         S U l         g )NzThawing endpoint %s)r9   r(   r)   r,   rB   s    r   thaw_endpointEndpoint.thaw_endpointI   s=    ++777*D1$$((*'+$r   c                     [         er   NotImplementedErrorrB   s    r   do_closeEndpoint.do_closeP       !!r   channelcommandmessagereturnc                     [         er   rI   )r   rN   rO   rP   s       r   r?   Endpoint.do_channel_controlS   rM   r   datac                     [         er   rI   r   rN   rT   s      r   r@   Endpoint.do_channel_dataV   rM   r   host
str | Nonegroupc                     [         er   rI   )r   rX   rZ   rP   s       r   rA   Endpoint.do_killY   rM   r   c                 :    U R                   R                  X5        g r   )r8   write_channel_datarV   s      r   send_channel_dataEndpoint.send_channel_data]   s    &&w5r   msgJsonObject | Nonekwargsc                     U R                   R                  " U4XS.UD6  US:X  aD  U R                   R                  U    R                  U5        U R                   R	                  U5        g g )N)rN   rO   close)r8   write_control	endpointsremovedrop_channel)r   rN   rO   ra   rc   s        r   send_channel_controlEndpoint.send_channel_control`   s\     	!!#RwR6RgKK!!$'..w7KK$$W- r   c                 >    U R                   R                  " X40 UD6  g r   )r8   shutdown_endpoint)r   ra   rc   s      r   rm   Endpoint.shutdown_endpointh   s    %%d:6:r   )__endpoint_frozen_queuer8   r8   r7   r   )r%   r.   r/   r0   __annotations__r9   r   r   r   rC   rF   rK   strr   r?   bytesr@   rA   r_   r   rj   rm   r2   r3   r   r   r5   r5   <   s    8<Xn5<u
,""# " "j "UY ""s "% "D ""L " "
 "W[ "6 6E 6d 6..%(./B.NW.	.;%8 ;9 ;Y] ; ;r   r5   c                       \ rS rSrSrg)RoutingErrorl   r3   N)r%   r.   r/   r0   r2   r3   r   r   ru   ru   l   s    r   ru   c                   H    \ rS rSr% S\S'   SS jrS\S\\   4S jr	S r
S	rg
)RoutingRulep   r7   r8   c                     Xl         g r   r8   r<   s     r   r   RoutingRule.__init__s   s    r   optionsrQ   c                     [         e)a5  Check if a routing rule applies to a given 'open' message.

This should inspect the options dictionary and do one of the following three things:

    - return an Endpoint to handle this channel
    - raise a RoutingError to indicate that the open should be rejected
    - return None to let the next rule run
rI   )r   r}   s     r   
apply_ruleRoutingRule.apply_rulev   s
     "!r   c                     [         er   rI   rB   s    r   shutdownRoutingRule.shutdown   rM   r   r{   Nrp   )r%   r.   r/   r0   rq   r   r   r   r5   r   r   r2   r3   r   r   rx   rx   p   s*    	"* 	"(1C 	""r   rx   c            	          \ rS rSr% \\   \S'   \\\	4   \S'   S\S'   \
R                  \S'   Sr\\S'   S\\   4S	 jrS
\4S jrS\S
\	4S jrS\S
S4S jrS\	S
S4S jrS&S\	SSS\S
S4S jjrSSSSS\S
S4S jrS\S\S\S
S4S jrS\S\S
S4S jrS
\4S  jrSr\\
R:                     \S!'   S"\\   S
S4S# jrS'S$ jr S%r!g)(r7      routing_rulesopen_channelszdict[Endpoint, set[str]]rg   no_endpointsF_eofc                     U H	  nXl         M     Xl        0 U l        0 U l        [        R
                  " 5       U l        U R                  R                  5         g r   )r8   r   r   rg   asyncioEventr   set)r   r   rules      r   r   Router.__init__   sF    !DK "*#MMOr   rQ   c                     0 $ )z7Used by the 'info' channel.  Gets overridden in Bridge.r3   rB   s    r   infoRouter.info   s    	r   r}   c                     U R                    HG  n[        R                  SU5        UR                  U5      nUc  M/  [        R                  SU5        Us  $    [        R                  S5        [	        S5      e)Nz  applying rule %sz    resulting endpoint is %sz  No rules matchedznot-supported)r   r(   r)   r   ru   )r   r}   r   endpoints       r   check_rulesRouter.check_rules   sc    &&DLL-t4w/H#;XF ' LL-.//r   rN   Nc                      U R                   R                  U5        [        R                  SU5        g ! [         a#    [        R                  SXR                   5         g f = f)Nzrouter dropped channel %sz.trying to drop non-existent channel %s from %s)r   popr(   r)   KeyErrorerror)r   rN   s     r   ri   Router.drop_channel   sQ    	h""7+LL4g> 	hLLI7TfTfg	hs   14 *A! A!r   c                 f    [        5       U R                  U'   U R                  R                  5         g r   )r   rg   r   clear)r   r   s     r   r;   Router.add_endpoint   s$    #&5x !r   ra   rb   rc   c                 (   U R                   R                  U5      n[        R                  SXU5        U H*  nU R                  " U4SUS.UD6  U R                  U5        M,     U R                   (       d  U R                  R                  5         U R                  (       at  [        R                  SU R                   5        U R                   (       dB  U R                  (       a0  [        R                  S5        U R                  R                  5         g g g g )Nz'shutdown_endpoint(%s, %s) will close %sre   rO   rN     endpoints remaining: %rz  close transport)rg   r   r(   r)   rf   ri   r   r   r   	transportre   )r   r   ra   rc   channelsrN   s         r   rm   Router.shutdown_endpoint   s    >>%%h/>RZ[GsOGWOOg&   ~~!!# 99LL4dnnE>>dnn01$$& '5> r   rX   rY   rZ   rP   c                     [        U R                  5      n[        R                  SX[	        U5      5        U H  nUR                  XU5        M     g )Nz+do_kill(%s, %s).  Considering %d endpoints.)r   rg   r(   r)   r*   rA   )r   rX   rZ   rP   rg   r   s         r   rA   Router.do_kill   sB    '	BDQTU^Q_`!HT'2 "r   rO   c                    US:X  a~  XR                   ;   a  [        S5      e [        R                  SXR	                  S5      5        U R                  U5      nX@R                   U'   U R                  U   R                  U5        O U R                   U   nUR                  XU5        g ! [         a)  nU R                  UR                  5       SUS9   S nAg S nAff = f! [         a     g f = f)Nopenzchannel is already openz5Trying to find endpoint for new channel %s payload=%spayloadre   r   )r   r
   r(   r)   getr   ru   rf   	get_attrsrg   addr   r?   )r   rN   rO   rP   r   excs         r   channel_control_receivedRouter.channel_control_received   s     f,,,*+DEETV]_j_jkt_uv++G4
 +3w'NN8$((1--g6 	##Gg>   ""3==?GW"U  s)   6B( C (
C2CC
C+*C+rT   c                 f     U R                   U   nUR                  X5        g ! [         a     g f = fr   )r   r   r@   )r   rN   rT   r   s       r   channel_data_receivedRouter.channel_data_received   s;    	))'2H 	  /  		s   # 
00c                    [         R                  SU 5        [        U R                  5      nU H  nUR	                  5         M     SU l        [         R                  SU R                  5        [        U R                  5      $ )Nzeof_received(%r)Tr   )r(   r)   r   rg   rK   r   bool)r   rg   r   s      r   eof_receivedRouter.eof_received   s`    '.'	!H " 	0$..ADNN##r   _communication_doner   c                     U R                   (       d  U R                  5         U R                  b;  Uc  U R                  R                  S 5        g U R                  R	                  U5        g g r   )r   r   r   
set_resultset_exception)r   r   s     r   	do_closedRouter.do_closed   sS    yy##/{((33D9((66s;	 0r   c                   #    U R                   b   e[        R                  " 5       R                  5       U l          U R                   I Sh  vN   SU l         U R                  R                  5       I Sh  vN   g N.! [        [
        4 a     N>f = f N! SU l         U R                  R                  5       I Sh  vN    f = f7f)zNWait until communication is complete on the router and all endpoints are done.N)r   r   get_running_loopcreate_futureBrokenPipeErrorConnectionResetErrorr   waitrB   s    r   communicateRouter.communicate  s     ''///#*#;#;#=#K#K#M 		+**** (,D$ ##((*** +!56 		 +	 (,D$ ##((***sd   8CA; 
A9A; $C3B4C9A; ;BB BB C%B?8B;9B??C)r   r   rg   r   r   r   r   )rQ   N)"r%   r.   r/   r0   r   rx   rq   r   rr   r5   r   r   r   r   r   r   r   r   ri   r;   r   rm   rA   r   rs   r   r   r   r   Future	Exceptionr   r   r2   r3   r   r   r7   r7      sP   $$X&&))--D$ d;&7  j 	0: 	0( 	0hC hD h"X "$ "'( '9L '_h 'mq '"3L 3 3
 3W[ 3? ?c ?J ?[_ ?60S 0 0$ 0	$d 	$ 59'..18	<Xi0 	<T 	<+r   r7   )r   r   loggingtypingr   r   r   jsonutilr   r   protocolr	   r
   r   	getLoggerr%   r(   r   r5   ru   rx   r7   r3   r   r   <module>r      si   $    ' ' + Q Q			8	$? ?>-; -;`	> 	" "*I+" I+r   