📄 syncmanager.cpp
字号:
maxMsgSize &&
syncItem &&
msgSize + changeOverhead + syncItem->getDataSize() - syncItemOffset > maxMsgSize) {
// avoid adding another item that exceeds the message size
break;
}
msgSize += changeOverhead;
msgSize +=
syncMLBuilder.addItem(modificationCommand,
syncItemOffset,
(maxMsgSize && loSupport) ? (maxMsgSize - msgSize) : LONG_MAX,
DELETE_COMMAND_NAME,
syncItem, sources[count]->getConfig().getType());
if (syncItem) {
if (syncItemOffset == syncItem->getDataSize()) {
// Fire Sync Item Event - Item Deleted
fireSyncItemEvent(sources[count]->getConfig().getURI(), sources[count]->getConfig().getName(), syncItem->getKey(), ITEM_DELETED_BY_CLIENT);
delete syncItem; syncItem = NULL;
} else {
assert(msgSize >= maxMsgSize);
break;
}
}
else {
step++;
break;
}
tot++;
} while(msgSize < maxMsgSize);
}
if (step == 6 && syncItem == NULL)
last = TRUE;
break;
}
}
if (modificationCommand) {
list->add(*modificationCommand);
delete modificationCommand;
modificationCommand = NULL;
}
sync->setCommands(list);
delete list;
commands->add(*sync);
delete sync;
//
// Check if all the sources were synced.
// If not the prepareSync doesn't use the <final/> tag
//
syncml = syncMLBuilder.prepareSyncML(commands, (iterator != toSync ? FALSE : last));
msg = syncMLBuilder.prepareMsg(syncml);
deleteSyncML(&syncml);
deleteArrayList(&commands);
if (msg == NULL) {
ret = lastErrorCode;
goto finally;
}
// Synchronization message:
long realMsgSize = strlen(msg);
LOG.debug("%s estimated size %ld, allowed size %ld, real size %ld / estimated size %ld = %ld%%",
MSG_MODIFICATION_MESSAGE,
msgSize, maxMsgSize, realMsgSize, msgSize,
msgSize ? (100 * realMsgSize / msgSize) : 100);
LOG.debug("%s", msg);
//Fire Modifications Event
fireSyncEvent(NULL, SEND_MODIFICATION);
responseMsg = transportAgent->sendMessage(msg);
if (responseMsg == NULL) {
ret=lastErrorCode;
goto finally;
}
// increment the msgRef after every send message
syncMLBuilder.increaseMsgRef();
syncMLBuilder.resetCommandID();
syncml = syncMLProcessor.processMsg(responseMsg);
safeDelete(&responseMsg);
safeDelete(&msg);
if (syncml == NULL) {
ret = lastErrorCode;
goto finally;
}
isFinalfromServer = syncml->isLastMessage();
ret = syncMLProcessor.processSyncHdrStatus(syncml);
if (isErrorStatus(ret)) {
lastErrorCode = ret;
sprintf(lastErrorMsg, "Server Failure: server returned error code %i", ret);
LOG.error(lastErrorMsg);
goto finally;
}
ret = 0;
//
// Process the status of the item sent by client. It invokes the
// source method
//
int itemret = syncMLProcessor.processItemStatus(*sources[count], syncml->getSyncBody());
if(itemret){
char *name = toMultibyte(sources[count]->getName());
LOG.error("Error #%d in source %s", itemret, name);
delete [] name;
// skip the source, and set an error
setSourceStateAndError(count, SOURCE_ERROR, itemret, lastErrorMsg);
lastErrorCode = itemret;
break;
}
// Fire SyncSourceEvent: END sync of a syncsource (client modifications)
if (last)
fireSyncSourceEvent(sources[count]->getConfig().getURI(), sources[count]->getConfig().getName(), sources[count]->getSyncMode(), 0, SYNC_SOURCE_END);
// The server might have included a <Sync> command without waiting
// for a 222 alert. If it hasn't, then nothing is done here.
ArrayList statusList;
if (checkForServerChanges(syncml, statusList)) {
goto finally;
}
if (statusList.size()) {
Status* status = syncMLBuilder.prepareSyncHdrStatus(NULL, 200);
commands->add(*status);
deleteStatus(&status);
commands->add(&statusList);
}
// deleteSyncML(&syncml);
} while (last == FALSE);
// Fire SyncSourceEvent: END sync of a syncsource (client modifications)
// fireSyncSourceEvent(sources[count]->getConfig().getURI(), sources[count]->getConfig().getName(), sources[count]->getSyncMode(), 0, SYNC_SOURCE_END);
} // end for (count = 0; count < sourcesNumber; count ++)
if (isToExit()) {
// error. no source to sync
ret = lastErrorCode;
goto finally;
}
deleteSyncML(&syncml);
//
// If this was the last chunk, we move the state to STATE_PKG3_SENT
// At this time "last" is always true. The client is going to send
// the 222 package for to get the server modification if at least a source is correct
//
last = TRUE;
currentState = STATE_PKG3_SENT;
//
// send 222 alert code to retrieve the item from server
//
if ( !isFinalfromServer && isAtLeastOneSourceCorrect ) {
status = syncMLBuilder.prepareSyncHdrStatus(NULL, 200);
commands->add(*status);
deleteStatus(&status);
for (count = 0; count < sourcesNumber; count ++) {
if(!sources[count]->getReport()->checkState()) {
continue;
}
if ((sources[count]->getSyncMode() != SYNC_ONE_WAY_FROM_CLIENT) &&
(sources[count]->getSyncMode() != SYNC_REFRESH_FROM_CLIENT))
{
alert = syncMLBuilder.prepareAlert(*sources[count]);
commands->add(*alert);
deleteAlert(&alert);
}
}
syncml = syncMLBuilder.prepareSyncML(commands, FALSE);
msg = syncMLBuilder.prepareMsg(syncml);
LOG.debug("Alert to request server changes");
LOG.debug("%s", msg);
responseMsg = transportAgent->sendMessage(msg);
if (responseMsg == NULL) {
LOG.debug("SyncManager::sync(): null responseMsg");
ret=lastErrorCode;
goto finally;
}
// increment the msgRef after every send message
syncMLBuilder.increaseMsgRef();
syncMLBuilder.resetCommandID();
deleteSyncML(&syncml);
safeDelete(&msg);
syncml = syncMLProcessor.processMsg(responseMsg);
safeDelete(&responseMsg);
deleteArrayList(&commands);
if (syncml == NULL) {
LOG.debug("SyncManager::sync(): null syncml");
ret = lastErrorCode;
goto finally;
}
ret = syncMLProcessor.processSyncHdrStatus(syncml);
if (isErrorStatus(ret)) {
lastErrorCode = ret;
sprintf(lastErrorMsg, "Server Failure: server returned error code %i", ret);
LOG.error(lastErrorMsg);
goto finally;
}
ret = 0;
//
// Process the items returned from server
//
do {
last = syncml->getSyncBody()->getFinalMsg();
ArrayList statusList;
status = syncMLBuilder.prepareSyncHdrStatus(NULL, 200);
commands->add(*status);
deleteStatus(&status);
if (checkForServerChanges(syncml, statusList)) {
goto finally;
}
commands->add(&statusList);
if (!last) {
deleteSyncML(&syncml);
syncml = syncMLBuilder.prepareSyncML(commands, last);
msg = syncMLBuilder.prepareMsg(syncml);
LOG.debug("Status to the server");
LOG.debug("%s", msg);
responseMsg = transportAgent->sendMessage(msg);
if (responseMsg == NULL) {
ret=lastErrorCode;
goto finally;
}
// increment the msgRef after every send message
syncMLBuilder.increaseMsgRef();
syncMLBuilder.resetCommandID();
deleteSyncML(&syncml);
safeDelete(&msg);
syncml = syncMLProcessor.processMsg(responseMsg);
safeDelete(&responseMsg);
deleteArrayList(&commands);
if (syncml == NULL) {
ret = lastErrorCode;
goto finally;
}
ret = syncMLProcessor.processSyncHdrStatus(syncml);
if (isErrorStatus(ret)) {
lastErrorCode = ret;
sprintf(lastErrorMsg, "Server Failure: server returned error code %i", ret);
LOG.error(lastErrorMsg);
goto finally;
}
ret = 0;
}
} while (last == FALSE);
}
finally:
if (isAtLeastOneSourceCorrect == TRUE)
{
fireSyncSourceEvent(prevSourceUri, prevSourceName, prevSyncMode, 0, SYNC_SOURCE_END);
safeDelete(&responseMsg);
safeDelete(&msg);
deleteSyncML(&syncml);
}
else
{
ret = -1;
LOG.debug("sources not available");
}
if (ret) {
//Fire Sync Error Event
fireSyncEvent(lastErrorMsg, SYNC_ERROR);
}
return ret;
}
int SyncManager::endSync() {
char* mapMsg = NULL;
char* responseMsg = NULL;
SyncML* syncml = NULL;
BOOL last = TRUE;
int ret = 0;
Map* map = NULL;
Status* status = NULL;
unsigned int iterator = 0;
unsigned int toSync = 0;
int i = 0, tot = -1;
// rough (pessimistic) estimation of 400 bytes per map item
int maxMapItems = maxMsgSize / 400;
// The real number of source to sync
for (count = 0; count < sourcesNumber; count ++) {
if (!sources[count]->getReport()->checkState()) {
continue;
}
toSync++;
}
for (count = 0; count < sourcesNumber; count ++) {
if (!sources[count]->getReport()->checkState()) {
continue;
}
iterator++;
if ( (sources[count]->getS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -