a
    ©ˆc4  ã                   @   sØ   d dl Z d dlZd dlZd dlZd dlZd dlmZ eej dd¡ƒZ	eej dd¡ƒZ
e e¡Ze ej¡ G dd„ dƒZG d	d
„ d
ƒZG dd„ dƒZG dd„ dƒZdd„ ZG dd„ dƒZddd„ZedkrÔeƒ  dS )é    N)ÚwebZWSLINK_MAX_MSG_SIZEi  @ ZWSLINK_HEART_BEATé   c                   @   sJ   e Zd Zddd„Zdd„ Zedd„ ƒZdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚWsClientConnectionTc                 C   s6   d | _ d | _d | _d| _d | _t ¡  ¡ | _|| _	d S ©Nr   )
Ú_urlÚ_sessionÚ_wsÚ
_connectedÚ_destinationÚasyncioZget_running_loopZcreate_futureÚ_readyÚpropagate_disconnect©Úselfr   © r   ú`/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/wslink/backends/aiohttp/relay.pyÚ__init__   s    zWsClientConnection.__init__c                 C   s
   || _ d S ©N©r
   ©r   Úvaluer   r   r   Úbind!   s    zWsClientConnection.bindc                 C   s   | j S r   )r   ©r   r   r   r   Úready$   s    zWsClientConnection.readyc              
   Ã   s   t  d|¡ || _| jd u �r’t ¡ 4 I d H �šJ}t  d¡ || _�z| | j¡4 I d H šÔ}t  d¡ || _|  jd7  _| j	 
d¡ |2 z\3 d H W }t  d¡ | jdk r¸t  d¡  qê| jrÜt  d¡ | j |¡I d H  qŠt  d	¡ qŠ6 |  ¡  | jj�s| j ¡ I d H  d | _d | _W d   ƒI d H  �qF1 I d H �s<0    Y  W | j	 
d
¡ n| j	 
d
¡ 0 W d   ƒI d H  �q’1 I d H �sˆ0    Y  t  d¡ d S )Nzclient::connect::%szclient::connect::sessionzclient::connect::wsé   Tzclient::connect::ws::msgz$client::connect::ws::msg::disconnectzclient::connect::ws::msg::sendz%ws-client: No destination for messageFzclient::connect::exit)ÚloggerÚdebugr   r   ÚaiohttpZClientSessionZ
ws_connectr   r	   r   Z
set_resultr
   ÚsendÚerrorÚ
disconnectÚclosedÚclose)r   ÚurlÚsessionÚwsÚmsgr   r   r   Úconnect(   s8    






6JzWsClientConnection.connectc                 Ã   s  | j dkrØ| jjsØt d¡ |jtjjkr@| j 	|j
¡I d H  n–|jtjjkrd| j |j
¡I d H  nr|jtjjkrˆ| j |j
¡I d H  nN|jtjjkr¬| j |j
¡I d H  n*|jtjjkrÌ| j ¡ I d H  n
t d¡ n(t d¡ t d| j | jj¡ t d¡ d S )Nr   zclient::sendúInvalid message to forwardzclient::send - NO SENDú%s - %sú<------------------------------------------------------------©r	   r   r!   r   r   Útyper   Z	WSMsgTypeZTEXTZsend_strÚdataÚBINARYZ
send_bytesZPINGZpingZPONGZpongZCLOSEr"   r   ©r   r&   r   r   r   r   N   s     

zWsClientConnection.sendc                 C   s8   t  d| j¡ | jdkr4d| _| jr4| jr4| j ¡  d S )Nzclient::disconnect %sr   éÿÿÿÿ©r   r   r	   r
   r   r    r   r   r   r   r    b   s
    
zWsClientConnection.disconnectc                 Ã   s   | j d ur| j  ¡ I d H  d S r   ©r   r"   r   r   r   r   r"   i   s    
zWsClientConnection.closeN)T)Ú__name__Ú
__module__Ú__qualname__r   r   Úpropertyr   r'   r   r    r"   r   r   r   r   r      s   
	
&r   c                   @   s>   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ ZdS )ÚWsServerConnectionTc                 C   s   d | _ d | _d| _|| _d S r   )r   r
   r	   r   r   r   r   r   r   r   s    zWsServerConnection.__init__c                 C   s
   || _ d S r   r   r   r   r   r   r   x   s    zWsServerConnection.bindc                 Ã   sè   t  d¡ tjttd�| _| j |¡I d H  t  d¡ |  jd7  _| jdkr´| j2 z\3 d H W }t  d¡ | jdk rx q´| j	r¦t  d¡ | j	 
|¡I d H  t  d¡ qTt  d	¡ qT6 |  ¡  | jjsÔ| j ¡ I d H  d | _t  d
¡ d S )Nzserver::connect)Zmax_msg_sizeZ	heartbeatzserver::connect::preparer   r   zserver::connect::ws::msgz$server::connect::ws::msg::send-beginz"server::connect::ws::msg::send-endz%ws-server: No destination for messagezserver::connect::exit)r   r   r   ZWebSocketResponseÚMAX_MSG_SIZEÚ
HEART_BEATr   Úpreparer	   r
   r   r   r    r!   r"   )r   Úrequestr&   r   r   r   r'   {   s,    
ÿ




zWsServerConnection.connectc                 Ã   s  | j dkrØ| jjsØt d¡ |jtjjkr@| j 	|j
¡I d H  n–|jtjjkrd| j |j
¡I d H  nr|jtjjkrˆ| j |j
¡I d H  nN|jtjjkr¬| j |j
¡I d H  n*|jtjjkrÌ| j ¡ I d H  n
t d¡ n(t d¡ t d| j | jj¡ t d¡ d S )Nr   zserver::sendr(   zserver::send - NO SENDr)   r*   r+   r/   r   r   r   r   ›   s     

zWsServerConnection.sendc                 C   s8   t  d| j¡ | jdkr4d| _| jr4| jr4| j ¡  d S )Nzserver::disconnect %sr   r0   r1   r   r   r   r   r    ¯   s
    
zWsServerConnection.disconnectc                 Ã   s   | j d ur| j  ¡ I d H  d S r   r2   r   r   r   r   r"   ¶   s    
zWsServerConnection.closeN)T)	r3   r4   r5   r   r   r'   r   r    r"   r   r   r   r   r7   q   s   
 r7   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚForwardConnectionc                 C   s<   || _ || _tƒ | _tƒ | _| j | j¡ | j | j¡ d S r   )Ú_reqr   r   Ú
_ws_clientr7   Ú
_ws_serverr   )r   r;   r#   r   r   r   r   ¿   s    zForwardConnection.__init__c                 ƒ   sL   t  ˆ j ˆ j¡¡}| ‡ fdd„¡ ˆ jjI d H  ˆ j ˆ j¡I d H  d S )Nc                     s
   ˆ j  ¡ S r   )r?   r    )ÚargsÚkwargsr   r   r   Ú<lambda>É   ó    z+ForwardConnection.connect.<locals>.<lambda>)	r   Zcreate_taskr>   r'   r   Zadd_done_callbackr   r?   r=   )r   Ztaskr   r   r   r'   Ç   s    zForwardConnection.connectc                 C   s   | j  ¡  | j ¡  d S r   )r>   r    r?   r   r   r   r   r    Í   s    
zForwardConnection.disconnectN)r3   r4   r5   r   r'   r    r   r   r   r   r<   ¾   s   r<   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚSinkConnectionc                 C   s   || _ d | _d | _d | _d S r   )Ú_process_reqÚ_client_reqÚ_process_wsÚ
_client_ws©r   r;   r   r   r   r   Ó   s    zSinkConnection.__init__c                 C   s.   | j |krdS | j|krdS | jd u r*dS dS )NTF)rE   rF   rH   rI   r   r   r   Ú
can_handleÙ   s    


zSinkConnection.can_handlec                 Ã   s´   | j |krBtƒ | _| j |¡I d H  | jd ur>| j ¡ I d H  dS | jd u r°|| _tdd�| _| j | j¡ | j | j¡ | j |¡I d H  | j d ¡ | j d ¡ d | _d | _dS )NTF)r   )rE   r7   rG   r'   rH   r"   rF   r   rI   r   r   r   r'   å   s"    


zSinkConnection.connectN)r3   r4   r5   r   rJ   r'   r   r   r   r   rD   Ò   s   rD   c                 Ã   s"   | j rt d| j › �¡S t d¡S )Nzindex.html?z
index.html)Zquery_stringr   Z	HTTPFound)r;   r   r   r   Ú_root_handler  s    rK   c                   @   s.   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
S )Ú	WsHandlerc                 C   s   i | _ i | _d S r   )Ú_forward_mapÚ
_relay_mapr   r   r   r   r     s    zWsHandler.__init__Úforwardc                 C   s6   t  d|¡ |dkr| jS |dkr(| jS t  d¡ d S )Nzget_handler %srO   ZrelayzNo handler !!!)r   ÚinfoÚforward_connectÚrelay_connectr   )r   Úmoder   r   r   Úget_handler  s    zWsHandler.get_handlerc                 Ã   s”   |j  dd¡}t|j  dd¡ƒ}|j  dd¡}d|› d|› d	|› �}t d
|¡ || jv rbt ¡ ‚t||ƒ}|| j|< | 	¡ I d H  | j 
|¡ d S )NÚhostÚ	localhostÚportZ1234Úpathr%   zws://ú:ú/z=> %s)Z
match_infoÚgetÚintr   rP   rM   r   ÚHTTPForbiddenr<   r'   Úpop)r   r;   rU   rW   rX   Ú
target_urlZ	forwarderr   r   r   rQ     s    


zWsHandler.forward_connectc                 Ã   s^   |j }|| jvr"t|ƒ}|| j|< | j| }| |¡s>t ¡ ‚| |¡I d H rZ| j |¡ d S r   )rX   rN   rD   rJ   r   r]   r'   r^   )r   r;   ÚidÚhandlerr   r   r   rR   1  s    



zWsHandler.relay_connectN)rO   )r3   r4   r5   r   rT   rQ   rR   r   r   r   r   rL     s   
rL   c                 C   sr  t ƒ }tjdtjd�}|jdtdddd� |jdd	td
ddd� |jdtddd� |jdtddd� |jdtdddd� | ¡ \}}| d u r’|j} |d u r |j	}|d u r®|j
}|d u r¼|j}|d u rÊ|j}t ¡  g }	|d u�rt d|¡ |	 t || |¡¡¡ |d u�r>t d|¡ |	 t dt¡¡ |	 t d|¡¡ t d| |¡ t ¡ }
|
 |	¡ tj|
| |d� d S )Nz+Start ws relay with static content delivery)ÚdescriptionÚformatter_classz--hostrV   z@the interface for the web-server to listen on (default: 0.0.0.0)rU   )r,   ÚdefaultÚhelpÚdestz-pz--porti�  z;port number for the web-server to listen on (default: 8080)rW   z--moderO   z0Working mode [forward, relay] (default: forward))r,   rd   re   z--wwwzDirectory to serveÚwww_path)r,   re   rf   z--proxy-routezeProxy URL pattern (default: /proxy/{port}) mode::forward(ws://{host=localhost}:{port=1234}/{path=ws})z/proxy/{port}Úproxy_route)r,   re   rd   rf   zProxy route: %szWWW: %srZ   zStarting relay server: %s %s)rU   rW   )rL   ÚargparseÚArgumentParserÚRawDescriptionHelpFormatterÚadd_argumentÚstrr\   Úparse_known_argsrU   rW   rS   rg   rh   ÚloggingÚbasicConfigr   rP   Úappendr   r[   rT   rK   ZstaticÚApplicationZ
add_routesZrun_app)rU   rW   rg   rh   rS   ZwsRelayÚparserr@   Ú_ZroutesZweb_appr   r   r   ÚmainF  st    þûúüû


ru   Ú__main__)NNNNN)ri   r   Úosro   r   r   r\   Úenvironr[   r8   r9   Ú	getLoggerr3   r   ÚsetLevelÚINFOr   r7   r<   rD   rK   rL   ru   r   r   r   r   Ú<module>   s$   
ZM2	9
Q