⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 syncmlprocessor.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 2 页
字号:

Sync* SyncMLProcessor::processSyncResponse(SyncSource& source, SyncML* syncml) {

    int iterator = 0, ret = 0;

    AbstractCommand* a  = NULL;
    Sync* sync          = NULL;

    ret = getStatusCode(syncml->getSyncBody(), &source, SYNC_HDR);
    if ((ret < 200) || (ret > 299)) {
        goto finally;
    }

    while((a = getCommand(syncml->getSyncBody(), SYNC, iterator)) != NULL){
        sync = (Sync*)a;
        const char *locuri = ((Target*)(sync->getTarget()))->getLocURI();
        if (strcmp(locuri, _wcc(source.getName())) == 0) {

            //
            // To handle the NumberOfChanges. The default is -1 that means the server doesn't send
            // any tag <NumberOfChanges>. Whit value >= 0 the value is correct
            //

            long noc = sync->getNumberOfChanges();
            fireSyncSourceEvent(source.getConfig().getURI(), source.getConfig().getName(), source.getSyncMode(), noc, SYNC_SOURCE_TOTAL_SERVER_ITEMS);

            break;
        }
        sync = NULL;
        iterator++;
    }

finally:

    return sync;

}

/*
 * Processes the map message response. Returns 0 in case of success, an
 * error code in case of error.
 * It feeds the given source with the server side modifications
 *
 * @param source the source
 * @param msg the response from the server
 */
int SyncMLProcessor::processMapResponse(SyncSource& source, SyncBody* syncBody) {
    int ret = -1;

    //
    // for now it is always ok
    //
    //
    // First of all check the status for the SyncHead
    //
    // TBD
    ret = getStatusCode(syncBody, &source, SYNC_HDR);

    // Fire Sync Status Event: map status from server (TBD)
    //fireSyncStatusEvent(MAP, ret, source.getConfig().getURI(), NULL, SERVER_STATUS);

    if ((ret < 200) || (ret >299)) {
        goto finally;
    }

    ret = 0;

finally:

    return ret;
}

/*
 * Returns the SyncHeader/RespURI element of the given message. If the element is not
 * found it returns NULL. The returned respURI is allocated with the new operator
 * and must be discarded with delete by the caller.
 *
 * @param SyncHdr - the SyncHdr object - NOT NULL
 */
const char* SyncMLProcessor::getRespURI(SyncHdr* syncHdr) {

    char* respURI = NULL;

    if (syncHdr == NULL) {
        goto finally;
    }
    respURI = stringdup(syncHdr->getRespURI());

finally:

    return respURI;
}


Chal* SyncMLProcessor::getChal(SyncBody* syncBody) {

    ArrayList* list = syncBody->getCommands();
    const char* name = NULL;
    Status* s     = NULL;
    Chal* chal    = NULL;

    for (int i = 0; i < list->size(); i++) {
        name = ((AbstractCommand*)(list->get(i)))->getName();    // is returned the pointer to the element not a new element
        if (name && strcmp(name, STATUS) == 0) {
            s = (Status*)list->get(i);
            if (strcmp(s->getCmd(), SYNC_HDR) == 0) {
                if (strcmp(s->getCmdRef(), "0") != 0) {

                    sprintf(lastErrorMsg, "Status/CmdRef either not found or not referring to SyncHeader!");
                    lastErrorCode = ERR_REPRESENTATION;
                    goto finally;
                }

                chal = s->getChal();
                if (chal == NULL) {
                    //
                    // no chal found
                    //
                    goto finally;
                }
                break;
            }
        }
    }

finally:

    return chal;
}

/*
* Return an array list of commands of the given command name. It return an ArrayList that have to be
* discarded by the caller
*/
ArrayList* SyncMLProcessor::getCommands(SyncBody* syncBody, const char*commandName) {

    ArrayList* ret = new ArrayList();
    AbstractCommand* a = NULL;

    for (int i = 0; i < syncBody->getCommands()->size(); i++) {
        a = getCommand(syncBody, commandName, i);
        if (a)
            ret->add(*a);
    }
    return ret;
}

// ------------------------------------------------------------- Private methods

