o
    ¨ÝjW@  ã                   @   s†   d dl Z d dlmZmZ 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	 ddl
mZ ddlmZ e ¡ Ze ¡ ZG dd„ dƒZdS )	é    N)ÚdatetimeÚtimezone)Úparse_qsé   )ÚEventBuffer)ÚConnectionRefusedErrorc                   @   sÔ   e Zd Z		d2dd„Zdd	„ Zd
d„ Zdd„ Zdd„ Zd3dd„Zd3dd„Z	d3dd„Z
dd„ Zdd„ Zdd„ Zd3dd„Zdd„ Z		d4d d!„Zd"d#„ Zd$d%„ Zd4d&d'„Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd3d0d1„ZdS )5ÚInstrumentedAsyncServerNú/adminFÚdevelopmenté   c                 C   s|   |du rt dƒ‚|| _|| _|| _|| _|p#t| jjdƒr"| jjjnt| _	|| _
|| _g | _tƒ | _d| _d| _|  ¡  dS )z…Instrument the Socket.IO server for monitoring with the `Socket.IO
        Admin UI <https://socket.io/docs/v4/admin-ui/>`_.
        Nzauth must be specifiedÚhost_id)Ú
ValueErrorÚsioÚauthÚadmin_namespaceÚ	read_onlyÚhasattrÚmanagerr   ÚHOSTNAMEÚ	server_idÚmodeÚserver_stats_intervalÚadmin_queuer   Úevent_bufferÚstop_stats_eventÚ
stats_taskÚ
instrument)Úselfr   r   Ú	namespacer   r   r   r   © r   úO/var/www/html/chatgpt/venv/lib/python3.10/site-packages/socketio/async_admin.pyÚ__init__   s"   þz InstrumentedAsyncServer.__init__c                 C   sž  | j jd| j| jd� | jdkry| js?| j jd| j| jd� | j jd| j| jd� | j jd| j| jd� | j jd| j	| jd� i | j j
_| j j| j _| j| j _| j j
j| j j
_| j| j j
_| j j
j| j j
_| j| j j
_| j j
j| j j
_| j| j j
_| j j d| j¡ | j j d| j¡ d	d
lm} | j jj| j j_| j| j j_|j|_ t! "| j#j$| ¡|_|j%|_&t! "| j#j'| ¡|_%| jdkrÍ|j(|_)t! "| j#j*| ¡|_(d S d S )NÚconnect©r   r
   ÚemitÚjoinZleaveZ_disconnectÚ
disconnectr   ©ÚAsyncSocket)+r   ÚonÚadmin_connectr   r   r   Ú
admin_emitÚadmin_enter_roomÚadmin_leave_roomÚadmin_disconnectr   Ú_timestampsÚ_trigger_eventÚ'_InstrumentedAsyncServer__trigger_eventÚbasic_enter_roomÚ*_InstrumentedAsyncServer__basic_enter_roomÚ_basic_enter_roomÚbasic_leave_roomÚ*_InstrumentedAsyncServer__basic_leave_roomÚ_basic_leave_roomr$   Ú_InstrumentedAsyncServer__emitÚ_emitÚeioÚ_handle_eio_connectÚ_handle_eio_disconnectÚengineio.async_socketr(   Ú_okÚ_InstrumentedAsyncServer__okÚ_eio_http_responseÚhandle_post_requestÚ-_InstrumentedAsyncServer__handle_post_requestÚ	functoolsÚpartialmethodÚ	__class__Ú_eio_handle_post_requestÚ_websocket_handlerÚ+_InstrumentedAsyncServer__websocket_handlerÚ_eio_websocket_handlerÚ
_send_pingÚ#_InstrumentedAsyncServer__send_pingÚ_eio_send_ping©r   r(   r   r   r    r   ,   s^   ÿ
ÿÿÿÿ

ÿÿÿÿ

ÿþz"InstrumentedAsyncServer.instrumentc                 C   sŒ   | j dkr#| jj| j_| jjj| jj_| jjj| jj_| jjj	| jj_
| jjj| jj_ddlm} |j|_|j|_| j dkrD|j|_d S d S )Nr
   r   r'   )r   r   r1   r0   r   r3   r2   r6   r5   r8   r$   r:   r?   r>   r=   r(   rB   rA   rH   rG   rK   rJ   rM   r   r   r    Úuninstrumentg   s   
