3
3kU^³  ã               @   s>   d dl mZ ddlZd dlmZ d dlmZ G dd„ dƒZdS )é   )Úguess_hostnameé    N)ÚPort)ÚPeerc               @   s<   e Zd ZdZddd„Zdd„ Zdd	d
„Zdd„ Zdd„ ZdS )ÚNodez3
    A vertex in a ported graph with ZIO edges
    r   ú	127.0.0.1c             C   s$   || _ || _|ptƒ | _tƒ | _dS )zÚ
        Create a node.

        A node has a nickname.  It may assert a unique origin number.
        A hostname may be specified for default binds of its ports or
        one will attempt to be autodetected.
        N)ÚnickÚoriginr   Ú	_hostnameÚdictÚports)Úselfr   r	   Úhostname© r   ú%/home/yuhw/zmq/zio/python/zio/node.pyÚ__init__   s    zNode.__init__c             C   s,   dj dd„ | jjƒ D ƒƒ}d| j| j|f S )Nú c             S   s   g | ]}t |ƒ‘qS r   )Ústr)Ú.0Úpr   r   r   ú
<listcomp>   s    z Node.__str__.<locals>.<listcomp>z[node %s]: origin:%d ports:[%s])Újoinr   Úvaluesr   r	   )r   Úspr   r   r   Ú__str__   s    zNode.__str__Nc             C   sL   || j kr| j | S |dkr(td| ƒ‚t||| jƒ}| j|_|| j |< |S )z™
        Return a port.

        If the port with this name exists, return it.  Otherwise,
        create one with the given ZeroMQ socket type.
        NzNo port "%s")r   ÚKeyErrorr   r
   r	   )r   ÚnameÚstypeÚportr   r   r   r      s    


z	Node.portc             K   sv   t | dƒrtd| j ƒ dS x$| jjƒ D ]}|jƒ }|j|ƒ q(W t| jf|Ž| _x| jjƒ D ]}|j	| jƒ q^W dS )zÏ
        Bring node online.

        The node will advertise a number of ZIO headers based on the
        ports that have been prepared.  Additional application headers
        may also be included.
        ÚpeerzNode "%s" already onlineN)
ÚhasattrÚprintr   r   r   Údo_bindsÚupdater   r   Úonline)r   Úheadersr   Zhhr   r   r   r$   /   s    
zNode.onlinec             C   s6   x| j jƒ D ]}|jƒ  qW tƒ | _ t| dƒr2| `dS )z‡
        Bring node offline.

        This will cause all ports to disconnect and unbind and the
        peer to be destroyed.
        r   N)r   r   Úoffliner   r    r   )r   r   r   r   r   r&   C   s
    
zNode.offline)r   r   )N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r$   r&   r   r   r   r   r   	   s   

r   )Úutilr   Úzmqr   r   r   r   r   r   r   r   r   Ú<module>   s   