From: Arvin Schnell Date: Thu, 4 Oct 2012 15:31:27 +0000 (+0200) Subject: - fixed client connect/disconnect handling X-Git-Tag: v0.1.3~91 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=90eddf8648cc7bc8540baece0623d08840e59e8c;p=thirdparty%2Fsnapper.git - fixed client connect/disconnect handling --- diff --git a/dbus/DBusConnection.cc b/dbus/DBusConnection.cc index c7fd2f46..77f2fb7c 100644 --- a/dbus/DBusConnection.cc +++ b/dbus/DBusConnection.cc @@ -117,7 +117,23 @@ namespace DBus dbus_error_init(&err); dbus_bus_add_match(conn, rule, &err); + if (dbus_error_is_set(&err)) + { + dbus_error_free(&err); + throw FatalException(); + } + } + + + void + Connection::remove_match(const char* rule) + { + boost::lock_guard lock(mutex); + + DBusError err; + dbus_error_init(&err); + dbus_bus_remove_match(conn, rule, &err); if (dbus_error_is_set(&err)) { dbus_error_free(&err); diff --git a/dbus/DBusConnection.h b/dbus/DBusConnection.h index 47672dbe..420fe344 100644 --- a/dbus/DBusConnection.h +++ b/dbus/DBusConnection.h @@ -49,6 +49,10 @@ namespace DBus Message send_with_reply_and_block(Message& m); void add_match(const char* rule); + void remove_match(const char* rule); + + void add_match(const string& rule) { add_match(rule.c_str()); } + void remove_match(const string& rule) { remove_match(rule.c_str()); } unsigned long get_unix_userid(const Message& m); diff --git a/dbus/DBusMainLoop.cc b/dbus/DBusMainLoop.cc index c46fb80e..f7e45611 100644 --- a/dbus/DBusMainLoop.cc +++ b/dbus/DBusMainLoop.cc @@ -45,11 +45,6 @@ namespace DBus throw FatalException(); dbus_connection_set_wakeup_main_function(conn, wakeup_main, this, NULL); - - // Filtering for the sender doesn't work for me. So also check the - // sender later when handling the signal. - add_match("type='signal', sender='" DBUS_SERVICE_DBUS "', path='" DBUS_PATH_DBUS "', " - "interface='" DBUS_INTERFACE_DBUS "', member='NameOwnerChanged'"); } @@ -278,6 +273,28 @@ namespace DBus } + void + DBus::MainLoop::add_client_match(const string& name) + { + // Filtering for the sender doesn't work for me. So also check the + // sender later when handling the signal. + add_match("type='signal', sender='" DBUS_SERVICE_DBUS "', path='" DBUS_PATH_DBUS "', " + "interface='" DBUS_INTERFACE_DBUS "', member='NameOwnerChanged', " + "arg0='" + name + "'"); + } + + + void + DBus::MainLoop::remove_client_match(const string& name) + { + // Filtering for the sender doesn't work for me. So also check the + // sender later when handling the signal. + remove_match("type='signal', sender='" DBUS_SERVICE_DBUS "', path='" DBUS_PATH_DBUS "', " + "interface='" DBUS_INTERFACE_DBUS "', member='NameOwnerChanged', " + "arg0='" + name + "'"); + } + + void DBus::MainLoop::dispatch_incoming(Message& msg) { @@ -301,9 +318,6 @@ namespace DBus DBus::Hihi hihi(msg); hihi >> name >> old_owner >> new_owner; - if (name == new_owner && old_owner.empty()) - client_connected(name); - if (name == old_owner && new_owner.empty()) client_disconnected(name); } diff --git a/dbus/DBusMainLoop.h b/dbus/DBusMainLoop.h index 5492c23b..ecf7717f 100644 --- a/dbus/DBusMainLoop.h +++ b/dbus/DBusMainLoop.h @@ -44,9 +44,11 @@ namespace DBus void set_idle_timeout(int s); void reset_idle_count(); + void add_client_match(const string& name); + void remove_client_match(const string& name); + virtual void method_call(Message& message) = 0; virtual void signal(Message& message) = 0; - virtual void client_connected(const string& name) = 0; virtual void client_disconnected(const string& name) = 0; virtual int periodic_timeout() = 0; virtual void periodic() = 0; diff --git a/server/snapperd.cc b/server/snapperd.cc index a71c4543..c48e9188 100644 --- a/server/snapperd.cc +++ b/server/snapperd.cc @@ -53,7 +53,6 @@ public: void method_call(DBus::Message& message); void signal(DBus::Message& message); - void client_connected(const string& name); void client_disconnected(const string& name); void periodic(); int periodic_timeout(); @@ -75,9 +74,8 @@ MyMainLoop::~MyMainLoop() void MyMainLoop::method_call(DBus::Message& msg) { - y2deb("method call sender:'" << msg.get_sender() << "' path:'" << - msg.get_path() << "' interface:'" << msg.get_interface() << - "' member:'" << msg.get_member() << "'"); + y2deb("method call sender:'" << msg.get_sender() << "' path:'" << msg.get_path() << + "' interface:'" << msg.get_interface() << "' member:'" << msg.get_member() << "'"); reset_idle_count(); @@ -93,6 +91,7 @@ MyMainLoop::method_call(DBus::Message& msg) if (client == clients.end()) { y2deb("client connected invisible '" << msg.get_sender() << "'"); + add_client_match(msg.get_sender()); client = clients.add(msg.get_sender()); set_idle_timeout(-1); } @@ -105,23 +104,8 @@ MyMainLoop::method_call(DBus::Message& msg) void MyMainLoop::signal(DBus::Message& msg) { - y2deb("signal sender:'" << msg.get_sender() << "' path:'" << - msg.get_path() << "' interface:'" << msg.get_interface() << - "' member:'" << msg.get_member() << "'"); -} - - -void -MyMainLoop::client_connected(const string& name) -{ - y2deb("client connected '" << name << "'"); - - boost::unique_lock lock(big_mutex); - - clients.add(name); - - reset_idle_count(); - set_idle_timeout(-1); + y2deb("signal sender:'" << msg.get_sender() << "' path:'" << msg.get_path() << + "' interface:'" << msg.get_interface() << "' member:'" << msg.get_member() << "'"); } @@ -130,6 +114,8 @@ MyMainLoop::client_disconnected(const string& name) { y2deb("client disconnected '" << name << "'"); + remove_client_match(name); + boost::unique_lock lock(big_mutex); Clients::iterator client = clients.find(name);