o
    åÅe*(  ã                   @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZejdkr'd dl	m
Z
 zd dlZW n ey8   dZY nw zd dlmZ dd„ ZW n
   dZdd„ ZY zd dlmZ W n   dZY ej d¡Zd	Zd
ZdZdZdZG dd„ deƒZeZG dd„ deƒZdd„ ZdS )é    NÚwin32)Úpatcherc                   C   s
   t  t¡S ©N)Úeventlet_patcherÚis_monkey_patchedÚselect© r   r   ú,/usr/lib/python3/dist-packages/ovs/poller.pyÚ_using_eventlet_green_select$   s   
r
   c                   C   s   dS )NFr   r   r   r   r	   r
   )   s   )ÚmonkeyÚpolleré   é   é   é   é    c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú_SelectSelectzh select.poll emulation by using select.select.
    Only register and poll are needed at the moment.
    c                 C   s   g | _ g | _g | _d S r   )ÚrlistÚwlistÚxlist©Úselfr   r   r	   Ú__init__D   s   
z_SelectSelect.__init__c                 C   s˜   t |tjƒr
| ¡ }trt |tjƒr| ¡ }tjdkr"t |tƒs"J ‚|t@ r1| j	 
|¡ |t M }|t@ r@| j 
|¡ |t M }|rJ| j 
|¡ d S d S )Nr   )Ú
isinstanceÚsocketÚfilenoÚsslÚ	SSLSocketÚsysÚplatformÚintÚPOLLINr   ÚappendÚPOLLOUTr   r   ©r   ÚfdÚeventsr   r   r	   ÚregisterI   s   


ÿz_SelectSelect.registerc           
      C   sŒ  |dkr	t ƒ r	d}tjdkrr| j| j | j }|sg S t|ƒtjj	kr't
dƒ‚|dkr.d}nt|ƒ}z
tj |d|¡}W n tjjyL   dtfg Y S w |tjjkrUg S || | jv r_t}n|| | jv rit}nt}|| |fgS |dkryd }nt|ƒd }t | j| j| j|¡\}}}i }|D ]}	| |	d¡tB ||	< q‘|D ]}	| |	d¡tB ||	< q |D ]}	| |	d¡ttB tB B ||	< q¯t| ¡ ƒS )	Nr   gš™™™™™¹?r   z,Cannot handle more than maximum waitobjects
éd   Féÿÿÿÿiè  )r
   r   r   r   r   r   ÚlenÚwinutilsÚ
