3
Õ@q^!  ã               @   sX   d Z ddlmZ ddlT ddlZejdƒZddlmZ G d	d
„ d
eƒZ	G dd„ dƒZ
dS )za
zio.flow implements ZIO flow protocol helper

This is equivalent to the C++ zio::flow namespace
é   )ÚMessageé   )Ú*é    NÚzio)ÚEnumc               @   s   e Zd ZdZdZdZdS )Ú	Directionr   r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú	undefinedÚextractÚinject© r   r   ú+/home/yuhw/zmq/zio/python/zio/flow/proto.pyr      s   r   c               @   s~   e Zd ZdZdZdZdZdZdZdZ	dd„ Z
dd„ Zdd	d
„Zddd„Zdd„ Zdd„ Zddd„Zeƒ fdd„Zddd„ZdS )ÚFlowz¶
    A Flow object provides ZIO flow protocol API

    It is equivalent to the C++ zio::flow::Flow class

    All timeouts are in milliseconds.  A timeout of None means forever.
    r   Tr   c             C   s
   || _ dS )z|
        Construct a flow object on a port.

        Application shall handle ports bind/connect and online states.
        N)Úport)Úselfr   r   r   r   Ú__init__%   s    zFlow.__init__c             C   sh   | j d	krtdƒ‚d|_t|ƒ}td
|Ž|_| j|_d |_| _ tj	d| j › d|› �ƒ | j
j|ƒ dS )z~
        Send a BOT message to the other end.

        Client calls send_bot() first, server calls send_bot() second.
        r   zBOT must be sent firstÚFLOWÚBOTr   z
send_bot: ú Néÿÿÿÿ)r   )Ú
send_seqnoÚRuntimeErrorÚformÚ	objectifyÚ	stringifyÚlabelÚ
routing_idÚseqnoÚlogÚdebugr   Úsend)r   ÚmsgÚfobjr   r   r   Úsend_bot-   s    
zFlow.send_botc             C   sþ   | j dkrtdƒ‚| jj|ƒ}|dkr.tdƒ‚|jdkrHtd|j› �ƒ‚|jdkrZtdƒ‚t|ƒ}|j	d	dƒ}|d
kr„td|› �ƒ‚|j	ddƒ}|dkr t
dƒ‚|j	ddƒ}|dkrÂd| _|| _n$|dkrØd| _d| _nt
d|› �ƒ‚|| _d| _ |j| _|S )z�
        Receive and return BOT message or None.

        Returns None if EOT was received.  

        Raises exceptions
        r   zBOT must be recv firstNzflow.recv_bot: timeoutr   zflow.recv_bot: BOT not seqno 0 r   zflow.recv_bot: not FLOW messageÚflowr   z$flow.recv_bot: unknown FLOW message Úcreditzflow.recv_bot: no creditÚ	directionr   Fr   Tz!flow.recv_bot: uknonwn direction r   )Ú
recv_seqnor   r   ÚrecvÚTimeoutErrorr    r   Ú	TypeErrorr   ÚgetÚ
ValueErrorÚ	is_senderr(   Útotal_creditr   )r   Útimeoutr$   r%   Úftyper(   Zfdirr   r   r   Úrecv_bot?   s8    


zFlow.recv_botNc             C   sÊ   | j dk rtdƒ‚| jj|ƒ}|dkr*dS |j| j  dkr>dS |jdkrLdS t|ƒ}|jddƒdkrxtj	d|f ƒ dS y|d	 }W n$ t
k
r¨   tj	d
|f ƒ dS X |  j|7  _|  j d7  _ |S )a_  
        Receive any waiting PAY messages

        The flow object will slurp prior to a sending a DAT but the
        application may call this at any time after BOT.  Number of
        credits slurped is returned.  None is returned if other than a
        PAY is received.  Caller should likely respond to that with
        send_eot(msg,0).
        r   zmust recv BOT before PAYNr   r   r'   ÚPAYzmalformed PAY flow: %sr(   zmalformed PAY credit: %s)r*   r   r   r+   r    r   r   r.   r!   r"   ÚKeyErrorr(   )r   r2   r$   r%   r(   r   r   r   Ú	slurp_payi   s*    


zFlow.slurp_payc             C   s”   | j | jk r| jdƒ x| j dkr.| jdƒ qW d|_tdt|ƒŽ|_| j|_| jdk r`t	dƒ‚|  jd7  _| j|_
| jj|ƒ |  j d8  _ dS )	z•
        Send a DAT message and slurp for any waiting PAY.

        Return True if sent, None if an EOT was received instead of
        PAY.
        r   r   r   ÚDATzmust send BOT before DATTr   )r8   )r(   r1   r7   r   r   r   r   r   r   r   r    r   r#   )r   r$   r   r   r   Úput‹   s    

zFlow.putc             C   s†   | j dkrdS | j }d| _ tdtd|d�d�}|  jd7  _| j|_| j|_tjd| j› d|› �ƒ | jdk rvtd	ƒ‚| j	j
|ƒ |S )
zÓ
        Send any accumulated credit as PAY.

        This is called in a get() but app may call any time after a
        BOT exchange.  Number of credits sent is returned.  This does
        not block.
        r   r   r5   )r(   )r   r   r   zflush_pay: r   zmust recv BOT before PAY)r(   r   r   r   r    r   r!   r"   r   r   r#   )r   Znsentr$   r   r   r   Ú	flush_pay¤   s    

zFlow.flush_payc             C   s4  | j dk rtdƒ‚tjd|› d�ƒ | jƒ  | jj|ƒ}|dkrRtjdƒ tdƒ‚|j| j  dkrŠtjd|j› d	| j › d
|› �ƒ t	dƒ‚|j
dkr¬tjd|› �ƒ tdƒ‚t|ƒ}|jddƒdkrØtjd|f ƒ dS |jddƒ}|dk�rtjd|f ƒ td|› �ƒ‚|  jd7  _|  j d7  _ | jƒ  |S )z¥
        Receive and return a DAT message and send any accumulated PAY.

        Return None if EOT was received instead of DAT.

        Exceptions raised.
        r   zmust recv BOT before DATz	flow.get(ú)Nzflow.get timeoutr   zflow.get bad seqno: z, last Ú
zflow.get bad seqnor   zflow.get not FLOW message
zflow.get not FLOW messager'   ÚEOTzEOT during flow:
%sr8   zmalformed DAT flow:
%szflow.get unexpected FLOW type )r*   r   r!   r"   r:   r   r+   r,   r    r/   r   r-   r   r.   Úwarningr(   )r   r2   r$   r%   r3   r   r   r   r.   »   s4    

 

zFlow.getc             C   st   d|_ td	t|ƒŽ|_| j|_| jdk r0tdƒ‚|  jd7  _| j|_tj	d| j› d|› �ƒ | j
j|ƒ d
| _dS )zá
        Send EOT message to other end.

        Note, if app initiates the EOT, it should then call
        recv_eot().  If it unexpectedly got EOT when recving another
        then it should send EOT as a response.
        r   r=   r   zmust send BOT before EOTr   z
send_eot: r   N)r=   r   )r   r   r   r   r   r   r   r    r!   r"   r   r#   )r   r$   r   r   r   Úsend_eotà   s    
zFlow.send_eotc             C   s„   x~| j dk rtdƒ‚| jj|ƒ}|dkr,dS |j| j  dkr>q|jdkrJq|  j d7  _ t|ƒ}|jddƒdkrd| _ |S qqW dS )	an  
        Recv an EOT message.

        EOT message is returned or None if timeout occurs.

        If app explicitly calls send_eot() it should call recv_eot()
        to wait for the ack from the other end.  If an app receives
        EOT as an unepxected message while receiving PAY or DAT then
        it should send_eot() but not expect another EOT ack.
        r   zmust recv BOT before EOTNr   r   r'   r=   r   )r*   r   r   r+   r    r   r   r.   )r   r2   r$   r%   r   r   r   Úrecv_eotô   s"    

zFlow.recv_eotr   r   r   )r   )N)N)N)r	   r
   r   Ú__doc__r(   r1   r0   r   r   r*   r   r&   r4   r7   r9   r:   r.   r   r?   r@   r   r   r   r   r      s"   
*
"
%
 r   )rA   Úmessager   ÚutilÚloggingÚ	getLoggerr!   Úenumr   r   r   r   r   r   r   Ú<module>   s   
