3
­Ý ^¼  ã               @   sf   d 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ddlmZ ej	e
ƒZeddƒZG dd„ dƒZdS )z
ZIO peering
é    N)Ú
namedtupleÚPeerInfozuuid nick headersc               @   sV   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Z	ddd„Z
ddd„ZdS )ÚPeerz 
    Peer at the network to discover others and advertise self.

    This is equivalent to C++ zio::Peer and is a wrapper around Pyre,
    the Python Zyre.
    c             K   sn   t j|ƒ| _tƒ | _x*|jƒ D ]\}}t|ƒ}| jj||ƒ qW | jjƒ  t	j
ƒ | _| jj| jjƒ t	jƒ dS )ze
        Create a peer with a nickname.

        Extra headers may be given as a dictionary.
        N)ÚpyreZPyreÚzyreÚdictÚpeersÚitemsÚstrZ
set_headerÚstartÚzmqÚPollerÚpollerÚregisterÚsocketÚPOLLIN)ÚselfÚnicknameÚheadersÚkÚv© r   ú%/home/yuhw/zmq/zio/python/zio/peer.pyÚ__init__   s    

zPeer.__init__c             C   s   | j ƒ  d S )N)Ústop)r   r   r   r   Ú__del__(   s    zPeer.__del__c             C   s$   t | dƒr | jƒ  | jjƒ  | `dS )z[
        Stop the peer.

        This MUST be called or the application will hang.
        r   N)ÚhasattrÚdrainr   r   )r   r   r   r   r   +   s    

z	Peer.stopr   c       	      C   s$  t | jj|ƒƒ}| jjƒ |kr"dS | jjƒ }tjtj	ƒrštj
d| jjƒ f ƒ xJt|ƒD ]>\}}t|ƒdk r€tj
d||f ƒ qXtj
d|t|ƒf ƒ qXW |d dkrêtj|d d	�}|d
 jdƒ}tj|d jdƒƒ}t|||ƒ| j|< |d dk�r tj|d d	�}| jj|dƒ�r | j|= dS )zÕ
        Poll the network for an update.

        Return True if an event was received.  True is returned on
        reception of any type of Zyre event.  Use timeout in msec to
        wait for an event.
        Nz[peer %s]: zyre message:éd   z  part %d: %sz  part %d: (long %d bytes)r   s   ENTERé   )Úbytesé   zutf-8é   s   EXITT)r   r   Úpollr   r   ÚrecvÚlogÚisEnabledForÚloggingÚDEBUGÚdebugÚnameÚ	enumerateÚlenÚuuidZUUIDÚdecodeÚjsonÚloadsr   r   Úget)	r   ÚtimeoutÚwhichÚmsgZindÚpartÚuidÚnickr   r   r   r   r#   6   s(    
z	Peer.pollc             C   s   x| j dƒrqW dS )zN
        Continually poll until all queued Zyre events are processed.
        r   N)r#   )r   r   r   r   r   V   s    z
Peer.drainc             C   s6   t ƒ }x*| jjƒ D ]\}}|j|kr|j|ƒ qW |S )z?
        Return UUIDs of all peers with matching nicks
        )Úlistr   r	   r7   Úappend)r   r7   Úretr6   Úpir   r   r   Ú	matchnick]   s
    
zPeer.matchnickr   c             C   s€   dt j ƒ  }xn| jƒ  | j|ƒ}|r(|S dt j ƒ  }|dkrT|||  }|dk rT|S tjd| jjƒ ||f ƒ | j|ƒ qW dS )z˜
        Wait for at least one peer with the nickname to be.

        Return a list of UUIDs of peers discovered to have this
        nickname.
        iè  r   z#[peer %s]: wait for %s (timeout=%d)N)Útimer   r<   r%   r)   r   r*   r#   )r   r   r2   Zt0msZgotZnowmsr   r   r   Úwaitforh   s    
zPeer.waitforc             C   sÈ   d
}|dkrt j ƒ d|  }t|ƒ}tƒ }| jƒ  x�x&| jjƒ D ]}|j|kr@|||j< q@W tdd„ |jƒ D ƒƒ}||kr~|S |dkr¦td|t j ƒ   ƒ}|dkr¦|S t	ddj
|ƒƒ | j|ƒ q4W d	S )z©
        Wait as set of nicks have been seen or until timeout.

        Return a peer dictionary holding last seen info about peers
        with matching nicks.
        r   r   gü©ñÒMbP?c             S   s   g | ]
}|j ‘qS r   )r7   )Ú.0r;   r   r   r   ú
<listcomp>’   s    zPeer.party.<locals>.<listcomp>iè  zseen:ú Néÿÿÿÿ)r=   Úsetr   r   r   Úvaluesr7   r-   ÚintÚprintÚjoinr#   )r   Znicksr2   ZuntilZwantÚseenr;   Zknowr   r   r   Úparty€   s&    
z
Peer.partyN)r   rB   )rB   rB   )rB   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r#   r   r<   r>   rI   r   r   r   r   r      s   
 
r   )rM   r   r   r-   r/   r=   r'   Úcollectionsr   Ú	getLoggerrJ   r%   r   r   r   r   r   r   Ú<module>   s   