win32eventÚMAXIMUM_WAIT_OBJECTSÚWindowsErrorr    ÚWaitForMultipleObjectsÚ
pywintypesÚerrorÚPOLLERRÚwinerrorÚWAIT_TIMEOUTr!   r#   Úfloatr   ÚgetÚPOLLHUPÚPOLLNVALÚlistÚitems)
r   Útimeoutr&   ÚretvalÚreventr   r   r   Úevents_dictr%   r   r   r	   Úpoll[   s`   
ýÿ
ýÿþz_SelectSelect.pollN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   r?   r   r   r   r	   r   @   s
    r   c                   @   sX   e 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 )ÚPollera=  High-level wrapper around the "poll" system call.

    Intended usage is for the program's main loop to go about its business
    servicing whatever events it needs to.  Then, when it runs out of immediate
    tasks, it calls each subordinate module or object's "wait" function, which
    in turn calls one (or more) of the functions Poller.fd_wait(),
    Poller.immediate_wake(), and Poller.timer_wait() to register to be awakened
    when the appropriate event occurs.  Then the main loop calls
    Poller.block(), which blocks until one of the registered events happens.c                 C   s   |   ¡  d S r   )Ú_Poller__resetr   r   r   r	   r   ª   s   zPoller.__init__c                 C   s   | j  ||¡ dS )aW  Registers 'fd' as waiting for the specified 'events' (which should
        be select.POLLIN or select.POLLOUT or their bitwise-OR).  The following
        call to self.block() will wake up when 'fd' becomes ready for one or
        more of the requested events.

        The event registration is one-shot: only the following call to
        self.block() is affected.  The event will need to be re-registered
        after self.block() is called if it is to persist.

        'fd' may be an integer file descriptor or an object with a fileno()
        method that returns an integer file descriptor.N)r?   r'   r$   r   r   r	   Úfd_wait­   s   zPoller.fd_waitc                 C   s"   | j dk s
|| j k r|| _ d S d S )Nr   ©r;   ©r   Úmsecr   r   r	   Ú__timer_wait»   s   
ÿzPoller.__timer_waitc                 C   s"   |dkr
|   ¡  dS |  |¡ dS )aŠ  Causes the following call to self.block() to block for no more than
        'msec' milliseconds.  If 'msec' is nonpositive, the following call to
        self.block() will not block at all.

        The timer registration is one-shot: only the following call to
        self.block() is affected.  The timer will need to be re-registered
        after self.block() is called if it is to persist.r   N)Úimmediate_wakeÚ_Poller__timer_waitrH   r   r   r	   Ú
timer_wait¿   s   zPoller.timer_waitc                 C   s0   t j ¡ }||kr|  ¡  dS |  || ¡ dS )aÓ  Causes the following call to self.block() to wake up when the
        current time, as returned by ovs.timeval.msec(), reaches 'msec' or
        later.  If 'msec' is earlier than the current time, the following call
        to self.block() will not block at all.

        The timer registration is one-shot: only the following call to
        self.block() is affected.  The timer will need to be re-registered
        after self.block() is called if it is to persist.N)ÚovsÚtimevalrI   rK   rL   )r   rI   Únowr   r   r	   Útimer_wait_untilÌ   s   
	zPoller.timer_wait_untilc                 C   s
   d| _ dS )z[Causes the following call to self.block() to wake up immediately,
        without blocking.r   NrG   r   r   r   r	   rK   Û   s   
zPoller.immediate_wakec              
   C   sä   zlz| j   | j¡}|  |¡ W nH ty3 } z	 |jtjkr)t dt 	|j¡ ¡ W Y d}~n0d}~w t
jyW } z|\}}|tjkrMt d|d  ¡ W Y d}~nd}~ww W |  ¡  dS W |  ¡  dS W |  ¡  dS |  ¡  w )zæBlocks until one or more of the events registered with
        self.fd_wait() occurs, or until the minimum duration registered with
        self.timer_wait() elapses, or not at all if self.immediate_wake() has
        been called.zpoll: %sNr   )r?   r;   Ú_Poller__log_wakeupÚOSErrorÚerrnoÚEINTRÚvlogÚerrÚosÚstrerrorr   r1   rE   )r   r&   Úer1   Úmsgr   r   r	   Úblockà   s*   €
€üú€ € zPoller.blockc                 C   s˜   |st  d| j ¡ d S |D ];\}}|dkrId}|t@ r |d7 }|t@ r(|d7 }|t@ r0|d7 }|t@ r8|d7 }|t@ r@|d7 }t  d	||f ¡ qd S )
Nz%d-ms timeoutr   Ú z[POLLIN]z	[POLLOUT]z	[POLLERR]z	[POLLHUP]z
[POLLNVAL]z%s on fd %d)rV   Údbgr;   r!   r#   r2   r7   r8   )r   r&   r%   ÚreventsÚsr   r   r	   Ú__log_wakeupö   s$   €ózPoller.__log_wakeupc                 C   s   t ƒ | _d| _d S )Nr)   )Ú
SelectPollr?   r;   r   r   r   r	   Ú__reset	  s   
zPoller.__resetN)r@   rA   rB   rC   r   rF   rL   rM   rQ   rK   r\   rR   rE   r   r   r   r	   rD   Ÿ   s    
rD   c                  C   s`   z&t ƒ rt d¡j} W | ƒ S tr t dd¡r t dd¡} W | ƒ S tj} W | ƒ S    t} Y | ƒ S )a[  Returns the original select.poll() object. If select.poll is
    monkey patched by eventlet or gevent library, it gets the original
    select.poll and returns an object of it using the
    eventlet.patcher.original/gevent.monkey.get_original functions.

    As a last resort, if there is any exception it returns the
    SelectPoll() object.
    r   r?   )	r
   r   Úoriginalr?   Úgevent_monkeyÚis_object_patchedÚget_originalr   rb   )Ú_system_pollr   r   r	   Úget_system_poll  s   		øÿüýri   ) rT   rX   r   r   r   Úovs.timevalrN   Úovs.vlogr   Úovs.winutilsr+   r   ÚImportErrorÚeventletr   r   r
   Úgeventr   re   rV   ÚVlogr!   r#   r2   r7   r8   Úobjectr   rb   rD   ri   r   r   r   r	   Ú<module>   sD   
ÿ
Yo