void CSyncSource::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=" + convertToStringA(getToken()); LOG(INFO) + "Pull changes from server. Url: " + (strUrl+strQuery); PROF_START("Net"); NetResponse(resp,getNet().pullData(strUrl+strQuery, &getSync())); PROF_STOP("Net"); if ( !resp.isOK() ) { getSync().stopSync(); m_nErrCode = RhoAppAdapter.getErrorFromResponse(resp); m_strError = resp.getCharData(); continue; } const char* szData = resp.getCharData(); //const char* szData = "[{\"version\":3},{\"token\":\"\"},{\"count\":0},{\"progress_count\":28},{\"total_count\":28},{\"source-error\":{\"login-error\":{\"message\":\"s currently connected from another machine\"}}}]"; //const char* szData = "[{\"version\":3},{\"token\":\"\"},{\"count\":0},{\"progress_count\":0},{\"total_count\":0},{\"create-error\":{\"0_broken_object_id\":{\"name\":\"wrongname\",\"an_attribute\":\"error create\"},\"0_broken_object_id-error\":{\"message\":\"error create\"}}}]"; //const char* szData = "[{\"version\":3},{\"token\":\"35639160294387\"},{\"count\":3},{\"progress_count\":0},{\"total_count\":3},{\"metadata\":\"{\\\"foo\\\":\\\"bar\\\"}\",\"insert\":{\"1\":{\"price\":\"199.99\",\"brand\":\"Apple\",\"name\":\"iPhone\"}}}]"; //LOG(INFO) + szData; PROF_START("Parse"); CJSONArrayIterator oJsonArr(szData); PROF_STOP("Parse"); processServerResponse_ver3(oJsonArr); if (getSync().getSourceOptions().getBoolProperty(getID(), "pass_through")) processToken(0); if ( getToken() == 0 ) break; } if ( getSync().isSchemaChanged() ) getSync().stopSync(); }
void CSyncSource::processServerData(const char* szData) { PROF_START("Parse"); CJSONArrayIterator oJsonArr(szData); PROF_STOP("Parse"); PROF_START("Data1"); if ( !oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("error") ) { m_strError = oJsonArr.getCurItem().getString("error"); m_nErrCode = RhoRuby.ERR_CUSTOMSYNCSERVER; getSync().stopSync(); return; } if ( !oJsonArr.isEnd() ) { setCurPageCount(oJsonArr.getCurItem().getInt("count")); oJsonArr.next(); } int nVersion = 0; if ( !oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("version") ) { nVersion = oJsonArr.getCurItem().getInt("version"); oJsonArr.next(); } if ( nVersion != getSync().SYNC_VERSION() ) { LOG(ERROR) + "Sync server send data with incompatible version. Client version: " + convertToStringA(getSync().SYNC_VERSION()) + "; Server response version: " + convertToStringA(nVersion) + ". Source name: " + getName(); getSync().stopSync(); m_nErrCode = RhoRuby.ERR_SYNCVERSION; return; } if ( !oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("rt") ) { setRefreshTime(oJsonArr.getCurItem().getInt("rt")); 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, RhoRuby.ERR_NONE, ""); if ( !oJsonArr.isEnd() ) { processToken(oJsonArr.getCurItem().getUInt64("token")); oJsonArr.next(); }else if ( getCurPageCount() == 0 ) { //oo conflicts getDB().executeSQL("DELETE FROM changed_values where source_id=? and sent>=3", getID() ); // processToken(0); } LOG(INFO) + "Got " + getCurPageCount() + "(Processed: " + getServerObjectsCount() + ") records of " + getTotalCount() + " from server. Source: " + getName() + ". Version: " + nVersion; PROF_STOP("Data1"); if ( !oJsonArr.isEnd() && getSync().isContinueSync() ) { PROF_START("Data"); //TODO: support DBExceptions getDB().startTransaction(); int nSavedPos = oJsonArr.getCurPos(); setSyncServerDataPass(edpNone); processServerData_Ver1(oJsonArr); setSyncServerDataPass(edpDeleteObjects); oJsonArr.reset(nSavedPos); processServerData_Ver1(oJsonArr); PROF_STOP("Data"); PROF_START("DB"); getDB().endTransaction(); PROF_STOP("DB"); getNotify().fireObjectsNotification(); } PROF_START("Data1"); if ( getCurPageCount() > 0 ) getNotify().fireSyncNotification(this, false, RhoRuby.ERR_NONE, ""); PROF_STOP("Data1"); }
void CSyncSource::syncServerChanges() { LOG(INFO) + "Sync server changes source ID :" + getID(); while( getSync().isContinueSync() ) { setCurPageCount(0); String strUrl = getUrl(); if ( m_strAction.length() > 0 ) strUrl = CFilePath::join( strUrl, m_strAction); String strQuery = getSync().SYNC_SOURCE_FORMAT() + "&client_id=" + getSync().getClientID() + "&p_size=" + getSync().SYNC_PAGE_SIZE() + "&version=" + convertToStringA(getSync().SYNC_VERSION()); if ( m_strParams.length() > 0 ) strQuery += m_strParams; if( m_strUrlParams.length() > 0 ) strQuery += "&" + m_strUrlParams; if ( getAskParams().length() > 0 ) { strUrl += getSync().SYNC_ASK_ACTION(); strQuery += "&question=" + getAskParams(); } if ( !m_bTokenFromDB && getToken() > 1 ) strQuery += "&ack_token=" + convertToStringA(getToken()); LOG(INFO) + "Pull changes from server. Url: " + (strUrl+strQuery); PROF_START("Net"); NetResponse(resp,getNet().pullData(strUrl+strQuery, &getSync())); PROF_STOP("Net"); if ( !resp.isOK() ) { getSync().stopSync(); if (resp.isResponseRecieved()) m_nErrCode = RhoRuby.ERR_REMOTESERVER; else m_nErrCode = RhoRuby.ERR_NETWORK; continue; } processServerData(resp.getCharData()); //String strData = //"[{count:10},{version:1},{total_count: 5425},{token: 123},{s:\"RhoDeleteSource\",ol:[{o:\"rho_del_obj\",av:[{i:55550425},{i:75665819},{i:338165272},{i:402396629},{i:521753981},{i:664143530},{i:678116186},{i:831092394},{i:956041217},{i:970452458}]}]}]"; /*"[{count: 124},{version: 1},{total_count: 5425},{token: 123}," "{s:\"Product\",ol:[" "{oo:\"123\",o:\"2ed2e0c7-8c4c-99c6-1b37-498d250bb8e7\",av:[" "{a:\"first_name\",i:47354289,v:\"Lars. \n\n Burgess\", t:\"blob\"}," "{a:\"second_name\",i:55555,v:\"Burgess\"}]}," "{oo:\"456\", e:\"Something went wrong creating this record on the backend: code 7\"}" "]}]"; */ /*"[{count: 1},{version: 1},{total_count: 1},{token: 123}," "{s:\"Product\",ol:[" "{oo:\"94\", e:\"Something went wrong creating this record on the backend: code 7\"}" "]}]";*/ /*"[{count: 1},{version: 1},{total_count: 1},{token: 123}," "{s:\"Product\",ol:[" "{o:\"94\", av:[" "{a:\"TEST\",i:55555,v:\"Geny\"}]}," "]}]"; //u:\"query\", processServerData(strData.c_str()); */ if ( getAskParams().length() > 0 || getCurPageCount() == 0 ) break; } }
void CSyncSource::processServerResponse_ver3(CJSONArrayIterator& 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: " + convertToStringA(getProtocol().getVersion()) + "; Server response version: " + convertToStringA(nVersion) + ". Source name: " + getName(); getSync().stopSync(); m_nErrCode = RhoAppAdapter.ERR_SYNCVERSION; 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() ) { CJSONEntry oCmds = oJsonArr.getCurItem(); PROF_START("Data"); //TODO: use isUIWaitDB inside processSyncCommand // if ( getDB().isUIWaitDB() ) // { // LOG(INFO) + "Commit transaction because of UI request."; // getDB().endTransaction(); // getDB().startTransaction(); // } if ( oCmds.hasName("schema-changed") ) { getSync().setSchemaChanged(true); }else if ( oCmds.hasName("source-error") ) { CJSONEntry errSrc = oCmds.getEntry("source-error"); CJSONStructIterator errIter(errSrc); for( ; !errIter.isEnd(); errIter.next() ) { m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER; m_strError = errIter.getCurValue().getString("message"); m_strErrorType = errIter.getCurKey(); } }else if ( oCmds.hasName("search-error") ) { CJSONEntry errSrc = oCmds.getEntry("search-error"); CJSONStructIterator errIter(errSrc); for( ; !errIter.isEnd(); errIter.next() ) { m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER; m_strError = errIter.getCurValue().getString("message"); m_strErrorType = errIter.getCurKey(); } }else if ( oCmds.hasName("create-error") ) { m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER; m_strErrorType = "create-error"; }else if ( oCmds.hasName("update-error") ) { m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER; m_strErrorType = "update-error"; }else if ( oCmds.hasName("delete-error") ) { m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER; m_strErrorType = "delete-error"; }else { getDB().startTransaction(); if (getSync().getSourceOptions().getBoolProperty(getID(), "pass_through")) { if ( m_bSchemaSource ) getDB().executeSQL( (String("DELETE FROM ") + getName()).c_str() ); 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") ); if ( oCmds.hasName("delete") && getSync().isContinueSync() ) processSyncCommand("delete", oCmds.getEntry("delete") ); if ( oCmds.hasName("insert") && getSync().isContinueSync() ) processSyncCommand("insert", oCmds.getEntry("insert") ); 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"); }