00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "../syncml.h"
00022 #include "../syncml_internals.h"
00023 #include "data_sync_devinf.h"
00024 #include "libsyncml/sml_error_internals.h"
00025
00026 static gboolean _prepare(GSource *source, gint *timeout_)
00027 {
00028 smlTrace(TRACE_INTERNAL, "%s(%p, %p)", __func__, source, timeout_);
00029 *timeout_ = 50;
00030 return FALSE;
00031 }
00032
00033 static gboolean _check(GSource *source)
00034 {
00035 SmlDataSyncObject *dsObject = *((SmlDataSyncObject **)(source + 1));
00036
00037 GList *o = dsObject->datastores;
00038 for (; o; o = o->next)
00039 {
00040 SmlDataSyncDatastore *datastore = o->data;
00041 if (datastore->session && smlDsSessionCheck(datastore->session))
00042 return TRUE;
00043 }
00044
00045 return smlManagerCheck(dsObject->manager);
00046 }
00047
00048 static gboolean _dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
00049 {
00050 smlTrace(TRACE_INTERNAL, "%s(%p, %p, %p)", __func__, source, callback, user_data);
00051 SmlDataSyncObject *dsObject = user_data;
00052
00053 GList *o = dsObject->datastores;
00054 for (; o; o = o->next)
00055 {
00056 SmlDataSyncDatastore *datastore = o->data;
00057 if (datastore->session)
00058 smlDsSessionDispatch(datastore->session);
00059 }
00060
00061 smlManagerDispatch(dsObject->manager);
00062 return TRUE;
00063 }
00064
00065 SmlBool smlDataSyncLoopStart(SmlDataSyncObject *dsObject, SmlError **error)
00066 {
00067 smlTrace(TRACE_ENTRY, "%s", __func__);
00068 CHECK_ERROR_REF
00069
00070
00071
00072 dsObject->functions = smlTryMalloc0(sizeof(GSourceFuncs), error);
00073 if (!dsObject->functions)
00074 goto error;
00075
00076 dsObject->functions->prepare = _prepare;
00077 dsObject->functions->check = _check;
00078 dsObject->functions->dispatch = _dispatch;
00079 dsObject->functions->finalize = NULL;
00080
00081
00082
00083 dsObject->context = g_main_context_new();
00084 if (!dsObject->context)
00085 goto error;
00086 dsObject->thread = smlThreadNew(dsObject->context, error);
00087 if (!dsObject->thread)
00088 goto error;
00089 smlThreadStart(dsObject->thread);
00090
00091
00092
00093 dsObject->source = g_source_new(dsObject->functions, sizeof(GSource) + sizeof(SmlDataSyncObject *));
00094 SmlDataSyncObject **ptr = (SmlDataSyncObject **)(dsObject->source + 1);
00095 *ptr = dsObject;
00096 g_source_set_callback(dsObject->source, NULL, dsObject, NULL);
00097 g_source_attach(dsObject->source, dsObject->context);
00098 g_main_context_ref(dsObject->context);
00099
00100 smlTrace(TRACE_EXIT, "%s - TRUE", __func__);
00101 return TRUE;
00102 error:
00103 smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error));
00104 return FALSE;
00105 }
00106
00107 void smlDataSyncLoopStop(SmlDataSyncObject *dsObject)
00108 {
00109 smlTrace(TRACE_ENTRY, "%s", __func__);
00110
00111
00112 smlThreadStop(dsObject->thread);
00113 smlThreadFree(dsObject->thread);
00114 dsObject->thread = NULL;
00115
00116
00117 g_source_unref(dsObject->source);
00118 dsObject->source = NULL;
00119
00120
00121 g_main_context_unref(dsObject->context);
00122 dsObject->context = NULL;
00123
00124
00125 smlSafeFree((gpointer *)&(dsObject->functions));
00126 dsObject->functions = NULL;
00127
00128 smlTrace(TRACE_EXIT, "%s - TRUE", __func__);
00129 }
00130