Example #1
0
        public void processServerResponse_ver3(JSONArrayIterator oJsonArr)
        {
            PROF.START("Data1");

            int nVersion = 0;

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("version"))
            {
                nVersion = oJsonArr.getCurItem().getInt("version");
                oJsonArr.next();
            }

            if (nVersion != getProtocol().getVersion())
            {
                LOG.ERROR("Sync server send data with incompatible version. Client version: " + getProtocol().getVersion() +
                          "; Server response version: " + nVersion + ". Source name: " + getName());
                getSync().stopSync();
                m_nErrCode = RhoAppAdapter.ERR_UNEXPECTEDSERVERRESPONSE;
                return;
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("token"))
            {
                processToken(oJsonArr.getCurItem().getUInt64("token"));
                oJsonArr.next();
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("source"))
            {
                //skip it. it uses in search only
                oJsonArr.next();
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("count"))
            {
                setCurPageCount(oJsonArr.getCurItem().getInt("count"));
                oJsonArr.next();
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("refresh_time"))
            {
                setRefreshTime(oJsonArr.getCurItem().getInt("refresh_time"));
                oJsonArr.next();
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("progress_count"))
            {
                //TODO: progress_count
                //setTotalCount(oJsonArr.getCurItem().getInt("progress_count"));
                oJsonArr.next();
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("total_count"))
            {
                setTotalCount(oJsonArr.getCurItem().getInt("total_count"));
                oJsonArr.next();
            }

            //if ( getServerObjectsCount() == 0 )
            //    getNotify().fireSyncNotification(this, false, RhoAppAdapter.ERR_NONE, "");

            if (getToken() == 0)
            {
                //oo conflicts
                getDB().executeSQL("DELETE FROM changed_values where source_id=? and sent>=3", getID());
                //
            }

            LOG.INFO("Got " + getCurPageCount() + "(Processed: " + getServerObjectsCount() + ") records of " + getTotalCount() + " from server. Source: " + getName()
                     + ". Version: " + nVersion);

            PROF.STOP("Data1");
            if (!oJsonArr.isEnd() && getSync().isContinueSync())
            {
                JSONEntry oCmds = oJsonArr.getCurItem();
                PROF.START("Data");

                if (oCmds.hasName("schema-changed"))
                {
                    getSync().setSchemaChanged(true);
                }
                else if (!processServerErrors(oCmds))
                {
                    getDB().startTransaction();

                    if (SyncEngine.getSourceOptions().getBoolProperty(getID(), "pass_through"))
                    {
                        if (m_bSchemaSource)
                        {
                            getDB().executeSQL("DELETE FROM " + getName());
                        }
                        else
                        {
                            getDB().executeSQL("DELETE FROM object_values WHERE source_id=?", getID());
                        }
                    }

                    if (oCmds.hasName("metadata") && getSync().isContinueSync())
                    {
                        String strMetadata = oCmds.getString("metadata");
                        getDB().executeSQL("UPDATE sources SET metadata=? WHERE source_id=?", strMetadata, getID());
                    }
                    if (oCmds.hasName("links") && getSync().isContinueSync())
                    {
                        processSyncCommand("links", oCmds.getEntry("links"), true);
                    }
                    if (oCmds.hasName("delete") && getSync().isContinueSync())
                    {
                        processSyncCommand("delete", oCmds.getEntry("delete"), true);
                    }
                    if (oCmds.hasName("insert") && getSync().isContinueSync())
                    {
                        processSyncCommand("insert", oCmds.getEntry("insert"), true);
                    }

                    PROF.STOP("Data");

                    PROF.START("DB");
                    getDB().endTransaction();
                    PROF.STOP("DB");

                    getNotify().fireObjectsNotification();
                }
            }

            PROF.START("Data1");
            if (getCurPageCount() > 0)
            {
                getNotify().fireSyncNotification(this, false, RhoAppAdapter.ERR_NONE, "");
            }
            PROF.STOP("Data1");
        }
