ó
Ì	g]c           @   sW   d  Z  d d l Z d d l Z d d l m Z e j e ƒ Z d e f d „  ƒ  YZ	 d S(   s   Handle AMQP HeartbeatsiÿÿÿÿN(   t   framet   HeartbeatCheckerc           B   sª   e  Z d  Z d Z d „  Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z e d
 „  ƒ Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z RS(   så   Sends heartbeats to the broker. The provided timeout is used to
    determine if the connection is stale - no received heartbeats or
    other activity will close the connection. See the parameter list for more
    details.

    s@   No activity or too many missed heartbeats in the last %i secondsc         C   sÊ   | d k  r" t  d | f ƒ ‚ n  | |  _ | |  _ t | ƒ d |  _ | d |  _ t j d |  j |  j |  j ƒ d |  _ d |  _	 d |  _
 d |  _ d |  _ d |  _ d |  _ |  j ƒ  |  j ƒ  d S(   s©  Create an object that will check for activity on the provided
        connection as well as receive heartbeat frames from the broker. The
        timeout parameter defines a window within which this activity must
        happen. If not, the connection is considered dead and closed.

        The value passed for timeout is also used to calculate an interval
        at which a heartbeat frame is sent to the broker. The interval is
        equal to the timeout value divided by two.

        :param pika.connection.Connection: Connection object
        :param int timeout: Connection idle timeout. If no activity occurs on the
                            connection nor heartbeat frames received during the
                            timeout window the connection will be closed. The
                            interval used to send heartbeats is calculated from
                            this value by dividing it by two.

        i   s   timeout must >= 0, but got %ri   i   s0   timeout: %f send_interval: %f check_interval: %fi    N(   t
   ValueErrort   _connectiont   _timeoutt   floatt   _send_intervalt   _check_intervalt   LOGGERt   debugt   _bytes_receivedt   _bytes_sentt   _heartbeat_frames_receivedt   _heartbeat_frames_sentt   _idle_byte_intervalst   Nonet   _send_timert   _check_timert   _start_send_timert   _start_check_timer(   t   selft
   connectiont   timeout(    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyt   __init__   s"    										
c         C   s
   |  j  j S(   sa   Return the number of bytes received by the connection bytes object.

        :rtype int

        (   R   t   bytes_received(   R   (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyt   bytes_received_on_connectionR   s    c         C   s   |  j  d k S(   ss   Returns true if the byte count hasn't changed in enough intervals
        to trip the max idle threshold.

        i    (   R   (   R   (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyt   connection_is_idle[   s    c         C   s    t  j d ƒ |  j d 7_ d S(   s#   Called when a heartbeat is receiveds   Received heartbeat framei   N(   R   R	   R   (   R   (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyt   receivedc   s    c         C   s%   t  j d ƒ |  j ƒ  |  j ƒ  d S(   sA   Invoked by a timer to send a heartbeat when we need to.

        s   Sending heartbeat frameN(   R   R	   t   _send_heartbeat_frameR   (   R   (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyt   _send_heartbeath   s    
c         C   sh   |  j  r d |  _ n |  j d 7_ t j d |  j |  j |  j ƒ |  j rZ |  j ƒ  d S|  j ƒ  d S(   s³   Invoked by a timer to check for broker heartbeats. Checks to see
        if we've missed any heartbeats and disconnect our connection if it's
        been idle too long.

        i    i   s8   Received %i heartbeat frames, sent %i, idle intervals %iN(	   t   _has_received_dataR   R   R	   R   R   R   t   _close_connectionR   (   R   (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyt   _check_heartbeatp   s    		
c         C   sn   |  j  r5 t j d ƒ |  j j |  j  ƒ d |  _  n  |  j rj t j d ƒ |  j j |  j ƒ d |  _ n  d S(   s   Stop the heartbeat checkers/   Removing timer for next heartbeat send intervals0   Removing timer for next heartbeat check intervalN(   R   R   R	   R   t   _adapter_remove_timeoutR   R   (   R   (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyt   stop‡   s    		c         C   sC   t  j d |  j ƒ t j |  j } |  j j t j	 j
 | ƒ ƒ d S(   s;   Close the connection with the AMQP Connection-Forced value.s+   Connection is idle, %i stale byte intervalsN(   R   t   infoR   R   t   _STALE_CONNECTIONR   R   t   _terminate_streamt   pikat
   exceptionst   AMQPHeartbeatTimeout(   R   t   text(    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyR   ’   s
    	
	c         C   s   |  j  |  j k S(   sQ   Returns True if the connection has received data.

        :rtype: bool

        (   R
   R   (   R   (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyR   ž   s    c           C   s
   t  j ƒ  S(   sL   Return a new heartbeat frame.

        :rtype pika.frame.Heartbeat

        (   R    t	   Heartbeat(    (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyt   _new_heartbeat_frame§   s    c         C   s6   t  j d ƒ |  j j |  j ƒ  ƒ |  j d 7_ d S(   s3   Send a heartbeat frame on the connection.

        s   Sending heartbeat framei   N(   R   R	   R   t   _send_frameR+   R   (   R   (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyR   °   s    	c         C   s"   |  j  j |  j |  j ƒ |  _ d S(   s!   Start a new heartbeat send timer.N(   R   t   _adapter_call_laterR   R   R   (   R   (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyR   ¹   s    	c         C   s,   |  j  ƒ  |  j j |  j |  j ƒ |  _ d S(   s"   Start a new heartbeat check timer.N(   t   _update_countersR   R-   R   R    R   (   R   (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyR   ¿   s    
	c         C   s"   |  j  j |  _ |  j  j |  _ d S(   sl   Update the internal counters for bytes sent and received and the
        number of frames received

        N(   R   t
   bytes_sentR   R   R
   (   R   (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyR.   Ë   s    (   t   __name__t
   __module__t   __doc__R$   R   t   propertyR   R   R   R   R    R"   R   R   t   staticmethodR+   R   R   R   R.   (    (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyR   
   s    	?												(
   R2   t   loggingt   pika.exceptionsR&   R    t	   getLoggerR0   R   t   objectR   (    (    (    s3   /srv/kernel/kteam-tools/dashboard/pika/heartbeat.pyt   <module>   s
   