From 8a1912e7b9f8a7a0351ef252359ae04ff9fc4402 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 09:58:05 -0600 Subject: Add an unwatch function with a basic handler. --- libindicator/indicator-service.c | 26 ++++++++++++++++++++++++++ libindicator/indicator-service.xml | 3 +++ 2 files changed, 29 insertions(+) diff --git a/libindicator/indicator-service.c b/libindicator/indicator-service.c index 69422c5..dda00a9 100644 --- a/libindicator/indicator-service.c +++ b/libindicator/indicator-service.c @@ -8,6 +8,7 @@ /* DBus Prototypes */ static gboolean _indicator_service_server_watch (IndicatorService * service, DBusGMethodInvocation * method); +static gboolean _indicator_service_server_un_watch (IndicatorService * service, DBusGMethodInvocation * method); #include "indicator-service-server.h" #include "dbus-shared.h" @@ -309,6 +310,31 @@ _indicator_service_server_watch (IndicatorService * service, DBusGMethodInvocati return TRUE; } +static gboolean +_indicator_service_server_un_watch (IndicatorService * service, DBusGMethodInvocation * method) +{ + g_return_val_if_fail(INDICATOR_IS_SERVICE(service), FALSE); + IndicatorServicePrivate * priv = INDICATOR_SERVICE_GET_PRIVATE(service); + + /* Remove us from the watcher list here */ + + + /* If we're out of watchers set the timeout for shutdown */ + if (priv->watchers == NULL) { + if (priv->timeout != 0) { + /* This should never really happen, but let's ensure that + bad things don't happen if it does. */ + g_warning("No watchers timeout set twice. Resolving, but odd."); + g_source_remove(priv->timeout); + priv->timeout = 0; + } + priv->timeout = g_timeout_add(500, timeout_no_watchers, service); + } + + dbus_g_method_return(method); + return TRUE; +} + /* API */ /** diff --git a/libindicator/indicator-service.xml b/libindicator/indicator-service.xml index d876ea8..8b389e2 100644 --- a/libindicator/indicator-service.xml +++ b/libindicator/indicator-service.xml @@ -9,6 +9,9 @@ + + + -- cgit v1.2.3 From c0e2510ba29adc62b7816995031d46f8655d208a Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 10:11:50 -0600 Subject: Finding the watcher and removing him from the list. --- libindicator/indicator-service.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/libindicator/indicator-service.c b/libindicator/indicator-service.c index dda00a9..c6ef077 100644 --- a/libindicator/indicator-service.c +++ b/libindicator/indicator-service.c @@ -310,6 +310,12 @@ _indicator_service_server_watch (IndicatorService * service, DBusGMethodInvocati return TRUE; } +static gint +find_watcher (gconstpointer a, gconstpointer b) +{ + return g_strcmp0((const gchar *)a, (const gchar *)b); +} + static gboolean _indicator_service_server_un_watch (IndicatorService * service, DBusGMethodInvocation * method) { @@ -317,7 +323,16 @@ _indicator_service_server_un_watch (IndicatorService * service, DBusGMethodInvoc IndicatorServicePrivate * priv = INDICATOR_SERVICE_GET_PRIVATE(service); /* Remove us from the watcher list here */ - + GList * watcher_item = g_list_find_custom(priv->watchers, dbus_g_method_get_sender(method), find_watcher); + if (watcher_item != NULL) { + /* Free the watcher */ + gchar * name = watcher_item->data; + priv->watchers = g_list_remove(priv->watchers, name); + g_free(name); + } else { + /* Odd that we couldn't find the person, but, eh */ + g_warning("Unable to find watcher who is unwatching: %s", dbus_g_method_get_sender(method)); + } /* If we're out of watchers set the timeout for shutdown */ if (priv->watchers == NULL) { @@ -328,6 +343,7 @@ _indicator_service_server_un_watch (IndicatorService * service, DBusGMethodInvoc g_source_remove(priv->timeout); priv->timeout = 0; } + /* If we don't get a new watcher quickly, we'll shutdown. */ priv->timeout = g_timeout_add(500, timeout_no_watchers, service); } -- cgit v1.2.3 From 67aef7b9c7d1044c60801a2b021bf1f7234feb18 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 10:12:51 -0600 Subject: Using the define for the version on both sides --- libindicator/indicator-service.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libindicator/indicator-service.c b/libindicator/indicator-service.c index c6ef077..5fb939d 100644 --- a/libindicator/indicator-service.c +++ b/libindicator/indicator-service.c @@ -306,7 +306,7 @@ _indicator_service_server_watch (IndicatorService * service, DBusGMethodInvocati priv->timeout = 0; } - dbus_g_method_return(method, 1); + dbus_g_method_return(method, INDICATOR_SERVICE_VERSION); return TRUE; } -- cgit v1.2.3 From 088eff44dd923a80d2bc357be72b9ace0c97d06e Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 10:19:11 -0600 Subject: When destroying the object we tell the watched service that we're not watching it. --- libindicator/indicator-service-manager.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libindicator/indicator-service-manager.c b/libindicator/indicator-service-manager.c index 4eaed23..956eb90 100644 --- a/libindicator/indicator-service-manager.c +++ b/libindicator/indicator-service-manager.c @@ -51,6 +51,7 @@ static void indicator_service_manager_finalize (GObject *object); static void set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec); static void get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec); static void start_service (IndicatorServiceManager * service); +static void unwatch_cb (DBusGProxy *proxy, GError *error, gpointer userdata); G_DEFINE_TYPE (IndicatorServiceManager, indicator_service_manager, G_TYPE_OBJECT); @@ -147,6 +148,12 @@ indicator_service_manager_dispose (GObject *object) priv->dbus_proxy = NULL; } + /* If we have a proxy, tell it we're shutting down. Just + to be polite about it. */ + if (priv->service_proxy != NULL) { + org_ayatana_indicator_service_un_watch_async(priv->service_proxy, unwatch_cb, NULL); + } + /* Destory our service proxy, we won't need it. */ if (priv->service_proxy != NULL) { g_object_unref(G_OBJECT(priv->service_proxy)); @@ -231,6 +238,12 @@ get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspe return; } +static void +unwatch_cb (DBusGProxy *proxy, GError *error, gpointer userdata) +{ + return; +} + static void watch_cb (DBusGProxy * proxy, gint service_version, GError * error, gpointer user_data) { -- cgit v1.2.3 From 824d1e8bc5bbc240140d6c2cea7246308c563fd8 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 10:20:36 -0600 Subject: Unwatching if we have mismatched version. --- libindicator/indicator-service-manager.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libindicator/indicator-service-manager.c b/libindicator/indicator-service-manager.c index 956eb90..d7e54bd 100644 --- a/libindicator/indicator-service-manager.c +++ b/libindicator/indicator-service-manager.c @@ -257,6 +257,7 @@ watch_cb (DBusGProxy * proxy, gint service_version, GError * error, gpointer use if (service_version != INDICATOR_SERVICE_VERSION) { g_warning("Service is using a different version of the service interface. Expecting %d and got %d.", INDICATOR_SERVICE_VERSION, service_version); + org_ayatana_indicator_service_un_watch_async(priv->service_proxy, unwatch_cb, NULL); return; } -- cgit v1.2.3 From 8f0f2ad3c02996ce2362eab5383f0a4ab9175427 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 10:40:17 -0600 Subject: Making the timeout a failure case, and making it so that we want a graceful shutdown. --- tests/service-manager-connect-service.c | 6 +++--- tests/service-manager-connect.c | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/service-manager-connect-service.c b/tests/service-manager-connect-service.c index 297f3ba..dbdf4fa 100644 --- a/tests/service-manager-connect-service.c +++ b/tests/service-manager-connect-service.c @@ -8,7 +8,7 @@ static gboolean passed = FALSE; gboolean timeout (gpointer data) { - passed = TRUE; + passed = FALSE; g_debug("Timeout with no shutdown."); g_main_loop_quit(mainloop); return FALSE; @@ -17,8 +17,8 @@ timeout (gpointer data) void shutdown (void) { - g_error("Shutdown"); - passed = FALSE; + g_debug("Shutdown"); + passed = TRUE; g_main_loop_quit(mainloop); return; } diff --git a/tests/service-manager-connect.c b/tests/service-manager-connect.c index c252542..b62c2c9 100644 --- a/tests/service-manager-connect.c +++ b/tests/service-manager-connect.c @@ -37,6 +37,8 @@ main (int argc, char ** argv) mainloop = g_main_loop_new(NULL, FALSE); g_main_loop_run(mainloop); + g_object_unref(is); + g_debug("Quiting"); if (passed) { g_debug("Passed"); -- cgit v1.2.3 From d69de93483b5ad9e709f59dac00af7ff7f4df477 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 10:43:10 -0600 Subject: Updating connection prototype to get all the data. --- tests/service-manager-connect.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/service-manager-connect.c b/tests/service-manager-connect.c index b62c2c9..4519b83 100644 --- a/tests/service-manager-connect.c +++ b/tests/service-manager-connect.c @@ -15,7 +15,7 @@ timeout (gpointer data) } void -connection (void) +connection (IndicatorServiceManager * sm, gboolean connected, gpointer user_data) { g_debug("Connection"); passed = TRUE; @@ -30,7 +30,7 @@ main (int argc, char ** argv) g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL); IndicatorServiceManager * is = indicator_service_manager_new("org.ayatana.test"); - g_signal_connect(G_OBJECT(is), INDICATOR_SERVICE_MANAGER_SIGNAL_CONNECTION_CHANGE, connection, NULL); + g_signal_connect(G_OBJECT(is), INDICATOR_SERVICE_MANAGER_SIGNAL_CONNECTION_CHANGE, G_CALLBACK(connection), NULL); g_timeout_add_seconds(1, timeout, NULL); -- cgit v1.2.3 From 6e89bf096ce0c261e2fece0cbeb6289ea21e8c5a Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 10:47:13 -0600 Subject: Checking to make sure we don't get connected twice, that'd be an error. --- tests/service-manager-connect.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/service-manager-connect.c b/tests/service-manager-connect.c index 4519b83..91d2bad 100644 --- a/tests/service-manager-connect.c +++ b/tests/service-manager-connect.c @@ -17,6 +17,20 @@ timeout (gpointer data) void connection (IndicatorServiceManager * sm, gboolean connected, gpointer user_data) { + static gboolean has_connected = FALSE; + + if (has_connected && connected) { + g_warning("We got two connected signals. FAIL."); + passed = FALSE; + return; + } + + if (!connected) { + g_debug("Not connected"); + return; + } + + has_connected = TRUE; g_debug("Connection"); passed = TRUE; g_main_loop_quit(mainloop); -- cgit v1.2.3 From 1b1a50d5d5075929eb18622eb0e8b6fc7bff9814 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 13:35:30 -0600 Subject: Adding a test to ensure that a service gets shutdown by someone unwatching it. --- tests/Makefile.am | 24 ++++++++++++ tests/service-manager-connect-service.c | 2 + tests/service-manager-nostart-connect.c | 65 +++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 tests/service-manager-nostart-connect.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 80c4191..09a227d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -6,6 +6,7 @@ check_PROGRAMS = \ service-manager-no-connect \ service-manager-connect \ service-manager-connect-service \ + service-manager-nostart-connect \ service-shutdown-timeout lib_LTLIBRARIES = \ @@ -199,6 +200,29 @@ service-manager-connect-tester: service-manager-connect service-manager-connect- TESTS += service-manager-connect-tester DISTCLEANFILES += service-manager-connect-tester session.conf service-manager-connect.service +############################# +# Service Manager Shutdown +############################# + +service_manager_nostart_connect_SOURCES = \ + service-manager-nostart-connect.c + +service_manager_nostart_connect_CFLAGS = \ + -Wall -Werror \ + $(LIBINDICATOR_CFLAGS) -I$(top_srcdir) + +service_manager_nostart_connect_LDADD = \ + $(LIBINDICATOR_LIBS) \ + $(top_builddir)/libindicator/.libs/libindicator.a + +service-manager-connect-nostart-tester: service-manager-nostart-connect service-manager-connect-service Makefile.am + @echo "#!/bin/sh" > $@ + @echo dbus-test-runner --task ./service-manager-nostart-connect --task ./service-manager-connect-service >> $@ + @chmod +x $@ + +TESTS += service-manager-connect-nostart-tester +DISTCLEANFILES += service-manager-connect-nostart-tester + ############################# # Test stuff ############################# diff --git a/tests/service-manager-connect-service.c b/tests/service-manager-connect-service.c index dbdf4fa..d60e414 100644 --- a/tests/service-manager-connect-service.c +++ b/tests/service-manager-connect-service.c @@ -28,6 +28,8 @@ main (int argc, char ** argv) { g_type_init(); + g_debug("Starting service"); + IndicatorService * is = indicator_service_new("org.ayatana.test"); g_signal_connect(G_OBJECT(is), INDICATOR_SERVICE_SIGNAL_SHUTDOWN, shutdown, NULL); diff --git a/tests/service-manager-nostart-connect.c b/tests/service-manager-nostart-connect.c new file mode 100644 index 0000000..7107f42 --- /dev/null +++ b/tests/service-manager-nostart-connect.c @@ -0,0 +1,65 @@ + +#include +#include "libindicator/indicator-service-manager.h" + +static GMainLoop * mainloop = NULL; +static gboolean passed = FALSE; + +gboolean +timeout (gpointer data) +{ + passed = FALSE; + g_error("Timeout with no connection."); + g_main_loop_quit(mainloop); + return FALSE; +} + +void +connection (IndicatorServiceManager * sm, gboolean connected, gpointer user_data) +{ + static gboolean has_connected = FALSE; + + if (has_connected && connected) { + g_warning("We got two connected signals. FAIL."); + passed = FALSE; + return; + } + + if (!connected) { + g_debug("Not connected"); + return; + } + + has_connected = TRUE; + g_debug("Connection"); + passed = TRUE; + g_main_loop_quit(mainloop); + return; +} + +int +main (int argc, char ** argv) +{ + g_type_init(); + g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL); + + g_usleep(150000); + + IndicatorServiceManager * is = indicator_service_manager_new("org.ayatana.test"); + g_signal_connect(G_OBJECT(is), INDICATOR_SERVICE_MANAGER_SIGNAL_CONNECTION_CHANGE, G_CALLBACK(connection), NULL); + + g_timeout_add_seconds(1, timeout, NULL); + + mainloop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(mainloop); + + g_object_unref(is); + + g_debug("Quiting"); + if (passed) { + g_debug("Passed"); + return 0; + } + g_debug("Failed"); + return 1; +} -- cgit v1.2.3 From 5757cfb41340a7bc73bd5a4f13de5447121d4d6d Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 13:37:00 -0600 Subject: New files to ignore. --- .bzrignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.bzrignore b/.bzrignore index f11a31f..1baab30 100644 --- a/.bzrignore +++ b/.bzrignore @@ -135,3 +135,5 @@ tests/service-manager-connect-tester tests/session.conf tests/service-manager-connect.service tools/indicator-loader +tests/service-manager-connect-nostart-tester +tests/service-manager-nostart-connect -- cgit v1.2.3 From 4ba1af342c78aefdf5dc163163c80de6b25f85ec Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 13:44:30 -0600 Subject: Trying to get a proxy before starting the service every time. If it exists, let's use it. --- libindicator/indicator-service-manager.c | 37 +++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/libindicator/indicator-service-manager.c b/libindicator/indicator-service-manager.c index d7e54bd..4dcc200 100644 --- a/libindicator/indicator-service-manager.c +++ b/libindicator/indicator-service-manager.c @@ -308,16 +308,43 @@ start_service_cb (DBusGProxy * proxy, guint status, GError * error, gpointer use static void start_service (IndicatorServiceManager * service) { + GError * error = NULL; IndicatorServiceManagerPrivate * priv = INDICATOR_SERVICE_MANAGER_GET_PRIVATE(service); g_return_if_fail(priv->dbus_proxy != NULL); g_return_if_fail(priv->name != NULL); - org_freedesktop_DBus_start_service_by_name_async (priv->dbus_proxy, - priv->name, - 0, - start_service_cb, - service); + /* Check to see if we can get a proxy to it first. */ + DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); + if (error != NULL) { + g_error("Unable to get session bus: %s", error->message); + g_error_free(error); + return; + } + + /* Tries to get the proxy first. */ + priv->service_proxy = dbus_g_proxy_new_for_name_owner(session_bus, + priv->name, + INDICATOR_SERVICE_OBJECT, + INDICATOR_SERVICE_INTERFACE, + &error); + + if (error != NULL) { + /* We don't care about the error, just start the service anyway. */ + g_error_free(error); + org_freedesktop_DBus_start_service_by_name_async (priv->dbus_proxy, + priv->name, + 0, + start_service_cb, + service); + } else { + /* If we got a proxy just because we're good people then + we need to call watch on it just like 'start_service_cb' + does. */ + org_ayatana_indicator_service_watch_async(priv->service_proxy, + watch_cb, + service); + } return; } -- cgit v1.2.3 From b7864d8626cd5bdde3fe94f2086d9a1cfdb66a49 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Dec 2009 16:07:26 -0600 Subject: Only getting the bus once. --- libindicator/indicator-service-manager.c | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/libindicator/indicator-service-manager.c b/libindicator/indicator-service-manager.c index 4dcc200..13c9015 100644 --- a/libindicator/indicator-service-manager.c +++ b/libindicator/indicator-service-manager.c @@ -16,6 +16,7 @@ struct _IndicatorServiceManagerPrivate { DBusGProxy * dbus_proxy; DBusGProxy * service_proxy; gboolean connected; + DBusGConnection * bus; }; /* Signals Stuff */ @@ -106,17 +107,18 @@ indicator_service_manager_init (IndicatorServiceManager *self) priv->dbus_proxy = NULL; priv->service_proxy = NULL; priv->connected = FALSE; + priv->bus = NULL; /* Start talkin' dbus */ GError * error = NULL; - DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); + priv->bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); if (error != NULL) { g_error("Unable to get session bus: %s", error->message); g_error_free(error); return; } - priv->dbus_proxy = dbus_g_proxy_new_for_name_owner(session_bus, + priv->dbus_proxy = dbus_g_proxy_new_for_name_owner(priv->bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, @@ -285,14 +287,7 @@ start_service_cb (DBusGProxy * proxy, guint status, GError * error, gpointer use } /* Woot! it's running. Let's do it some more. */ - DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); - if (error != NULL) { - g_error("Unable to get session bus: %s", error->message); - g_error_free(error); - return; - } - - priv->service_proxy = dbus_g_proxy_new_for_name_owner(session_bus, + priv->service_proxy = dbus_g_proxy_new_for_name_owner(priv->bus, priv->name, INDICATOR_SERVICE_OBJECT, INDICATOR_SERVICE_INTERFACE, @@ -315,15 +310,7 @@ start_service (IndicatorServiceManager * service) g_return_if_fail(priv->name != NULL); /* Check to see if we can get a proxy to it first. */ - DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); - if (error != NULL) { - g_error("Unable to get session bus: %s", error->message); - g_error_free(error); - return; - } - - /* Tries to get the proxy first. */ - priv->service_proxy = dbus_g_proxy_new_for_name_owner(session_bus, + priv->service_proxy = dbus_g_proxy_new_for_name_owner(priv->bus, priv->name, INDICATOR_SERVICE_OBJECT, INDICATOR_SERVICE_INTERFACE, -- cgit v1.2.3