Example #2
0
        public void doSearch(Vector <String> arSources, String strParams, String strAction, boolean bSearchSyncChanges, int nProgressStep)
        {
            try
            {
                prepareSync(esSearch, null);
                if (!isContinueSync())
                {
                    if (getState() != esExit)
                    {
                        setState(esNone);
                    }

                    return;
                }

                TimeInterval startTime = TimeInterval.getCurrentTime();

                if (bSearchSyncChanges)
                {
                    for (int i = 0; i < (int)arSources.size(); i++)
                    {
                        SyncSource pSrc = findSourceByName((String)arSources.elementAt(i));
                        if (pSrc != null)
                        {
                            pSrc.syncClientChanges();
                        }
                    }
                }

                while (isContinueSync())
                {
                    int    nSearchCount = 0;
                    String strUrl       = getProtocol().getServerQueryUrl(strAction);
                    String strQuery     = getProtocol().getServerQueryBody("", getClientID(), getSyncPageSize());

                    if (strParams.length() > 0)
                    {
                        strQuery += strParams;
                    }

                    String strTestResp = "";
                    for (int i = 0; i < (int)arSources.size(); i++)
                    {
                        SyncSource pSrc = findSourceByName((String)arSources.elementAt(i));
                        if (pSrc != null)
                        {
                            strQuery += "&sources[][name]=" + pSrc.getName();

                            if (!pSrc.isTokenFromDB() && pSrc.getToken() > 1)
                            {
                                strQuery += "&sources[][token]=" + pSrc.getToken();
                            }

                            strTestResp = getSourceOptions().getProperty(pSrc.getID(), "rho_server_response");
                        }
                    }

                    LOG.INFO("Call search on server. Url: " + (strUrl + strQuery));
                    NetResponse resp = getNet().pullData(strUrl + strQuery, this);

                    if (!resp.isOK())
                    {
                        stopSync();
                        m_nErrCode = RhoAppAdapter.getErrorFromResponse(resp);
                        m_strError = resp.getCharData();
                        continue;
                    }

                    String szData = null;
                    if (strTestResp != null && strTestResp.length() > 0)
                    {
                        szData = strTestResp;
                    }
                    else
                    {
                        szData = resp.getCharData();
                    }

                    JSONArrayIterator oJsonArr = new JSONArrayIterator(szData);

                    for ( ; !oJsonArr.isEnd() && isContinueSync(); oJsonArr.next())
                    {
                        JSONArrayIterator oSrcArr = oJsonArr.getCurArrayIter();        //new JSONArrayIterator(oJsonArr.getCurItem());
                        if (oSrcArr.isEnd())
                        {
                            break;
                        }

                        int nVersion = 0;
                        if (!oSrcArr.isEnd() && oSrcArr.getCurItem().hasName("version"))
                        {
                            nVersion = oSrcArr.getCurItem().getInt("version");
                            oSrcArr.next();
                        }

                        if (nVersion != getProtocol().getVersion())
                        {
                            LOG.ERROR("Sync server send search data with incompatible version. Client version: " + getProtocol().getVersion() +
                                      "; Server response version: " + nVersion);
                            stopSync();
                            m_nErrCode = RhoAppAdapter.ERR_SYNCVERSION;
                            continue;
                        }

                        if (!oSrcArr.isEnd() && oSrcArr.getCurItem().hasName("token"))
                        {
                            oSrcArr.next();
                        }

                        if (!oSrcArr.getCurItem().hasName("source"))
                        {
                            LOG.ERROR("Sync server send search data without source name.");
                            stopSync();
                            m_nErrCode = RhoAppAdapter.ERR_UNEXPECTEDSERVERRESPONSE;
                            m_strError = szData;
                            continue;
                        }

                        String     strSrcName = oSrcArr.getCurItem().getString("source");
                        SyncSource pSrc       = findSourceByName(strSrcName);
                        if (pSrc == null)
                        {
                            LOG.ERROR("Sync server send search data for unknown source name:" + strSrcName);
                            stopSync();
                            m_nErrCode = RhoAppAdapter.ERR_UNEXPECTEDSERVERRESPONSE;
                            m_strError = szData;
                            continue;
                        }

                        oSrcArr.reset(0);
                        pSrc.setProgressStep(nProgressStep);
                        pSrc.processServerResponse_ver3(oSrcArr);

                        nSearchCount += pSrc.getCurPageCount();

                        if (pSrc.getServerError().length() > 0)
                        {
                            if (m_strServerError.length() > 0)
                            {
                                m_strServerError += "&";
                            }

                            m_strServerError += pSrc.getServerError();
                            m_nErrCode        = pSrc.getErrorCode();
                        }
                    }

                    if (nSearchCount == 0)
                    {
                        for (int i = 0; i < (int)arSources.size(); i++)
                        {
                            SyncSource pSrc = findSourceByName((String)arSources.elementAt(i));
                            if (pSrc != null)
                            {
                                pSrc.processToken(0);
                            }
                        }
                        break;
                    }
                }

                getNotify().fireAllSyncNotifications(true, m_nErrCode, m_strError, m_strServerError);

                //update db info
                TimeInterval endTime = TimeInterval.getCurrentTime();
                //unsigned long timeUpdated = CLocalTime().toULong();
                for (int i = 0; i < (int)arSources.size(); i++)
                {
                    SyncSource oSrc = findSourceByName((String)arSources.elementAt(i));
                    if (oSrc == null)
                    {
                        continue;
                    }
                    oSrc.getDB().executeSQL("UPDATE sources set last_updated=?,last_inserted_size=?,last_deleted_size=?, " +
                                            "last_sync_duration=?,last_sync_success=?, backend_refresh_time=? WHERE source_id=?",
                                            endTime.toULong() / 1000, oSrc.getInsertedCount(), oSrc.getDeletedCount(),
                                            endTime.minus(startTime).toULong(), oSrc.getGetAtLeastOnePage()?1:0,
                                            oSrc.getRefreshTime(), oSrc.getID());
                }
                //

                getNotify().cleanCreateObjectErrors();
                if (getState() != esExit)
                {
                    setState(esNone);
                }
            } catch (Exception exc) {
                LOG.ERROR("Search failed.", exc);

                getNotify().fireAllSyncNotifications(true, RhoAppAdapter.ERR_RUNTIME, "", "");
            }
        }
