Use GApplication to gain bus ownership of org.genivi.LUCHandler1
authorJannis Pohlmann <jannis.pohlmann@codethink.co.uk>
Wed, 13 Jun 2012 11:18:37 +0000 (12:18 +0100)
committerJannis Pohlmann <jannis.pohlmann@codethink.co.uk>
Wed, 13 Jun 2012 11:18:37 +0000 (12:18 +0100)
In order for the service to respond to client requests properly, we
need to own the org.genivi.LUCHandler1 name on the bus. GApplication
transparently handles this for us. This commit adds a new
LUCHandlerApplication class and uses that for managing the daemon's
lifecycle, including bus ownership, main loop and shutdown after a
period of inactivity.

luc-handler/Makefile.am
luc-handler/luc-handler-application.c [new file with mode: 0644]
luc-handler/luc-handler-application.h [new file with mode: 0644]
luc-handler/main.c

index 8dc5fbc..bf1992f 100644 (file)
@@ -16,6 +16,8 @@ luc_handler_built_sources =                                           \
 luc_handler_SOURCES =                                                  \
        ../common/watchdog-client.c                                     \
        ../common/watchdog-client.h                                     \
+       luc-handler-application.c                                       \
+       luc-handler-application.h                                       \
        luc-handler-service.c                                           \
        luc-handler-service.h                                           \
        main.c                                                          \
