1 /* vi:set et ai sw=2 sts=2 ts=2: */
3 * Copyright (c) 2012 GENIVI.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 #include <glib-object.h>
16 #include <glib-unix.h>
18 #include <systemd/sd-daemon.h>
20 #include <common/nsm-consumer-dbus.h>
21 #include <common/nsm-lifecycle-control-dbus.h>
22 #include <common/watchdog-client.h>
24 #include <nsm-dummy/nsm-consumer-service.h>
25 #include <nsm-dummy/nsm-dummy-application.h>
26 #include <nsm-dummy/nsm-lifecycle-control-service.h>
30 /* property identifiers */
35 PROP_CONSUMER_SERVICE,
36 PROP_LIFECYCLE_CONTROL_SERVICE
40 static void nsm_dummy_application_constructed (GObject *object);
41 static void nsm_dummy_application_finalize (GObject *object);
42 static void nsm_dummy_application_get_property (GObject *object,
46 static void nsm_dummy_application_set_property (GObject *object,
50 static void nsm_dummy_application_startup (GApplication *application);
51 static gboolean nsm_dummy_application_int_handler (GApplication *application);
55 struct _NSMDummyApplicationClass
57 GApplicationClass __parent__;
60 struct _NSMDummyApplication
62 GApplication __parent__;
64 /* the connection to D-Bus */
65 GDBusConnection *connection;
67 /* systemd watchdog client that repeatedly asks systemd to update
68 * the watchdog timestamp */
69 WatchdogClient *watchdog_client;
71 /* service objects that implements the NSM Dummy D-Bus interfaces */
72 NSMLifecycleControlService *lifecycle_control_service;
73 NSMConsumerService *consumer_service;
75 /* signal handler IDs */
78 /* Identifier for the registered bus name */
84 G_DEFINE_TYPE (NSMDummyApplication, nsm_dummy_application, G_TYPE_APPLICATION);
89 nsm_dummy_application_class_init (NSMDummyApplicationClass *klass)
91 GApplicationClass *gapplication_class;
92 GObjectClass *gobject_class;
94 gobject_class = G_OBJECT_CLASS (klass);
95 gobject_class->finalize = nsm_dummy_application_finalize;
96 gobject_class->constructed = nsm_dummy_application_constructed;
97 gobject_class->get_property = nsm_dummy_application_get_property;
98 gobject_class->set_property = nsm_dummy_application_set_property;
100 gapplication_class = G_APPLICATION_CLASS (klass);
101 gapplication_class->startup = nsm_dummy_application_startup;
103 g_object_class_install_property (gobject_class,
105 g_param_spec_object ("connection",
107 "The connection to D-Bus",
108 G_TYPE_DBUS_CONNECTION,
110 G_PARAM_CONSTRUCT_ONLY |
111 G_PARAM_STATIC_STRINGS));
113 g_object_class_install_property (gobject_class,
114 PROP_CONSUMER_SERVICE,
115 g_param_spec_object ("nsm-consumer-service",
116 "nsm-consumer-service",
117 "nsm-consumer-service",
118 NSM_CONSUMER_TYPE_SERVICE,
120 G_PARAM_CONSTRUCT_ONLY |
121 G_PARAM_STATIC_STRINGS));
122 g_object_class_install_property (gobject_class,
123 PROP_LIFECYCLE_CONTROL_SERVICE,
124 g_param_spec_object ("nsm-lifecycle-control-service",
125 "nsm-lifecycle-control-service",
126 "nsm-lifecycle-control-service",
127 NSM_LIFECYCLE_CONTROL_TYPE_SERVICE,
129 G_PARAM_CONSTRUCT_ONLY |
130 G_PARAM_STATIC_STRINGS));
132 /* inform systemd that this process has started */
133 sd_notify (0, "READY=1");
139 nsm_dummy_application_init (NSMDummyApplication *application)
141 /* update systemd's watchdog timestamp every 120 seconds */
142 application->watchdog_client = watchdog_client_new (120);
144 /* install the signal handler */
145 application->sigint_id =
146 g_unix_signal_add (SIGINT, (GSourceFunc) nsm_dummy_application_int_handler,
153 nsm_dummy_application_finalize (GObject *object)
155 NSMDummyApplication *application = NSM_DUMMY_APPLICATION (object);
157 /* release the D-Bus connection object */
158 if (application->connection != NULL)
159 g_object_unref (application->connection);
161 /* release the bus name */
162 g_bus_unown_name (application->bus_name_id);
164 /* release the signal handler */
165 g_source_remove (application->sigint_id);
167 /* release the watchdog client */
168 g_object_unref (application->watchdog_client);
170 /* release the NSM Dummy service implementations */
171 if (application->consumer_service != NULL)
172 g_object_unref (application->consumer_service);
174 if (application->lifecycle_control_service != NULL)
175 g_object_unref (application->lifecycle_control_service);
177 (*G_OBJECT_CLASS (nsm_dummy_application_parent_class)->finalize) (object);
183 nsm_dummy_application_constructed (GObject *object)
185 NSMDummyApplication *application = NSM_DUMMY_APPLICATION (object);
187 /* get a bus name on the given connection */
188 application->bus_name_id =
189 g_bus_own_name_on_connection (application->connection,
190 "com.contiautomotive.NodeStateManager",
191 G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL);
197 nsm_dummy_application_get_property (GObject *object,
202 NSMDummyApplication *application = NSM_DUMMY_APPLICATION (object);
206 case PROP_CONNECTION:
207 g_value_set_object (value, application->connection);
209 case PROP_CONSUMER_SERVICE:
210 g_value_set_object (value, application->consumer_service);
212 case PROP_LIFECYCLE_CONTROL_SERVICE:
213 g_value_set_object (value, application->lifecycle_control_service);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 nsm_dummy_application_set_property (GObject *object,
229 NSMDummyApplication *application = NSM_DUMMY_APPLICATION (object);
233 case PROP_CONNECTION:
234 application->connection = g_value_dup_object (value);
236 case PROP_CONSUMER_SERVICE:
237 application->consumer_service = g_value_dup_object (value);
239 case PROP_LIFECYCLE_CONTROL_SERVICE:
240 application->lifecycle_control_service = g_value_dup_object (value);
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251 nsm_dummy_application_int_handler (GApplication *app)
253 NSMDummyApplication *application = NSM_DUMMY_APPLICATION (app);
255 /* call the shutdown consumer method */
256 nsm_shutdown_consumers (application->consumer_service);
264 nsm_dummy_application_startup (GApplication *app)
266 NSMDummyApplication *application = NSM_DUMMY_APPLICATION (app);
268 /* chain up to the parent class */
269 (*G_APPLICATION_CLASS (nsm_dummy_application_parent_class)->startup) (app);
271 /* update systemd's watchdog timestamp every 120 seconds */
272 application->watchdog_client = watchdog_client_new (120);
274 /* increment the reference count holding the application running to test the services */
275 g_application_hold (app);
280 NSMDummyApplication *
281 nsm_dummy_application_new (GDBusConnection *connection,
282 NSMConsumerService *consumer_service,
283 NSMLifecycleControlService *lifecycle_control_service)
285 g_return_val_if_fail (NSM_CONSUMER_IS_SERVICE (consumer_service), NULL);
286 g_return_val_if_fail (NSM_LIFECYCLE_CONTROL_IS_SERVICE (lifecycle_control_service), NULL);
288 return g_object_new (NSM_DUMMY_TYPE_APPLICATION,
289 "application-id", "com.contiautomotive.NodeStateManager",
290 "flags", G_APPLICATION_IS_SERVICE,
291 "connection", connection,
292 "nsm-consumer-service", consumer_service,
293 "nsm-lifecycle-control-service", lifecycle_control_service,