00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <opensync.h>
00022 #include "opensync_internals.h"
00023
00024 OSyncContext *osync_context_new(OSyncMember *member)
00025 {
00026 OSyncContext *ctx = g_malloc0(sizeof(OSyncContext));
00027 ctx->member = member;
00028 return ctx;
00029 }
00030
00031 void osync_context_free(OSyncContext *context)
00032 {
00033 g_assert(context);
00034
00035 g_free(context);
00036 }
00037
00038 void *osync_context_get_plugin_data(OSyncContext *context)
00039 {
00040 g_assert(context);
00041 g_assert(context->member);
00042 return context->member->plugindata;
00043 }
00044
00045 void osync_context_report_osyncerror(OSyncContext *context, OSyncError **error)
00046 {
00047 osync_trace(TRACE_ENTRY, "%s(%p, %p:(%s))", __func__, context, error, osync_error_print(error));
00048 g_assert(context);
00049 if (context->callback_function)
00050 (context->callback_function)(context->member, context->calldata, error);
00051 osync_context_free(context);
00052 osync_trace(TRACE_EXIT, "%s", __func__);
00053 }
00054
00055 void osync_context_report_error(OSyncContext *context, OSyncErrorType type, const char *format, ...)
00056 {
00057 osync_trace(TRACE_ENTRY, "%s(%p, %i, %s)", __func__, context, type, format);
00058 g_assert(context);
00059 OSyncError *error = NULL;
00060 va_list args;
00061 va_start(args, format);
00062 osync_error_set_vargs(&error, type, format, args);
00063 osync_trace(TRACE_INTERNAL, "ERROR is: %s", osync_error_print(&error));
00064 if (context->callback_function)
00065 (context->callback_function)(context->member, context->calldata, &error);
00066 va_end (args);
00067 osync_context_free(context);
00068 osync_trace(TRACE_EXIT, "%s", __func__);
00069 }
00070
00071 void osync_context_report_success(OSyncContext *context)
00072 {
00073 osync_trace(TRACE_ENTRY, "%s(%p)", __func__, context);
00074 g_assert(context);
00075 if (context->callback_function)
00076 (context->callback_function)(context->member, context->calldata, NULL);
00077 osync_context_free(context);
00078 osync_trace(TRACE_EXIT, "%s", __func__);
00079 }
00080
00081 void osync_context_report_change(OSyncContext *context, OSyncChange *change)
00082 {
00083 osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, context, change);
00084 g_assert(context);
00085 OSyncMember *member = context->member;
00086 g_assert(member);
00087 osync_change_set_member(change, member);
00088
00089 osync_assert_msg(change->uid, "You forgot to set a uid on the change you reported!");
00090 osync_assert_msg(change->data || change->changetype == CHANGE_DELETED, "You need to report some data unless you report CHANGE_DELETED");
00091 osync_assert_msg((!(change->data) && change->size == 0) || (change->data && change->size != 0), "No data and datasize was not 0!");
00092 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");
00093
00094 osync_assert_msg((osync_change_get_objformat(change) != NULL) || change->changetype == CHANGE_DELETED, "The reported change did not have a format set");
00095 osync_assert_msg((osync_change_get_objtype(change) != NULL) || change->changetype == CHANGE_DELETED, "The reported change did not have a objtype set");
00096 osync_assert_msg((osync_change_get_changetype(change) != CHANGE_UNKNOWN), "The reported change did not have a changetype set");
00097
00098
00099 if (change->changetype == CHANGE_DELETED)
00100 change->has_data = TRUE;
00101
00102 change->initial_format = osync_change_get_objformat(change);
00103
00104 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");
00105
00106 osync_assert_msg(member->memberfunctions->rf_change, "The engine must set a callback to receive changes");
00107 member->memberfunctions->rf_change(member, change, context->calldata);
00108 osync_trace(TRACE_EXIT, "%s", __func__);
00109 }
00110
00111 void osync_context_send_log(OSyncContext *ctx, const char *message, ...)
00112 {
00113 g_assert(ctx);
00114 OSyncMember *member = ctx->member;
00115 g_assert(member);
00116
00117 va_list arglist;
00118 char buffer[1024];
00119 memset(buffer, 0, sizeof(buffer));
00120 va_start(arglist, message);
00121 g_vsnprintf(buffer, 1024, message, arglist);
00122
00123 osync_debug("OSYNC", 3, "Sending logmessage \"%s\"", buffer);
00124 if (member->memberfunctions->rf_log)
00125 member->memberfunctions->rf_log(member, buffer);
00126
00127 va_end(arglist);
00128 }
00129
00130 void osync_report_message(OSyncMember *member, const char *message, void *data)
00131 {
00132 member->memberfunctions->rf_message(member, message, data, FALSE);
00133 }
00134
00135 void *osync_report_message_sync(OSyncMember *member, const char *message, void *data)
00136 {
00137 return member->memberfunctions->rf_message(member, message, data, TRUE);
00138 }
00139
00140 OSyncMember *osync_context_get_member(OSyncContext *ctx)
00141 {
00142 g_assert(ctx);
00143 return ctx->member;
00144 }