ó
ÍÿŠTc           @   s…   d  Z  d d l Z d d l Z d d l m Z m Z m Z d e f d „  ƒ  YZ d „  Z	 d „  Z
 d e f d	 „  ƒ  YZ d
 „  Z d S(   sh  Communication between components in different services via twisted AMP.

The Landscape client is composed by several processes that need to talk to
each other. For example the monitor and manager processes need to talk to
the broker in order to ask it to add new messages to the outgoing queue, and
the broker needs to talk to them in order to dispatch them incoming messages
from the server.

This module implements a few conveniences built around L{landscape.lib.amp} to
let the various services connect to each other in an easy and idiomatic way,
and have them respond to standard requests like "ping" or "exit".
iÿÿÿÿN(   t   MethodCallClientFactoryt   MethodCallServerFactoryt   RemoteObjectt   ComponentPublisherc           B   s/   e  Z d  Z e Z d „  Z d „  Z d „  Z RS(   s  Publish a Landscape client component using a UNIX socket.

    Other Landscape client processes can then connect to the socket and invoke
    methods on the component remotely, using L{MethodCall} commands.

    @param component: The component to publish. It can be any Python object
        implementing the methods listed in the C{methods} class variable.
    @param reactor: The L{LandscapeReactor} used to listen to the socket.
    @param config: The L{Configuration} object used to build the socket path.
    c         C   sC   | |  _  | |  _ | |  _ d  |  _ t t | ƒ ƒ j ƒ  |  _ d  S(   N(	   t   _reactort   _configt
   _componentt   Nonet   _portt   get_remote_methodst   typet   keyst   methods(   t   selft	   componentt   reactort   config(    (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyt   __init__"   s
    				c         C   sF   t  |  j |  j ƒ } t |  j |  j ƒ } |  j j | | ƒ |  _ d S(   s   Start accepting connections.N(   R   R   R   t   _get_socket_pathR   R   t   listen_unixR   (   R   t   factoryt   socket_path(    (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyt   start)   s    c         C   s   |  j  j ƒ  S(   s   Stop accepting connections.(   R   t   stopListening(   R   (    (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyt   stop/   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   R   (    (    (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyR      s
   
		c         C   s[   i  } xN t  |  ƒ D]@ } t |  | ƒ } t | d d ƒ } | d k	 r | | | <q q W| S(   sr   Get all the remote methods declared on a class.

    @param klass: A class to search for AMP-exposed methods.
    t   amp_exposedN(   t   dirt   getattrR   (   t   klasst   remote_methodst   attribute_namet   potential_methodt   name(    (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyR	   4   s    c         C   s   |  j  |  _ |  S(   sa   
    A decorator for marking a method as remotely accessible as a method on a
    component.
    (   R   R   (   t   method(    (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyt   remoteB   s    t   ComponentConnectorc           B   sG   e  Z d  Z e Z d Z e Z e	 d „ Z
 d d e	 d „ Z d „  Z RS(   sù  Utility superclass for creating connections with a Landscape component.

    @cvar component: The class of the component to connect to, it is expected
        to define a C{name} class attribute, which will be used to find out
        the socket to use. It must be defined by sub-classes.
    @cvar factory: The factory class to use for building protocols.
    @cvar remote: The L{RemoteObject} class or sub-class used for building
        remote objects.

    @param reactor: A L{LandscapeReactor} object.
    @param config: A L{LandscapeConfiguration}.
    @param retry_on_reconnect: If C{True} the remote object built by this
        connector will retry L{MethodCall}s that failed due to lost
        connections.

    @see: L{MethodCallClientFactory}.
    c         C   s(   | |  _  | |  _ | |  _ d  |  _ d  S(   N(   R   R   t   _retry_on_reconnectR   t
   _connector(   R   R   R   t   retry_on_reconnect(    (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyR   a   s    			c            sä   ˆ  j  ˆ  j j ƒ ‰ d ˆ _ ˆ _ ˆ  j ˆ _ ˆ  j ˆ _ | ˆ _ | rX | ˆ _ n  ‡  f d †  ‰ ‡ ‡ f d †  } ‡  f d †  } t	 ˆ  j
 ˆ  j ƒ } ˆ j ƒ  } ˆ  j j | ˆ ƒ ˆ  _ | s× | j | ƒ n  | j | ƒ S(   s  Connect to the remote Landscape component.

        If the connection is lost after having been established, and then
        it is established again by the reconnect mechanism, an event will
        be fired.

        @param max_retries: If given, the connector will keep trying to connect
            up to that number of times, if the first connection attempt fails.
        @param factor: Optionally a float indicating by which factor the
            delay between subsequent retries should increase. Smaller values
            result in a faster reconnection attempts pace.
        @param quiet: A boolean indicating whether to log errors.
        gš™™™™™©?c            s   ˆ  j  j d ˆ  j j ƒ d  S(   Ns   %s-reconnect(   R   t   fireR   R#   (   t   ignored(   R   (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyt   fire_reconnect}   s    c            s   ˆ j  ˆ  ƒ |  S(   N(   t   notifyOnConnect(   R%   (   R,   R   (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyt	   connected€   s    c            s   t  j d ˆ  j j ƒ |  S(   Ns   Error while connecting to %s(   t   loggingt   errorR   R#   (   t   failure(   R   (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyt	   log_error„   s    (   R   R   t   initialDelayt   delayR'   t   retryOnReconnectR%   t
   maxRetriest   factorR   R   R   t   getRemoteObjectt   connect_unixR(   t
   addErrbackt   addCallback(   R   t   max_retriesR7   t   quietR.   R2   R   t   deferred(    (   R   R   R,   s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyt   connectg   s     	c         C   sB   |  j  d k	 r> |  j  j } | j ƒ  |  j  j ƒ  d |  _  n  d S(   s4   Disconnect the L{RemoteObject} that we have created.N(   R(   R   R   t
   stopTryingt
   disconnect(   R   R   (    (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyRA   ‘   s
    
N(   R   R   R   R    R   R   R   R   R%   t   FalseR   R?   RA   (    (    (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyR&   K   s   *c         C   s   t  j j | j |  j d ƒ S(   Ns   .sock(   t   ost   patht   joint   sockets_pathR#   (   R   R   (    (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyR   š   s    (   R   RC   R/   t   landscape.lib.ampR    R   R   t   objectR   R	   R%   R&   R   (    (    (    s1   /usr/lib/python2.7/dist-packages/landscape/amp.pyt   <module>   s    			O