summaryrefslogtreecommitdiff
path: root/common/shutdown-client.c
blob: fb62ed0fa49fe0253d7a5df387bc96a9ffe4061e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* 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/nsm-enum-types.h>
#include <common/shutdown-client.h>
#include <common/shutdown-consumer-dbus.h>



/**
 * SECTION: shutdown-client
 * @title: ShutdownClient
 * @short_description: A class to bundle information about a #ShutdownConsumer.
 * @stability: Internal
 * 
 * The #ShutdownClient is a container for a #ShutdownConsumerSkeleton if it is being used
 * by the #LAHandlerService in the Node Startup Controller, and a #ShutdownConsumerProxy 
 * if it is being used by the #NSMConsumerService in the Node State Manager Dummy.
 * 
 * The #LAHandlerService uses it to identify which #ShutdownConsumer was told to shut
 * down, which it will then use to stop the associated systemd unit.
 * 
 * The #NSMConsumerService uses it to locate the #ShutdownConsumer, shut it down with the
 * appropriate mode, and wait for an appropriate length of time when expecting a response.
 */



/* property identifiers */
enum
{
  PROP_0,
  PROP_BUS_NAME,
  PROP_OBJECT_PATH,
  PROP_SHUTDOWN_MODE,
  PROP_TIMEOUT,
  PROP_CONSUMER,
};



static void shutdown_client_finalize     (GObject      *object);
static void shutdown_client_get_property (GObject      *object,
                                          guint         prop_id,
                                          GValue       *value,
                                          GParamSpec   *pspec);
static void shutdown_client_set_property (GObject      *object,
                                          guint         prop_id,
                                          const GValue *value,
                                          GParamSpec   *pspec);



struct _ShutdownClientClass
{
  GObjectClass __parent__;
};

struct _ShutdownClient
{
  GObject           __parent__;

  gchar            *bus_name;
  gchar            *object_path;
  NSMShutdownType   shutdown_mode;
  guint             timeout;

  ShutdownConsumer *consumer;
};



G_DEFINE_TYPE (ShutdownClient, shutdown_client, G_TYPE_OBJECT);



static void
shutdown_client_class_init (ShutdownClientClass *klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = shutdown_client_finalize;
  gobject_class->get_property = shutdown_client_get_property;
  gobject_class->set_property = shutdown_client_set_property;

  /**
   * ShutdownClient:bus-name:
   * 
   * The bus name which this shutdown consumer is registered with.
   * e.g. org.genivi.NodeStartupController1.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_BUS_NAME,
                                   g_param_spec_string ("bus-name",
                                                        "bus-name",
                                                        "bus-name",
                                                        NULL,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));

  /**
   * ShutdownClient:object-path:
   * 
   * The D-Bus object path this shutdown consumer uses.
   * e.g. /org/genivi/NodeStartupController1/ShutdownConsumer/1
   */
  g_object_class_install_property (gobject_class,
                                   PROP_OBJECT_PATH,
                                   g_param_spec_string ("object-path",
                                                        "object-path",
                                                        "object-path",
                                                        NULL,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));

  /**
   * ShutdownClient:shutdown-mode:
   * 
   * The shutdown mode of this shutdown client.
   * For a full list of shutdown modes, see #NSMShutdownType.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_SHUTDOWN_MODE,
                                   g_param_spec_flags ("shutdown-mode",
                                                       "shutdown-mode",
                                                       "shutdown-mode",
                                                       TYPE_NSM_SHUTDOWN_TYPE,
                                                       NSM_SHUTDOWN_TYPE_NOT,
                                                       G_PARAM_READWRITE |
                                                       G_PARAM_CONSTRUCT_ONLY |
                                                       G_PARAM_STATIC_STRINGS));

  /**
   * ShutdownClient:timeout:
   * 
   * The amount of time the Node State Manager will wait before deciding that the
   * shutdown client is not responding.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_TIMEOUT,
                                   g_param_spec_uint ("timeout",
                                                      "timeout",
                                                      "timeout",
                                                      0, G_MAXUINT, 120,
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT_ONLY |
                                                      G_PARAM_STATIC_STRINGS));

  /**
   * ShutdownClient:consumer:
   * 
   * The #ShutdownConsumerSkeleton or #ShutdownConsumerProxy for this shutdown client.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_CONSUMER,
                                   g_param_spec_object ("consumer",
                                                        "consumer",
                                                        "consumer",
                                                        TYPE_SHUTDOWN_CONSUMER,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_STATIC_STRINGS));

}



static void
shutdown_client_init (ShutdownClient *client)
{
}



static void
shutdown_client_finalize (GObject *object)
{
  ShutdownClient *client = SHUTDOWN_CLIENT (object);

  /* free bus name and object path */
  g_free (client->bus_name);
  g_free (client->object_path);

  /* release the consumer, if we have one */
  if (client->consumer != NULL)
    {
      if (IS_SHUTDOWN_CONSUMER_SKELETON (client->consumer))
        g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (client->consumer));
      g_object_unref (client->consumer);
    }

  (*G_OBJECT_CLASS (shutdown_client_parent_class)->finalize) (object);
}



