
    |;ih                        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y)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                  x    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y)AsyncEnginez*An engine wraps sockets in the event loop.)
_cleanup_timer_listen_socket_respond_sockets_setup_taskloop	protocolsreadersrunning_futuresenderszcc                    d | _         || _        g | _        g | _        g | _        d | _        || _        || _        d | _        d | _	        y N)
r   r   r   r   r   r   r   r   r   r   )selfzeroconflisten_socketrespond_socketss       C/home/ubuntu/myenv/lib/python3.12/site-packages/zeroconf/_engine.py__init__zAsyncEngine.__init__>   sO     7;	.00202BF+ /:>6:    c                    || _         |j                         | _        | j                   j                  | j	                  |            | _        y)Set up the instance.N)r   create_futurer   create_task_async_setupr   )r   r   loop_thread_readys      r"   setupzAsyncEngine.setupO   s?     	"00299001B1BCT1UVr$   c                  K   | j                          | j                          d{    | j                  J | j                  j                         s| j                  j	                  d       |r|j                          yy7 [w)r&   NT)"_async_schedule_next_cache_cleanup_async_create_endpointsr   done
set_resultset)r   r*   s     r"   r)   zAsyncEngine._async_setupY   su     //1**,,,""...""'')**40!!# 	 	-s   $BBABc           	        K    j                   J  j                   }g }g } j                  r|j                   j                          j                  D ](  }||vr|j                  |       |j                  |       * |D ]  }|j	                   fd|       d{   \  }} j
                  j                  t        t        |              j                  j                  t        t        t        j                  |                   ||v s j                  j                  t        t        t        j                  |                    y7 w)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    dgg. r$   )sock)r   r   appendr   create_datagram_endpointr   r   r   r   r   asyncioDatagramTransportr   )r   r   reader_socketssender_socketss	transportprotocols   `      r"   r.   z#AsyncEngine._async_create_endpointsc   s-    yy$$$yy!!$"5"56&& 	%A&%%a(!!!$	%
   	hA(,(E(E. )F ) #Ix NN!!$}h"?@LL 6tG<U<UW`7a bcN"##$:4@Y@Y[d;e$fg	h#s   BEEA0EA Ec           
        t               }| j                  j                  j                  |       | j                  j                  j                  || j                  j                  j                  |      D cg c]  }t        ||       c}       | j                  j                  j                  d       | j                          yc c}w )zPeriodic cache cleanup.FN)
r
   r   question_historyasync_expirerecord_managerasync_updatescacher   async_updates_completer-   )r   nowrecords      r"   _async_cache_cleanupz AsyncEngine._async_cache_cleanupz   s    !#  --c2,,8<8R8RSV8WXf\&&)X	
 	55e<//1 Ys   7C
c                    | j                   }|J |j                  |j                         t        z   | j                        | _        y)z Schedule the next cache cleanup.N)r   call_attimer   rI   r   )r   r   s     r"   r-   z.AsyncEngine._async_schedule_next_cache_cleanup   s=    yy"ll499;9P+PRVRkRklr$   c                  K   | j                   J | j                    d{    | j                          t        j                  d       d{    | j                  J | j                  j                          y7 Z7 /w)z/Cancel and wait for the cleanup task to finish.Nr   )r   _async_shutdownr9   sleepr   cancelr4   s    r"   _async_closezAsyncEngine._async_close   sr     +++mmA""...""$	 	s!   A?A;,A?A=.A?=A?c                   | j                   J | j                  J | j                  j                         | _         t        j                  | j
                  | j                        D ]  }|j                  j                           y)z Shutdown transports and sockets.N)	r   r   r'   	itertoolschainr   r   r>   close)r   wrapped_transports     r"   rN   zAsyncEngine._async_shutdown   sn    ""...yy$$$"ii557!*t||!L 	0''--/	0r$   c                    | j                   J t               | j                   k(  r| j                          y| j                   j                         syt	        | j                         | j                   t               y)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   rN   
is_runningr	   rQ   _CLOSE_TIMEOUTr4   s    r"   rU   zAsyncEngine.close   s^     yy$$$*  "yy##%d//1499nMr$   N)r   r   r    zsocket.socket | Noner!   zlist[socket.socket]returnNone)r   zasyncio.AbstractEventLoopr*   threading.Event | NonerZ   r[   )r*   r\   rZ   r[   )rZ   r[   )__name__
__module____qualname____doc__	__slots__r#   r+   r)   r.   rI   r-   rQ   rN   rU    r$   r"   r   r   .   s    4I;; ,; -	;
 
;"W'W 2W 
	W$h.	2m%0Nr$   r   )r`   
__future__r   r9   rS   socket	threadingtypingr   r   _record_updater   _utils.asyncior   r	   _utils.timer
   constr   _corer   	_listenerr   
_transportr   r   rY   r   rb   r$   r"   <module>rn      sK   , #     & ( C , * % A|N |Nr$   