ó
½-'Nc           @   sŒ   d  d l  m Z 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(   iÿÿÿÿ(   t   infot	   exception(   t   maybeDeferred(   t   format_object(   t   gather_resultst   HandlerNotFoundErrorc           B   s   e  Z d  Z RS(   s3   A handler for the given message type was not found.(   t   __name__t
   __module__t   __doc__(    (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyR   	   s   t   BrokerClientPluginc           B   s2   e  Z d  Z d Z e Z d „  Z e d „  ƒ Z RS(   s  A convenience for writing L{BrokerClient} plugins.

    This provides a register method which will set up a bunch of
    reactor handlers in the idiomatic way.

    If C{run} is defined on subclasses, it will be called every C{run_interval}
    seconds after being registered.

    @cvar run_interval: The interval, in seconds, to execute the C{run} method.
        If set to C{None}, then C{run} will not be scheduled.
    @cvar run_immediately: If C{True} the plugin will be run immediately after
        it is registered.
    i   c         C   sl   | |  _  t |  d d  ƒ d  k	 rh |  j r7 |  j ƒ  n  |  j d  k	 rh |  j  j j |  j |  j ƒ qh n  d  S(   Nt   run(   t   clientt   getattrt   Nonet   run_immediatelyR
   t   run_intervalt   reactort
   call_every(   t   selfR   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt   register   s    		c         C   s   |  j  S(   s%   An alias for the C{client} attribute.(   R   (   R   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt   registry'   s    (	   R   R   R   R   t   FalseR   R   t   propertyR   (    (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyR	      s
   	t   BrokerClientc           B   s‰   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z RS(   sï  Basic plugin registry for clients that have to deal with the broker.

    This knows about the needs of a client when dealing with the Landscape
    broker, including interest in messages of a particular type delivered
    by the broker to the client.

    @cvar name: The name used when registering to the broker, it must be
        defined by sub-classes.
    @ivar broker: A reference to a connected L{RemoteBroker}, it must be set
        by the connecting machinery at service startup.
    R   c         C   sp   t  t |  ƒ j ƒ  | |  _ d |  _ i  |  _ g  |  _ i  |  _ |  j j	 d |  j
 ƒ |  j j	 d |  j ƒ d S(   s6   
        @param reactor: A L{TwistedReactor}.
        s   impending-exchanges   broker-reconnectN(   t   superR   t   __init__R   R   t   brokert   _registered_messagest   _pluginst   _plugin_namest   call_ont   notify_exchanget   handle_reconnect(   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyR   ;   s    					c         C   s   t  S(   s   Return C{True}(   t   True(   R   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt   pingJ   s    c         C   sV   t  d t | ƒ ƒ |  j j | ƒ t | d ƒ rE | |  j | j <n  | j |  ƒ d S(   s  Add a plugin.

        The plugin's C{register} method will be called with this broker client
        as its argument.

        If the plugin has a C{plugin_name} attribute, it will be possible to
        look up the plugin later with L{get_plugin}.
        s   Registering plugin %s.t   plugin_nameN(   R    R   R   t   appendt   hasattrR   R#   R   (   R   t   plugin(    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt   addN   s
    	c         C   s   |  j  S(   s   Get the list of plugins.(   R   (   R   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt   get_plugins]   s    c         C   s   |  j  | S(   s    Get a particular plugin by name.(   R   (   R   t   name(    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt
   get_plugina   s    c         C   s   | |  j  | <|  j j | ƒ S(   st  
        Register interest in a particular type of Landscape server->client
        message.

        @param type: The type of message to register C{handler} for.
        @param handler: A callable taking a message as a parameter, called
            when messages of C{type} are received.
        @return: A C{Deferred} that will fire when registration completes.
        (   R   R   t%   register_client_accepted_message_type(   R   t   typet   handler(    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt   register_messagee   s    
c         C   sg   | d } |  j  j | ƒ } | d k r7 t | ƒ ‚ n  y | | ƒ SWn t d | | f ƒ n Xd S(   sÆ   Run the handler registered for the type of the given message.

        @return: The return value of the handler, if found.
        @raises: HandlerNotFoundError if the handler was not found
        R,   s-   Error running message handler for type %r: %rN(   R   t   getR   R   R   (   R   t   messageR,   R-   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt   dispatch_messager   s    
c         C   s.   y |  j  | ƒ t SWn t k
 r) t SXd S(   sŠ   Call C{dispatch_message} for the given C{message}.

        @return: A boolean indicating if a handler for the message was found.
        N(   R1   R!   R   R   (   R   R0   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyR0   ‚   s
    c         C   sO   xH |  j  ƒ  D]: } t | d ƒ r y | j ƒ  WqG t d ƒ qG Xq q Wd S(   s    Call C{exchange} on all plugins.t   exchanges   Error during plugin exchangeN(   R(   R%   R2   R   (   R   R&   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyR2      s    c         C   s   t  d ƒ |  j ƒ  d S(   s/   Notify all plugins about an impending exchange.s>   Got notification of impending exchange. Notifying all plugins.N(   R    R2   (   R   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyR   –   s    
c         O   s   | d k r> | d } | d } |  j  j | | f | ƒ } n |  j  j | | | Ž } t g  | D] } t d „  | ƒ ^ q` ƒ S(   s´   Fire an event of a given type.

        @return: A L{Deferred} resulting in a list of returns values of
            the fired event handlers, in the order they were fired.
        s   message-type-acceptance-changedi    i   c         S   s   |  S(   N(    (   t   x(    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt   <lambda>¨   s    (   R   t   fireR   R   (   R   t
   event_typet   argst   kwargst   message_typet
   acceptancet   resultst   result(    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt
   fire_event›   s    

c         C   s;   x! |  j  D] } |  j j | ƒ q
 W|  j j |  j ƒ d S(   s  Called when the connection with the broker is established again.

        The following needs to be done:

          - Re-register any previously registered message types, so the broker
            knows we have interest on them.

          - Re-register ourselves as client, so the broker knows we exist and
            will talk to us firing events and dispatching messages.
        N(   R   R   R+   t   register_clientR)   (   R   R,   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyR    ª   s    c         C   s   |  j  j d |  j  j ƒ d S(   s&   Stop the reactor and exit the process.gš™™™™™¹?N(   R   t
   call_latert   stop(   R   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt   exit¹   s    (   R   R   R   R)   R   R"   R'   R(   R*   R.   R1   R0   R2   R   R=   R    RA   (    (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyR   -   s   													N(   t   loggingR    R   t   twisted.internet.deferR   t   landscape.logR   t   landscape.lib.twisted_utilR   t	   ExceptionR   t   objectR	   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/landscape/broker/client.pyt   <module>   s    