3
ÎmU^0  ã               @   sZ  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ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ ejeƒZye W n ek
r¶   eZY nX G d
d„ deƒZdd„ Zedk�rVejƒ  ejdƒjejƒ e jƒ Z e
j!e eƒZ"xByeƒ Z#e"j$e#ƒ W n& e%e&fk
�rD   e"j$dƒ P Y nX �q
W ej'dƒ dS )é    Né   )Ú__version_info__)Úzbeacon)Úzhelper)ÚZActor)ÚZSocket)ÚPyreNode)Ú	PyreEventc                   s*  e Zd ZdF‡ f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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(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Zd<d=„ Z d>d?„ Z!d@dA„ Z"dBdC„ Z#e$dDdE„ ƒZ%‡  Z&S )GÚPyreNc                s’   t t| ƒj||Ž |jdƒ}|dkr,tjƒ }|| _d| _|| _d| _	t
j| jƒ\| _| _t| jt| jƒ| _| jrŽ| jjdtjƒ | jj| jƒ dS )aÄ  Constructor, creates a new Zyre node. Note that until you start the
        node it is silent and invisible to other nodes on the network.
        The node name is provided to other nodes during discovery. If you
        specify NULL, Zyre generates a randomized node name from the UUID.

        Args:
            name (str): The name of the node

        Kwargs:
            ctx: PyZMQ Context, if not specified a new context will be created
        ÚctxNFzSET NAME)Úsuperr
   Ú__init__ÚgetÚzmqÚContextZ_ctxÚ_uuidÚ_nameÚverboser   Zzcreate_pipeÚinboxZ_outboxr   r   ÚactorÚsend_unicodeÚSNDMORE)ÚselfÚnamer   ÚargsÚkwargs)Ú	__class__© ú</home/yuhw/zmq/venv/lib/python3.6/site-packages/pyre/pyre.pyr      s    
zPyre.__init__c             C   s   dS )z>Determine whether the object is valid by converting to booleanTr   )r   r   r   r   Ú__bool__>   s    zPyre.__bool__c             C   s   dS )z>Determine whether the object is valid by converting to booleanTr   )r   r   r   r   Ú__nonzero__B   s    zPyre.__nonzero__c             C   s,   | j s&| jjdƒ tj| jjƒ d�| _ | j S )z<Return our node UUID string, after successful initializationÚUUID)Úbytes)r   r   r   Úuuidr!   Úrecv)r   r   r   r   r#   F   s    z	Pyre.uuidc             C   s*   | j s$| jjdƒ | jjƒ jdƒ| _ | j S )z5Return our node name, after successful initializationÚNAMEzutf-8)r   r   r   r$   Údecode)r   r   r   r   r   N   s    z	Pyre.namec             C   s*   t jdƒ | jjdtjƒ | jj|ƒ d S )NzADEPRECATED: set name in constructor, this method will be removed!zSET NAME)ÚloggerÚwarningr   r   r   r   )r   r   r   r   r   Úset_nameV   s    
zPyre.set_namec             C   s4   | j jdtjd� | j j|tjd� | j j|ƒ dS )zkSet node header; these are provided to other nodes during discovery
        and come in each ENTER message.z
SET HEADER)ÚflagsN)r   r   r   r   )r   ÚkeyÚvaluer   r   r   Ú
set_header[   s    zPyre.set_headerc             C   s   | j jdƒ dS )z]Set verbose mode; this tells the node to log all traffic as well as
        all major events.zSET VERBOSEN)r   r   )r   r   r   r   Úset_verboseb   s    zPyre.set_verbosec             C   s    | j jdtjƒ | j j|ƒ dS )zÜSet UDP beacon discovery port; defaults to 5670, this call overrides
        that so you can create independent clusters on the same network, for
        e.g. development vs. production. Has no effect after zyre_start().zSET PORTN)r   r   r   r   Úsend)r   Zport_nbrr   r   r   Úset_portg   s    zPyre.set_portc             C   s    | j jdtjƒ | j j|ƒ dS )zˆSet UDP beacon discovery interval, in milliseconds. Default is instant
        beacon exploration followed by pinging every 1,000 msecs.zSET INTERVALN)r   r   r   r   )r   Úintervalr   r   r   Úset_intervaln   s    zPyre.set_intervalc             C   s   t jdƒ dS )zâSet network interface for UDP beacons. If you do not set this, CZMQ will
        choose an interface for you. On boxes with several interfaces you should
        specify which one you want to use, or strange things can happen.zset_interface not implementedN)ÚloggingÚdebug)r   r,   r   r   r   Úset_interfacet   s    zPyre.set_interfacec             G   s    | j jdtjƒ | j j|ƒ dS )aM  By default, Zyre binds to an ephemeral TCP port and broadcasts the local
        host name using UDP beaconing. When you call this method, Zyre will use
        gossip discovery instead of UDP beaconing. You MUST set-up the gossip
        service separately using zyre_gossip_bind() and _connect(). Note that the
        endpoint MUST be valid for both bind and connect operations. You can use
        inproc://, ipc://, or tcp:// transports (for tcp://, use an IP address
        that is meaningful to remote as well as local nodes). Returns 0 if
        the bind was successful, else -1.zSET ENDPOINTN)r   r   r   r   )r   Úformatr   r   r   r   Úset_endpoint{   s    	zPyre.set_endpointc             C   s   | j jdƒ | j jƒ jƒ  dS )z¯Start node, after setting header values. When you start a node it
        begins discovery and connection. Returns 0 if OK, -1 if it wasn't
        possible to start the node.ZSTARTN)r   r   ÚresolveÚwait)r   r   r   r   Ústart‹   s    z