ÿÿ
ÿz$InstrumentedAsyncServer.uninstrumentc                 ƒ   s’   �d}ˆ j r:d}tˆ j tƒr|ˆ j k}n tˆ j tƒr |ˆ j v }nt ˆ j ¡r/ˆ   |¡I d H }nˆ   |¡}|s:tdƒ‚‡ fdd„}ˆ j ||¡ d S )NTFzauthentication failedc              	   “   sÒ   �ˆ j  d¡I d H  dg}ˆ js|g d¢7 }ˆ jdkr | d¡ ˆ j jdd|i| ˆ jd�I d H  ˆ jdkrgg }ˆ j j ¡ D ]}ˆ j j 	|d ¡D ]\} }| ˆ  
| ||¡¡ qGq=ˆ j jd	|| ˆ jd�I d H  d S d S )
Ngš™™™™™¹?ZAGGREGATED_EVENTS)ZEMITZJOINZLEAVEZ
DISCONNECTZMJOINZMLEAVEZMDISCONNECTr
   Z
ALL_EVENTSÚconfigZsupportedFeatures©Útor   Úall_sockets)r   Úsleepr   r   Úappendr$   r   r   Úget_namespacesÚget_participantsÚserialize_socket)ÚsidÚfeaturesrR   ÚnspÚeio_sid©r   r   r    rO   ‡   s0   €

