21 #include "data_sync_server.h" 22 #include "data_sync_common.h" 23 #include "libsyncml/sml_error_internals.h" 24 #include "data_sync_callbacks.h" 25 #include "libsyncml/objects/sml_ds_server.h" 26 #include "data_sync_devinf.h" 27 #include "libsyncml/sml_support.h" 29 static SmlBool smlDataSyncServerAlertCallback(
31 SmlAlertType recvType,
36 smlTrace(TRACE_ENTRY,
"%s(%p, %i, %s, %s, %p)", __func__, dsession, recvType, VA_STRING(last), VA_STRING(next), userdata);
42 SmlAlertType sentType = recvType;
50 if (recvType != SML_ALERT_TWO_WAY &&
51 recvType != SML_ALERT_SLOW_SYNC &&
52 recvType != SML_ALERT_TWO_WAY_BY_SERVER)
54 smlErrorSet(&error, SML_ERROR_NOT_IMPLEMENTED,
"Unsupported alert type %d.", recvType);
58 char *remote_key = g_strdup_printf(
"remoteanchor%s", smlDsSessionGetLocation(dsession));
59 datastore->remoteNext = g_strdup(next);
65 if (recvType == SML_ALERT_TWO_WAY || recvType == SML_ALERT_TWO_WAY_BY_SERVER)
69 smlTrace(TRACE_INTERNAL,
"%s: TWO-WAY-SYNC but last is missing", __func__);
70 sentType = SML_ALERT_SLOW_SYNC;
74 if (dsObject->getAnchorCallback)
75 cached = dsObject->getAnchorCallback(
78 dsObject->getAnchorUserdata,
82 if (!cached || strcmp(cached, last))
85 "%s: TWO-WAY-SYNC but received LAST(%s) and cached LAST (%s) mismatch",
86 __func__, VA_STRING(last), VA_STRING(cached));
88 smlSafeCFree(&cached);
89 sentType = SML_ALERT_SLOW_SYNC;
95 if (dsObject->getAlertTypeCallback)
97 SmlAlertType alertType;
98 alertType = dsObject->getAlertTypeCallback(
100 datastore->sourceUri,
102 dsObject->getAlertTypeUserdata,
104 if (alertType == SML_ALERT_UNKNOWN || error)
106 if (alertType == SML_ALERT_SLOW_SYNC &&
107 alertType != recvType)
109 sentType = alertType;
112 smlSafeCFree(&remote_key);
121 recvType != SML_ALERT_SLOW_SYNC &&
122 sentType != SML_ALERT_SLOW_SYNC &&
126 recvType != SML_ALERT_SLOW_SYNC &&
127 sentType == SML_ALERT_SLOW_SYNC &&
131 recvType == SML_ALERT_SLOW_SYNC &&
132 sentType != SML_ALERT_SLOW_SYNC
135 recvType == SML_ALERT_SLOW_SYNC &&
136 sentType == SML_ALERT_SLOW_SYNC &&
143 "The library user tries to respond an alert %d " \
144 "with an alert %d and status 200 which is illegal.",
148 "The library user tries to respond an alert %d " \
149 "with an alert %d and status 508 which is illegal.",
156 char *local_key = g_strdup_printf(
"localanchor%s", smlDsSessionGetLocation(dsession));
157 char *local_last = NULL;
158 if (dsObject->getAnchorCallback)
159 local_last = dsObject->getAnchorCallback(
162 dsObject->getAnchorUserdata,
164 if (!local_last && error)
166 if (datastore->localNext)
167 smlSafeCFree(&(datastore->localNext));
168 if (local_last == NULL || strlen(local_last) == 0)
173 if (smlDataSyncIsTimestamp(next, dsObject->useTimestampAnchor) != dsObject->useTimestampAnchor)
179 "%s: libsyncml uses different timestamp anchor modes.",
183 datastore->localNext = smlDataSyncGetNextAnchor(datastore, local_last, &error);
184 if (!datastore->localNext)
190 local_last, datastore->localNext,
191 smlDataSyncAlertStatusCallback,
196 smlSafeCFree(&local_key);
198 smlSafeCFree(&local_last);
200 smlTrace(TRACE_EXIT,
"%s: %i", __func__, ret);
204 smlDataSyncSendEvent(
205 dsObject, SML_DATA_SYNC_EVENT_ERROR,
206 dsObject->eventUserdata, error);
208 smlErrorDeref(&error);
216 dsObject->manager = smlManagerNew(dsObject->tsp, error);
217 if (!dsObject->manager)
219 smlManagerSetEventCallback(dsObject->manager, smlDataSyncEventCallback, dsObject);
220 smlManagerSetLocalMaxMsgSize(dsObject->manager, dsObject->maxMsgSize);
221 smlManagerSetLocalMaxObjSize(dsObject->manager, dsObject->maxObjSize);
224 dsObject->funcDatastoreAlert = smlDataSyncServerAlertCallback;
227 dsObject->auth = smlAuthNew(error);
230 smlAuthSetVerifyCallback(dsObject->auth, smlDataSyncVerifyUserCallback, dsObject);
231 if (!dsObject->username) {
232 smlAuthSetEnable(dsObject->auth, FALSE);
234 smlAuthSetEnable(dsObject->auth, TRUE);
235 smlAuthSetType(dsObject->auth, dsObject->authType);
237 if (!smlAuthRegister(dsObject->auth, dsObject->manager, error))
241 if (!smlDataSyncDevInfInit(dsObject, SML_DEVINF_DEVTYPE_SERVER, error))
245 GList *o = dsObject->datastores;
246 for (; o; o = o->next) {
250 SmlLocation *loc = smlLocationNew(datastore->sourceUri, NULL, error);
254 datastore->server = smlDsServerNew(datastore->contentType, loc, error);
255 if (!datastore->server) {
256 smlLocationUnref(loc);
259 smlLocationUnref(loc);
261 if (!smlDsServerRegister(datastore->server, dsObject->manager, error))
266 smlDataSyncDatastoreConnectCallback,
270 if (!smlDataSyncDevInfAddDatastore(dsObject->localDevInf, datastore, error))
275 if (!smlManagerStart(dsObject->manager, error))
282 smlTrace(TRACE_EXIT,
"%s - TRUE", __func__);
This object represents an OMA DS datastore.
const char * smlErrorPrint(SmlError **error)
Returns the message of the error.
This is the central synchronization object.
SmlBool smlDsSessionSendAlert(SmlDsSession *dsession, SmlAlertType type, const char *last, const char *next, SmlStatusReplyCb callback, void *userdata, SmlError **error)
Sends the alert to the remote side.
void smlDsServerSetConnectCallback(SmlDsServer *server, SmlDsSessionConnectCb callback, void *userdata)
Registers a callback that will get called once a client connects.
SmlBool smlTransportInitialize(SmlTransport *tsp, SmlError **error)
Initializes the transport with the given config.
void smlTrace(SmlTraceType type, const char *message,...)
Used for tracing the application.
void smlErrorSet(SmlError **error, SmlErrorType type, const char *format,...)
Sets the error.