libsyncml  0.5.4
data_sync_loop.c
1 /*
2  * libsyncml - A syncml protocol implementation
3  * Copyright (C) 2008 Michael Bell <michael.bell@opensync.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 #include "../syncml.h"
22 #include "../syncml_internals.h"
23 #include "data_sync_devinf.h"
24 #include "libsyncml/sml_error_internals.h"
25 
26 static gboolean _prepare(GSource *source, gint *timeout_)
27 {
28  smlTrace(TRACE_INTERNAL, "%s(%p, %p)", __func__, source, timeout_);
29  *timeout_ = 50;
30  return FALSE;
31 }
32 
33 static gboolean _check(GSource *source)
34 {
35  SmlDataSyncObject *dsObject = *((SmlDataSyncObject **)(source + 1));
36 
37  GList *o = dsObject->datastores;
38  for (; o; o = o->next)
39  {
40  SmlDataSyncDatastore *datastore = o->data;
41  if (datastore->session && smlDsSessionCheck(datastore->session))
42  return TRUE;
43  }
44 
45  return smlManagerCheck(dsObject->manager);
46 }
47 
48 static gboolean _dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
49 {
50  smlTrace(TRACE_INTERNAL, "%s(%p, %p, %p)", __func__, source, callback, user_data);
51  SmlDataSyncObject *dsObject = user_data;
52 
53  GList *o = dsObject->datastores;
54  for (; o; o = o->next)
55  {
56  SmlDataSyncDatastore *datastore = o->data;
57  if (datastore->session)
58  smlDsSessionDispatch(datastore->session);
59  }
60 
61  smlManagerDispatch(dsObject->manager);
62  return TRUE;
63 }
64 
65 SmlBool smlDataSyncLoopStart(SmlDataSyncObject *dsObject, SmlError **error)
66 {
67  smlTrace(TRACE_ENTRY, "%s", __func__);
68  CHECK_ERROR_REF
69 
70  /* prepare function */
71 
72  dsObject->functions = smlTryMalloc0(sizeof(GSourceFuncs), error);
73  if (!dsObject->functions)
74  goto error;
75 
76  dsObject->functions->prepare = _prepare;
77  dsObject->functions->check = _check;
78  dsObject->functions->dispatch = _dispatch;
79  dsObject->functions->finalize = NULL;
80 
81  /* prepare context and thread */
82 
83  dsObject->context = g_main_context_new();
84  if (!dsObject->context)
85  goto error;
86  dsObject->thread = smlThreadNew(dsObject->context, error);
87  if (!dsObject->thread)
88  goto error;
89  smlThreadStart(dsObject->thread);
90 
91  /* prepare source for thread's main loop */
92 
93  dsObject->source = g_source_new(dsObject->functions, sizeof(GSource) + sizeof(SmlDataSyncObject *));
94  SmlDataSyncObject **ptr = (SmlDataSyncObject **)(dsObject->source + 1);
95  *ptr = dsObject;
96  g_source_set_callback(dsObject->source, NULL, dsObject, NULL);
97  g_source_attach(dsObject->source, dsObject->context);
98  g_main_context_ref(dsObject->context);
99 
100  smlTrace(TRACE_EXIT, "%s - TRUE", __func__);
101  return TRUE;
102 error:
103  smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error));
104  return FALSE;
105 }
106 
107 void smlDataSyncLoopStop(SmlDataSyncObject *dsObject)
108 {
109  smlTrace(TRACE_ENTRY, "%s", __func__);
110 
111  /* stop thread */
112  smlThreadStop(dsObject->thread);
113  smlThreadFree(dsObject->thread);
114  dsObject->thread = NULL;
115 
116  /* detach source */
117  g_source_unref(dsObject->source);
118  dsObject->source = NULL;
119 
120  /* free context */
121  g_main_context_unref(dsObject->context);
122  dsObject->context = NULL;
123 
124  /* free functions */
125  smlSafeFree((gpointer *)&(dsObject->functions));
126  dsObject->functions = NULL;
127 
128  smlTrace(TRACE_EXIT, "%s - TRUE", __func__);
129 }
130 
This object represents an OMA DS datastore.
Definition: data_sync.h:93
const char * smlErrorPrint(SmlError **error)
Returns the message of the error.
Definition: sml_error.c:299
This is the central synchronization object.
Definition: data_sync.h:110
void smlTrace(SmlTraceType type, const char *message,...)
Used for tracing the application.
Definition: sml_support.c:120
void * smlTryMalloc0(long n_bytes, SmlError **error)
Safely mallocs.
Definition: sml_support.c:335
Represent an error.