[gnome-pilot/dbus] Register GpilotDaemon to dbus, try dbus-send.



commit 2ba5b3db21c60d0dc4cb43fe3c47aedf208d7162
Author: Halton Huo <halton huo sun com>
Date:   Wed Mar 10 16:16:15 2010 +0800

    Register GpilotDaemon to dbus, try dbus-send.
    
    dbus-send --session --dest=org.gnome.GnomePilot \
    --type=method_call --print-reply --reply-timeout=2000 \
    /org/gnome/GnomePilot/Daemon \
    org.gnome.GnomePilot.Daemon.Noop

 .gitignore                                         |    2 +-
 gpilotd/Makefile.am                                |   12 +-
 gpilotd/gnome-pilot-client.gob                     |    6 +-
 gpilotd/gpilot-daemon.c                            |  114 +++++++++++++++++---
 gpilotd/gpilot-daemon.h                            |   16 ++--
 gpilotd/gpilotd.c                                  |   99 ++++++++++++++++-
 ....service.in => org.gnome.GnomePilot.service.in} |    2 +-
 7 files changed, 212 insertions(+), 39 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 171ef96..280b93e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -56,7 +56,7 @@ conduits/file/file.conduit
 conduits/file/gpilot-install-file
 conduits/file/palm.keys
 conduits/test/test.conduit
-gpilotd/org.gnome.GPilotd.service
+gpilotd/org.gnome.GnomePilot.service
 gpilotd/gpilot-daemon-glue.h
 gpilotd/gnome-pilot-client-private.h
 gpilotd/gnome-pilot-client.c
diff --git a/gpilotd/Makefile.am b/gpilotd/Makefile.am
index fc11c32..4df19b0 100644
--- a/gpilotd/Makefile.am
+++ b/gpilotd/Makefile.am
@@ -106,8 +106,8 @@ DBUS_SRCLIST = 		\
 
 BUILT_SOURCES = $(DBUS_SRCLIST) $(GOB_BUILT_SRCS) gpmarshal.c gpmarshal.h
 
-gpilot-daemon-glue.h: gpilot-daemon.xml Makefile.am
-	dbus-binding-tool --prefix=gpilot_daemon --mode=glib-server --output=gpilot-daemon-glue.h $(srcdir)/gpilot-daemon.xml
+gpilot-daemon-glue.h: gpilot-daemon.xml
+	dbus-binding-tool --prefix=gpilot_daemon --mode=glib-server $< > gpilot-daemon-glue.h
 
 CLEANFILES = $(DBUS_SRCLIST) $(service_DATA)
 MAINTAINERCLEANFILES = $(GOB_BUILT_SRCS)
@@ -221,9 +221,9 @@ gpilotd_session_wrapper_LDADD = 	\
 
 
 servicedir = $(datadir)/dbus-1/services
-service_DATA = org.gnome.GPilotd.service
+service_DATA = org.gnome.GnomePilot.service
 
-org.gnome.GPilotd.service: org.gnome.GPilotd.service.in \
+org.gnome.GnomePilot.service: org.gnome.GnomePilot.service.in \
 	Makefile
 	@sed -e "s|[ ]libexecdir[@]|$(libexecdir)|" $< > $@
 
@@ -234,11 +234,11 @@ devicedir  = $(datadir)/gnome-pilot
 device_DATA = devices.xml
 
 EXTRA_DIST = 			\
-	org.gnome.GPilotd.service.in \
+	org.gnome.GnomePilot.service.in \
 	gpmarshal.list		\
 	gpmarshal.h		\
 	gpmarshal.c		\
-	gpilotd-daemon.xml      \
+	gpilot-daemon.xml	\
 	$(gobfiles) 		\
 	$(mime_DATA)		\
 	$(device_DATA)		\
