dbus: stop discovery when the searching client disappears

This is the single-client version only, we can deal with multiple clients
later.

Fixes #21
This commit is contained in:
Peter Hutterer 2018-01-24 10:39:59 +10:00
parent 71dd70cc95
commit cce63d267b
1 changed files with 30 additions and 5 deletions

View File

@ -330,6 +330,7 @@ class TuhiDBusServer(GObject.Object):
self._bus_name_aquired, self._bus_name_aquired,
self._bus_name_lost) self._bus_name_lost)
self._is_searching = False self._is_searching = False
self._searching_client = None
def _bus_aquired(self, connection, name): def _bus_aquired(self, connection, name):
introspection = Gio.DBusNodeInfo.new_for_xml(INTROSPECTION_XML) introspection = Gio.DBusNodeInfo.new_for_xml(INTROSPECTION_XML)
@ -354,10 +355,10 @@ class TuhiDBusServer(GObject.Object):
return None return None
if methodname == 'StartSearch': if methodname == 'StartSearch':
self._start_search() self._start_search(connection, sender)
invocation.return_value() invocation.return_value()
elif methodname == 'StopSearch': elif methodname == 'StopSearch':
self._stop_search() self._stop_search(connection, sender)
invocation.return_value() invocation.return_value()
def _property_read_cb(self, connection, sender, objpath, interface, propname): def _property_read_cb(self, connection, sender, objpath, interface, propname):
@ -372,20 +373,43 @@ class TuhiDBusServer(GObject.Object):
def _property_write_cb(self): def _property_write_cb(self):
pass pass
def _start_search(self): def _start_search(self, connection, sender):
# FIXME: need to handle multiple clients here
if self._is_searching: if self._is_searching:
return return
self._is_searching = True self._is_searching = True
s = connection.signal_subscribe(sender='org.freedesktop.DBus',
interface_name='org.freedesktop.DBus',
member='NameOwnerChanged',
object_path='/org/freedesktop/DBus',
arg0=None,
flags=Gio.DBusSignalFlags.NONE,
callback=self._on_name_owner_changed_signal_cb,
user_data=sender)
self._searching_client = (sender, s)
self.emit("search-start-requested", self._on_search_stop) self.emit("search-start-requested", self._on_search_stop)
for d in self._devices: for d in self._devices:
if not d.paired: if not d.paired:
self._emit_pairable_signal(d) self._emit_pairable_signal(d)
def _stop_search(self): def _on_name_owner_changed_signal_cb(self, connection, sender, object_path,
if not self._is_searching: interface_name, node,
out_user_data, user_data):
name, old_owner, new_owner = out_user_data
if name != user_data:
return return
self._stop_search(connection, user_data)
def _stop_search(self, connection, sender):
# FIXME: need to handle multiple clients here
if not self._is_searching or sender != self._searching_client[0]:
return
connection.signal_unsubscribe(self._searching_client[1])
self._is_searching = False self._is_searching = False
self.emit("search-stop-requested") self.emit("search-stop-requested")
@ -399,6 +423,7 @@ class TuhiDBusServer(GObject.Object):
status = GLib.Variant.new_tuple(status) status = GLib.Variant.new_tuple(status)
self._connection.emit_signal(None, BASE_PATH, INTF_MANAGER, self._connection.emit_signal(None, BASE_PATH, INTF_MANAGER,
"SearchStopped", status) "SearchStopped", status)
self._searching_client = None
for dev in self._devices: for dev in self._devices:
if dev.paired: if dev.paired: