
    tfD>                         d dl Z d dlZd dlZd dl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mZ d	Zd
 Zd Zd Z G d dej                  ZddZdS )    N)wraps)WampWebSocketClientFactory)WampRawSocketClientFactory)	component)TransportLost)Session)create_transport_serializerscreate_transport_serializer)	Componentrunc                 <    t                      fd| D             S )zM
    Return a list with unique elements from sequence, preserving order.
    c                 F    g | ]}|v                     |          |S  )add).0xseens     Y/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/autobahn/asyncio/component.py
<listcomp>z _unique_list.<locals>.<listcomp>3   s,    @@@!atmmDHHQKKmAmmm    )set)seqr   s    @r   _unique_listr   .   s(     55D@@@@s@@@@r   c                     |                      d          }|d         d                    d |dd          D                       z   S )N_r    c              3   >   K   | ]}|                                 V  d S N)
capitalize)r   ss     r   	<genexpr>z._camel_case_from_snake_case.<locals>.<genexpr>8   s*      @@allnn@@@@@@r      )splitjoin)snakepartss     r   _camel_case_from_snake_caser'   6   sE    KKE8bgg@@eABBi@@@@@@@r   c           
      <   |j         dk    r-t          |          }t          ||j        ||j                  }nA|j         dk    r,t          |j        d                   }t          ||          }n
J d            |j        	                                D ]\  }}	  |j
        d	i ||i # t          t          f$ r_ 	  |j
        d	i t          |          |i n># t          t          f$ r* t          d                    |j         ||                    w xY wY w xY w|S )
z3
    Create a WAMP-over-XXX transport factory.
    	websocket)urlserializersproxy	rawsocketr   )
serializerFshould not arrive herez"Unknown {} transport option: {}={}r   )typer	   r   r*   r,   r
   r+   r   optionsitemssetProtocolOptions	TypeErrorKeyErrorr'   
ValueErrorformat)loop	transportsession_factoryr+   factoryr.   kvs           r   _create_transport_factoryr>   ;   s    ~$$29==,#/	
 
 
 
;	&	&01Fq1IJJ
,_TTT 	0//// !''))  1	&G&00!Q00008$ 	 	 	**  2155q9    x(    8??	PQSTUU   	 Ns*   B))D;CD;DDDc                   V    e Zd ZdZ ej                    ZeZ	 d Z	d Z
d Zd ZddZdS )	r   z
    A component establishes a transport and attached a session
    to a realm using the transport for communication.

    The transports a component tries to use can be configured,
    as well as the auto-reconnect strategy.
    c                 6    t          |t          j                  S )z"
        Internal helper.
        )
isinstancesslSSLError)selfes     r   _is_ssl_errorzComponent._is_ssl_errorr   s     !S\***r   c                     t          |t                    rWd|v rQ|d         }t          |t          t          f          rd S t          |t          j                  rd S t          d          d S t          d          )Ntlsz?'tls' configuration must be a dict, bool or SSLContext instancezI'endpoint' configuration must be a dict or IStreamClientEndpoint provider)rA   dictboolrB   
SSLContextr6   )rD   endpointrH   s      r   _check_native_endpointz Component._check_native_endpointx   s    h%% 	  uocD$<00 DS^44 D$.   !    r   c                 :	   t          |||          }|j        r|j                            dd          }t	          |          t
          k    r/t          d                    t	          |                              |                    ||j        d         |j        d                   }t          j
        t          j        ||                    }|                     |||          S |j        d         d	k    r|j                            d
d          }	|	dvr"t          d                    |	                    |j        d         }
t	          |
          t          k    r/t          d                    t	          |
                              |j        d         }t	          |          t
          k    r/t          d                    t	          |                              |j                            dd          }t	          |          t
          k    r/t          d                    t	          |                              |j                            dd          }d}|r9t          |t                    r|                                D ](}|dvr"t          d                    |                    )|                    d|
          }t	          |          t          k    r"t          d                    |                    |                    dd          }|}d}|%t#          j        t"          j        j        |          }nUt          |t"          j                  r|
}n8|dv r|r|
}n/t-          d                    t	          |                              |                    ||
|||          }t          j
        t          j        ||                    }|                     |||          S |j        d         dk    r|j        d         }t          |j                            dd                    }|                    ||          }t          j
        t          j        ||                    }|                     |||          S J d             )!z?
        Create and connect a WAMP-over-XXX transport.
        timeout
   z<invalid type {} for timeout in client endpoint configurationhostport)protocol_factoryrQ   rR   )rO   r0   tcpversion   )rV      z6invalid IP version {} in client endpoint configurationz9invalid type {} for host in client endpoint configurationz9invalid type {} for port in client endpoint configurationrH   N)hostname
