ó
½-'Nc           @   sø   d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d l m	 Z	 d d l
 m Z d d l m Z d	 e f d
     YZ d e f d     YZ d e f d     YZ d S(   s   
Implementation of a lightweight exchange-triggering mechanism via
small HTTP requests asking if we should do a full exchange.
i’’’’N(   t   info(   t   Failure(   t   defer(   t   loads(   t   fetch(   t   log_failuret
   PingClientc           B   s,   e  Z d  Z d d  Z d   Z d   Z RS(   sB   An HTTP client which asks: Are there messages for computer X?
    c         C   s=   | d k r t } n  | |  _ | |  _ | |  _ | |  _ d S(   s°   
        @param url: The URL to ask the question to.
        @type identity: L{landscape.broker.registration.Identity}
        @param identity: This client's identity.
        N(   t   NoneR   t   _reactort	   _identityt   get_paget   url(   t   selft   reactorR   t   identityR
   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   __init__   s    				c            s«   |  j  j } | d k	 r i d d 6} t j i | d 6 } t j       f d   } |  j j   j	 | |  j
 |  j d t d | d |   j |  j    St j t  S(	   sā   Ask the question.

        Hits the URL previously specified with the insecure_id gotten
        from the identity.

        @return: A deferred resulting in True if there are messages
            and False otherwise.
        s!   application/x-www-form-urlencodeds   Content-Typet   insecure_idc            s     j  t | |  |   d  S(   N(   t   errbackR   (   t   typet   valuet   tb(   t   page_deferred(    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR   1   s    t   postt   datat   headersN(   R	   R   R   t   urllibt	   urlencodeR   t   DeferredR   t   call_in_threadt   callbackR
   R   t   Truet   addCallbackt   _got_resultt   succeedt   False(   R   R   R   R   R   (    (   R   s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   ping"   s    	c         C   s!   t  |  i t d 6k r t Sd S(   sŗ   
        Given a response that came from a ping server, return True if
        the response indicates that their are messages waiting for
        this computer, False otherwise.
        t   messagesN(   R   R   (   R   t   webtext(    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR    :   s    N(   t   __name__t
   __module__t   __doc__R   R   R#   R    (    (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR      s   	t   Pingerc           B   se   e  Z d  Z d e d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z RS(   sy   
    A plugin which pings the Landscape server with HTTP requests to
    see if a full exchange should be initiated.
    i   c         C   s_   | |  _  | |  _ | |  _ | |  _ | |  _ d |  _ d |  _ | |  _ | j	 d |  j
  d S(   s  
        @param reactor: The reactor to schedule calls with
        @param url: The URL to ping
        @param interval: How often to send the pings
        @param exchanger: The L{landscape.broker.exchange.MessageExchange} to
            trigger exchanges with.
        t   messageN(   t   _urlt	   _intervalR	   R   t
   _exchangerR   t   _call_idt   _ping_clientt   ping_client_factoryt   call_ont   _handle_set_intervals(   R   R   R   R   t	   exchangert   intervalR0   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR   J   s    									c         C   s   |  j  S(   N(   R+   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   get_url]   s    c         C   s+   | |  _  |  j d  k	 r' | |  j _ n  d  S(   N(   R+   R/   R   R   (   R   R   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   set_url`   s    	c         C   s   |  j  S(   N(   R,   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   get_intervale   s    c         C   sC   |  j  |  j |  j |  j  |  _ |  j j |  j |  j  |  _ d S(   s   Start pinging.N(	   R0   R   R+   R	   R/   t
   call_everyR,   R#   R.   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   starth   s    c         C   s3   |  j  j   } | j |  j  | j |  j  d S(   s8   Perform a ping; if there are messages, fire an exchange.N(   R/   R#   R   R    t
   addErrbackt
   _got_error(   R   t   ping_deferred(    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR#   n   s    c         C   s*   | r& t  d  |  j j d t  n  d  S(   Ns@   Ping indicates message available. Scheduling an urgent exchange.t   urgent(   R    R-   t   schedule_exchangeR   (   R   t   exchange(    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR    t   s    
c         C   s   t  | d |  j f  d  S(   Ns"   Error contacting ping server at %s(   R   R+   (   R   t   failure(    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR;   z   s    c         C   s   | d d k r= d | k r= | d |  _  t d |  j   n  |  j d  k	 r |  j j |  j  |  j j |  j  |  j  |  _ n  d  S(   NR   s   set-intervalsR#   s    Ping interval set to %d seconds.(   R,   R    R.   R   R   t   cancel_callR8   R#   (   R   R*   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR2   ~   s    (   R&   R'   R(   R   R   R5   R6   R7   R9   R#   R    R;   R2   (    (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR)   D   s   							t
   FakePingerc           B   s   e  Z d    Z d   Z RS(   c         O   s   d  S(   N(    (   R   t   argst   kwargs(    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR      s    c         C   s   d  S(   N(    (   R   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR9      s    (   R&   R'   R   R9   (    (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyRB      s   	(   R(   R   t   loggingR    t   twisted.python.failureR   t   twisted.internetR   t   landscape.lib.bpickleR   t   landscape.lib.fetchR   t   landscape.lib.logR   t   objectR   R)   RB   (    (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   <module>   s   3C