Pyre.startc             C   s.   | j jdtjd� | j jƒ jƒ  | j jƒ  dS )z£Stop node; this signals to other peers that this node will go away.
        This is polite; however you can also just destroy the node without
        stopping it.ÚSTOP)r*   N)r   r   r   ZDONTWAITr8   r9   Zdestroy)r   r   r   r   Ústop“   s    z	Pyre.stopc             C   s
   | j jƒ S )z�Receive next message from network; the message may be a control
        message (ENTER, EXIT, JOIN, LEAVE) or data (WHISPER, SHOUT).
        )r   Zrecv_multipart)r   r   r   r   r$   �   s    z	Pyre.recvc             C   s"   | j jdtjd� | j j|ƒ dS )zˆJoin a named group; after joining a group you can send messages to
        the group and all Zyre nodes in that group will receive them.ZJOIN)r*   N)r   r   r   r   )r   Úgroupr   r   r   Újoin£   s    z	Pyre.joinc             C   s"   | j jdtjd� | j j|ƒ dS )zLeave a groupZLEAVE)r*   N)r   r   r   r   )r   r=   r   r   r   Úleave©   s    z
Pyre.leavec             C   sN   | j jdtjd� | j j|jtjd� t|tƒr>| j j|ƒ n| j j|ƒ dS )z^Send message to single peer, specified as a UUID string
        Destroys message after sendingÚWHISPER)r*   N)	r   r   r   r   r/   r"   Ú
isinstanceÚlistÚsend_multipart)r   ÚpeerÚmsg_pr   r   r   Úwhisper¯   s
    
zPyre.whisperc             C   sL   | j jdtjd� | j j|tjd� t|tƒr<| j j|ƒ n| j j|ƒ dS )zDSend message to a named group
        Destroys message after sendingÚSHOUT)r*   N)r   r   r   r   rA   rB   rC   r/   )r   r=   rE   r   r   r   Úshout¹   s
    
z
Pyre.shoutc             G   s6   | j jdtjd� | j j|jtjd� | j j|ƒ dS )z?Send formatted string to a single peer specified as UUID stringr@   )r*   N)r   r   r   r   r/   r"   )r   rD   r6   r   r   r   r   ÚwhispersÄ   s    zPyre.whispersc             G   s4   | j jdtjd� | j j|tjd� | j j|ƒ dS )z&Send formatted string to a named grouprG   )r*   N)r   r   r   r   )r   r=   r6   r   r   r   r   ÚshoutsÊ   s    zPyre.shoutsc             C   s   | j jdƒ | j jƒ }|S )z Return list of current peer ids.ZPEERS)r   r   Ú
recv_pyobj)r   Úpeersr   r   r   rL   Ð   s    
z
Pyre.peersc             C   s,   | j jdtjd� | j j|ƒ | j jƒ }|S )z Return list of current peer ids.zPEERS BY GROUP)r*   )r   r   r   r   rK   )r   r=   Úpeers_by_groupr   r   r   rM   Ö   s    
zPyre.peers_by_groupc             C   s   | j jdƒ | j jƒ }|S )zReturn own endpointZENDPOINT)r   r   Úrecv_unicode)r   Úendpointr   r   r   rO   Ý   s    
zPyre.endpointc             c   s*   x$| j ƒ jtjƒtj@ r$t| ƒV  qW dS )z(Iterator that yields recent `PyreEvent`sN)Úsocketr   r   ZEVENTSÚPOLLINr	   )r   r   r   r   Úrecent_eventsã   s    zPyre.recent_eventsc             c   s   xt | ƒV  qW dS )z.Iterator that yields `PyreEvent`s indefinitelyN)r	   )r   r   r   r   Úeventsè   s    zPyre.eventsc             C   s6   t jdƒ | jjdtjƒ | jj|jƒ | jjƒ }|S )Nz.get_peer_name() is deprecated, will be removedz	PEER NAME)	r'   r(   r   r   r   r   r/   r"   rN   )r   rD   r   r   r   r   Úget_peer_nameñ   s
    

