syncmanager.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 1,631 行 · 第 1/5 页

CPP
1,631
字号
    // Copy and validate given
    sourcesNumber = assignSources(s);
    if (getLastErrorCode()) {
        ret = getLastErrorCode();    // set when a source has an invalid config
    }

    if (isToExit()) {
        if (!ret) {
            // error: no source to sync
            setError(ERR_NO_SOURCE_TO_SYNC, ERRMSG_NO_SOURCE_TO_SYNC);
            ret = getLastErrorCode();
        }

        goto finally;
    }

    // Set proxy username/password if proxy is used.
    if (config.getUseProxy()) {
        //char* proxyHost = config.getProxyHost();
        //int    proxyPort = config.getProxyPort();
        const char* proxyUser = config.getProxyUsername();
        const char* proxyPwd  = config.getProxyPassword();
        proxy.setProxy(NULL, 0, proxyUser, proxyPwd);
    }

    mappings = new ArrayList[sourcesNumber + 1];
    for (count = 0; count < sourcesNumber; count++) {
        LOG.info(MSG_PREPARING_SYNC, sources[count]->getConfig().getName());
    }

    syncMLBuilder.resetCommandID();
    syncMLBuilder.resetMessageID();
    config.setBeginSync(timestamp);

    // Create the device informations.
    devInf = createDeviceInfo();

    // check device information for changes
    if (devInf) {
        char md5[16];
        devInfStr = Formatter::getDevInf(devInf);
        LOG.debug("Checking devinfo...");
        // Add syncUrl to devInfHash, so hash changes if syncUrl has changed
        devInfStr->append("<SyncURL>");
        devInfStr->append(config.getSyncURL());
        devInfStr->append("</SyncURL>");
        calculateMD5(devInfStr->c_str(), devInfStr->length(), md5);
        devInfHash[b64_encode(devInfHash, md5, sizeof(md5))] = 0;
        LOG.debug("devinfo hash: %s", devInfHash);

        // compare against previous device info hash:
        // if different, then the local config has changed and
        // infos should be sent again
        if (strcmp(devInfHash, config.getDevInfHash())) {
            putDevInf = true;
        }
        LOG.debug("devinfo %s", putDevInf ? "changed, retransmit" : "unchanged, no need to send");
    } else {
        LOG.debug("no devinfo available");
    }

    if (isServerAuthRequired == false) {
        isServerAuthenticated = true;
    }

    // Authentication
    do {
        deleteCred(&cred);
        deleteAlert(&alert);
        deleteSyncML(&syncml);
        deleteArrayList(&alerts);
        

        bool addressChange = false;

        // credential of the client
        if (isClientAuthenticated == false) {
            char anc[DIM_ANCHOR];
            timestamp = (unsigned long)time(NULL);
            for (count = 0; count < sourcesNumber; count ++) {
                if (!sources[count]->getReport()->checkState())
                    continue;
                sources[count]->setNextSync(timestamp);
                timestampToAnchor(sources[count]->getNextSync(), anc);
                sources[count]->setNextAnchor(anc);
                // Test if this source is for AddressChangeNotification
                int prefmode = sources[count]->getPreferredSyncMode();
                if( prefmode == SYNC_ADDR_CHANGE_NOTIFICATION ) {
                    alert = syncMLBuilder.prepareAddrChangeAlert(*sources[count]);
                    if(!alert) {
                        LOG.error("Error preparing Address Change Sync");
                        goto finally;
                    }
                    addressChange = true;   // remember that this sync is for
                                            // address change notification
                }
                else {
                    alert = syncMLBuilder.prepareInitAlert(*sources[count], maxObjSize);
                }
                alerts->add(*alert);
                deleteAlert(&alert);
            }
            cred = credentialHandler.getClientCredential();
            strcpy(credentialInfo, cred->getAuthentication()->getPassword());

        }

        // actively send out device infos?
        if (putDevInf) {
            AbstractCommand* put = syncMLBuilder.prepareDevInf(NULL, *devInf);
            if (put) {
                commands.add(*put);
                delete put;
            }
            putDevInf = false;
        }

        // "cred" only contains an encoded strings as username, also
        // need the original username for LocName
        syncml = syncMLBuilder.prepareInitObject(cred, alerts, &commands, maxMsgSize, maxObjSize);
        if (syncml == NULL) {
            ret = getLastErrorCode();
            goto finally;
        }

        initMsg = syncMLBuilder.prepareMsg(syncml);
        if (initMsg == NULL) {
            ret = getLastErrorCode();
            goto finally;
        }

        LOG.debug("%s", MSG_INITIALIZATATION_MESSAGE);

        currentState = STATE_PKG1_SENDING;

        if (transportAgent == NULL) {
            transportAgent = TransportAgentFactory::getTransportAgent(url, proxy, responseTimeout, maxMsgSize);
            transportAgent->setReadBufferSize(readBufferSize);
            transportAgent->setSSLServerCertificates(config.getSSLServerCertificates());
            transportAgent->setSSLVerifyServer(config.getSSLVerifyServer());
            transportAgent->setSSLVerifyHost(config.getSSLVerifyHost());
            // Here we also ensure that the user agent string is valid
            const char* ua = getUserAgent(config);
            LOG.debug("User Agent = %s", ua);
            transportAgent->setUserAgent(ua);
            transportAgent->setCompression(config.getCompression());
            delete [] ua; ua = NULL;
        }
        else {
            transportAgent->setURL(url);
        }
        if (getLastErrorCode() != 0) { // connection: lastErrorCode = 2005: Impossible to establish internet connection
            ret = getLastErrorCode();
            goto finally;
        }

        deleteSyncML(&syncml);
        deleteChal(&serverChal);
        deleteCred(&cred);
        commands.clear();

        //Fire Initialization Event
        fireSyncEvent(NULL, SEND_INITIALIZATION);

        responseMsg = transportAgent->sendMessage(initMsg);
        // Non-existant or empty reply?
        // Synthesis server replies with empty message to
        // a message that it cannot parse.
        if (responseMsg == NULL || !responseMsg[0]) {
            if (responseMsg) {
                delete [] responseMsg;
                responseMsg = NULL;
            }

            if ( addressChange && getLastErrorCode() == ERR_READING_CONTENT ) {
                // This is not an error if it's an AddressChange
                ret = 0;
            }
            else {
                // use last error code if one has been set (might not be the case)
                ret = getLastErrorCode();
                /*
                if (!ret) {
                    ret = ERR_READING_CONTENT;
                }
                */
            }
            goto finally;
        }

        // increment the msgRef after every send message
        syncMLBuilder.increaseMsgRef();
        syncMLBuilder.resetCommandID();

        syncml = syncMLProcessor.processMsg(responseMsg);
        safeDelete(&responseMsg);
        safeDelete(&initMsg);

        if (syncml == NULL) {
            ret = getLastErrorCode();
            LOG.error("Error processing alert response.");
            goto finally;
        }

        // ret = syncMLProcessor.processInitResponse(*sources[0], syncml, alerts);

        ret = syncMLProcessor.processSyncHdrStatus(syncml);

        if (ret == -1) {
            ret = getLastErrorCode();
            LOG.error("Error processing SyncHdr Status");
            goto finally;

        } else if (isErrorStatus(ret) && ! isAuthFailed(ret)) {
            setErrorF(ret, "Error from server: status = %d", ret);
            goto finally;
        }

        for (count = 0; count < sourcesNumber; count ++) {
            if (!sources[count]->getReport()->checkState())
                continue;

            int sourceRet = syncMLProcessor.processAlertStatus(*sources[count], syncml, alerts);
            if (isAuthFailed(ret) && sourceRet == -1) {
                // Synthesis server does not include SourceRefs if
                // authentication failed. Remember the authentication
                // failure in that case, otherwise we'll never get to the getChal() below.
            } else {
                ret = sourceRet;
            }

            if (ret == -1 || ret == 404 || ret == 415) {
                setErrorF(ret, "AlertStatus from server %d", ret);
                LOG.error("%s", getLastErrorMsg());
                setSourceStateAndError(count, SOURCE_ERROR, ret, getLastErrorMsg());
            }
        }
        if (isToExit()) {
            // error. no source to sync
            ret = getLastErrorCode();
            goto finally;
        }

        //
        // Set the uri with session
        //
        respURI = syncMLProcessor.getRespURI(syncml->getSyncHdr());
        if (respURI) {
            url = respURI;
            transportAgent->setURL(url);
        }
        //
        // Server Authentication
        //
        if (isServerAuthenticated == false) {

            cred = syncml->getSyncHdr()->getCred();
            if (cred == NULL) {
                if (serverAuthRetries == 1) {
                    // create the serverNonce if needed and set into the CredentialHendler, serverNonce property
                    serverChal = credentialHandler.getServerChal(isServerAuthenticated);
                    authStatusCode = 407;
                    serverAuthRetries++;
                } else {
                     ret = -1;
                     goto finally;
                }

            } else {
                isServerAuthenticated = credentialHandler.performServerAuth(cred);
                if (isServerAuthenticated) {
                    serverChal   = credentialHandler.getServerChal(isServerAuthenticated);
                    authStatusCode = 212;
                }
                else {
                    if (strcmp(credentialHandler.getServerAuthType(), AUTH_TYPE_MD5) == 0 ||
                        serverAuthRetries == 1)
                    {
                        serverChal   = credentialHandler.getServerChal(isServerAuthenticated);
                        authStatusCode = 401;

                    } else {
                        ret = -1;   //XXX
                        LOG.error("Server not authenticated");
                        goto finally;
                    }
                    serverAuthRetries++;
                }
            }
            cred = NULL; // this cred is only a reference
        } else  {
            authStatusCode = 200;
        }
        status = syncMLBuilder.prepareSyncHdrStatus(serverChal, authStatusCode);
        commands.add(*status);
        deleteStatus(&status);
        list = syncMLProcessor.getCommands(syncml->getSyncBody(), ALERT);
        for (count = 0; count < sourcesNumber; count ++) {
            if (!sources[count]->getReport()->checkState())
                continue;

            status = syncMLBuilder.prepareAlertStatus(*sources[count], list, authStatusCode);
            if (status) {
                commands.add(*status);
                deleteStatus(&status);
            }
        }
        if (list) {
            delete list;
            list = NULL;
        }

        //
        // Process Put/Get commands
        //
        list = syncml->getSyncBody()->getCommands();
        int cmdindex;
        for (cmdindex = 0; cmdindex < list->size(); cmdindex++) {
            AbstractCommand* cmd = (AbstractCommand*)list->get(cmdindex);
            const char* name = cmd->getName();
            if (name) {
                bool isPut = !strcmp(name, PUT);
                bool isGet = !strcmp(name, GET);

                if (isGet || isPut) {
                    int statusCode = 200; // if set, then send it (on by default)

⌨️ 快捷键说明

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