OpenSync  0.22
opensync_context.c
1 /*
2  * libopensync - A synchronization framework
3  * Copyright (C) 2004-2005 Armin Bauer <armin.bauer@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 <opensync.h>
22 #include "opensync_internals.h"
23 
24 OSyncContext *osync_context_new(OSyncMember *member)
25 {
26  OSyncContext *ctx = g_malloc0(sizeof(OSyncContext));
27  ctx->member = member;
28  return ctx;
29 }
30 
31 void osync_context_free(OSyncContext *context)
32 {
33  g_assert(context);
34  //FIXME Do we need to free the user_data?
35  g_free(context);
36 }
37 
38 void *osync_context_get_plugin_data(OSyncContext *context)
39 {
40  g_assert(context);
41  g_assert(context->member);
42  return context->member->plugindata;
43 }
44 
45 void osync_context_report_osyncerror(OSyncContext *context, OSyncError **error)
46 {
47  osync_trace(TRACE_ENTRY, "%s(%p, %p:(%s))", __func__, context, error, osync_error_print(error));
48  g_assert(context);
49  if (context->callback_function)
50  (context->callback_function)(context->member, context->calldata, error);
51  osync_context_free(context);
52  osync_trace(TRACE_EXIT, "%s", __func__);
53 }
54 
55 void osync_context_report_error(OSyncContext *context, OSyncErrorType type, const char *format, ...)
56 {
57  osync_trace(TRACE_ENTRY, "%s(%p, %i, %s)", __func__, context, type, format);
58  g_assert(context);
59  OSyncError *error = NULL;
60  va_list args;
61  va_start(args, format);
62  osync_error_set_vargs(&error, type, format, args);
63  osync_trace(TRACE_INTERNAL, "ERROR is: %s", osync_error_print(&error));
64  if (context->callback_function)
65  (context->callback_function)(context->member, context->calldata, &error);
66  va_end (args);
67  osync_context_free(context);
68  osync_trace(TRACE_EXIT, "%s", __func__);
69 }
70 
71 void osync_context_report_success(OSyncContext *context)
72 {
73  osync_trace(TRACE_ENTRY, "%s(%p)", __func__, context);
74  g_assert(context);
75  if (context->callback_function)
76  (context->callback_function)(context->member, context->calldata, NULL);
77  osync_context_free(context);
78  osync_trace(TRACE_EXIT, "%s", __func__);
79 }
80 
81 void osync_context_report_change(OSyncContext *context, OSyncChange *change)
82 {
83  osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, context, change);
84  g_assert(context);
85  OSyncMember *member = context->member;
86  g_assert(member);
87  osync_change_set_member(change, member);
88 
89  osync_assert_msg(change->uid, "You forgot to set a uid on the change you reported!");
90  osync_assert_msg(change->data || change->changetype == CHANGE_DELETED, "You need to report some data unless you report CHANGE_DELETED");
91  osync_assert_msg((!(change->data) && change->size == 0) || (change->data && change->size != 0), "No data and datasize was not 0!");
92  osync_assert_msg((!change->data && change->changetype == CHANGE_DELETED) || (change->data && change->changetype != CHANGE_DELETED), "You cannot report data if you report CHANGE_DELETED. Just report the uid");
93 
94  osync_assert_msg((osync_change_get_objformat(change) != NULL) || change->changetype == CHANGE_DELETED, "The reported change did not have a format set");
95  osync_assert_msg((osync_change_get_objtype(change) != NULL) || change->changetype == CHANGE_DELETED, "The reported change did not have a objtype set");
96  osync_assert_msg((osync_change_get_changetype(change) != CHANGE_UNKNOWN), "The reported change did not have a changetype set");
97 
98 
99  if (change->changetype == CHANGE_DELETED)
100  change->has_data = TRUE;
101 
102  change->initial_format = osync_change_get_objformat(change);
103 
104  osync_trace(TRACE_INTERNAL, "Reporting change with uid %s, changetype %i, data %p, objtype %s and format %s", osync_change_get_uid(change), osync_change_get_changetype(change), osync_change_get_data(change), osync_change_get_objtype(change) ? osync_objtype_get_name(osync_change_get_objtype(change)) : "None", osync_change_get_objformat(change) ? osync_objformat_get_name(osync_change_get_objformat(change)) : "None");
105 
106  osync_assert_msg(member->memberfunctions->rf_change, "The engine must set a callback to receive changes");
107  member->memberfunctions->rf_change(member, change, context->calldata);
108  osync_trace(TRACE_EXIT, "%s", __func__);
109 }
110 
111 void osync_context_send_log(OSyncContext *ctx, const char *message, ...)
112 {
113  g_assert(ctx);
114  OSyncMember *member = ctx->member;
115  g_assert(member);
116 
117  va_list arglist;
118  char buffer[1024];
119  memset(buffer, 0, sizeof(buffer));
120  va_start(arglist, message);
121  g_vsnprintf(buffer, 1024, message, arglist);
122 
123  osync_debug("OSYNC", 3, "Sending logmessage \"%s\"", buffer);
124  if (member->memberfunctions->rf_log)
125  member->memberfunctions->rf_log(member, buffer);
126 
127  va_end(arglist);
128 }
129 
130 void osync_report_message(OSyncMember *member, const char *message, void *data)
131 {
132  member->memberfunctions->rf_message(member, message, data, FALSE);
133 }
134 
135 void *osync_report_message_sync(OSyncMember *member, const char *message, void *data)
136 {
137  return member->memberfunctions->rf_message(member, message, data, TRUE);
138 }
139 
140 OSyncMember *osync_context_get_member(OSyncContext *ctx)
141 {
142  g_assert(ctx);
143  return ctx->member;
144 }
OSyncChangeType osync_change_get_changetype(OSyncChange *change)
Gets the changetype of a change.
Represent an error.
OSyncErrorType
Defines the possible error types.
Definition: opensync_error.h:5
void osync_error_set_vargs(OSyncError **error, OSyncErrorType type, const char *format, va_list args)
Sets a error from a va_list.
const char * osync_change_get_uid(OSyncChange *change)
Gets the uid of a change.
A member of a group which represent a single device.
OSyncObjFormat * osync_change_get_objformat(OSyncChange *change)
Gets the object format of a change.
void osync_debug(const char *subpart, int level, const char *message,...)
Used for debugging.
const char * osync_objformat_get_name(OSyncObjFormat *format)
Returns the name of a object format.
A change object.
void osync_change_set_member(OSyncChange *change, OSyncMember *member)
Sets the member of a change.
const char * osync_error_print(OSyncError **error)
Returns the message of the error.
void osync_trace(OSyncTraceType type, const char *message,...)
Used for tracing the application.
const char * osync_objtype_get_name(OSyncObjType *type)
Returns the name of a object type.
OSyncObjType * osync_change_get_objtype(OSyncChange *change)
Gets the object type of a change.
OSyncObjFormat * initial_format
OSyncChangeType changetype
char * osync_change_get_data(OSyncChange *change)
Gets the data of a change.