ÿ
ÿÿþÿùz5InstrumentedAsyncServer.admin_connect.<locals>.config)	r   Ú
isinstanceÚdictÚlistÚinspectÚiscoroutinefunctionr   r   Ústart_background_task)r   rX   ÚenvironZclient_authÚauthenticatedrO   r   r\   r    r*   w   s   €
z%InstrumentedAsyncServer.admin_connectc                 Ç   s    �| j j||||d�I d H  d S )NrP   )r   r$   )r   Ú_r   Úroom_filterÚeventÚdatar   r   r    r+   ¡   s   €z"InstrumentedAsyncServer.admin_emitc                 Ã   ó8   �| j j ||¡D ]\}}| j j|||d�I d H  q	d S ©Nr#   )r   r   rV   Z
enter_room©r   re   r   Úroomrf   rX   r   r   r    r,   ¤   ó   €ÿþz(InstrumentedAsyncServer.admin_enter_roomc                 Ã   ri   rj   )r   r   rV   Z
leave_roomrk   r   r   r    r-   ©   rm   z(InstrumentedAsyncServer.admin_leave_roomc                 Ã   s6   �| j j ||¡D ]\}}| j j||d�I d H  q	d S rj   )r   r   rV   r&   )r   re   r   Úcloserf   rX   r   r   r    r.   ®   s   €ÿþz(InstrumentedAsyncServer.admin_disconnectc                 Ã   s<   �| j r| j ¡  t | j ¡I d H  d | _ | j ¡  d S d S ©N)r   r   ÚsetÚasyncioZgatherÚclearr\   r   r   r    Úshutdown³   s   €
üz InstrumentedAsyncServer.shutdownc           	   	   Ç   sB  �t   ¡ }|d }|dkrL| jj ||¡}|| jjj|< |  |||¡}| jjd|t |t	j
¡ ¡ f| jd�I d H  | jj |¡jsK| j | j|||¡ nG|dkrs| jjj|= |d }| jjd|||t |t	j
¡ ¡ f| jd�I d H  n | jjd|||g|dd … ¢R t |t	j
¡ ¡ f| jd�I d H  | jj||g|¢R Ž I d H S )	Nr   r"   Úsocket_connectedr#   r&   r   Zsocket_disconnectedZevent_received)Útimer   r   Úeio_sid_from_sidr/   rW   r$   r   Úfromtimestampr   ÚutcÚ	isoformatr   r:   Ú_get_socketÚupgradedrb   Ú_check_for_upgrader1   )	r   rg   r   ÚargsÚtrX   r[   Úserialized_socketÚreasonr   r   r    r0   º   sL   €þý
ÿ€üûüûz&InstrumentedAsyncServer._trigger_eventc              	   Ã   sv   �t dƒD ]3}| j d¡I d H  z| jj |¡jr.| jjd||ddœ| jd�I d H  W  d S W q ty8   Y qw d S )Né   Zsocket_updatedÚ	websocket)ÚidrZ   Ú	transportr#   )	Úranger   rS   r:   rz   r{   r$   r   ÚKeyError)r   r[   rX   r   re   r   r   r    r|   Ú   s$   €ýüúÿöz*InstrumentedAsyncServer._check_for_upgradec              	   C   s@   | j j ||||¡}|r| j d|||t tj¡ 	¡ ff¡ |S )NZroom_joined)
r   r   r3   r   rT   r   Únowr   rx   ry   )r   rX   r   rl   r[   Úretr   r   r    r4   é   s   ÿüz)InstrumentedAsyncServer._basic_enter_roomc              	   C   s:   |r| j  d|||t tj¡ ¡ ff¡ | jj 	|||¡S )NZ	room_left)
r   rT   r   r‡   r   rx   ry   r   r   r6   )r   rX   r   rl   r   r   r    r7   õ   s   üz)InstrumentedAsyncServer._basic_leave_roomc              	   Ë   s¸   �| j jj|||f|||dœ|¤ŽI d H }|| jkrZt|tƒr&|gt|ƒ n||g}	t|tƒs2|g}| j j ||¡D ]\}
}|
|vrY| j jd||
|	t	 
tj¡ ¡ f| jd�I d H  q:|S )N)rl   Úskip_sidÚcallbackZ
event_sentr#   )r   r   r8   r   r]   Útupler_   rV   r$   r   r‡   r   rx   ry   )r   rg   rh   r   rl   r‰   rŠ   Úkwargsrˆ   Z
event_datarX   re   r   r   r    r9   ÿ   s6   €ÿþþ
ÿ
üû€zInstrumentedAsyncServer._emitc                 Ã   sT   �| j d u r| jj ¡ | _ | jd u r| j | j¡| _| j d¡ | j 	||¡I d H S )NZrawConnection)
r   r   r:   Zcreate_eventr   rb   Ú_emit_server_statsr   Úpushr;   )r   r[   rc   r   r   r    r;     s   €

ÿz+InstrumentedAsyncServer._handle_eio_connectc                 Ã   s"   �| j  d¡ | j ||¡I d H S )NZrawDisconnection)r   rŽ   r   r<   )r   r[   r€   r   r   r    r<     s   €z.InstrumentedAsyncServer._handle_eio_disconnectc                 C   s:   | j jj|||d�}| j d¡ | j dt|d ƒ¡ |S )N)ÚpacketsÚheadersÚjsonp_indexÚ
packetsOutÚbytesOutÚresponse)r   r:   r?   r   rŽ   Úlen)r   r�   r�   r‘   rˆ   r   r   r    r@   !  s   ÿz*InstrumentedAsyncServer._eio_http_responsec                 Ã   s<   �|   |¡I d H }|j d¡ |j dt| dd¡ƒ¡ |S )NÚ	packetsInÚbytesInZCONTENT_LENGTHr   )rB   r   rŽ   ÚintÚget)Úsocketr   rc   rˆ   r   r   r    rF   (  s   €ÿz0InstrumentedAsyncServer._eio_handle_post_requestc                 ƒ   sV   �‡ fdd„}‡ fdd„}|j |_t ||¡|_ |j|_t ||¡|_|  |¡I d H S )Nc                 “   s0   �ˆ j  d¡ ˆ j  dt|ƒ¡ |  |¡I d H S )Nr’   r“   )r   rŽ   r•   Ú_InstrumentedAsyncServer__send)Úwsrh   r\   r   r    Ú_send0  s   €z=InstrumentedAsyncServer._eio_websocket_handler.<locals>._sendc                 “   s6   �|   ¡ I d H }ˆ j d¡ ˆ j dt|pdƒ¡ |S )Nr–   r—   Ú )Ú_InstrumentedAsyncServer__waitr   rŽ   r•   )rœ   rˆ   r\   r   r    Ú_wait5  s
   €z=InstrumentedAsyncServer._eio_websocket_handler.<locals>._wait)Úsendr›   rC   ÚpartialÚwaitrŸ   rH   )rš   r   rœ   r�   r    r   r\   r    rI   /  s   €z.InstrumentedAsyncServer._eio_websocket_handlerc                 Ã   s~   �| j }t ¡ }|jj ¡ D ])}|jj ||¡}|r7| |||¡}|jjd|t 	|t
