3
Õ@q^†  ã               @   sH   d Z ddlZddlZddlmZ ddlmZmZmZ G dd„ de	ƒZ
dS )a%  Majordomo Protocol Client API, Python version.

Implements the MDP/Worker spec at http:#rfc.zeromq.org/spec:7.

Author: Min RK <benjaminrk@gmail.com>
Based on Java example by Arkadiusz Orzechowski

Originally from Zguide examples, generalized to use CLIENT 
or DEALER by brett.viren@gmail.com
é    Né   )ÚMDP)ÚdumpÚclientish_recvÚclientish_sendc               @   sP   e Zd ZdZdZdZdZdZdZdZ	e
jdfdd„Zdd„ Zd	d
„ Zdd„ ZdS )ÚClientzyMajordomo Protocol Client API, Python version.

      Implements the MDP/Worker spec at http:#rfc.zeromq.org/spec:7.
    NiÄ	  Fc             C   sD   || _ || _|| _tjƒ | _tjƒ | _tj	ddtj
d� | jƒ  d S )Nz%(asctime)s %(message)sz%Y-%m-%d %H:%M:%S)ÚformatÚdatefmtÚlevel)ÚbrokerÚsocket_typeÚverboseÚzmqÚContextÚctxÚPollerÚpollerÚloggingÚbasicConfigÚINFOÚreconnect_to_broker)Úselfr   r   r   © r   ú,/home/yuhw/zmq/zio/python/zio/domo/client.pyÚ__init__   s    


zClient.__init__c             C   sn   | j r| jj| j ƒ | j jƒ  | jj| jƒ| _ d| j _| j j| j	ƒ | jj
| j tjƒ | jrjtjd| j	ƒ dS )zConnect or reconnect to brokerr   z I: connecting to broker at %s...N)Úclientr   Ú
unregisterÚcloser   Úsocketr   ÚlingerÚconnectr   Úregisterr   ÚPOLLINr   r   Úinfo)r   r   r   r   r   +   s    
zClient.reconnect_to_brokerc             C   sH   t |tƒs|g}tj|g| }| jr8tjd|ƒ t|ƒ t| j	|ƒ dS )zSend request to broker
        z!I: send request to '%s' service: N)
Ú
isinstanceÚlistr   ÚC_CLIENTr   r   Úwarnr   r   r   )r   ÚserviceÚrequestr   r   r   Úsend7   s    
zClient.sendc             C   s’   y| j j| jƒ}W n tk
r&   dS X |r„t| jƒ}| jrNtjdƒ t	|ƒ t
|ƒdks^t‚|jdƒ}tj|ksvt‚|jdƒ}|S tjdƒ dS )z8Returns the reply message or None if there was no reply.NzI: received reply:é   r   z&W: permanent error, abandoning request)r   ÚpollÚtimeoutÚKeyboardInterruptr   r   r   r   r#   r   ÚlenÚAssertionErrorÚpopr   r&   r'   )r   ÚitemsÚmsgÚheaderr(   r   r   r   ÚrecvH   s    



zClient.recv)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r-   r   r   ÚROUTERr   r   r*   r5   r   r   r   r   r      s   r   )r9   r   r   Ú r   Zzhelpersr   r   r   Úobjectr   r   r   r   r   Ú<module>
   s
   