Example #3
0
        void syncServerChanges()
        {
            LOG.INFO("Sync server changes source ID :" + getID());

            while (getSync().isContinueSync())
            {
                setCurPageCount(0);
                String strUrl   = getProtocol().getServerQueryUrl("");
                String strQuery = getProtocol().getServerQueryBody(getName(), getSync().getClientID(), getSync().getSyncPageSize());

                if (!m_bTokenFromDB && getToken() > 1)
                {
                    strQuery += "&token=" + getToken();
                }

                LOG.INFO("Pull changes from server. Url: " + (strUrl + strQuery));

                NetResponse resp = null;
                try{
                    PROF.START("Net");
                    resp = getNet().pullData(strUrl + strQuery, getSync());
                    PROF.STOP("Net");

                    if (!resp.isOK())
                    {
                        getSync().stopSync();
                        m_nErrCode = RhoAppAdapter.getErrorFromResponse(resp);
                        m_strError = resp.getCharData();
                        continue;
                    }
                }catch (Exception exc)
                {
                    m_nErrCode = RhoAppAdapter.getNetErrorCode(exc);
                    throw exc;
                }

                String szData      = null;
                String strTestResp = SyncEngine.getSourceOptions().getProperty(getID(), "rho_server_response");
                if (strTestResp != null && strTestResp.length() > 0)
                {
                    szData = strTestResp;
                }
                else
                {
                    szData = resp.getCharData();
                }

                PROF.START("Parse");
                JSONArrayIterator oJsonArr = new JSONArrayIterator(szData);
                PROF.STOP("Parse");

                processServerResponse_ver3(oJsonArr);

                if (SyncEngine.getSourceOptions().getBoolProperty(getID(), "pass_through"))
                {
                    processToken(0);
                }

                if (getToken() == 0)
                {
                    break;
                }
            }

            if (getSync().isSchemaChanged())
            {
                getSync().stopSync();
            }
        }