ó
Ýà‹]c           @@  sÓ   d  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 d d l	 Z	 d d l	 m
 Z
 d d l m Z d d l m Z e d Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ e j e ƒ d S(   sC   Base classes to manage a Client's interaction with a running kerneli    (   t   absolute_importN(   t   Threadt   Event(   t   ZMQError(   t   protocol_version_infoi   (   t   HBChannelABCt   InvalidPortNumberc           B@  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyR      s   t	   HBChannelc           B@  sÅ   e  Z d  Z d Z d Z d Z d Z e Z	 d Z
 d Z d Z d Z d Z d d d d „ Z e e j d „  ƒ ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z RS(   sô   The heartbeat channel which monitors the kernel heartbeat.

    Note that the heartbeat channel is paused by default. As long as you start
    this channel, the kernel manager will ensure that it is paused and un-paused
    as appropriate.
    g      ð?c         C@  s©   t  t |  ƒ j ƒ  t |  _ | |  _ | |  _ t | t ƒ ro | d d k rb d } t	 | ƒ ‚ n  d | } n  | |  _
 t |  _ t ƒ  |  _ t |  _ t j ƒ  |  _ d S(   sK  Create the heartbeat monitor thread.

        Parameters
        ----------
        context : :class:`zmq.Context`
            The ZMQ context to use.
        session : :class:`session.Session`
            The session to use.
        address : zmq url
            Standard (ip, port) tuple that the kernel is listening on.
        i   i    s*   The port number for a channel cannot be 0.s   tcp://%s:%iN(   t   superR	   t   __init__t   Truet   daemont   contextt   sessiont
   isinstancet   tupleR   t   addresst   Falset   _runningR   t   _exitt   _pauset   zmqt   Pollert   poller(   t   selfR   R   R   t   message(    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyR   2   s    						c           C@  s   t  d  k	 r t t  _ n  d  S(   N(   R	   t   NoneR   t   _exiting(    (    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyt   _notice_exitQ   s    c         C@  s†   |  j  d  k	 r2 |  j j |  j  ƒ |  j  j ƒ  n  |  j j  t j ƒ |  _  d |  j  _ |  j  j	 |  j
 ƒ |  j j |  j  t j ƒ d  S(   Niè  (   t   socketR   R   t
   unregistert   closeR   R   t   REQt   lingert   connectR   t   registert   POLLIN(   R   (    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyt   _create_socketY   s    c         C@  sÌ   |  j  t j ƒ  | } t | d ƒ } g  } x™ t rÇ y |  j j d | ƒ } Wnq t k
 r¢ } | j t j k rœ |  j  t j ƒ  | } t | d ƒ } qÄ ‚  q/ t	 k
 rÂ |  j
 r¼ PqÄ ‚  q/ XPq/ W| S(   s  poll for heartbeat replies until we reach self.time_to_dead.

        Ignores interrupts, and returns the result of poll(), which
        will be an empty list if no messages arrived before the timeout,
        or the event tuple if there is a message to receive.
        gü©ñÒMbP?iè  (   t   time_to_deadt   timet   maxR   R   t   pollR   t   errnot   EINTRt	   ExceptionR   (   R   t
   start_timet
   until_deadt   eventst   e(    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyt   _polld   s$    		c         C@  s  |  j  ƒ  t |  _ t |  _ xë |  j r	|  j rJ |  j j |  j ƒ q n  d } |  j j	 d ƒ t
 j
 ƒ  } |  j | ƒ } | rÓ t |  _ |  j j ƒ  |  j t
 j
 ƒ  | } | d k r |  j j | ƒ q q q t |  _ t
 j
 ƒ  | } |  j | ƒ |  j  ƒ  q q Wd S(   s2   The thread's main activity.  Call start() instead.g        t   pingi    N(   R'   R   R   t   _beatingR   R   t   waitR(   R   t   sendR)   R3   t   recvR   t   call_handlers(   R   t   since_last_heartbeatt   request_timet   readyt	   remainder(    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyt   run…   s.    
					
c         C@  s   t  |  _ d S(   s   Pause the heartbeat.N(   R   R   (   R   (    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyt   pauseª   s    c         C@  s   t  |  _ d S(   s   Unpause the heartbeat.N(   R   R   (   R   (    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyt   unpause®   s    c         C@  s+   |  j  ƒ  r# |  j r# |  j r# t St Sd S(   s9   Is the heartbeat running and responsive (and not paused).N(   t   is_aliveR   R5   R   R   (   R   (    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyt
   is_beating²   s    c         C@  s.   t  |  _ |  j j ƒ  |  j ƒ  |  j ƒ  d S(   s2   Stop the channel's event loop and join its thread.N(   R   R   R   t   sett   joinR!   (   R   (    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyt   stop¹   s    	
c         C@  sJ   |  j  d  k	 rF y |  j  j d d ƒ Wn t k
 r9 n Xd  |  _  n  d  S(   NR#   i    (   R   R   R!   R.   (   R   (    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyR!   À   s    c         C@  s   d S(   si  This method is called in the ioloop thread when a message arrives.

        Subclasses should override this method to handle incoming messages.
        It is important to remember that this method is called in the thread
        so that some logic must be done to ensure that the application level
        handlers are called in the application thread.
        N(    (   R   R:   (    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyR9   È   s    N(   R   R   t   __doc__R   R   R   R   R   R   R   R(   R   R   R   R5   R   t   staticmethodt   atexitR%   R   R'   R3   R>   R?   R@   RB   RE   R!   R9   (    (    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyR	      s.   		!	%					(   RF   t
   __future__R    RH   R,   t	   threadingR   R   R)   R   R   t   jupyter_clientR   t   channelsabcR   t   major_protocol_versionR.   R   R	   R%   (    (    (    s6   lib/python2.7/site-packages/jupyter_client/channels.pyt   <module>   s   
´