j¡ ¡ f|jd�I d H  q|  ¡ I d H S )Nrt   r#   )rX   ru   r   r   rU   Zsid_from_eio_sidrW   r$   r   rw   r   rx   ry   r   rK   )rš   r   r[   r~   r   rX   r   r   r   r    rL   A  s"   €ÿþý€z&InstrumentedAsyncServer._eio_send_pingc                 ƒ   sô   �t   ¡ }tˆ jj ¡ ƒ}| ¡  ˆ j ¡ sxˆ j ˆ j	¡I d H  ˆ jj
dˆ jttt   ¡ | tˆ jjjƒtdd„ ˆ jjj ¡ D ƒƒˆ j ¡ ‡ fdd„|D ƒdœˆ jd�I d H  ˆ jrqˆ j d¡\}}ˆ jj
||ˆ jd�I d H  ˆ jsYˆ j ¡ rd S d S )NZserver_statsc                 S   s   g | ]}|j s|‘qS r   )r{   )Ú.0Úsr   r   r    Ú
<listcomp>\  s    
ÿz>InstrumentedAsyncServer._emit_server_stats.<locals>.<listcomp>c              	      s4   g | ]}|t ˆ jjj |d g i¡ d g ¡ƒdœ‘qS )N)ÚnameZsocketsCount)r•   r   r   Úroomsr™   )r¤   rZ   r\   r   r    r¦   _  s    ýÿÿ
þ)ZserverIdÚhostnameÚpidZuptimeZclientsCountZpollingClientsCountZaggregatedEventsÚ
namespacesr#   r   )ru   r_   r   ÚhandlersÚkeysÚsortr   Úis_setrS   r   r$   r   r   ÚPIDr•   r:   ZsocketsÚvaluesr   Zget_and_clearr   r   Úpop)r   Ú
start_timer«   rg   r}   r   r\   r    r�   O  s:   €

ÿ
üöñ
ÿþîz*InstrumentedAsyncServer._emit_server_statsc                 C   sì   |d u r| j j |¡}| j j |¡}| j j |i ¡}|| j jjv r(| j jj| nd}|||jr1dnd|i | dd¡dd„ | 	¡ D ƒdd„ t
| d	d¡ƒ 	¡ D ƒ| d
d¡dk| dd¡|d |rit |tj¡ ¡ nddœ| j j ||¡dœS )Nr   r‚   ZpollingZREMOTE_ADDRrž   c                 S   s,   i | ]\}}|  d ¡r|dd…  ¡ |“qS )ZHTTP_r�   N)Ú
startswithÚlower©r¤   ÚkÚvr   r   r    Ú
<dictcomp>y  s    ÿz<InstrumentedAsyncServer.serialize_socket.<locals>.<dictcomp>c                 S   s*   i | ]\}}|t |ƒd kr|d n|“qS )r   r   )r•   r¶   r   r   r    r¹   {  s   * ZQUERY_STRINGzwsgi.url_schemeÚhttpsZ	PATH_INFOiè  )Úaddressr�   ÚqueryÚsecureÚurlZissuedru   )rƒ   ZclientIdr„   rZ   rh   Ú	handshaker¨   )r   r   rv   r:   rz   rc   r™   r/   r{   Úitemsr   r   rw   r   rx   ry   Z	get_rooms)r   rX   r   r[   rš   rc   Útmr   r   r    rW   j  s<   ÿÿ

ÿÿ
ÿöîz(InstrumentedAsyncServer.serialize_socket)Nr	   FNr
   r   ro   )NNN)Ú__name__Ú
__module__Ú__qualname__r!   r   rN   r*   r+   r,   r-   r.   rs   r0   r|   r4   r7   r9   r;   r<   r@   rF   rI   rL   r�   rW   r   r   r   r    r      s6    
ÿ;*


 


ÿ

r   )rq   r   r   rC   r`   Úosrš   ru   Úurllib.parser   Zadminr   Ú
exceptionsr   Úgethostnamer   Úgetpidr°   r   r   r   r   r    Ú<module>   s    