diff --git a/gpilotd/gnome-pilot-client.gob b/gpilotd/gnome-pilot-client.gob
index c5bfc99..2dff904 100644
--- a/gpilotd/gnome-pilot-client.gob
+++ b/gpilotd/gnome-pilot-client.gob
@@ -50,9 +50,9 @@
 
 #include <gpilotd/gpilot-daemon.h>
 
-#define GP_DBUS_NAME              "org.gnome.GPilotd"
-#define GP_DBUS_PATH              "/org/gnome/GPilotd"
-#define GP_DBUS_INTERFACE         "org.gnome.GPilotd"
+#define GP_DBUS_NAME              "org.gnome.GnomePilot"
+#define GP_DBUS_PATH              "/org/gnome/GnomePilot/Daemon"
+#define GP_DBUS_INTERFACE         "org.gnome.GnomePilot.Daemon"
 
 #define IS_STR_SET(x) (x != NULL && x[0] != '\0')
 
diff --git a/gpilotd/gpilot-daemon.c b/gpilotd/gpilot-daemon.c
index 4619ba6..011a895 100644
--- a/gpilotd/gpilot-daemon.c
+++ b/gpilotd/gpilot-daemon.c
@@ -59,6 +59,8 @@
 #include <libhal.h>
 #endif
 
+#define GP_DBUS_PATH         "/org/gnome/GnomePilot/Daemon"
+
 #define GPILOT_DAEMON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GPILOT_TYPE_DAEMON, GpilotDaemonPrivate))
 
 #define DEBUG_CODE
@@ -67,6 +69,8 @@ static void     gpilot_daemon_class_init  (GpilotDaemonClass *klass);
 static void     gpilot_daemon_init        (GpilotDaemon      *daemon);
 static void     gpilot_daemon_finalize    (GObject           *object);
 
+static gpointer daemon_object = NULL;
+
 /* Set to true when the config should be reloaded */
 static void monitor_channel (GPilotDevice *dev, GPilotContext *context);
 static gboolean device_in (GIOChannel *, GIOCondition, GPilotContext *);
@@ -102,6 +106,9 @@ struct GpilotDaemonPrivate
         GHashTable *notifications;
         /* maps from a client_id to GSList** of pilotnames begin monitored */
         GHashTable *monitors;