trust_rootz Invalid key '{}' in 'tls' configrX   zAinvalid type {} for hostname in TLS client endpoint configurationrY   T)purposecafile)FTz4unknown type {} for "tls" configuration in transport)rS   rQ   rR   rB   server_hostnameunixpath)rS   r^   Fr/   )r>   r,   rL   getr0   intr6   r7   create_connectionasyncioensure_futurewait_for_wrap_connection_futurestrrA   rI   keysrB   create_default_contextPurposeSERVER_AUTHrK   RuntimeErrorcreate_unix_connection)rD   r8   r9   r:   doner;   rO   ftime_frU   rQ   rR   rH   tls_hostnamer<   rX   
cert_fnamer^   s                     r   _connect_transportzComponent._connect_transport   s    ,D)_MM
 ? W	4(,,Y;;GG}}## !_!f!fgklsgtgt!u!uvvv &&!(_V,_V, '  A
 *7+;Aw+O+O+OPPF//	4HHH'500(,,Y::Gf$$ !Y!`!`ah!i!ijjj%f-DDzzS   !\!c!cdhimdndn!o!oppp%f-DDzzS   !\!c!cdhimdndn!o!oppp(,,Y;;GG}}## !_!f!fgklsgtgt!u!uvvv$((55CL  qc4(( q XXZZ [ [$>>>",-O-V-VWX-Y-Y"Z"ZZ ?"wwz488HH~~,,()l)s)st|)})}~~~!$t!<!<J#+LC!-!8$'K$;#-  
  S^44 	q#'LLM)) ,'+ '']'d'deijmenen'o'oppp&&!( , '  A *7+;Aw+O+O+OPPF//	4HHH'611%f-D),00B??@@G++!( ,  A *7+;Aw+O+O+OPPF//	4HHH 43333r   c                 x    fd}fd}t          j        ||d            t          j        |d |           |S )Nc                     | \  }}|                                 rIt          j                  s3t          |dd          }t          j        t          |                     d S |j        t                    fd            }||_        d S )N_onclose_reasonzConnection already closedc                      |           }t          j                  s&| t          d          } t          j        |            |S )Nzfailed to complete connection)txaio	is_calledr   reject)failrtnrm   origs     r   lostzKComponent._wrap_connection_future.<locals>.on_connect_success.<locals>.lost  sN    d4jjt,, - |,-LMMLt,,,
r   )
is_closingrw   rx   getattrry   r   connection_lostr   )resultr9   protoreasonr}   r|   rm   s        @r   on_connect_successz=Component._wrap_connection_future.<locals>.on_connect_success   s    %Iu ##%% t,, >$U,=?Z[[FL}V'<'<=== (D4[[	 	 	 	 	 [	 %)E!!!r   c                 R    xj         dz  c_         t          j        |            d S )Nr"   )connect_failuresrw   ry   )errrm   r9   s    r   on_connect_failurez=Component._wrap_connection_future.<locals>.on_connect_failure  s/    &&!+&&Ls#####r   )rw   add_callbacks)rD   r9   rm   conn_fr   r   s    ``   r   re   z!Component._wrap_connection_future   sq    &	) &	) &	) &	) &	)P	$ 	$ 	$ 	$ 	$ 	$
 	F$6=== 	FD*<===r   Nc                     |-| j                             d           t          j                    }|                     |          S )a~  
        This starts the Component, which means it will start connecting
        (and re-connecting) to its configured transports. A Component
        runs until it is "done", which means one of:
        - There was a "main" function defined, and it completed successfully;
        - Something called ``.leave()`` on our session, and we left successfully;
        - ``.stop()`` was called, and completed successfully;
        - none of our transports were able to connect successfully (failure);

        :returns: a Future which will resolve (to ``None``) when we are
            "done" or with an error if something went wrong.
        NzUsing default loop)r8   )logwarnrb   get_event_loop_start)rD   r8   s     r   startzComponent.start$  s@     <HMM.///)++D{{{%%%r   r   )__name__