/*
* To get a generic array element. It returns the <index> arrayElement it founds.
* 0-based.
*/
ArrayElement* SyncMLProcessor::getArrayElement(ArrayList* list, int index) {

    if (list == NULL)
        return NULL;

    ArrayElement* a     = NULL;
    int count           = 0;
    for (int i = 0; i < list->size(); i++) {
            if (count == index) {
                a = list->get(i);
                break;
            }
            ++ count;
    }
    return a;
}

/*
* Return the index number of occurrence of this command. If doesn't exists return NULL;
* The first command has number 0.
*/
AbstractCommand* SyncMLProcessor::getCommand(SyncBody* syncBody, const char*commandName, int index) {

    int iterator = 0, found = 0;
    ArrayList* list     = syncBody->getCommands();
    int l = list->size();
    AbstractCommand* a  = NULL;
    const char* name = NULL;
    do {
        a = (AbstractCommand*)getArrayElement(list, iterator);
        if (a) {
            name = a->getName();    // is returned the pointer to the element not a new element
            if (name && strcmp(name, commandName) == 0) {
                if (found == index)
                    break;
                else
                    found++;
            }
        }
        ++iterator;
    } while(a);

    return a;
}


int SyncMLProcessor::getStatusCode(SyncBody* syncBody, SyncSource* source, const char*commandName) {
    int ret = -1;

    ArrayList* list = syncBody->getCommands();
    const char* name = NULL;
    Status* s     = NULL;
    Data* data    = NULL;

    for (int i = 0; i < list->size(); i++) {
        name = ((AbstractCommand*)(list->get(i)))->getName();    // is returned the pointer to the element not a new element
        if (name && strcmp(name, STATUS) == 0) {
            s = (Status*)list->get(i);
            if (strcmp(s->getCmd(), commandName) == 0) {
                if (strcmp(commandName, SYNC_HDR) == 0) {
                    ret = getSyncHeaderStatusCode(s);
                } else if (strcmp(commandName, ALERT) == 0) {
                    ret = getAlertStatusCode(s, (char*)source->getName());
                }
                break;
            }
        }
    }

    return ret;

}

/*
 * Returns the status code for the SyncHeader command included
 * in the message sent by the client.
 *
 * @param syncBody - the SyncBody content
 */
int SyncMLProcessor::getSyncHeaderStatusCode(Status* s) {

    int ret = -1;
    Data* data    = NULL;

    if (s == NULL)
        goto finally;

    if (strcmp(s->getCmdRef(), "0") != 0) {

        sprintf(lastErrorMsg, "Status/CmdRef either not found or not referring to SyncHeader!");
        lastErrorCode = ERR_REPRESENTATION;
        goto finally;
    }

    data = s->getData();
    if (data->getData() == NULL) {
         //
        // It should not happen
        //
        sprintf(lastErrorMsg, "Status/Data not found!");
        lastErrorCode = ERR_REPRESENTATION;
        goto finally;
    }
    ret = strtol(data->getData() , NULL, 10);


finally:

    return ret;
}

/*
 * Returns the status code for the Alert relative to the given source.
 *
 * @param syncBody - the SyncBody content
 * @param sourceName - the name of the source
 */

int SyncMLProcessor::getAlertStatusCode(Status* s, const char* sourceName) {
    int ret = -1;
    if (s == NULL) {
        return ret;
    }

    Data* data = NULL;
    ArrayList* sourceRefs = s->getSourceRef();

    if (strcmp(((SourceRef*)(sourceRefs->get(0)))->getValue(), sourceName) == 0) {
        data = s->getData();
        if (data->getData() == NULL) {
            //
            // It should not happen
            //
            sprintf(lastErrorMsg, "Status/Data not found!");
            lastErrorCode = ERR_REPRESENTATION;
            return ret;
        }
        ret = strtol(data->getData(), NULL, 10);
    }

    return ret;
}

/*
 * Processes the initialization response. Returns 0 in case of success, an
 * error code in case of error.
 *
 * @param msg the response from the server
 */

Sync* SyncMLProcessor::getSyncResponse(SyncML* syncml, int index) {

    AbstractCommand* a  = NULL;
    Sync* sync          = NULL;

    a = getCommand(syncml->getSyncBody(), SYNC, index);
    sync = (Sync*)a;

    return sync;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -