Beispiel #1
0
        String requestClientIDByNet()
        {
            //String strBody = "";
            //TODO: send client register info in client create
            //        if ( ClientRegister.getInstance() != null )
            //            strBody += ClientRegister.getInstance().getRegisterBody();

            NetResponse resp = getNetClientID().pullData(getProtocol().getClientCreateUrl(), this);

            if (resp.isOK() && resp.getCharData() != null)
            {
                String szData = resp.getCharData();

                JSONEntry oJsonEntry = new JSONEntry(szData);

                //if (oJsonEntry.hasName("sources") )
                //    processServerSources(szData);

                JSONEntry oJsonObject = oJsonEntry.getEntry("client");
                if (!oJsonObject.isEmpty())
                {
                    return(oJsonObject.getString("client_id"));
                }
            }
            else
            {
                m_nErrCode = RhoAppAdapter.getErrorFromResponse(resp);
                if (m_nErrCode == RhoAppAdapter.ERR_NONE)
                {
                    m_nErrCode = RhoAppAdapter.ERR_UNEXPECTEDSERVERRESPONSE;
                    m_strError = resp.getCharData();
                }
            }

            return("");
        }
Beispiel #2
0
        void loadBulkPartition(String strPartition)
        {
            DBAdapter dbPartition = getDB(strPartition);
            String    serverUrl = RhoConf.getInstance().getPath("syncserver");
            String    strUrl = serverUrl + "bulk_data";
            String    strQuery = "?client_id=" + m_clientID + "&partition=" + strPartition;
            String    strDataUrl = "", strCmd = "", strCryptKey = "";

            getNotify().fireBulkSyncNotification(false, "start", strPartition, RhoAppAdapter.ERR_NONE);

            while (strCmd.length() == 0 && isContinueSync())
            {
                NetResponse resp = getNet().pullData(strUrl + strQuery, this);
                if (!resp.isOK() || resp.getCharData() == null)
                {
                    LOG.ERROR("Bulk sync failed: server return an error.");
                    stopSync();
                    getNotify().fireBulkSyncNotification(true, "", strPartition, RhoAppAdapter.getErrorFromResponse(resp));
                    return;
                }

                LOG.INFO("Bulk sync: got response from server: " + resp.getCharData());

                String    szData     = resp.getCharData();
                JSONEntry oJsonEntry = new JSONEntry(szData);
                strCmd = oJsonEntry.getString("result");
                if (oJsonEntry.hasName("url"))
                {
                    strDataUrl = oJsonEntry.getString("url");
                }

                if (strCmd.compareTo("wait") == 0)
                {
                    int nTimeout = RhoConf.getInstance().getInt("bulksync_timeout_sec");
                    if (nTimeout == 0)
                    {
                        nTimeout = 5;
                    }

                    SyncThread.getInstance().wait(nTimeout);
                    strCmd = "";
                }
            }

            if (strCmd.compareTo("nop") == 0)
            {
                LOG.INFO("Bulk sync return no data.");
                getNotify().fireBulkSyncNotification(true, "", strPartition, RhoAppAdapter.ERR_NONE);
                return;
            }

            if (!isContinueSync())
            {
                return;
            }

            getNotify().fireBulkSyncNotification(false, "download", strPartition, RhoAppAdapter.ERR_NONE);

            String fDataName     = makeBulkDataFileName(strDataUrl, dbPartition.getDBPath(), "");
            String strZip        = ".rzip";
            String strSqlDataUrl = CFilePath.join(getHostFromUrl(serverUrl), strDataUrl) + strZip;

            LOG.INFO("Bulk sync: download data from server: " + strSqlDataUrl);
            {
                NetResponse resp1 = getNet().pullFile(strSqlDataUrl, fDataName + strZip, this, null);
                if (!resp1.isOK())
                {
                    LOG.ERROR("Bulk sync failed: cannot download database file.");
                    stopSync();
                    getNotify().fireBulkSyncNotification(true, "", strPartition, RhoAppAdapter.getErrorFromResponse(resp1));
                    return;
                }
            }

            if (!isContinueSync())
            {
                return;
            }

            LOG.INFO("Bulk sync: unzip db");

            if (!RHODESAPP().unzip_file(fDataName + strZip))
            {
                CRhoFile.deleteFile(fDataName + strZip);
                LOG.ERROR("Bulk sync failed: cannot unzip database file.");
                stopSync();
                getNotify().fireBulkSyncNotification(true, "", strPartition, RhoAppAdapter.ERR_UNEXPECTEDSERVERRESPONSE);
                return;
            }
            CRhoFile.deleteFile(fDataName + strZip);

            LOG.INFO("Bulk sync: start change db");
            getNotify().fireBulkSyncNotification(false, "change_db", strPartition, RhoAppAdapter.ERR_NONE);

            dbPartition.setBulkSyncDB(fDataName, strCryptKey);
            processServerSources("{\"partition\":\"" + strPartition + "\"}");

            LOG.INFO("Bulk sync: end change db");
            getNotify().fireBulkSyncNotification(false, "", strPartition, RhoAppAdapter.ERR_NONE);
        }
Beispiel #3
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");
        }