zPyre.get_peer_namec             C   s,   | j jdtjƒ | j j|jƒ | j jƒ }|S )z(Return the endpoint of a connected peer.zPEER ENDPOINT)r   r   r   r   r/   r"   rN   )r   rD   Zadrr   r   r   Úpeer_addressø   s    
zPyre.peer_addressc             C   s<   | j jdtjƒ | j j|jtjƒ | j j|ƒ | j jƒ }|S )zcReturn the value of a header of a conected peer.
        Returns null if peer or key doesn't exist.zPEER HEADER)r   r   r   r   r/   r"   rN   )r   rD   r   r,   r   r   r   Úpeer_header_valueÿ   s
    
zPyre.peer_header_valuec             C   s,   | j jdtjƒ | j j|jƒ | j jƒ }|S )zcReturn the value of a header of a conected peer.
        Returns null if peer or key doesn't exist.zPEER HEADERS)r   r   r   r   r/   r"   rK   )r   rD   Úheadersr   r   r   Úpeer_headers  s    
zPyre.peer_headersc             C   s   | j jdƒ | j jƒ }|S )z'Return list of currently joined groups.z
OWN GROUPS)r   r   rK   )r   Úgroupsr   r   r   Ú
own_groups  s    
zPyre.own_groupsc             C   s   | j jdƒ | j jƒ }|S )z4Return list of groups known through connected peers.zPEER GROUPS)r   r   rK   )r   rY   r   r   r   Úpeer_groups  s    
zPyre.peer_groupsc             C   s   | j S )z7Return socket for talking to the Zyre node, for polling)r   )r   r   r   r   rP     s    zPyre.socketc               C   s   t S )N)r   r   r   r   r   Úversion!  s    zPyre.version)NN)'Ú__name__Ú
__module__Ú__qualname__r   r   r    r#   r   r)   r-   r.   r0   r2   r5   r7   r:   r<   r$   r>   r?   rF   rH   rI   rJ   rL   rM   rO   rR   rS   rT   rU   rV   rX   rZ   r[   rP   Ústaticmethodr\   Ú__classcell__r   r   )r   r   r
      sD   "

		r
   c             C   sþ   t | d�}|jdƒ |jƒ  tjƒ }|j|tjƒ |j|jƒ tjƒ x¬t|j	ƒ ƒ}||krˆ|j
ƒ }|dkrlP tjdj|ƒƒ |jd|ƒ |jƒ |krFt|ƒ}tjdj|jƒƒ tjdj|jƒƒ |jdkrÜtjdj|jƒƒ tjd	j|jƒƒ qFW |jƒ  d S )
N)r   ZCHATz$TERMzCHAT_TASK: {0}zNODE_MSG TYPE: {0}zNODE_MSG PEER: {0}rG   zNODE_MSG GROUP: {0}zNODE_MSG CONT: {0})r
   r>   r:   r   ZPollerÚregisterrQ   rP   ÚdictÚpollr$   r'   r4   r6   rH   r	   ÚtypeZ	peer_uuidr=   Úmsgr<   )r   ÚpipeÚnZpollerÚitemsÚmessageÚeventr   r   r   Ú	chat_task%  s,    


rl   Ú__main__z$TERMZExiting)(r   ÚtimeÚstructrP   r#   r3   ÚsysÚ r   r   r   Zzactorr   Zzsocketr   Z	pyre_noder   Z
pyre_eventr	   Ú	getLoggerr]   r'   Z	raw_inputÚ	NameErrorÚinputÚobjectr
   rl   ÚbasicConfigÚsetLevelÚDEBUGr   r   Zzthread_forkZ	chat_piperf   Zsend_stringÚKeyboardInterruptÚ
SystemExitr4   r   r   r   r   Ú<module>   sF   

  