static void
shutdown_client_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  ShutdownClient *client = SHUTDOWN_CLIENT (object);

  switch (prop_id)
    {
    case PROP_BUS_NAME:
      g_value_set_string (value, client->bus_name);
      break;
    case PROP_OBJECT_PATH:
      g_value_set_string (value, client->object_path);
      break;
    case PROP_SHUTDOWN_MODE:
      g_value_set_flags (value, client->shutdown_mode);
      break;
    case PROP_TIMEOUT:
      g_value_set_uint (value, client->timeout);
      break;
    case PROP_CONSUMER:
      g_value_set_object (value, client->consumer);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static void
shutdown_client_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  ShutdownClient *client = SHUTDOWN_CLIENT (object);

  switch (prop_id)
    {
    case PROP_BUS_NAME:
      client->bus_name = g_value_dup_string (value);
      break;
    case PROP_OBJECT_PATH:
      client->object_path = g_value_dup_string (value);
      break;
    case PROP_SHUTDOWN_MODE:
      client->shutdown_mode = g_value_get_flags (value);
      break;
    case PROP_TIMEOUT:
      client->timeout = g_value_get_uint (value);
      break;
    case PROP_CONSUMER:
      shutdown_client_set_consumer (client, g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



/**
 * shutdown_client_new:
 * @bus_name:      The bus name which this shutdown consumer is registered with.
 *                 e.g. org.genivi.NodeStartupController1.
 * @object_path:   The D-Bus object path this shutdown consumer uses.
 *                 e.g. /org/genivi/NodeStartupController1/ShutdownConsumer/1
 * @shutdown_mode: The shutdown mode of this shutdown client.
 *                 For a full list of shutdown modes, see #NSMShutdownType.
 * @timeout:       The amount of time the Node State Manager will wait before deciding
 *                 that the shutdown client is not responding.
 * 
 * Creates a new shutdown client.
 * 
 * Returns: A new #ShutdownClient object.
 */
ShutdownClient *
shutdown_client_new (const gchar    *bus_name,
                     const gchar    *object_path,
                     NSMShutdownType shutdown_mode,
                     guint           timeout)
{
  return g_object_new (TYPE_SHUTDOWN_CLIENT,
                       "bus-name", bus_name,
                       "object-path", object_path,
                       "shutdown-mode", shutdown_mode,
                       "timeout", timeout,
                       NULL);
}



/**
 * shutdown_client_get_bus_name:
 * @client: The #ShutdownClient.
 * 
 * Retrieves the bus name of a #ShutdownClient.
 * 
 * Returns: The bus name for this #ShutdownClient.
 */
const gchar *
shutdown_client_get_bus_name (ShutdownClient *client)
{
  g_return_val_if_fail (IS_SHUTDOWN_CLIENT (client), NULL);
  return client->bus_name;
}



/**
 * shutdown_client_get_object_path:
 * @client: The #ShutdownClient.
 * 
 * Retrieves the object path of a #ShutdownClient.
 * 
 * Returns: The object path for this #ShutdownClient.
 */
const gchar *
shutdown_client_get_object_path (ShutdownClient *client)
{
  g_return_val_if_fail (IS_SHUTDOWN_CLIENT (client), NULL);
  return client->object_path;
}



/**
 * shutdown_client_get_shutdown_mode:
 * @client: The #ShutdownClient.
 * 
 * Retrieves the %shutdown_mode of a #ShutdownClient.
 * 
 * Returns: The %shutdown_mode for this #ShutdownClient.
 */
NSMShutdownType
shutdown_client_get_shutdown_mode (ShutdownClient *client)
{
  g_return_val_if_fail (IS_SHUTDOWN_CLIENT (client), NSM_SHUTDOWN_TYPE_NOT);
  return client->shutdown_mode;
}


/**
 * shutdown_client_set_shutdown_mode:
 * @client:        The #ShutdownClient.
 * @shutdown_mode: The #NSMShutdownType for the @client.
 * 
 * Sets the shutdown_mode of @client.
 */
void
shutdown_client_set_shutdown_mode (ShutdownClient *client,
                                   NSMShutdownType shutdown_mode)
{
  g_return_if_fail (IS_SHUTDOWN_CLIENT (client));

  /* do nothing if the shutdown_mode value is not changing */
  if (client->shutdown_mode == shutdown_mode)
    return;

  /* update the shutdown_mode property */
  client->shutdown_mode = shutdown_mode;

  /* notify the callers that this property has changed */
  g_object_notify (G_OBJECT (client), "shutdown-mode");
}


/**
 * shutdown_client_get_timeout:
 * @client: The #ShutdownClient.
 * 
 * Retrieves the %timeout of a #ShutdownClient.
 * 
 * Returns: The %timeout for this #ShutdownClient.
 */
guint
shutdown_client_get_timeout (ShutdownClient *client)
{
  g_return_val_if_fail (IS_SHUTDOWN_CLIENT (client), 0);
  return client->timeout;
}


/**
 * shutdown_client_set_timeout:
 * @client: The #ShutdownClient.
 * @timeout: The timeout for @client.
 * 
 * Sets the timeout of @client.
 */
void
shutdown_client_set_timeout (ShutdownClient *client,
                             guint           timeout)
{
  g_return_if_fail (IS_SHUTDOWN_CLIENT (client));

  /* do nothing if the timeout value is not changing */
  if (client->timeout == timeout)
    return;

  /* update the timeout property */
  client->timeout = timeout;

  /* notify the callers that this property has changed */
  g_object_notify (G_OBJECT (client), "timeout");
}


/**
 * shutdown_client_get_consumer:
 * @client: The #ShutdownClient
 * 
 * Retrieves the %consumer of a #ShutdownClient.
 * 
 *Note: Ownership of the #ShutdownConsumer is not transferred. The object returned by this
 * function should not be released.
 * 
 * Returns: The #ShutdownConsumer for this #ShutdownClient.
 */
ShutdownConsumer *
shutdown_client_get_consumer (ShutdownClient *client)
{
  g_return_val_if_fail (IS_SHUTDOWN_CLIENT (client), NULL);
  return client->consumer;
}


/**
 * shutdown_client_set_consumer:
 * @client:   The #ShutdownClient.
 * @consumer: The #ShutdownConsumer for @client.
 * 
 * Sets the consumer of @client.
 */
void
shutdown_client_set_consumer (ShutdownClient   *client,
                              ShutdownConsumer *consumer)
{
  g_return_if_fail (IS_SHUTDOWN_CLIENT (client));
  g_return_if_fail (consumer == NULL || IS_SHUTDOWN_CONSUMER (consumer));

  /* release the existing consumer if we have one */
  if (client->consumer != NULL)
    {
      /* also, if we are trying to reset the same consumer, ignore */
      if (client->consumer == consumer)
        return;

      /* release and reset the consumer member */
      g_object_unref (client->consumer);
      client->consumer = NULL;
    }

  /* grab a reference on the new consumer */
  if (consumer != NULL)
    client->consumer = g_object_ref (consumer);

  /* notify others that the property has changed */
  g_object_notify (G_OBJECT (client), "consumer");
}