ó
½-'Nc           @   s   d  Z  d d l Z d d l Z d d l m Z d d l m Z d d l m Z m	 Z	 d d l
 m Z d d l m Z m Z d e f d	 „  ƒ  YZ d
 „  Z d S(   sG   The part of the broker which deals with communications with the server.iÿÿÿÿN(   t   md5(   t   succeed(   t   got_next_expectedt   ANCIENT(   t   format_delta(   t
   SERVER_APIt
   CLIENT_APIt   MessageExchangec           B   sæ   e  Z d  Z d Z d d e j d „ Z d „  Z d „  Z e	 d „ Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z e	 e	 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sã   
    The Message Exchange is the place where messages are sent to go
    out to the Landscape server.

    The Message Exchange will accumulate messages in its message store
    and periodically deliver them to the server.
    s   message-exchangeid   c
   
      C   s  | |  _  | |  _ |	 |  _ | |  _ | |  _ | |  _ | j |  _ | j |  _	 | |  _
 d |  _ d |  _ t |  _ t |  _ t ƒ  |  _ d |  _ i  |  _ | |  _ |  j d |  j ƒ |  j d |  j ƒ |  j d |  j ƒ | j d |  j ƒ | j d |  j ƒ d S(   sú  
        @param reactor: A L{TwistedReactor} used to fire events in response
            to messages received by the server.
        @param store: A L{MessageStore} used to queue outgoing messages.
        @param transport: A L{HTTPTransport} used to deliver messages.
        @param exchange_interval: time interval between subsequent
            exchanges of non-urgent messages.
        @param urgent_exchange_interval: time interval between subsequent
            exchanges of urgent messages.
        s   accepted-typest   resynchronizes   set-intervalss   resynchronize-clientss   pre-exitN(   t   _reactort   _message_storet   _create_timet
   _transportt   _registration_infot   _configt   exchange_intervalt   _exchange_intervalt   urgent_exchange_intervalt   _urgent_exchange_intervalt   _max_messagest   Nonet   _notification_idt   _exchange_idt   Falset   _exchangingt   _urgent_exchanget   sett   _client_accepted_typest   _client_accepted_types_hasht   _message_handlerst   _exchange_storet   register_messaget   _handle_accepted_typest   _handle_resynchronizet   _handle_set_intervalst   call_ont   _resynchronizet   stop(
   t   selft   reactort   storet	   transportt   registration_infot   exchange_storet   configt   monitor_intervalt   max_messagest   create_time(    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyt   __init__   s,    														c         C   s   |  j  |  j f S(   s@   Return a binary tuple with urgent and normal exchange intervals.(   R   R   (   R&   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyt   get_exchange_intervals@   s    c         C   sp   d | k r t  S| d } |  j j | ƒ } | d k rM t j d | ƒ t  S|  j j | j k } | j ƒ  | S(   s’   Returns C{True} if message is obsolete.

        A message is considered obsolete if the secure ID changed since it was
        received.
        s   operation-ids4   No message context for message with operation-id: %sN(	   R   R   t   get_message_contextR   t   loggingt   warningR   t	   secure_idt   remove(   R&   t   messaget   operation_idt   contextt   result(    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyt   _message_is_obsoleteD   s    

c         C   s„   |  j  | ƒ r- t j d | j d ƒ ƒ d Sd | k rU t |  j j ƒ  ƒ | d <n  |  j j	 | ƒ } | r€ |  j
 d t ƒ n  | S(   sÊ   Include a message to be sent in an exchange.

        If urgent is True, an exchange with the server will be
        scheduled urgently.

        @param message: Same as in L{MessageStore.add}.
        sn   Response message with operation-id %s was discarded because the client's secure ID has changed in the meantimes   operation-idt	   timestampt   urgentN(   R;   R3   t   infot   getR   t   intR	   t   timeR
   t   addt   schedule_exchanget   True(   R&   R7   R=   t
   message_id(    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyt   send\   s    c         C   s   |  j  d t ƒ d S(   s9   Start scheduling exchanges. The first one will be urgent.R=   N(   RC   RD   (   R&   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyt   startr   s    c         C   s`   |  j  d  k	 r. |  j j |  j  ƒ d  |  _  n  |  j d  k	 r\ |  j j |  j ƒ d  |  _ n  d  S(   N(   R   R   R	   t   cancel_callR   (   R&   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyR%   v   s    c         C   sÓ   t  |  j j ƒ  ƒ } t  | d ƒ } t | | ƒ } |  j j | ƒ t j d | ƒ |  j j d ƒ ry |  j d t	 ƒ n  x( | | D] } |  j
 j d | t ƒ q„ Wx( | | D] } |  j
 j d | t	 ƒ q¯ Wd S(   s‚  
        When the server updates us about the types of message it
        accepts, update our message store.

        If this makes existing held messages available for sending,
        urgently exchange messages.

        If new types are made available or old types are dropped a
        C{("message-type-acceptance-changed", type, bool)} reactor
        event will be fired.
        t   typess   Accepted types changed: %si   R=   s   message-type-acceptance-changedN(   R   R
   t   get_accepted_typest   get_accepted_types_difft   set_accepted_typesR3   R>   t   get_pending_messagesRC   RD   R	   t   fireR   (   R&   R7   t	   old_typest	   new_typest   difft   type(    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyR    ~   s    c         C   s<   | d } |  j  j i d d 6| d 6ƒ |  j j d ƒ d  S(   Ns   operation-idR   RR   s   resynchronize-clients(   R
   RB   R	   RN   (   R&   R7   t   opid(    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyR!   –   s    
c         C   s   |  j  d t ƒ d  S(   NR=   (   RC   RD   (   R&   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyR$   œ   s    c         C   s   d | k r? | d |  _  |  j  |  j _ t j d |  j  ƒ n  d | k r~ | d |  _ |  j |  j _ t j d |  j ƒ n  |  j j ƒ  d  S(   Nt   exchanges$   Exchange interval set to %d seconds.s   urgent-exchanges+   Urgent exchange interval set to %d seconds.(   R   R   R   R3   R>   R   R   t   write(   R&   R7   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyR"   Ÿ   s    		c            sÑ   ˆ j  r d St ˆ _  ˆ j j d ƒ ˆ j ƒ  ‰ ˆ j ƒ  ‰  ˆ j rd t j d ˆ j	 j
 ƒ  ƒ n t j d ˆ j	 j
 ƒ  ƒ ‡  ‡ ‡ f d †  } ˆ j j | d ˆ j	 j ˆ ˆ j j ˆ j d ƒ ƒ t d ƒ S(   så  Send pending messages to the server and process responses.

        An C{pre-exchange} reactor event will be emitted just before the
        actual exchange takes place.

        An C{exchange-done} or C{exchange-failed} reactor event will be
        emitted after a successful or failed exchange.

        @return: A L{Deferred} that is fired when exchange has completed.

        XXX Actually that is a lie right now. It returns before exchange is
        actually complete.
        Ns   pre-exchanges)   Starting urgent message exchange with %s.s"   Starting message exchange with %s.c            s¥   t  ˆ _ |  rD ˆ j r1 t j d ƒ t  ˆ _ n  ˆ j ˆ |  ƒ n ˆ j j d ƒ t j d ƒ ˆ j d t	 ƒ ˆ j j d ƒ t j d t
 ˆ j ƒ  ˆ  ƒ ƒ d  S(   Ns"   Switching to normal exchange mode.s   exchange-faileds   Message exchange failed.t   forces   exchange-dones!   Message exchange completed in %s.(   R   R   R   R3   R>   t   _handle_resultR	   RN   RC   RD   R   R   (   R:   (   t
   start_timeR&   t   payload(    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyt   handle_resultÌ   s    			s
   server-api(   R   RD   R	   RN   t   make_payloadR   R   R3   R>   R   t   get_urlt   call_in_threadR   RT   R   R5   R?   R   (   R&   RZ   (    (   RX   RY   R&   s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyRT   ­   s"    						c         C   s   |  j  S(   sM   Return a bool showing whether there is an urgent exchange scheduled.
        (   R   (   R&   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyt	   is_urgentä   s    c         C   sê   |  j  ræ | s/ |  j d k s/ | ræ |  j ræ | rA t |  _ n  |  j r` |  j j |  j ƒ n  |  j ru |  j } n	 |  j } |  j	 d k	 r£ |  j j |  j	 ƒ n  | d } |  j j
 | |  j ƒ |  _	 |  j j
 | |  j ƒ |  _ n  d S(   s_  Schedule an exchange to happen.

        The exchange will occur after some time based on whether C{urgent} is
        True. An C{impending-exchange} reactor event will be emitted
        approximately 10 seconds before the exchange is started.

        @param urgent: If true, ensure an exchange happens within the
            urgent interval.  This will reschedule the exchange if necessary.
            If another urgent exchange is already scheduled, nothing happens.
        @param force: If true, an exchange will necessarily be scheduled,
            even if it was already scheduled before.
        i
   N(   R   R   R   R   RD   R	   RH   R   R   R   t
   call_latert   _notify_impending_exchangeRT   (   R&   R=   RV   t   intervalt   notification_interval(    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyRC   é   s"    
			
	c         C   s   |  j  j d ƒ d  S(   Ns   impending-exchange(   R	   RN   (   R&   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyR`     s    c         C   sC  |  j  } |  j | j ƒ  ƒ } | j |  j ƒ } | j ƒ  } | r¿ | d j d ƒ } x9 t | ƒ D]% \ } } | j d ƒ | k rb Pqb qb Wd } | d k	 r§ | | 3n  | d k rÅ d } qÅ n t	 } i | d 6t
 d 6| j ƒ  d 6| d 6| d 6| j ƒ  d	 6| d
 6} |  j ƒ  }	 |  j |	 ƒ }
 |
 |  j k r?|	 | d <n  | S(   sè   Return a dict representing the complete exchange payload.

        The payload will contain all pending messages eligible for
        delivery, up to a maximum of C{max_messages} as passed to
        the L{__init__} method.
        i    t   apis   2.0s
   server-apis
   client-apit   sequencet   messagess   total-messagess   next-expected-sequences   accepted-typess   client-accepted-typesN(   R
   t   _hash_typesRJ   RM   R   t   count_pending_messagesR?   t	   enumerateR   R   R   t   get_sequencet   get_server_sequencet!   get_client_accepted_message_typesR   (   R&   R(   t   accepted_types_digestRe   t   total_messagest
   server_apit   iR7   RY   t   accepted_client_typest   accepted_client_types_hash(    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyR[     s6    	


c         C   s   d j  | ƒ } t | ƒ j ƒ  S(   Nt   ;(   t   joinR    t   digest(   R&   RI   t   accepted_types_str(    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyRf   A  s    c         C   s»  |  j  } | j d ƒ |  _ | j d ƒ } | j ƒ  } | d k re | j ƒ  } | t | d ƒ 7} n  t | | ƒ } | j ƒ  | t k r¾ t	 j
 d ƒ | j i d d 6ƒ |  j j d ƒ n  | j ƒ  } | j d ƒ } | | k r"t	 j
 d	 | | f ƒ |  j j d
 | | ƒ | j | ƒ n  | j ƒ  }	 xH | j d d ƒ D]4 }
 |  j |
 ƒ |	 d 7}	 | j |	 ƒ | j ƒ  qAW| j d ƒ r·t	 j
 d ƒ | | k r·|  j d t ƒ q·n  d S(   sÀ  Handle a response from the server.

        Called by L{exchange} after a batch of messages has been
        successfully delivered to the server.

        If the C{server_uuid} changed, a C{"server-uuid-changed"} event
        will be fired.

        Call L{handle_message} for each message in C{result}.

        @param payload: The payload that was sent to the server.
        @param result: The response got in reply to the C{payload}.
        s   client-accepted-types-hashs   next-expected-sequenceRe   sI   Server asked for ancient data: resynchronizing all state with the server.R   RR   s   resynchronize-clientss   server-uuids%   Server UUID changed (old=%s, new=%s).s   server-uuid-changedi   s0   Pending messages remain after the last exchange.R=   N(    (   R
   R?   R   Ri   R   t   lenR   t   commitR   R3   R>   RB   R	   RN   t   get_server_uuidt   set_server_uuidRj   t   handle_messaget   set_server_sequenceRM   RC   RD   (   R&   RY   R:   t   message_storet   next_expectedt   old_sequencet   message_store_statet   old_uuidt   new_uuidRd   R7   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyRW   E  s>    	
	
c         C   s0   |  j  j | g  ƒ j | ƒ |  j j | ƒ d S(   s  Register a handler for the given message type.

        The C{handler} callable will to be executed when a message of
        type C{type} has been received from the server.

        Multiple handlers for the same type will be called in the
        order they were registered.
        N(   R   t
   setdefaultt   appendR   RB   (   R&   RR   t   handler(    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyR     s    	c         C   s†   d | k r3 |  j  j | d |  j j | d ƒ n  |  j j d | ƒ | d |  j k r‚ x& |  j | d D] } | | ƒ qk Wn  d S(   s–   
        Handle a message received from the server.

        Any message handlers registered with L{register_message} will
        be called.
        s   operation-idRR   R7   N(   R   t   add_message_contextR   R5   R	   RN   R   (   R&   R7   R„   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyRz     s    	c         C   s   |  j  j t | ƒ ƒ d  S(   N(   R   RB   t   str(   R&   RR   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyt%   register_client_accepted_message_type¡  s    c         C   s   t  |  j ƒ S(   N(   t   sortedR   (   R&   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyRk   ¦  s    N(   t   __name__t
   __module__t   __doc__t   plugin_nameR   RA   R0   R1   R;   R   RF   RG   R%   R    R!   R$   R"   RT   R^   RC   R`   R[   Rf   RW   R   Rz   R‡   Rk   (    (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyR      s2   #									7	&		/		<			c         C   sµ   t  |  ƒ }  t  | ƒ } | |  } |  | @} |  | } g  } | j g  | D] } d | ^ qI ƒ | j g  | D] } d | ^ qm ƒ | j g  | D] } d | ^ q‘ ƒ d j | ƒ S(   Ns   +%ss   %ss   -%st    (   R   t   extendRs   (   RO   RP   t   added_typest   stable_typest   removed_typesRQ   RR   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyRK   ª  s    


$$$(   R‹   RA   R3   t   landscape.lib.hashlibR    t   twisted.internet.deferR   t   landscape.lib.messageR   R   t   landscape.logR   t	   landscapeR   R   t   objectR   RK   (    (    (    s=   /usr/lib/python2.7/dist-packages/landscape/broker/exchange.pyt   <module>   s   ÿ ž