~free.ekanayaka/landscape-client/lucid-1.5.4-0ubuntu0.10.04.0

« back to all changes in this revision

Viewing changes to landscape/plugin.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-06-28 18:07:18 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100628180718-vytyqgbtkiirv5sb
Tags: 1.5.2.1-0ubuntu0.10.04.0
Filter duplicate network interfaces in get_active_interfaces (LP: #597000)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from logging import info, exception
 
1
from logging import info
2
2
 
3
 
from landscape.lib.bpickle import loads
4
 
from landscape.lib.dbus_util import Object, array_to_string
5
3
from landscape.log import format_object
6
4
 
7
5
 
8
 
class HandlerNotFoundError(Exception):
9
 
    """A handler for the given message type was not found."""
10
 
 
11
 
 
12
6
class PluginConfigError(Exception):
13
7
    """There was an error registering or configuring a plugin."""
14
8
 
44
38
        return self._plugin_names[name]
45
39
 
46
40
 
47
 
class BrokerClientPluginRegistry(PluginRegistry):
48
 
    """Basic plugin registry for clients that have to deal with the broker.
49
 
 
50
 
    This knows about the needs of a client when dealing with the Landscape
51
 
    broker, including interest in messages of a particular type delivered
52
 
    by the broker to the client.
53
 
    """
54
 
 
55
 
    def __init__(self, broker):
56
 
        super(BrokerClientPluginRegistry, self).__init__()
57
 
        self._registered_messages = {}
58
 
        self.broker = broker
59
 
 
60
 
    def register_message(self, type, handler):
61
 
        """
62
 
        Register interest in a particular type of Landscape server->client
63
 
        message.
64
 
        """
65
 
        self._registered_messages[type] = handler
66
 
        return self.broker.register_client_accepted_message_type(type)
67
 
 
68
 
    def broker_started(self):
69
 
        """
70
 
        Re-register any previously registered message types when the broker
71
 
        restarts.
72
 
        """
73
 
        for type in self._registered_messages:
74
 
            self.broker.register_client_accepted_message_type(type)
75
 
 
76
 
    def dispatch_message(self, message):
77
 
        """Run the handler registered for the type of the given message."""
78
 
        type = message["type"]
79
 
        handler = self._registered_messages.get(type)
80
 
        if handler is not None:
81
 
            try:
82
 
                return handler(message)
83
 
            except:
84
 
                exception("Error running message handler for type %r: %r"
85
 
                          % (type, handler))
86
 
        else:
87
 
            raise HandlerNotFoundError(type)
88
 
 
89
 
    def exchange(self):
90
 
        """Call C{exchange} on all plugins."""
91
 
        for plugin in self._plugins:
92
 
            if hasattr(plugin, "exchange"):
93
 
                try:
94
 
                    plugin.exchange()
95
 
                except:
96
 
                    exception("Error during plugin exchange")
97
 
 
98
 
 
99
41
class Plugin(object):
100
42
    """A convenience for writing plugins.
101
43
 
116
58
        self.registry = registry
117
59
        if hasattr(self, "run") and self.run_interval is not None:
118
60
            registry.reactor.call_every(self.run_interval, self.run)
119
 
 
120
 
 
121
 
class BrokerPlugin(Object):
122
 
    """
123
 
    A DBus object which exposes the 'plugin' interface that the Broker expects
124
 
    of its clients.
125
 
    """
126
 
 
127
 
    def __init__(self, bus, registry):
128
 
        """
129
 
        @param bus: a DBus connection, typically a C{dbus.SystemBus} object.
130
 
        @param registry: an instance of L{PluginRegistry} or of a subclass of
131
 
            it.
132
 
        """
133
 
        Object.__init__(self, bus)
134
 
        self.registry = registry
135
 
        bus.add_signal_receiver(self.notify_exchange, "impending_exchange")
136
 
 
137
 
    def notify_exchange(self):
138
 
        info("Got notification of impending exchange. Notifying all plugins.")
139
 
        self.registry.exchange()
140
 
 
141
 
    def ping(self):
142
 
        return True
143
 
 
144
 
    def exit(self):
145
 
        from twisted.internet import reactor
146
 
        reactor.callLater(0.1, reactor.stop)
147
 
 
148
 
    def dispatch_message(self, blob):
149
 
        """
150
 
        Call the L{PluginRegistry}'s C{dispatch_message} method and return True
151
 
        if a message handler was found and False otherwise.
152
 
        """
153
 
        message = loads(array_to_string(blob))
154
 
        try:
155
 
            self.registry.dispatch_message(message)
156
 
            return True
157
 
        except HandlerNotFoundError:
158
 
            return False
159
 
 
160
 
    def message(self, blob):
161
 
        """
162
 
        Call L{dispatch_message} with C{blob} and return the result.
163
 
        """
164
 
        return self.dispatch_message(blob)