Esempio n. 1
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, "", "");
            }
        }
Esempio n. 2
0
	    void doFireSyncNotification( SyncSource src, boolean bFinish, int nErrCode, String strError, String strParams, String strServerError )
	    {
		    if ( getSync().isStoppedByUser() )
			    return;
		
		    try{
			    SyncNotification pSN = null;		
			
		        String strBody = "";
		        boolean bRemoveAfterFire = bFinish;
		        {
		    	    lock(m_mxSyncNotifications){
		    		    pSN = getSyncNotifyBySrc(src);
			            if ( pSN == null )
			                return;
			
				        strBody = "";

		                if ( src != null )
		                {
				            strBody += "total_count=" + src.getTotalCount();
				            strBody += "&processed_count=" + src.getCurPageCount();
				            strBody += "&processed_objects_count=" + getLastSyncObjectCount(src.getID());
				            strBody += "&cumulative_count=" + src.getServerObjectsCount();			        
				            strBody += "&source_id=" + src.getID();
				            strBody += "&source_name=" + src.getName();
		                }
		            
		                if ( strParams.length() > 0 )
		            	    strBody += (strBody.length() > 0 ? "&" : "") + strParams;
		                else
		            	    strBody += (strBody.length() > 0 ? "&" : "") + "sync_type=incremental";
			        
			            strBody += "&status=";
			            if ( bFinish )
			            {
				            if ( nErrCode == RhoAppAdapter.ERR_NONE )
				            {
				        	    if ( getSync().isSchemaChanged() )
				        		    strBody += "schema_changed";
				        	    else				        	
				        		    strBody += (src == null && strParams.length() == 0) ? "complete" : "ok";
				            }
				            else
				            {
				        	    if ( getSync().isStoppedByUser() )
		                            nErrCode = RhoAppAdapter.ERR_CANCELBYUSER;
				        	
				        	    strBody += "error";				        	
						        strBody += "&error_code=" + nErrCode;

                                if (strError != null && strError.length() > 0)
                                {
                                    if (strError.length() > MAX_SERVER_ERROR_LEN)
                                        strError = strError.substring(0, MAX_SERVER_ERROR_LEN);
                                    strBody += "&error_message=" + URI.urlEncode(strError);
                                }
                                else if (src != null && src.m_strError != null)
                                {
                                    if (src.m_strError.length() > MAX_SERVER_ERROR_LEN)
                                        src.m_strError = src.m_strError.substring(0, MAX_SERVER_ERROR_LEN);
                                    strBody += "&error_message=" + URI.urlEncode(src.m_strError);
                                }

                                if (strServerError != null && strServerError.length() > 0)
                                {
                                    if (strServerError.length() > MAX_SERVER_ERROR_LEN)
                                        strServerError = strServerError.substring(0, MAX_SERVER_ERROR_LEN);
                                    strBody += "&" + strServerError;
                                }
                                else if (src != null && src.m_strServerError != null && src.m_strServerError.length() > 0)                              
                                {
                                    if ( src.m_strServerError.length() > MAX_SERVER_ERROR_LEN )
                                        src.m_strServerError = src.m_strServerError.substring(0, MAX_SERVER_ERROR_LEN);	
                                    strBody += "&" + src.m_strServerError;	                }						    
				                }
				        
		                    if ( src != null )
		                        strBody += makeCreateObjectErrorBody( src.getID());
			            }
			            else
			        	    strBody += "in_progress";
			        
			            strBody += "&rho_callback=1";
			            if ( pSN.m_strParams != null && pSN.m_strParams.length() > 0 )
			            {
			        	    if ( !pSN.m_strParams.startsWith("&") )
			        		    strBody += "&";
			        	
			        	    strBody += pSN.m_strParams;
			            }			        	
			        
			            bRemoveAfterFire = bRemoveAfterFire && pSN.m_bRemoveAfterFire;
		            }
		        }
		        if ( bRemoveAfterFire )
		    	    clearNotification(src);
		    
		        LOG.INFO("Fire notification. Source : " + (src != null ? (src).getName():"") + "; " + pSN.ToString());
		    
                if ( callNotify(pSN, strBody) )
                    clearNotification(src);
		    }catch(Exception exc)
		    {
			    LOG.ERROR("Fire notification failed.", exc);
		    }
	    }
Esempio n. 3
0
        void doFireSyncNotification(SyncSource src, boolean bFinish, int nErrCode, String strError, String strParams, String strServerError)
        {
            if (getSync().isStoppedByUser())
            {
                return;
            }

            try{
                SyncNotification pSN = null;

                String  strBody          = "";
                boolean bRemoveAfterFire = bFinish;
                {
                    lock (m_mxSyncNotifications){
                        pSN = getSyncNotifyBySrc(src);
                        if (pSN == null)
                        {
                            return;
                        }

                        strBody = "";

                        if (src != null)
                        {
                            strBody += "total_count=" + src.getTotalCount();
                            strBody += "&processed_count=" + src.getCurPageCount();
                            strBody += "&processed_objects_count=" + getLastSyncObjectCount(src.getID());
                            strBody += "&cumulative_count=" + src.getServerObjectsCount();
                            strBody += "&source_id=" + src.getID();
                            strBody += "&source_name=" + src.getName();
                        }

                        if (strParams.length() > 0)
                        {
                            strBody += (strBody.length() > 0 ? "&" : "") + strParams;
                        }
                        else
                        {
                            strBody += (strBody.length() > 0 ? "&" : "") + "sync_type=incremental";
                        }

                        strBody += "&status=";
                        if (bFinish)
                        {
                            if (nErrCode == RhoAppAdapter.ERR_NONE)
                            {
                                if (getSync().isSchemaChanged())
                                {
                                    strBody += "schema_changed";
                                }
                                else
                                {
                                    strBody += (src == null && strParams.length() == 0) ? "complete" : "ok";
                                }
                            }
                            else
                            {
                                if (getSync().isStoppedByUser())
                                {
                                    nErrCode = RhoAppAdapter.ERR_CANCELBYUSER;
                                }

                                strBody += "error";
                                strBody += "&error_code=" + nErrCode;

                                if (strError != null && strError.length() > 0)
                                {
                                    strBody += "&error_message=" + URI.urlEncode(strError);
                                }
                                else if (src != null)
                                {
                                    strBody += "&error_message=" + URI.urlEncode(src.m_strError);
                                }

                                if (strServerError != null && strServerError.length() > 0)
                                {
                                    strBody += "&" + strServerError;
                                }
                                else if (src != null && src.m_strServerError != null && src.m_strServerError.length() > 0)
                                {
                                    strBody += "&" + src.m_strServerError;
                                }
                            }

                            if (src != null)
                            {
                                strBody += makeCreateObjectErrorBody(src.getID());
                            }
                        }
                        else
                        {
                            strBody += "in_progress";
                        }

                        strBody += "&rho_callback=1";
                        if (pSN.m_strParams != null && pSN.m_strParams.length() > 0)
                        {
                            if (!pSN.m_strParams.startsWith("&"))
                            {
                                strBody += "&";
                            }

                            strBody += pSN.m_strParams;
                        }

                        bRemoveAfterFire = bRemoveAfterFire && pSN.m_bRemoveAfterFire;
                    }
                }
                if (bRemoveAfterFire)
                {
                    clearNotification(src);
                }

                LOG.INFO("Fire notification. Source : " + (src != null ? (src).getName():"") + "; " + pSN.ToString());

                if (callNotify(pSN, strBody))
                {
                    clearNotification(src);
                }
            }catch (Exception exc)
            {
                LOG.ERROR("Fire notification failed.", exc);
            }
        }