o
    BŠ]  ã                   @   sŽ   d dl Z d dlZd dlZd dlmZ d dlmZ zd dlZW n ey+   d dl	ZY nw e 
¡ dkZG dd„ deƒZeedƒsEedƒ dS dS )	é    N)ÚAbstractTransportDispatcher)ÚPySnmpError)Ú3Ú4r   c                   @   s@   e Zd ZdZdd„ Zejdd„ ƒZddd„Zd	d
„ Z	dd„ Z
dS )ÚAsyncioDispatcherz-AsyncioDispatcher based on asyncio event loopc                 O   sB   t  | ¡ d| _d|v r|  |d ¡ d | _| dt ¡ ¡| _d S )Nr   ÚtimeoutÚloop)	r   Ú__init__Ú"_AsyncioDispatcher__transportCountZsetTimerResolutionÚloopingcallÚpopÚasyncioZget_event_loopr   )ÚselfÚargsÚkwargs© r   úA/usr/lib/python3/dist-packages/pysnmp/carrier/asyncio/dispatch.pyr	   1   s   
zAsyncioDispatcher.__init__c                 c   s,    	 t  t  |  ¡ ¡¡V  |  | j ¡ ¡ q)N)r   ÚFromÚsleepÚgetTimerResolutionZhandleTimerTickr   Útime)r   r   r   r   Úhandle_timeout9   s
   €þz AsyncioDispatcher.handle_timeoutç        c              	   C   sT   | j  ¡ s(z| j  ¡  W d S  ty   ‚  ty'   td tjt	 
¡ Ž ¡ƒ‚w d S )Nú;)r   Z
is_runningZrun_foreverÚKeyboardInterruptÚ	Exceptionr   ÚjoinÚ	tracebackÚformat_exceptionÚsysÚexc_info)r   r   r   r   r   ÚrunDispatcher?   s   
ÿûzAsyncioDispatcher.runDispatcherc                 C   s`   | j d u r |  ¡ dkr trt |  ¡ ¡| _ n
ttdƒ|  ¡ ƒ| _ t | ||¡ |  j	d7  _	d S )Nr   Úasyncé   )
r   r   ÚIS_PYTHON_344_PLUSr   Zensure_futurer   Úgetattrr   ÚregisterTransportr
   )r   ÚtDomainZ	transportr   r   r   r&   H   s   ÿz#AsyncioDispatcher.registerTransportc                 C   s^   t  | |¡}|d urt  | |¡ |  jd8  _| jdkr+| j ¡ s-| j ¡  d | _d S d S d S )Nr#   r   )r   ZgetTransportÚunregisterTransportr
   r   ZdoneZcancel)r   r'   Útr   r   r   r(   T   s   

þz%AsyncioDispatcher.unregisterTransportN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   Ú	coroutiner   r!   r&   r(   r   r   r   r   r   .   s    

	r   r   zÙ@asyncio.coroutine
def handle_timeout(self):
    while True:
        yield from asyncio.sleep(self.getTimerResolution())
        self.handleTimerTick(self.loop.time())
AsyncioDispatcher.handle_timeout = handle_timeout)r   Úplatformr   Zpysnmp.carrier.baser   Zpysnmp.errorr   r   ÚImportErrorZtrolliusZpython_version_tupler$   r   ÚhasattrÚexecr   r   r   r   Ú<module>   s    ÿ
3ÿ