__module____qualname____doc__rw   make_loggerr   r   r:   rF   rM   rr   re   r   r   r   r   r   r   b   s          %


CO+ + +  (`4 `4 `4D3 3 3l& & & & & &r   r   Tinfoc                    |t          j        |           t          j                                                    rIt          j        t          j                               t          j                    t           j        _        t          j	                    fd	 
                    t          j        fd           
                    t          j        fd           n# t          $ r Y nw xY wd }t          j        | |           |rA	                                  n# t          j        $ r Y nw xY w                                 dS dS )a.  
    High-level API to run a series of components.

    This will only return once all the components have stopped
    (including, possibly, after all re-connections have failed if you
    have re-connections enabled). Under the hood, this calls

    XXX fixme for asyncio

    -- if you wish to manage the loop yourself, use the
    :meth:`autobahn.asyncio.component.Component.start` method to start
    each component yourself.

    :param components: the Component(s) you wish to run
    :type components: instance or list of :class:`autobahn.asyncio.component.Component`

    :param start_loop: When ``True`` (the default) this method
        start a new asyncio loop.
    :type start_loop: bool

    :param log_level: a valid log-level (or None to avoid calling start_logging)
    :type log_level: string
    N)levelc                   K                        d|            	 t          j                                        }n## t          $ r t          j                    }Y nw xY w|D ]]}	 t          j                                        }n## t          $ r t          j                    }Y nw xY w||ur|                                 ^fd}t          j        | }|                    |           d S )NzShutting down due to {signal})signalc                 4   	 |                                   nS# t          j        $ r                     d           Y n-t          $ r!}                    d|           Y d }~nd }~ww xY w                                 d S #                                  w xY w)NzAll task cancelledz&Error while shutting down: {exception})	exception)r   rb   CancelledErrordebug	Exceptionerrorstop)futrE   r   r8   s     r   cancel_all_callbackz5run.<locals>.nicely_exit.<locals>.cancel_all_callbacky  s    

) 0 0 0		.///// Q Q Q		Ba	PPPPPPPPQ 				s6    B $A(B 	A(A#B #A((B B)	r   rb   Task	all_tasksAttributeErrorcurrent_taskcancelgatheradd_done_callback)r   taskstaskr   r   r   r   r8   s         r   nicely_exitzrun.<locals>.nicely_exite  s     0@@@	(L**,,EE 	( 	( 	(%''EEE	(  		 		D6&|88::! 6 6 6&3556 <''	 	 	 	 	 	 ne$122222s!   ; AA#BB"!B"c                  >    t          j          d                    S )NSIGINTrb   rc   r   s   r   <lambda>zrun.<locals>.<lambda>  s    w7L[[YaMbMb7c7c r   c                  >    t          j          d                    S )NSIGTERMr   r   s   r   r   zrun.<locals>.<lambda>  s    8MkkZcNdNd8e8e r   c                 .    |                                   d S r   )r   )r8   args     r   done_callbackzrun.<locals>.done_callback  s    		r   )rw   start_loggingrb   r   	is_closedset_event_loopnew_event_loopconfigr8   r   add_signal_handlerr   r   r   NotImplementedErrorr   _runrun_foreverr   close)
components
start_loop	log_levelr   r   r8   r   s       @@@r   r   r   9  s   8 ),,,,!##D~~ !w577888%'' 



C3 3 3 3 3 3B/c/c/c/cddd0e0e0e0effff      N4]333 	 % 	 	 	D	 	

 s%   %AC, ,
C98C9D- -D?>D?)Tr   )rb   rB   r   	functoolsr   rw   autobahn.asyncio.websocketr   autobahn.asyncio.rawsocketr   autobahn.wampr   autobahn.wamp.exceptionr   autobahn.asyncio.wampr   autobahn.wamp.serializerr	   r
   __all__r   r'   r>   r   r   r   r   r   <module>r      sD  6  



         A A A A A A A A A A A A # # # # # # 1 1 1 1 1 1 ) ) ) ) ) ) ^ ^ ^ ^ ^ ^ ^ ^ A A AA A A
$ $ $NT& T& T& T& T&	# T& T& T&nh h h h h hr   