ó
ÍÿŠTc           @   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(   sR  
Implementation of a lightweight exchange-triggering mechanism via
small HTTP requests asking if we should do a full exchange.

Ping Sequence
=============

Diagram::

  1. BrokerService --> Pinger              :  Start

  2. [Loop forever]
  |
  |  2.1 Pinger     --> PingClient         :  Schedule Ping
  |
  |  2.2 PingClient --> {Server} WebPing   :  Ping
  |
  |  2.3 PingClient <-- {Server} WebPing   :  return(messages waiting?
  |                                        :    [Boolean])
  |
  |  2.4 Pinger     <-- PingClient         :  return(messages waiting?
  |                                             [Boolean])
  |
  |  2.5 [If: messages waiting == True ]
  |    |
  |    |  2.5.1 Pinger --> MessageExchange  :  Schedule urgent exchange
  |    |
  |    --[End If]
  |
  |  2.6 [Wait: for ping interval to expire]
  |
  --[End Loop]

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(   s:   An HTTP client which knows how to talk to the ping server.c         C   s+   | d  k r t } n  | |  _ | |  _ d  S(   N(   t   NoneR   t   _reactort   get_page(   t   selft   reactorR	   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   __init__2   s    		c            sœ   | d k	 r i d d 6} t j i | d 6ƒ } t j ƒ  ‰  ‡  f d †  } |  j j ˆ  j | |  j | d t	 d | d | ƒˆ  j
 |  j ƒ ˆ  St j t ƒ S(	   sr  Ask the question: are there messages for this computer ID?

        @param url: The URL of the ping server to hit.
        @param insecure_id: This client's insecure ID, if C{None} no HTTP
            request will be performed and the result will be C{False}.

        @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   G   s    t   postt   datat   headersN(   R   t   urllibt	   urlencodeR   t   DeferredR   t   call_in_threadt   callbackR	   t   Truet   addCallbackt   _got_resultt   succeedt   False(   R
   t   urlR   R   R   R   (    (   R   s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   ping8   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   Q   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   sk   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z RS(   s‰  
    A plugin which pings the Landscape server with HTTP requests to
    see if a full exchange should be initiated.

    @param reactor: The reactor to schedule calls with.
    @param identity: The L{Identity} holding the insecure ID used when pinging.
    @param exchanger: The L{MessageExchange} to trigger exchanges with.
    @param config: The L{BrokerConfiguration} to get the 'ping_url' and
        'ping_interval' parameters from. The 'ping_url' specifies what URL
        to hit when pinging, and 'ping_interval' how frequently to ping.
        Changes in the configuration object will take effect from the next
        scheduled ping.
    c         C   sV   | |  _  | |  _ | |  _ | |  _ d  |  _ d  |  _ | |  _ | j d |  j	 ƒ d  S(   Nt   message(
   t   _configt	   _identityR   t
   _exchangerR   t   _call_idt   _ping_clientt   ping_client_factoryt   call_ont   _handle_set_intervals(   R
   R   t   identityt	   exchangert   configR.   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR   j   s    							c         C   s
   |  j  j S(   N(   R)   t   ping_url(   R
   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   get_urlu   s    c         C   s
   |  j  j S(   N(   R)   t   ping_interval(   R
   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   get_intervalx   s    c         C   s#   |  j  |  j ƒ |  _ |  j ƒ  d S(   s   Start pinging.N(   R.   R   R-   t	   _schedule(   R
   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   start{   s    c            s[   ˆ  j  j ˆ  j j ˆ  j j ƒ } | j ˆ  j ƒ | j ˆ  j	 ƒ | j
 ‡  f d †  ƒ d S(   s8   Perform a ping; if there are messages, fire an exchange.c            s
   ˆ  j  ƒ  S(   N(   R8   (   t   _(   R
   (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   <lambda>†   s    N(   R-   R!   R)   R4   R*   R   R   R   t
   addErrbackt
   _got_errort   addBoth(   R
   t   deferred(    (   R
   s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR!   €   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   ˆ   s    
c         C   s   t  | d |  j j f ƒ d  S(   Ns"   Error contacting ping server at %s(   R   R-   R    (   R
   t   failure(    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR=   Ž   s    c         C   s%   |  j  j |  j j |  j ƒ |  _ d S(   s4   Schedule a new ping using the current ping interval.N(   R   t
   call_laterR)   R6   R!   R,   (   R
   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR8   “   s    c         C   sƒ   | d d k rP d | k rP | d |  j  _ |  j  j ƒ  t d |  j  j ƒ n  |  j d  k	 r |  j j |  j ƒ |  j ƒ  n  d  S(   NR   s   set-intervalsR!   s    Ping interval set to %d seconds.(	   R)   R6   t   writeR    R,   R   R   t   cancel_callR8   (   R
   R(   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR0   ˜   s    c         C   s2   |  j  d k	 r. |  j j |  j  ƒ d |  _  n  d S(   s    Stop pinging the message server.N(   R,   R   R   RF   (   R
   (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   stop¢   s    (   R$   R%   R&   R   R   R5   R7   R9   R!   R   R=   R8   R0   RG   (    (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyR'   [   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.pyRH   ©   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'   RH   (    (    (    s9   /usr/lib/python2.7/dist-packages/landscape/broker/ping.pyt   <module>"   s   ,N