Merge lp:~zeitgeist/zeitgeist/bug673008 into lp:zeitgeist/0.1

Proposed by Siegfried Gevatter
Status: Merged
Merged at revision: 1667
Proposed branch: lp:~zeitgeist/zeitgeist/bug673008
Merge into: lp:zeitgeist/0.1
Diff against target: 109 lines (+30/-6)
1 file modified
zeitgeist/client.py (+30/-6)
To merge this branch: bzr merge lp:~zeitgeist/zeitgeist/bug673008
Reviewer Review Type Date Requested Status
Mikkel Kamstrup Erlandsen Approve
Review via email: mp+48914@code.launchpad.net

Commit message

ZeitgeistClient: reinstall monitors upon reconnection (LP: #673008)

Description of the change

This will re-install all monitors set by ZeitgeistClient if ZeitgeistInterface (or, actually, _DBusInterface) sees it has lost of the connection to Zeitgeist and reconnects to it.

To post a comment you must log in.
Revision history for this message
Mikkel Kamstrup Erlandsen (kamstrup) wrote :

Looks good to me

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'zeitgeist/client.py'
2--- zeitgeist/client.py 2011-02-08 12:54:20 +0000
3+++ zeitgeist/client.py 2011-02-08 13:36:25 +0000
4@@ -2,7 +2,7 @@
5
6 # Zeitgeist
7 #
8-# Copyright © 2009-2010 Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com>
9+# Copyright © 2009-2011 Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com>
10 # Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
11 # Copyright © 2009 Markus Korn <thekorn@gmx.de>
12 #
13@@ -41,6 +41,8 @@
14 class _DBusInterface(object):
15 """Wrapper around dbus.Interface adding convenience methods."""
16
17+ reconnect_callbacks = []
18+
19 @staticmethod
20 def get_members(introspection_xml):
21 """Parses the XML context returned by Introspect() and returns
22@@ -58,6 +60,13 @@
23 pass
24 return methods, signals
25
26+ def reconnect(self):
27+ self.__proxy = dbus.SessionBus().get_object(self.__iface.requested_bus_name,
28+ self.__object_path)
29+ self.__iface = dbus.Interface(self.__proxy, self.__interface_name)
30+ for callback in self.reconnect_callbacks:
31+ callback()
32+
33 def _disconnection_safe(self, meth, *args, **kwargs):
34 """
35 Executes the given method. If it fails because the D-Bus connection
36@@ -70,10 +79,7 @@
37 def reconnecting_error_handler(e):
38 error = e.get_dbus_name()
39 if error == "org.freedesktop.DBus.Error.ServiceUnknown":
40- self.__proxy = dbus.SessionBus().get_object(
41- self.__iface.requested_bus_name, self.__object_path)
42- self.__iface = dbus.Interface(self.__proxy,
43- self.__interface_name)
44+ self.reconnect()
45 # We don't use the reconnecting_error_handler here since that'd
46 # get us into an endless loop if Zeitgeist really isn't there.
47 return meth(*args, **original_kwargs)
48@@ -171,7 +177,7 @@
49 dbus_interface.proxy.get_dbus_method("Get",
50 dbus_interface=dbus.PROPERTIES_IFACE),
51 self.INTERFACE_NAME, "extensions")
52-
53+
54 def get_extension(cls, name, path, busname=None):
55 """ Returns an interface to the given extension.
56
57@@ -209,6 +215,8 @@
58 self.__shared_state["extension_interfaces"] = {}
59 self.__shared_state["dbus_interface"] = _DBusInterface(proxy,
60 self.INTERFACE_NAME, self.OBJECT_PATH)
61+ self.reconnect_callbacks = \
62+ self.__shared_state["dbus_interface"].reconnect_callbacks
63
64 class Monitor(dbus.service.Object):
65 """
66@@ -317,6 +325,8 @@
67 DBus calls use the raw DBus API found in the ZeitgeistDBusInterface class.
68 """
69
70+ _installed_monitors = []
71+
72 @staticmethod
73 def get_event_and_extra_arguments(arguments):
74 """ some methods of :class:`ZeitgeistClient` take a variable
75@@ -335,6 +345,18 @@
76 self._iface = ZeitgeistDBusInterface()
77 self._registry = self._iface.get_extension("DataSourceRegistry",
78 "data_source_registry")
79+
80+ # Reconnect all active monitors if the connection is reset.
81+ def reconnect_monitors():
82+ log.info("Reconnected to Zeitgeist engine...")
83+ for monitor in self._installed_monitors:
84+ self._iface.InstallMonitor(monitor.path,
85+ monitor.time_range,
86+ monitor.templates,
87+ reply_handler=self._void_reply_handler,
88+ error_handler=lambda err: log.warn(
89+ "Error reinstalling monitor: %s" % err))
90+ self._iface.reconnect_callbacks.append(reconnect_monitors)
91
92 def _safe_error_handler(self, error_handler, *args):
93 if error_handler is not None:
94@@ -837,6 +859,7 @@
95 reply_handler=self._void_reply_handler,
96 error_handler=lambda err: log.warn(
97 "Error installing monitor: %s" % err))
98+ self._installed_monitors.append(mon)
99 return mon
100
101 def remove_monitor (self, monitor, monitor_removed_handler=None):
102@@ -875,6 +898,7 @@
103 self._iface.RemoveMonitor(path,
104 reply_handler=reply_handler,
105 error_handler=error_handler)
106+ self._installed_monitors.remove(monitor)
107
108 def register_data_source(self, unique_id, name, description, event_templates):
109 """