📄 syncmanager.cpp
字号:
//
clientChal = syncMLProcessor.getChal(syncml->getSyncBody());
if (isAuthFailed(ret)) {
if (clientChal == NULL) {
requestedAuthType = credentialHandler.getClientAuthType();
} else {
requestedAuthType = clientChal->getType();
}
if (strcmp(credentialHandler.getClientAuthType(),requestedAuthType) != 0 ) {
if (clientChal && strcmp(requestedAuthType, AUTH_TYPE_MD5) == 0) {
if (clientChal->getNextNonce()) {
credentialHandler.setClientNonce(clientChal->getNextNonce()->getValueAsBase64());
}
}
} else {
if (strcmp(requestedAuthType, AUTH_TYPE_MD5) == 0 && clientAuthRetries == 1) {
if (clientChal->getNextNonce()) {
credentialHandler.setClientNonce(clientChal->getNextNonce()->getValueAsBase64());
}
} else {
lastErrorCode = 401;
sprintf(lastErrorMsg, "Client not authenticated");
ret = lastErrorCode;
goto finally;
}
}
credentialHandler.setClientAuthType(requestedAuthType);
clientAuthRetries++;
} else {
if (clientChal && strcmp(clientChal->getType(), AUTH_TYPE_MD5) == 0) {
if (clientChal->getNextNonce()) {
credentialHandler.setClientNonce(clientChal->getNextNonce()->getValueAsBase64());
}
}
isClientAuthenticated = TRUE;
// Get sorted source list from Alert commands sent by server.
if (sortedSourcesFromServer) {
delete [] sortedSourcesFromServer;
sortedSourcesFromServer = NULL;
}
sortedSourcesFromServer = syncMLProcessor.getSortedSourcesFromServer(syncml, sourcesNumber);
for (count = 0; count < sourcesNumber; count ++) {
if (!sources[count]->getReport()->checkState())
continue;
ret = syncMLProcessor.processServerAlert(*sources[count], syncml);
if (isErrorStatus(ret)) {
sprintf(logmsg, "AlertStatus from server %d", ret);
LOG.error(logmsg);
setSourceStateAndError(count, SOURCE_ERROR, ret, logmsg);
}
fireSyncSourceEvent(sources[count]->getConfig().getURI(),
sources[count]->getConfig().getName(),
sources[count]->getSyncMode(),
0, SYNC_SOURCE_SYNCMODE_REQUESTED);
}
}
} while(isClientAuthenticated == FALSE || isServerAuthenticated == FALSE);
config.getAccessConfig().setClientNonce(credentialHandler.getClientNonce());
config.getAccessConfig().setServerNonce(credentialHandler.getServerNonce());
config.getDeviceConfig().setDevInfHash(devInfHash);
if (isToExit()) {
// error. no source to sync
if (!ret) {
// error: no source to sync
ret = lastErrorCode = ERR_NO_SOURCE_TO_SYNC;
sprintf(lastErrorMsg, ERRMSG_NO_SOURCE_TO_SYNC);
}
goto finally;
}
currentState = STATE_PKG1_SENT;
// ---------------------------------------------------------------------------------------
finally:
if(ret) {
//Fire Sync Error Event
fireSyncEvent(lastErrorMsg, SYNC_ERROR);
}
if (respURI) {
delete [] respURI;
}
if (responseMsg) {
safeDelete(&responseMsg);
}
if (initMsg) {
safeDelete(&initMsg);
}
if (devInfStr) {
delete devInfStr;
}
deleteSyncML(&syncml);
deleteCred(&cred);
deleteAlert(&alert);
deleteArrayList(&alerts);
deleteStatus(&status);
deleteChal(&serverChal);
return ret;
}
//
// utility function to process any <Sync> command that the server might
// have included in its <SyncBody>
//
// @param syncml the server response
// @param statusList list to which statuses for changes are to be added
// @return TRUE if a fatal error occurred
//
BOOL SyncManager::checkForServerChanges(SyncML* syncml, ArrayList &statusList)
{
BOOL result = FALSE;
// Danger, danger: count is a member variable!
// It has to be because that's the context for some of
// the other methods. Modifying it has to be careful to
// restore the initial value before returning because
// our caller might use it, too.
int oldCount = this->count;
//
// Get the server modifications for each syncsource.
// We need to work on syncsources in the same order as the server sends them.
// (use 'sortedSourcesFromServer' list of source names)
//
char* sourceUri = NULL;
int i=0;
while (sortedSourcesFromServer[i]) {
sourceUri = sortedSourcesFromServer[i];
// Retrieve the correspondent index for this syncsource.
for (count = 0; count < sourcesNumber; count ++) {
if ( !strcmp(sourceUri, sources[count]->getConfig().getName()) ) {
break;
}
}
if (count >= sourcesNumber) {
LOG.error("Source uri not recognized: %s", sourceUri);
goto finally;
}
// Sync* sync = syncMLProcessor.processSyncResponse(*sources[count], syncml);
Sync* sync = syncMLProcessor.getSyncResponse(syncml, i);
if (sync) {
const char *locuri = ((Target*)(sync->getTarget()))->getLocURI();
for (int k = 0; k < sourcesNumber; k++) {
if (strcmp(locuri, sources[k]->getConfig().getName()) == 0) {
count = k;
break;
}
}
if (count >= sourcesNumber) {
LOG.error("Source uri not recognized: %s", sourceUri);
goto finally;
}
if (!sources[count]->getReport()->checkState()) {
i++;
continue;
}
if (strcmp(prevSourceName, "") == 0) {
strcpy(prevSourceName, locuri);
}
if (strcmp(prevSourceName, locuri) != 0) {
isFiredSyncEventBEGIN = FALSE;
fireSyncSourceEvent(prevSourceUri, prevSourceName, prevSyncMode, 0, SYNC_SOURCE_END);
strcpy(prevSourceName, locuri);
}
}
if (sync) {
// Fire SyncSource event: BEGIN sync of a syncsource (server modifications)
// (fire only if <sync> tag exist)
if (isFiredSyncEventBEGIN == FALSE) {
fireSyncSourceEvent(sources[count]->getConfig().getURI(),
sources[count]->getConfig().getName(),
sources[count]->getSyncMode(), 0, SYNC_SOURCE_BEGIN);
strcpy(prevSourceUri, sources[count]->getConfig().getURI());
prevSyncMode = sources[count]->getSyncMode();
long noc = sync->getNumberOfChanges();
fireSyncSourceEvent(sources[count]->getConfig().getURI(),
sources[count]->getConfig().getName(),
sources[count]->getSyncMode(), noc, SYNC_SOURCE_TOTAL_SERVER_ITEMS);
isFiredSyncEventBEGIN = TRUE;
}
ArrayList* items = sync->getCommands();
Status* status = syncMLBuilder.prepareSyncStatus(*sources[count], sync);
statusList.add(*status);
deleteStatus(&status);
ArrayList* previousStatus = new ArrayList();
for (int i = 0; i < items->size(); i++) {
CommandInfo cmdInfo;
ModificationCommand* modificationCommand = (ModificationCommand*)(items->get(i));
Meta* meta = modificationCommand->getMeta();
ArrayList* list = modificationCommand->getItems();
cmdInfo.commandName = modificationCommand->getName();
cmdInfo.cmdRef = modificationCommand->getCmdID()->getCmdID();
if (meta) {
cmdInfo.dataType = meta->getType();
cmdInfo.format = meta->getFormat();
cmdInfo.size = meta->getSize();
}
else {
cmdInfo.dataType = 0;
cmdInfo.format = 0;
cmdInfo.size = 0;
}
for (int j = 0; j < list->size(); j++) {
Item *item = (Item*)list->get(j);
if (item == NULL) {
LOG.error("SyncManager::checkForServerChanges() - unexpected NULL item.");
result = TRUE;
goto finally;
}
// Size might have been included in either the command or the item meta information.
// The check for size > 0 is necessary because the function returns 0 even if no
// size information was sent by the server - that's okay, for items that really have
// size 0 the value doesn't matter as they shouldn't be split into chunks.
Meta *itemMeta = item->getMeta();
if (itemMeta && itemMeta->getSize() > 0) {
cmdInfo.size = itemMeta->getSize();
}
//
// set the syncItem element
//
status = processSyncItem(item, cmdInfo, syncMLBuilder);
if (status) {
syncMLBuilder.addItemStatus(previousStatus, status);
deleteStatus(&status);
}
}
if (previousStatus) {
statusList.add(previousStatus);
deleteArrayList(&previousStatus);
}
}
// Fire SyncSourceEvent: END sync of a syncsource (server modifications)
//fireSyncSourceEvent(sources[count]->getConfig().getURI(), sources[count]->getConfig().getName(), sources[count]->getSyncMode(), 0, SYNC_SOURCE_END);
}
i++;
} // End: while (sortedSourcesFromServer[i])
finally:
this->count = oldCount;
return result;
}
int SyncManager::sync() {
char* msg = NULL;
char* responseMsg = NULL;
Status* status = NULL;
SyncML* syncml = NULL;
/** Current item to be transmitted. Might be split across multiple messages if LargeObjectSupport is on. */
SyncItem* syncItem = NULL;
/** number of bytes already transmitted from syncItem */
long syncItemOffset = 0;
Alert* alert = NULL;
ModificationCommand* modificationCommand = NULL;
unsigned int tot = 0;
unsigned int step = 0;
unsigned int toSync = 0;
unsigned int iterator= 0;
int ret = 0;
BOOL last = FALSE;
ArrayList* list = new ArrayList();
BOOL isFinalfromServer = FALSE;
BOOL isAtLeastOneSourceCorrect = FALSE;
//for refresh from server sync (TO BE REMOVED?)
allItemsList = new ArrayList*[sourcesNumber];
//
// If this is the first message, currentState is STATE_PKG1_SENT,
// otherwise it is already in STATE_PKG3_SENDING.
//
if (currentState == STATE_PKG1_SENT) {
currentState = STATE_PKG3_SENDING;
}
// The real number of source to sync (XXX REMOVE ME)
for (count = 0; count < sourcesNumber; count ++) {
if (!sources[count]->getReport()->checkState())
continue;
toSync++;
}
for (count = 0; count < sourcesNumber; count ++) {
allItemsList[count] = NULL;
if (!sources[count]->getReport()->checkState())
continue;
// note: tot == 0 is used to detect when to start iterating over
// items from the beginning
tot = 0;
step = 0;
last = FALSE;
iterator++;
// Fire SyncSource event: BEGIN sync of a syncsource (client modifications)
fireSyncSourceEvent(sources[count]->getConfig().getURI(), sources[count]->getConfig().getName(), sources[count]->getSyncMode(), 0, SYNC_SOURCE_BEGIN);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -