3
Õ@q^á  ã               @   s\   d Z ddlZddlmZ ddlZddlZddlmZmZ ddl	Z	e	j
dƒZG dd„ dƒZdS )	z*
ZIO brokers bring together ZIO clients.

é    N)Ú
namedtupleé   )Ú	objectifyÚswitch_directionÚzioc               @   s&   e Zd Zdd„ Zddd„Zdd„ ZdS )	ÚBrokerc             C   s    || _ || _tƒ | _tƒ | _dS )a\  Create a flow broker.

        Parameters
        ----------
        server : zio.Port
            An online SERVER port
        factory : callable
            See below
        
        This broker routes messages between a pair of clients: a
        remote client and its handler.  It does this by adding a flow
        initiation protocol to ZIO flow protocol.

        On receipt of a BOT, the broker passes it to the factory which
        should return True if the BOT was accepted else None.  The
        factory call should return as promptly as possible as the
        broker blocks.

        If factory returns True then it is expected that the factory
        has started some other client which contacs the broker with
        the BOT.  The factory or the new client may modify the BOT as
        per flow protorocl but must leave intact the `cid` attribute
        placed in the flow object by the broker.

        Note that the flow handler protocol does not communicate the
        location of the broker's SERVER socket.  It is up to handler
        implementations to locate the the server.

        N)ÚserverÚfactoryÚdictÚotherÚsetÚhandlers)Úselfr   r	   © r   ú,/home/yuhw/zmq/zio/python/zio/flow/broker.pyÚ__init__   s    zBroker.__init__Nc       
      C   s<  | j j|ƒ}|s.tjd|› �ƒ td|› �ƒ‚tjd|› �ƒ |j}| jj|dƒ}|r´|| j	kr€tjd|› d|› d|› d�ƒ ntjd|› d|› d|› d�ƒ ||_| j j
|ƒ dS t|ƒ}|jd	dƒ}|sÔtd
ƒ‚|dkrätdƒ‚|jddƒ}|�rº| j	j|ƒ || j|< || j|< |d= tjd|› d|› �ƒ tj|ƒ}t|ƒ}tj|ƒ|_||_tjd|› d|› d|› d�ƒ | j j
|ƒ ||_||_tjd|› d|› d|› d�ƒ | j j
|ƒ dS tjd|› �ƒ t|ƒ}||d< tj|ƒ|_d|_| j|ƒ}	|	�r dS d|d	< tj|ƒ|_||_| j j
|ƒ tdt|jƒ ƒ‚dS )z`
        Poll for at most one message from the SERVER port

        Raises exceptions.

        zbroker poll timeout with zbroker recv Nzbroker route c2h z -> z:
Ú
zbroker route h2c Úflowzmessage not type FLOWÚBOTzflow message not type BOTÚcidzbroker route from handler z <--> zbroker route from client r   ÚEOTzbroker factory rejects )r   ÚrecvÚlogÚerrorÚTimeoutErrorÚdebugÚ
routing_idr   Úgetr   Úsendr   Ú	TypeErrorÚaddÚjsonÚdumpsr   Úlabelr	   ÚRuntimeErrorÚstr)
r   ÚtimeoutÚmsgZridZoridÚfobjÚftyper   Zlabel_for_clientÚgotr   r   r   Úpoll3   sf    
 



zBroker.pollc             C   s   | j jƒ  d S )N)r	   Ústop)r   r   r   r   r,   ~   s    
zBroker.stop)N)Ú__name__Ú
__module__Ú__qualname__r   r+   r,   r   r   r   r   r      s   #
Kr   )Ú__doc__r!   Úcollectionsr   Úzmqr   Úutilr   r   ÚloggingÚ	getLoggerr   r   r   r   r   r   Ú<module>   s   
