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

📄 syncmanager.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        LOG.debug("devinfo: %s", devInfStr->c_str());
        // Add syncUrl to devInfHash, so hash changes if syncUrl has changed
        devInfStr->append("<SyncURL>");
        devInfStr->append(config.getAccessConfig().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.getDeviceConfig().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) {
                        ret = lastErrorCode = 745; // FIXME
                        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 = lastErrorCode;
            goto finally;
        }

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

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

        currentState = STATE_PKG1_SENDING;

        if (transportAgent == NULL) {
            transportAgent = TransportAgentFactory::getTransportAgent(url, proxy, responseTimeout, maxMsgSize);
            transportAgent->setReadBufferSize(readBufferSize);
            // 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.getAccessConfig().getCompression());
            delete [] ua; ua = NULL;
        }
        else {
            transportAgent->setURL(url);
        }
        if (lastErrorCode != 0) { // connection: lastErrorCode = 2005: Impossible to establish internet connection
            ret = lastErrorCode;
            goto finally;
        }

        deleteSyncML(&syncml);
        deleteChal(&serverChal);
        deleteArrayList(&commands);
        deleteCred(&cred);

        //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 && lastErrorCode == 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 = lastErrorCode;
                /*
                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 = lastErrorCode;
            LOG.error("Error processing alert response.");
            goto finally;
        }

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

        ret = syncMLProcessor.processSyncHdrStatus(syncml);

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

        } else if (isErrorStatus(ret) && ! isAuthFailed(ret)) {
            lastErrorCode = ret;
            sprintf(lastErrorMsg, "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) {
                lastErrorCode = ret;
                sprintf(logmsg, "Alert Status from server = %d", ret);
                LOG.error(logmsg);
                setSourceStateAndError(count, SOURCE_ERROR, ret, logmsg);
            }
        }
        if (isToExit()) {
            // error. no source to sync
            ret = lastErrorCode;
            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);
            }
        }

        //
        // 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)

                    if (isGet) {
                        Get *get = (Get *)cmd;
                        ArrayList *items = get->getItems();
                        BOOL sendDevInf = FALSE;

                        Results results;
                        for (int i = 0; i < items->size(); i++) {
                            Item *item = (Item *)items->get(i);

                            // we are not very picky: as long as the Item is
                            // called "./devinf11" as required by the standard
                            // we return our device infos
                            Target *target = item->getTarget();
                            if (target && target->getLocURI() &&
                                !strcmp(target->getLocURI(),
                                         DEVINF_URI)) {
                                sendDevInf = TRUE;
                            } else {
                                LOG.debug("ignoring request to Get item #%d", i);
                            }
                        }

                        // cannot send if we have nothing, then simply acknowledge the request,
                        // but ignore it
                        if (sendDevInf && devInf) {
                            AbstractCommand *result = syncMLBuilder.prepareDevInf(cmd, *devInf);
                            if (result) {
                                commands->add(*result);
                                delete result;
                            }
                        }
                    } else {
                        // simply acknowledge Put
                    }

                    if (statusCode) {
                        status = syncMLBuilder.prepareCmdStatus(*cmd, statusCode);
                        if (status) {
		                    // Fire Sync Status Event: status from client
                            fireSyncStatusEvent(status->getCmd(), status->getStatusCode(), NULL, NULL, NULL , CLIENT_STATUS);

                            commands->add(*status);
                            deleteStatus(&status);
                        }
                    }
                }
            }
        }

        //
        // Client Authentication. The auth of the client on the server

⌨️ 快捷键说明

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