ó
ÍÿŠTc           @   s¸   d  Z  d d l Z d d l Z d d l Z d d l 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 m Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d S(   s   Low-level server communication.iÿÿÿÿN(   t   fetch(   t   bpickle(   t   format_delta(   t
   SERVER_APIt   VERSIONt   HTTPTransportc           B   sG   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d d e d „ Z	 RS(   sÇ   Transport makes a request to exchange message data over HTTP.

    @param url: URL of the remote Landscape server message system.
    @param pubkey: SSH public key used for secure communication.
    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   t   _reactort   _urlt   _pubkey(   t   selft   reactort   urlt   pubkey(    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyt   __init__   s    		c         C   s   |  j  S(   s)   Get the URL of the remote message system.(   R   (   R	   (    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyt   get_url   s    c         C   s   | |  _  d S(   s)   Set the URL of the remote message system.N(   R   (   R	   R   (    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyt   set_url   s    c         C   s‹   i | d 6d t  d 6d d 6} | r2 | | d <n  | rK t | ƒ | d <n  t j ƒ  } | t |  j d t d	 | d
 | d |  j d | ƒf S(   Ns   X-Message-APIs   landscape-client/%ss
   User-Agents   application/octet-streams   Content-Types   X-Computer-IDs   X-Exchange-Tokent   postt   datat   headerst   cainfot   curl(   R   t   strt   pycurlt   CurlR    R   t   TrueR   (   R	   t   payloadt   computer_idt   exchange_tokent   message_apiR   R   (    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyt   _curl#   s    

c   
      C   s1  t  j | ƒ } t j ƒ  } t j ƒ  j ƒ  t j k rR t j d t j	 | ƒ ƒ n  y" |  j
 | | | | ƒ \ } } Wn t j d |  j ƒ ‚  n3 Xt j d t | ƒ t | ƒ t t j ƒ  | ƒ ƒ y t  j | ƒ }	 Wn t j d | ƒ d SXt j ƒ  j ƒ  t j k r-t j d t j	 |	 ƒ ƒ n  |	 S(   s>  Exchange message data with the server.

        @param payload: The object to send, it must be L{bpickle}-compatible.
        @param computer_id: The computer ID to send the message as (see
            also L{Identity}).
        @param exchange_token: The token that the server has given us at the
            last exchange. It's used to prove that we are still the same
            client.

        @type: C{dict}
        @return: The server's response to sent message or C{None} in case
            of error.

        @note: This code is thread safe (HOPEFULLY).

        s   Sending payload:
%ss"   Error contacting the server at %s.s*   Sent %d bytes and received %d bytes in %s.s    Server returned invalid data: %rs   Received payload:
%sN(   R   t   dumpst   timet   loggingt	   getLoggert   getEffectiveLevelt   DEBUGt   debugt   pprintt   pformatR   t	   exceptionR   t   infot   lenR   t   loadst   None(
   R	   R   R   R   R   t   spayloadt
   start_timet   curlyR   t   response(    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyt   exchange/   s,    	N(
   t   __name__t
   __module__t   __doc__R+   R   R   R   R   R   R0   (    (    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyR      s   			t   FakeTransportc           B   sD   e  Z d  Z d d d d „ Z d „  Z d „  Z d d e d „ Z RS(   s$   Fake transport for testing purposes.c         C   sg   | |  _  g  |  _ g  |  _ d |  _ d |  _ d  |  _ d  |  _ d  |  _ i  |  _	 | |  _
 | |  _ d  S(   Ni    (   R   t   payloadst	   responsest   _current_responset   next_expected_sequenceR+   R   R   R   t   extraR   R   (   R	   R
   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyR   `   s    										c         C   s   |  j  S(   N(   R   (   R	   (    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyR   m   s    c         C   s   | |  _  d  S(   N(   R   (   R	   R   (    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyR   p   s    c         C   sâ   |  j  j | ƒ | |  _ | |  _ | |  _ |  j t | j d g  ƒ ƒ 7_ |  j t |  j	 ƒ k  r† |  j	 |  j } |  j d 7_ n g  } t
 | t ƒ r¤ | ‚ n  i |  j d 6t t j ƒ  ƒ d 6| d 6} | j |  j ƒ | S(   Nt   messagesi   s   next-expected-sequences   next-exchange-token(   R5   t   appendR   R   R   R8   R)   t   getR7   R6   t
   isinstancet	   Exceptiont   unicodet   uuidt   uuid4t   updateR9   (   R	   R   R   R   R   R/   t   result(    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyR0   s   s     			!	
N(	   R1   R2   R3   R+   R   R   R   R   R0   (    (    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyR4   ]   s   		(   R3   R   R    R%   R@   R   t   landscape.lib.fetchR    t   landscape.libR   t   landscape.logR   t	   landscapeR   R   t   objectR   R4   (    (    (    s>   /usr/lib/python2.7/dist-packages/landscape/broker/transport.pyt   <module>   s   N