+
+        DBusGProxy      *bus_proxy;
+        DBusGConnection *connection;
 };
 
 enum {
@@ -1686,7 +1693,13 @@ gpilot_daemon_reread_config (GpilotDaemon   *daemon,
 }
 
 /* A no-operation call, used by client to occasionally
-   check to see if the daemon has blown up */
+   check to see if the daemon has blown up
+Example:
+dbus-send --session --dest=org.gnome.GnomePilot \
+--type=method_call --print-reply --reply-timeout=2000 \
+/org/gnome/GnomePilot/Daemon \
+org.gnome.GnomePilot.Daemon.Noop
+*/
 gboolean
 gpilot_daemon_noop (GpilotDaemon   *daemon)
 {
@@ -1699,7 +1712,7 @@ gpilot_daemon_request_install (GpilotDaemon   *daemon,
                                const char     *pilot_name,
                                const char     *filename,
                                const char     *description,
-                               GNOME_Pilot_Survival        survival,
+                               GNOME_Pilot_Survival survival,
                                unsigned long   timeout,
                                unsigned long  *handle,
                                GError        **error)
@@ -1760,7 +1773,7 @@ gboolean
 gpilot_daemon_request_restore (GpilotDaemon   *daemon,
                                const char     *pilot_name,
                                const char     *directory,
-                               GNOME_Pilot_Survival        survival,
+                               GNOME_Pilot_Survival survival,
                                unsigned long   timeout,
                                unsigned long  *handle,
                                GError        **error)
@@ -1822,7 +1835,7 @@ gpilot_daemon_request_conduit (GpilotDaemon   *daemon,
                                const char     *pilot_name,
                                const char     *conduit_name,
                                GNOME_Pilot_ConduitOperation operation,
-                               GNOME_Pilot_Survival        survival,
+                               GNOME_Pilot_Survival survival,
                                unsigned long   timeout,
                                unsigned long  *handle,
                                GError        **error)
@@ -1904,7 +1917,7 @@ gpilot_daemon_remove_request (GpilotDaemon   *daemon,
 gboolean
 gpilot_daemon_get_system_info (GpilotDaemon   *daemon,
                                const char     *cradle,
-                               GNOME_Pilot_Survival        survival,
+                               GNOME_Pilot_Survival survival,
                                unsigned long   timeout,
                                unsigned long  *handle,
                                GError        **error)
@@ -2239,10 +2252,16 @@ gpilot_daemon_get_databases_from_cache (GpilotDaemon   *daemon,
 }
 
 /* admin operations */
+/* Example:
+dbus-send --session --dest=org.gnome.GnomePilot \
+--type=method_call --print-reply --reply-timeout=2000 \
+/org/gnome/GnomePilot/Daemon \
+org.gnome.GnomePilot.Daemon.GetUserInfo
+*/
 gboolean
 gpilot_daemon_get_user_info (GpilotDaemon   *daemon,
                              const char     *cradle,
-                             GNOME_Pilot_Survival        survival,
+                             GNOME_Pilot_Survival survival,
                              unsigned long   timeout,
                              unsigned long  *handle,
                              GError        **error)
@@ -2277,7 +2296,7 @@ gboolean
 gpilot_daemon_set_user_info (GpilotDaemon   *daemon,
                              const char     *cradle,
                              gboolean        continue_sync,
-                             GNOME_Pilot_Survival        survival,
+                             GNOME_Pilot_Survival survival,
                              unsigned long   timeout,
                              unsigned long   uid,
                              const char     *username,
@@ -2808,6 +2827,53 @@ dbus_notify_daemon_error (const gchar       *pilot_id,
 }
 
 static void
+bus_name_owner_changed (DBusGProxy   *bus_proxy,
+                        const char   *service_name,
+                        const char   *old_service_name,
+                        const char   *new_service_name,
+                        GpilotDaemon *daemon)
+{
+        g_debug ("NameOwnerChanged: service_name='%s', old_service_name='%s' new_service_name='%s'",
+                   service_name, old_service_name, new_service_name);
+}
+
+static gboolean
+register_daemon (GpilotDaemon *daemon)
+{
+        GError *error = NULL;
+
+        daemon->priv->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
+        if (daemon->priv->connection == NULL) {
+                if (error != NULL) {
+                        g_critical ("error getting session bus: %s", error->message);
+                        g_error_free (error);
+                }
+                exit (1);
+        }
+
+        daemon->priv->bus_proxy = dbus_g_proxy_new_for_name (daemon->priv->connection,
+                                                              DBUS_SERVICE_DBUS,
+                                                              DBUS_PATH_DBUS,
+                                                              DBUS_INTERFACE_DBUS);
+
+        dbus_g_proxy_add_signal (daemon->priv->bus_proxy,
+                                 "NameOwnerChanged",
+                                 G_TYPE_STRING,
+                                 G_TYPE_STRING,
+                                 G_TYPE_STRING,
+                                 G_TYPE_INVALID);
+        dbus_g_proxy_connect_signal (daemon->priv->bus_proxy,
+                                     "NameOwnerChanged",
+                                     G_CALLBACK (bus_name_owner_changed),
+                                     daemon,
+                                     NULL);
+
+        dbus_g_connection_register_g_object (daemon->priv->connection, GP_DBUS_PATH, G_OBJECT (daemon));
+
+        return TRUE;
+}
+
+static void
 gpilot_daemon_class_init (GpilotDaemonClass *klass)
 {
         GObjectClass   *object_class = G_OBJECT_CLASS (klass);
@@ -2847,10 +2913,10 @@ gpilot_daemon_class_init (GpilotDaemonClass *klass)
                                             2,
                                             G_TYPE_STRING,
                                             G_TYPE_UINT);
-        signals [USERINFO_REQUESTED] = g_signal_new ("userinfo-requested",
+        signals [USERINFO_REQUESTED] = g_signal_new ("user-info-requested",
                                             G_TYPE_FROM_CLASS (object_class),
                                             G_SIGNAL_RUN_LAST,
-                                            G_STRUCT_OFFSET (GpilotDaemonClass, userinfo_requested),
+                                            G_STRUCT_OFFSET (GpilotDaemonClass, user_info_requested),
                                             NULL,
                                             NULL,
                                             gp_marshal_VOID__STRING_UINT_STRING,
@@ -2859,10 +2925,10 @@ gpilot_daemon_class_init (GpilotDaemonClass *klass)
                                             G_TYPE_STRING,
                                             G_TYPE_UINT,
                                             G_TYPE_STRING);
-        signals [SYSINFO_REQUESTED] = g_signal_new ("sysinfo-requested",
+        signals [SYSINFO_REQUESTED] = g_signal_new ("sys-info-requested",
                                             G_TYPE_FROM_CLASS (object_class),
                                             G_SIGNAL_RUN_LAST,
-                                            G_STRUCT_OFFSET (GpilotDaemonClass, sysinfo_requested),
+                                            G_STRUCT_OFFSET (GpilotDaemonClass, sys_info_requested),
                                             NULL,
                                             NULL,
                                             gp_marshal_VOID__STRING_UINT_UINT_UINT_STRING_STRING_STRING_UINT,
@@ -2982,6 +3048,8 @@ gpilot_daemon_class_init (GpilotDaemonClass *klass)
                                             G_TYPE_BOOLEAN);
 
         g_type_class_add_private (klass, sizeof (GpilotDaemonPrivate));
+
+        dbus_g_object_type_install_info (GPILOT_TYPE_DAEMON, &dbus_glib_gpilot_daemon_object_info);
 }
 
 static void
@@ -3054,7 +3122,7 @@ gpilot_daemon_finalize (GObject *object)
         g_return_if_fail (object != NULL);
         g_return_if_fail (GPILOT_IS_DAEMON (object));
 
-        daemon= GPILOT_DAEMON(object);
+        daemon= GPILOT_DAEMON (object);
 
         g_return_if_fail (daemon->priv != NULL);
 
@@ -3067,15 +3135,31 @@ gpilot_daemon_finalize (GObject *object)
                                     NULL);
         g_hash_table_destroy (daemon->priv->notifications);
 
+        if (daemon->priv->bus_proxy != NULL) {
+                g_object_unref (daemon->priv->bus_proxy);    
+        }
+
         G_OBJECT_CLASS (gpilot_daemon_parent_class)->finalize (object);
 }
 
 GpilotDaemon*
 gpilot_daemon_new ()
 {
-        GObject *object;
+        if (daemon_object != NULL) {
+                g_object_ref (daemon_object);
+        } else {
+                gboolean res;
+
+                daemon_object = g_object_new (GPILOT_TYPE_DAEMON, NULL);
+                g_object_add_weak_pointer (daemon_object,
+                                           (gpointer *) &daemon_object);
+                res = register_daemon (daemon_object);
+                if (! res) {
+                        g_object_unref (daemon_object);
+                        return NULL;
+                }
 
-        object = g_object_new (GPILOT_TYPE_DAEMON, NULL);
+        }
 
-        return GPILOT_DAEMON (object);
+        return GPILOT_DAEMON (daemon_object);
 }
diff --git a/gpilotd/gpilot-daemon.h b/gpilotd/gpilot-daemon.h
index 82957c4..7af15e1 100644
--- a/gpilotd/gpilot-daemon.h
+++ b/gpilotd/gpilot-daemon.h
@@ -59,11 +59,11 @@ typedef struct
         void          (* request_completed)  (GpilotDaemon   *gpilot,
                                               const char     *pilot_id,
                                               unsigned long   request_id);
-        void          (* userinfo_requested) (GpilotDaemon   *gpilot,
+        void          (* user_info_requested) (GpilotDaemon   *gpilot,
                                               const char     *device,
                                               unsigned long   uid,
                                               const char     *username);
-        void          (* sysinfo_requested)  (GpilotDaemon   *gpilot,
+        void          (* sys_info_requested)  (GpilotDaemon   *gpilot,
                                               const char     *pilot_id,
                                               int             rom_size,
                                               int             ram_size,
@@ -182,14 +182,14 @@ gboolean        gpilot_daemon_request_install   (GpilotDaemon   *daemon,
                                                  const char     *pilot_name,
                                                  const char     *filename,
                                                  const char     *description,
-                                                 GNOME_Pilot_Survival        survival,
+                                                 GNOME_Pilot_Survival survival,
                                                  unsigned long   timeout,
                                                  unsigned long  *handle,
                                                  GError        **error);
 gboolean        gpilot_daemon_request_restore   (GpilotDaemon   *daemon,
                                                  const char     *pilot_name,
                                                  const char     *directory,
-                                                 GNOME_Pilot_Survival        survival,
+                                                 GNOME_Pilot_Survival survival,
                                                  unsigned long   timeout,
                                                  unsigned long  *handle,
                                                  GError        **error);
@@ -197,7 +197,7 @@ gboolean        gpilot_daemon_request_conduit   (GpilotDaemon   *daemon,
                                                  const char     *pilot_name,
                                                  const char     *conduit_name,
                                                  GNOME_Pilot_ConduitOperation operation,
-                                                 GNOME_Pilot_Survival        survival,
+                                                 GNOME_Pilot_Survival survival,
                                                  unsigned long   timeout,
                                                  unsigned long  *handle,
                                                  GError        **error);
@@ -207,7 +207,7 @@ gboolean        gpilot_daemon_remove_request    (GpilotDaemon   *daemon,
 /* information operations */
 gboolean        gpilot_daemon_get_system_info   (GpilotDaemon   *daemon,
                                                  const char     *cradle,
-                                                 GNOME_Pilot_Survival        survival,
+                                                 GNOME_Pilot_Survival survival,
                                                  unsigned long   timeout,
                                                  unsigned long  *handle,
                                                  GError        **error);
@@ -266,14 +266,14 @@ gboolean        gpilot_daemon_get_databases_from_cache
 /* admin operations */
 gboolean        gpilot_daemon_get_user_info     (GpilotDaemon   *daemon,
                                                  const char     *cradle,
-                                                 GNOME_Pilot_Survival        survival,
+                                                 GNOME_Pilot_Survival survival,
                                                  unsigned long   timeout,
                                                  unsigned long  *handle,
                                                  GError        **error);
 gboolean        gpilot_daemon_set_user_info     (GpilotDaemon   *daemon,
                                                  const char     *cradle,
                                                  gboolean        continue_sync,
-                                                 GNOME_Pilot_Survival        survival,
+                                                 GNOME_Pilot_Survival survival,
                                                  unsigned long   timeout,
                                                  unsigned long   uid,
                                                  const char     *username,
diff --git a/gpilotd/gpilotd.c b/gpilotd/gpilotd.c
index 33057cb..34e72a7 100644
--- a/gpilotd/gpilotd.c
+++ b/gpilotd/gpilotd.c
@@ -27,10 +27,19 @@
 #include "config.h"
 #endif
 
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <glib.h>
 #include <glib/gi18n.h>
 
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
 #include "gpilot-daemon.h"
 
+#define GPILOTD_DBUS_NAME         "org.gnome.GnomePilot"
+
 static void remove_pid_file (void);
 
 static void 
@@ -138,12 +147,82 @@ dump_build_info (void)
 	g_string_free (str, TRUE);
 }
 
+static DBusGConnection *
+get_session_bus (void)
+{
+        GError          *error;
+        DBusGConnection *bus;
+        DBusConnection  *connection;
+                        
+        error = NULL;
+        bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
+        if (bus == NULL) {
+                g_warning ("Couldn't connect to system bus: %s",
+                           error->message);
+                g_error_free (error);
+                goto out;
+        }
+
+        connection = dbus_g_connection_get_connection (bus);
+        dbus_connection_set_exit_on_disconnect (connection, FALSE);
+
+ out:
+        return bus;
+}
+
+static gboolean
+dbus_acquire_name (DBusGConnection *connection)
+{
+        DBusGProxy *bus_proxy;
+        GError     *error;
+        guint       result;
+        gboolean    ret;
+
+        ret = FALSE;
+        bus_proxy = dbus_g_proxy_new_for_name (connection,
+                                               DBUS_SERVICE_DBUS,
+                                               DBUS_PATH_DBUS,
+                                               DBUS_INTERFACE_DBUS);
+        if (bus_proxy == NULL) {
+                goto out;
+        }
+
+        error = NULL;
+        if (!dbus_g_proxy_call (bus_proxy,
+                                "RequestName",
+                                &error,
+                                G_TYPE_STRING, GPILOTD_DBUS_NAME,
+                                G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
+                                G_TYPE_INVALID,
+                                G_TYPE_UINT, &result,
+                                G_TYPE_INVALID)) {
+                if (error != NULL) {
+                        g_warning ("Failed to acquire %s: %s", GPILOTD_DBUS_NAME, error->message);
+                        g_error_free (error);
+                } else {
+                        g_warning ("Failed to acquire %s", GPILOTD_DBUS_NAME);
+                }
+                goto out;
+        }
+
+        if (result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
+                g_warning (_("gpilotd already running; exiting ...\n"));
+                goto out;
+        }
+
+        ret = TRUE;
+
+ out:
+        return ret;
+}
+
 int 
 main (int argc, char *argv[])
 {
-        GMainLoop    *loop;
-        GpilotDaemon *daemon;
-        int           ret;
+        GMainLoop       *loop;
+        GpilotDaemon    *daemon;
+        DBusGConnection *connection;
+        int              ret;
 
 	/*g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
 				G_LOG_LEVEL_CRITICAL |
@@ -154,8 +233,6 @@ main (int argc, char *argv[])
 	g_message ("%s %s starting...", PACKAGE, VERSION);
 	dump_build_info ();
 	
-        loop = g_main_loop_new (NULL, FALSE);
-
 #ifdef WITH_HAL
 	if ((hal_ctx = gpilotd_hal_init ()) != NULL)
 		hal_initialised = 1; /* if 0, fall back to polling sysfs */
@@ -167,11 +244,23 @@ main (int argc, char *argv[])
 	
 	g_type_init ();
 
+        connection = get_session_bus ();
+        if (connection == NULL) {
+                goto out;
+        }
+
         daemon = gpilot_daemon_new ();
         if (daemon == NULL) {
                 goto out;
         }
 
+        if (! dbus_acquire_name (connection) ) {
+                g_warning ("Could not acquire name; bailing out");
+                goto out;
+        }
+
+        loop = g_main_loop_new (NULL, FALSE);
+
 	signal (SIGTERM, sig_term_handler);
 	signal (SIGINT, sig_int_handler);
 	signal (SIGHUP, sig_hup_handler);
diff --git a/gpilotd/org.gnome.GPilotd.service.in b/gpilotd/org.gnome.GnomePilot.service.in
similarity index 61%
rename from gpilotd/org.gnome.GPilotd.service.in
rename to gpilotd/org.gnome.GnomePilot.service.in
index 2f7b4f0..45140a5 100644
--- a/gpilotd/org.gnome.GPilotd.service.in
+++ b/gpilotd/org.gnome.GnomePilot.service.in
@@ -1,3 +1,3 @@
 [D-BUS Service]
-Name=org.gnome.GPilotd
+Name=org.gnome.GnomePilot
 Exec= libexecdir@/gpilotd



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]