summaryrefslogtreecommitdiff
path: root/src/pop-transaction.c
blob: 50b9ac9a1396ea3c3b5a81b159353a604bb58842 (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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* pop-transaction.c - queues actions to be run sequentially
 *
 * Copyright (C) 2007 Ray Strode <rstrode@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.  
 */
#include "config.h"
#include "pop-transaction.h"

#include <errno.h>
#include <string.h>

#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n.h>

struct _PopTransactionPrivate
{
  PopTransactionStatus status;
  GQueue *actions;
  GList  *current_action_node;

  GClosure *completion_closure;
  GMainContext *context;

  guint32 is_commited : 1;
  guint idle_id;
};

typedef struct
{
  PopActionProcessFunc process_func;
  PopActionRollbackFunc rollback_func;
} PopAction;

typedef gboolean (* PopTransactionIdleFunc) (PopTransaction *transaction);

static void pop_transaction_finalize (GObject * object);
static void pop_transaction_class_install_signals (PopTransactionClass *
						   transaction_class);
static void pop_transaction_class_install_properties (PopTransactionClass *
						      transaction_class);

static void pop_transaction_set_property (GObject * object,
					  guint prop_id,
					  const GValue * value,
					  GParamSpec * pspec);
static void pop_transaction_get_property (GObject * object,
					  guint prop_id,
					  GValue * value, GParamSpec * pspec);

enum
{
  PROP_0 = 0,
  PROP_COMPLETION_CLOSURE,
};

enum
{
  PROCESS = 0,
  WAIT,
  RESUME,
  CANCELED,
  NUMBER_OF_SIGNALS
};

static guint pop_transaction_signals[NUMBER_OF_SIGNALS];

G_DEFINE_TYPE (PopTransaction, pop_transaction, G_TYPE_OBJECT);

static void
pop_transaction_class_init (PopTransactionClass * transaction_class)
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (transaction_class);

  object_class->finalize = pop_transaction_finalize;

  pop_transaction_class_install_properties (transaction_class);
  pop_transaction_class_install_signals (transaction_class);

  g_type_class_add_private (transaction_class,
			    sizeof (PopTransactionPrivate));
}

static void
pop_transaction_class_install_signals (PopTransactionClass *
				       transaction_class)
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (transaction_class);

  pop_transaction_signals[FOO] =
    g_signal_new ("foo", G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (PopTransactionClass, foo),
		  NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
  transaction_class->foo = NULL;
}