diff --git a/luc-handler/luc-handler-application.c b/luc-handler/luc-handler-application.c
new file mode 100644 (file)
index 0000000..a1bafa0
--- /dev/null
@@ -0,0 +1,180 @@
+/* vi:set et ai sw=2 sts=2 ts=2: */
+/* -
+ * Copyright (c) 2012 GENIVI.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+#include <common/watchdog-client.h>
+
+#include <luc-handler/luc-handler-dbus.h>
+#include <luc-handler/luc-handler-application.h>
+#include <luc-handler/luc-handler-service.h>
+
+
+
+/* property identifiers */
+enum
+{
+  PROP_0,
+  PROP_LUC_HANDLER_SERVICE,
+};
+
+
+
+static void luc_handler_application_finalize     (GObject      *object);
+static void luc_handler_application_get_property (GObject      *object,
+                                                  guint         prop_id,
+                                                  GValue       *value,
+                                                  GParamSpec   *pspec);
+static void luc_handler_application_set_property (GObject      *object,
+                                                  guint         prop_id,
+                                                  const GValue *value,
+                                                  GParamSpec   *pspec);
+static void luc_handler_application_startup      (GApplication *application);
+
+
+
+struct _LUCHandlerApplicationClass
+{
+  GApplicationClass __parent__;
+};
+
+struct _LUCHandlerApplication
+{
+  GApplication       __parent__;
+
+  /* systemd watchdog client that repeatedly asks systemd to update
+   * the watchdog timestamp */
+  WatchdogClient    *watchdog_client;
+
+  /* service object that implements the LUC Handler D-Bus interface */
+  LUCHandlerService *service;
+};
+
+
+
+G_DEFINE_TYPE (LUCHandlerApplication, luc_handler_application, G_TYPE_APPLICATION);
+
+
+
+static void
+luc_handler_application_class_init (LUCHandlerApplicationClass *klass)
+{
+  GApplicationClass *gapplication_class;
+  GObjectClass *gobject_class;
+
+  gobject_class = G_OBJECT_CLASS (klass);
+  gobject_class->finalize = luc_handler_application_finalize;
+  gobject_class->get_property = luc_handler_application_get_property;
+  gobject_class->set_property = luc_handler_application_set_property;
+
+  gapplication_class = G_APPLICATION_CLASS (klass);
+  gapplication_class->startup = luc_handler_application_startup;
+
+  g_object_class_install_property (gobject_class,
+                                   PROP_LUC_HANDLER_SERVICE,
+                                   g_param_spec_object ("luc-handler-service",
+                                                        "luc-handler-service",
+                                                        "luc-handler-service",
+                                                        LUC_HANDLER_TYPE_SERVICE,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT_ONLY));
+}
+
+
+
+static void
+luc_handler_application_init (LUCHandlerApplication *application)
+{
+}
+
+
+
+static void
+luc_handler_application_finalize (GObject *object)
+{
+  LUCHandlerApplication *application = LUC_HANDLER_APPLICATION (object);
+
+  /* release the watchdog client */
+  g_object_unref (application->watchdog_client);
+
+  /* release the LUC Handler service implementation */
+  if (application->service != NULL)
+    g_object_unref (application->service);
+
+  (*G_OBJECT_CLASS (luc_handler_application_parent_class)->finalize) (object);
+}
+
+
+
+static void
+luc_handler_application_get_property (GObject      *object,
+                                      guint         prop_id,
+                                      GValue       *value,
+                                      GParamSpec   *pspec)
+{
+  LUCHandlerApplication *application = LUC_HANDLER_APPLICATION (object);
+
+  switch (prop_id)
+    {
+    case PROP_LUC_HANDLER_SERVICE:
+      g_value_set_object (value, application->service);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+
+
+static void luc_handler_application_set_property (GObject      *object,
+                                                  guint         prop_id,
+                                                  const GValue *value,
+                                                  GParamSpec   *pspec)
+{
+  LUCHandlerApplication *application = LUC_HANDLER_APPLICATION (object);
+
+  switch (prop_id)
+    {
+    case PROP_LUC_HANDLER_SERVICE:
+      application->service = g_value_dup_object (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+
+
+static void
+luc_handler_application_startup (GApplication *app)
+{
+  LUCHandlerApplication *application = LUC_HANDLER_APPLICATION (app);
+
+  /* update systemd's watchdog timestamp every 120 seconds */
+  application->watchdog_client = watchdog_client_new (120);
+}
+
+
+
+LUCHandlerApplication *
+luc_handler_application_new (LUCHandlerService *service)
+{
+  return g_object_new (LUC_HANDLER_TYPE_APPLICATION,
+                       "application-id", "org.genivi.LUCHandler1",
+                       "flags", G_APPLICATION_IS_SERVICE,
+                       "luc-handler-service", service,
+                       NULL);
+}
diff --git a/luc-handler/luc-handler-application.h b/luc-handler/luc-handler-application.h
new file mode 100644 (file)
index 0000000..6dd0c18
--- /dev/null
@@ -0,0 +1,34 @@
+/* vi:set et ai sw=2 sts=2 ts=2: */
+/* -
+ * Copyright (c) 2012 GENIVI.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef __LUC_HANDLER_APPLICATION_H__
+#define __LUC_HANDLER_APPLICATION_H__
+
+#include <luc-handler/luc-handler-service.h>
+
+G_BEGIN_DECLS
+
+#define LUC_HANDLER_TYPE_APPLICATION            (luc_handler_application_get_type ())
+#define LUC_HANDLER_APPLICATION(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), LUC_HANDLER_TYPE_APPLICATION, LUCHandlerApplication))
+#define LUC_HANDLER_APPLICATION_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), LUC_HANDLER_TYPE_APPLICATION, LUCHandlerApplicationClass))
+#define LUC_HANDLER_IS_APPLICATION(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), LUC_HANDLER_TYPE_APPLICATION))
+#define LUC_HANDLER_IS_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), LUC_HANDLER_TYPE_APPLICATION)
+#define LUC_HANDLER_APPLICATION_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), LUC_HANDLER_TYPE_APPLICATION, LUCHandlerApplicationClass))
+
+typedef struct _LUCHandlerApplicationClass LUCHandlerApplicationClass;
+typedef struct _LUCHandlerApplication      LUCHandlerApplication;
+
+GType                 luc_handler_application_get_type (void) G_GNUC_CONST;
+
+LUCHandlerApplication *luc_handler_application_new      (LUCHandlerService *service) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
+
+G_END_DECLS
+
+#endif /* !__LUC_HANDLER_APPLICATION_H__ */
+
index 4b2c6a1..7caf0c6 100644 (file)
@@ -18,8 +18,7 @@
 #include <glib.h>
 #include <gio/gio.h>
 
-#include <common/watchdog-client.h>
-
+#include <luc-handler/luc-handler-application.h>
 #include <luc-handler/luc-handler-service.h>
 
 
@@ -28,12 +27,12 @@ int
 main (int    argc,
       char **argv)
 {
-  LUCHandlerService *service;
-  GDBusConnection   *connection;
-  WatchdogClient    *watchdog_client;
-  GMainLoop         *main_loop;
-  GError            *error = NULL;
-  gint               exit_status = EXIT_SUCCESS;
+  LUCHandlerApplication *application;
+  LUCHandlerService     *service;
+  GDBusConnection       *connection;
+  GMainLoop             *main_loop;
+  GError                *error = NULL;
+  gint                   exit_status = EXIT_SUCCESS;
 
   /* initialize the GType type system */
   g_type_init ();
@@ -64,17 +63,14 @@ main (int    argc,
       return EXIT_FAILURE;
     }
 
-  /* update systemd's watchdog timestamp every 120 seconds */
-  watchdog_client = watchdog_client_new (120);
-
-  /* create and run the application's main loop */
-  main_loop = g_main_loop_new (NULL, FALSE);
-  g_main_loop_run (main_loop);
+  /* create and run the main application */
+  application = luc_handler_application_new (service);
+  exit_status = g_application_run (G_APPLICATION (application), 0, NULL);
+  g_object_unref (application);
 
   /* release allocated objects */
-  g_object_unref (watchdog_client);
   g_object_unref (service);
   g_object_unref (connection);
 
-  return EXIT_SUCCESS;
+  return exit_status;
 }