
    8j!                        d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	 ddl
mZ ddlmZmZ ddlmZ dd	lmZ erdd
lmZ ddlmZ ddlmZmZ dZ G d d          ZdS )a  Multicast DNS Service Discovery for Python, v0.14-wmcbrine
Copyright 2003 Paul Scott-Murphy, 2014 William McBrine

This module provides a framework for the use of DNS Service Discovery
using IP multicast.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
    )annotationsN)TYPE_CHECKINGcast   RecordUpdate)get_running_looprun_coro_with_timeout)current_time_millis)_CACHE_CLEANUP_INTERVAL)Zeroconf)AsyncListener)_WrappedTransportmake_wrapped_transporti  c                  ^    e Zd ZdZdZddZddZddZddZddZ	ddZ
ddZddZddZdS )AsyncEnginez*An engine wraps sockets in the event loop.)
_cleanup_timer_listen_socket_respond_sockets_setup_taskloop	protocolsreadersrunning_futuresenderszczeroconfr   listen_socketsocket.socket | Nonerespond_socketslist[socket.socket]returnNonec                    d | _         || _        g | _        g | _        g | _        d | _        || _        || _        d | _        d | _	        d S N)
r   r   r   r   r   r   r   r   r   r   )selfr   r   r    s       F/home/user/adhan/venv/lib/python3.11/site-packages/zeroconf/_engine.py__init__zAsyncEngine.__init__>   sU     7;	.00202BF+ /:>6:    r   asyncio.AbstractEventLooploop_thread_readythreading.Event | Nonec                    || _         |                                | _        | j                             |                     |                    | _        dS )Set up the instance.N)r   create_futurer   create_task_async_setupr   )r&   r   r+   s      r'   setupzAsyncEngine.setupO   sL     	"00229001B1BCT1U1UVVr)   c                  K   |                                   |                                  d{V  | j        J | j                                        s| j                            d           |r|                                 dS dS )r.   NT)"_async_schedule_next_cache_cleanup_async_create_endpointsr   done
set_resultset)r&   r+   s     r'   r1   zAsyncEngine._async_setupY   s      //111**,,,,,,,,,"..."'')) 	1**4000 	$!!#####	$ 	$r)   c           	        K    j         J  j         }g }g } j        r|                     j                    j        D ]0}||vr|                    |           |                    |           1|D ]}|                     fd|           d{V \  }} j                            t          t          |                      j                            t          t          t          j        |                               ||v r? j                            t          t          t          j        |                               | j        u rd _        | j        v r j                            |           
dS )z%Create endpoints to send and receive.Nc                 ,    t           j                  S r%   )r   r   r&   s   r'   <lambda>z5AsyncEngine._async_create_endpoints.<locals>.<lambda>r   s    dg.. r)   )sock)r   r   appendr   create_datagram_endpointr   r   r   r   r   asyncioDatagramTransportr   remove)r&   r   reader_socketssender_socketss	transportprotocols   `      r'   r5   z#AsyncEngine._async_create_endpointsc   s     y$$$y 	7!!$"5666& 	% 	%A&&%%a(((!!!$$$$ 	0 	0A(,(E(E.... )F ) ) # # # # # #Ix N!!$}h"?"?@@@L 6tG<UW`7a7a b bcccN""##$:4@Y[d;e;e$f$fgggD'''&*#D)))%,,Q///	0 	0r)   c                T   t                      }| j        j                            |           | j        j                            |d | j        j                            |          D                        | j        j                            d           |                                  dS )zPeriodic cache cleanup.c                .    g | ]}t          ||          S  r   ).0records     r'   
<listcomp>z4AsyncEngine._async_cache_cleanup.<locals>.<listcomp>   s"    XXXf\&&))XXXr)   FN)	r   r   question_historyasync_expirerecord_managerasync_updatescacheasync_updates_completer4   )r&   nows     r'   _async_cache_cleanupz AsyncEngine._async_cache_cleanup   s    !## --c222,,XX8R8RSV8W8WXXX	
 	
 	
 	55e<<<//11111r)   c                    | j         }|J |                    |                                t          z   | j                  | _        dS )z Schedule the next cache cleanup.N)r   call_attimer   rU   r   )r&   r   s     r'   r4   z.AsyncEngine._async_schedule_next_cache_cleanup   sC    y"ll499;;9P+PRVRkllr)   c                8  K   | j         J 	 | j          d{V  n/# t          j        $ r | j                                         s Y nw xY w|                                  t          j        d           d{V  | j        | j                                         dS dS )z/Cancel and wait for the cleanup task to finish.Nr   )r   r@   CancelledError	cancelled_async_shutdownsleepr   cancelr;   s    r'   _async_closezAsyncEngine._async_close   s      +++	"""""""""% 	 	 	#--//  	 	mA*&&((((( +*s    )AAc                   | j         J | j        J | j                                        | _         | j        2| j                                        s| j                                         t          j        | j        | j	                  D ]}|j
                                         | j         | j                                         d| _        | j        D ]}|                                 g | _        dS )z9Shutdown transports and sockets; safe to call repeatedly.N)r   r   r/   r   r6   r^   	itertoolschainr   r   rF   closer   r   )r&   wrapped_transportrE   s      r'   r\   zAsyncEngine._async_shutdown   s    "...y$$$"i5577 '0@0E0E0G0G'##%%%!*t|!L!L 	0 	0'--////*%%'''"&D& 	 	AGGIIII "r)   c                   | j         J t                      | j         k    r|                                  dS | j                                         sdS t	          |                                 | j         t                     dS )zClose from sync context.

        While it is not expected during normal operation,
        this function may raise EventLoopBlocked if the underlying
        call to `_async_close` cannot be completed.
        N)r   r	   r\   
is_runningr
   r_   _CLOSE_TIMEOUTr;   s    r'   rc   zAsyncEngine.close   s}     y$$$**  """Fy##%% 	Fd//1149nMMMMMr)   N)r   r   r   r   r    r!   r"   r#   )r   r*   r+   r,   r"   r#   )r+   r,   r"   r#   )r"   r#   )__name__
__module____qualname____doc__	__slots__r(   r2   r1   r5   rU   r4   r_   r\   rc   rJ   r)   r'   r   r   .   s        44I; ; ; ;"W W W W$ $ $ $0 0 0 0<	2 	2 	2 	2m m m m) ) ) ) # # # #&N N N N N Nr)   r   )rk   
__future__r   r@   ra   socket	threadingtypingr   r   _record_updater   _utils.asyncior	   r
   _utils.timer   constr   _corer   	_listenerr   
_transportr   r   rg   r   rJ   r)   r'   <module>rx      sD   , # " " " " "           & & & & & & & & ( ( ( ( ( ( C C C C C C C C , , , , , , * * * * * *   % $ $ $ $ $ A A A A A A A AUN UN UN UN UN UN UN UN UN UNr)   