static void
pop_transaction_class_install_properties (PopTransactionClass *transaction_class)
{
  GObjectClass *object_class;
  GParamSpec *param_spec;

  object_class = G_OBJECT_CLASS (transaction_class);
  object_class->set_property = pop_transaction_set_property;
  object_class->get_property = pop_transaction_get_property;

  param_spec = g_param_spec_pointer ("completion-closure", _("Completion Closure"),
				     _("Closure to invoke when transaction is complete"),
				     NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
  g_object_class_install_property (object_class, PROP_COMPLETION_CLOSURE, param_spec);
}

static void
pop_transaction_init (PopTransaction *transaction)
{
  transaction->priv = G_TYPE_INSTANCE_GET_PRIVATE (transaction,
						   POP_TYPE_TRANSACTION,
						   PopTransactionPrivate);

}

static void
pop_transaction_finalize (GObject * object)
{
  PopTransaction *transaction;
  GObjectClass *parent_class;

  transaction = POP_TRANSACTION (object);

  parent_class = G_OBJECT_CLASS (pop_transaction_parent_class);

  if (parent_class->finalize != NULL)
    parent_class->finalize (object);
}

static void
pop_transaction_set_completion_closure (PopTransaction *transaction,
                                        GClosure       *closure)
{
  g_assert (transaction->priv->completion_closure == NULL);

  transaction->priv->completion_closure = g_closure_ref (closure);
  g_closure_sink (closure);
}

static void
pop_transaction_set_property (GObject      *object,
			      guint         prop_id,
			      const GValue *value,
                              GParamSpec   *pspec)
{
  PopTransaction *transaction = POP_TRANSACTION (object);

  switch (prop_id)
    {
    case PROP_COMPLETION_CLOSURE:
      pop_transaction_set_completion_closure (transaction, 
                                              (GClosure *) 
                                              g_value_get_pointer (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
pop_transaction_get_property (GObject    *object,
			      guint       prop_id,
			      GValue     *value,
                              GParamSpec *pspec)
{
  PopTransaction *transaction = POP_TRANSACTION (object);

  switch (prop_id)
    {
    case PROP_COMPLETION_CLOSURE:
      g_value_set_pointer (value, g_closure_ref (transaction->priv->completion_closure));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

GQuark
pop_transaction_error_quark (void)
{
  static GQuark error_quark = 0;

  if (error_quark == 0)
    error_quark = g_quark_from_static_string ("pop-transaction");

  return error_quark;
}

PopTransaction *
pop_transaction_new (PopTransactionCompleteFunc complete_func,
                     gpointer                   user_data);
{
  PopTransaction *transaction;
  GClosure *closure;

  closure = g_cclosure_new (G_CALLBACK (complete_func), user_data, NULL);

  transaction = g_object_new (POP_TYPE_TRANSACTION, 
                              "completion-closure", closure,
                              NULL);

  return transaction;
}

static PopAction *
pop_action_new (PopActionProcessFunc  action_process_func,
                PopActionRollbackFunc action_rollback_func)
{
  PopAction *action;

  action = g_slice_new (PopAction);

  action->process_func = action_process_func;
  action->rollback_func = action_rollback_func;
 
  return action;
}

static void
pop_action_free (PopAction *action)
{
  g_slice_free (action); 
}

void 
pop_transaction_add_action (PopTransaction        *transaction,
                            PopActionProcessFunc   action_process_func,
                            PopActionRollbackFunc  action_rollback_func)
{
  PopAction *action;

  g_return_if_fail (POP_IS_TRANSACTION (transaction));

  action = pop_action_new (action_process_func, action_rollback_func);

  g_queue_push_tail (transaction->priv->actions, action);
}

void
pop_transaction_wait_for_fd (PopTransaction *transaction,
                             int             fd,
                             GIOCondition    condition,
                             GDestroyNotify  destroy_notify)
{
  g_return_if_fail (POP_IS_TRANSACTION (transaction));

}

void
pop_transaction_wait_for_timeout (PopTransaction *transaction,
                                  const char     *key,
                                  int             milliseconds)
{
  g_return_if_fail (POP_IS_TRANSACTION (transaction));

}

void 
pop_transaction_wait_for_source (PopTransaction *transaction,
                                 GSource        *source)
{
  g_return_if_fail (POP_IS_TRANSACTION (transaction));

}

static gboolean
pop_transaction_is_attached (PopTransaction *transaction)
{
  return transaction->priv->context != NULL;
}

void 
pop_transaction_attach (PopTransaction *transaction,
                        GMainContext   *context)
{
  g_return_if_fail (POP_IS_TRANSACTION (transaction));
  g_return_if_fail (!pop_transaction_is_attached (transaction));

  transaction->priv->context = g_main_context_ref (context);
}

static void
pop_transaction_complete (PopTransaction *transaction)
{
  transaction->priv->status = POP_TRANSACTION_STATUS_FINISHED;
}

static guint
pop_transaction_call_on_idle (PopTransaction *transaction,
                              PopTransactionIdleFunc callback)
{
  guint idle_id;

  source = g_idle_source_new ();
  g_source_set_callback (source, (GSourceFunc) callback, transaction, NULL);

  g_source_remove (transaction->priv->idle_id);
  idle_id = g_source_attach (source, transaction->priv->context); 
  g_source_unref (source);

  return idle_id;
}

static gboolean
pop_transaction_rewind (PopTransaction *transaction)
{
  g_assert (transaction->priv->current_action_node != NULL);

  transaction->priv->current_action_node =
  transaction->priv->current_action_node->prev;

  return transaction->priv->current_action_node == NULL;
}

static gboolean
pop_transaction_seek_forward (PopTransaction *transaction)
{
  g_assert (transaction->priv->current_action_node != NULL);

  transaction->priv->current_action_node =
  transaction->priv->current_action_node->next;

  return transaction->priv->current_action_node == NULL;
}

static gboolean
pop_transaction_rollback_current_action_and_rewind (PopTransaction *transaction)
{
  PopAction *action;
  PopActionRollbackStatus status;
  gboolean should_continue;

  g_assert (transaction->priv->current_action_node != NULL);

  action = (PopAction *) transaction->priv->current_action_node->data;

  status = action->rollback_func (transaction);

  switch (status)
    {
      case POP_ACTION_ROLLBACK_STATUS_NOT_FINISHED:
        should_continue = TRUE;
      break;

      default:
        g_warning ("status %d is not valid rollback status\n", (int) status);

        /* Fall through and finish the action because it's not behaving
         */

      case POP_ACTION_ROLLBACK_STATUS_FINISHED:
        should_continue = pop_transaction_rewind (transaction);
      break;

       should_continue = FALSE;
      break;
    }

  if (!should_continue)
    pop_transaction_complete (transaction);

  return should_continue;
}

static void
pop_transaction_rollback_on_idle (PopTransaction *transaction)
{
  transaction->priv->current_action_node = transaction->priv->current_action_node->prev;

  transaction->priv->rollback_idle_id =
    pop_transaction_call_on_idle (transaction,
                                  pop_transaction_rollback_current_action_and_rewind); 
}

static void
pop_transaction_fail (PopTransaction *transaction)
{
  transaction->priv->status = POP_TRANSACTION_STATUS_FAILED;
  pop_transaction_rollback_on_idle (transaction);
}

static gboolean
pop_transaction_process_current_action_and_seek_forward (PopTransaction *transaction)
{
  PopAction *action;
  PopActionProcessStatus status;
  gboolean should_continue;

  g_assert (transaction->priv->current_action_node != NULL);

  action = (PopAction *) transaction->priv->current_action_node->data;

  status = action->process_func (transaction);

  switch (status)
    {
      case POP_ACTION_PROCESS_STATUS_NOT_FINISHED:
	should_continue = TRUE;
      break;

      case POP_ACTION_PROCESS_STATUS_FINISHED:
        if (!pop_transaction_seek_forward (transaction))
          {
            pop_transaction_complete (transaction);
	    should_continue = FALSE;
          }
        else
	  should_continue = TRUE;
      break;

      default:
        g_warning ("status %d is not valid processing status\n", (int) status);

        /* Fall through and fail the action because it's not behaving
         */

      case POP_ACTION_PROCESS_STATUS_FAILED:
	should_continue = FALSE;
        pop_transaction_fail (transaction);
      break;
    }

  return should_continue;
}

static void
pop_transaction_process_on_idle (PopTransaction *transaction)
{
  transaction->priv->current_action_node = 
      g_queue_peek_head_link (transaction->priv->actions);

  transaction->priv->process_idle_id =
    pop_transaction_call_on_idle (transaction,
		                  pop_transaction_process_current_action_and_seek_forward); 
}

static gboolean
pop_transaction_is_empty (PopTransaction *transaction)
{
  return g_queue_is_empty (transaction->priv->actions);
}

static gboolean
pop_transaction_is_committed (PopTransaction *transaction)
{
  return transaction->priv->is_commited;
}

void
pop_transaction_commit (PopTransaction *transaction)
{
  g_return_if_fail (POP_IS_TRANSACTION (transaction));
  g_return_if_fail (!pop_transaction_is_empty (transaction));
  g_return_if_fail (pop_transaction_is_commited (transaction));

  pop_transaction_process_on_idle (transaction);

  transaction->priv->is_commited = TRUE;
}

gboolean 
pop_transaction_cancel (PopTransaction *transaction)
{
  g_return_if_fail (POP_IS_TRANSACTION (transaction));

  if (transaction->priv->status != POP_TRANSACTION_STATUS_NOT_FINISHED)
    return FALSE;

  transaction->priv->status = POP_TRANSACTION_STATUS_CANCELED;
  pop_transaction_rollback_on_idle (transaction);
  
  return TRUE;
}

#ifdef POP_TRANSACTION_ENABLE_TEST

#include <stdio.h>
#include <glib.h>

int
main (int argc, char **argv)
{
  PopTransaction *transaction;
  int exit_code;

  g_log_set_always_fatal (G_LOG_LEVEL_ERROR
			  | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING);

  g_type_init ();

  g_message ("creating instance of 'transaction' object...");
  transaction = pop_transaction_new ();
  g_message ("'transaction' object created successfully");

  g_message ("destroying previously created 'transaction' object...");
  g_object_unref (transaction);
  g_message ("'transaction' object destroyed successfully");

  exit_code = 0;

  return exit_code;
}
#endif /* POP_TRANSACTION_ENABLE_TEST */