Search in sources :

Example 1 with ConnectionResultType

use of com.ichi2.libanki.sync.Syncer.ConnectionResultType in project Anki-Android by ankidroid.

the class FullSyncer method download.

@NonNull
public ConnectionResultType download() throws UnknownHttpResponseException {
    InputStream cont;
    ResponseBody body = null;
    try {
        Response ret = super.req("download");
        if (ret == null || ret.body() == null) {
            return null;
        }
        body = ret.body();
        cont = body.byteStream();
    } catch (IllegalArgumentException e1) {
        if (body != null) {
            body.close();
        }
        throw new RuntimeException(e1);
    }
    String path;
    if (mCol != null) {
        Timber.i("Closing collection for full sync");
        // Usual case where collection is non-null
        path = mCol.getPath();
        mCol.close();
        mCol = null;
    } else {
        // Allow for case where collection is completely unreadable
        Timber.w("Collection was unexpectedly null when doing full sync download");
        path = CollectionHelper.getCollectionPath(AnkiDroidApp.getInstance());
    }
    String tpath = path + ".tmp";
    try {
        super.writeToFile(cont, tpath);
        Timber.d("Full Sync - Downloaded temp file");
        FileInputStream fis = new FileInputStream(tpath);
        if ("upgradeRequired".equals(super.stream2String(fis, 15))) {
            Timber.w("Full Sync - 'Upgrade Required' message received");
            return UPGRADE_REQUIRED;
        }
    } catch (FileNotFoundException e) {
        Timber.e(e, "Failed to create temp file when downloading collection.");
        throw new RuntimeException(e);
    } catch (IOException e) {
        Timber.e(e, "Full sync failed to download collection.");
        return SD_ACCESS_ERROR;
    } finally {
        body.close();
    }
    // check the received file is ok
    mCon.publishProgress(R.string.sync_check_download_file);
    DB tempDb = null;
    try {
        tempDb = new DB(tpath);
        if (!"ok".equalsIgnoreCase(tempDb.queryString("PRAGMA integrity_check"))) {
            Timber.e("Full sync - downloaded file corrupt");
            return REMOTE_DB_ERROR;
        }
    } catch (SQLiteDatabaseCorruptException e) {
        Timber.e("Full sync - downloaded file corrupt");
        return REMOTE_DB_ERROR;
    } finally {
        if (tempDb != null) {
            tempDb.close();
        }
    }
    Timber.d("Full Sync: Downloaded file was not corrupt");
    // overwrite existing collection
    File newFile = new File(tpath);
    if (newFile.renameTo(new File(path))) {
        Timber.i("Full Sync Success: Overwritten collection with downloaded file");
        return SUCCESS;
    } else {
        Timber.w("Full Sync: Error overwriting collection with downloaded file");
        return OVERWRITE_ERROR;
    }
}
Also used : Response(okhttp3.Response) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) SQLiteDatabaseCorruptException(android.database.sqlite.SQLiteDatabaseCorruptException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) DB(com.ichi2.libanki.DB) ResponseBody(okhttp3.ResponseBody) NonNull(androidx.annotation.NonNull)

Example 2 with ConnectionResultType

use of com.ichi2.libanki.sync.Syncer.ConnectionResultType in project Anki-Android by ankidroid.

the class Syncer method sync.

public Pair<ConnectionResultType, Object> sync(Connection con) throws UnknownHttpResponseException {
    mSyncMsg = "";
    // if the deck has any pending changes, flush them first and bump mod time
    mCol.getSched()._updateCutoff();
    mCol.save();
    // step 1: login & metadata
    Response ret = mRemoteServer.meta();
    if (ret == null) {
        return null;
    }
    int returntype = ret.code();
    if (returntype == 403) {
        return new Pair<>(BAD_AUTH, null);
    }
    try {
        mCol.getDb().getDatabase().beginTransaction();
        try {
            Timber.i("Sync: getting meta data from server");
            JSONObject rMeta = new JSONObject(ret.body().string());
            mCol.log("rmeta", rMeta);
            mSyncMsg = rMeta.getString("msg");
            if (!rMeta.getBoolean("cont")) {
                // Don't add syncMsg; it can be fetched by UI code using the accessor
                return new Pair<>(SERVER_ABORT, null);
            } else {
            // don't abort, but ui should show messages after sync finishes
            // and require confirmation if it's non-empty
            }
            throwExceptionIfCancelled(con);
            long rscm = rMeta.getLong("scm");
            int rts = rMeta.getInt("ts");
            long rMod = rMeta.getLong("mod");
            mMaxUsn = rMeta.getInt("usn");
            // skip uname, AnkiDroid already stores and shows it
            trySetHostNum(rMeta);
            Timber.i("Sync: building local meta data");
            JSONObject lMeta = meta();
            mCol.log("lmeta", lMeta);
            long lMod = lMeta.getLong("mod");
            mMinUsn = lMeta.getInt("usn");
            long lscm = lMeta.getLong("scm");
            int lts = lMeta.getInt("ts");
            long diff = Math.abs(rts - lts);
            if (diff > 300) {
                mCol.log("clock off");
                return new Pair<>(CLOCK_OFF, diff);
            }
            if (lMod == rMod) {
                Timber.i("Sync: no changes - returning");
                mCol.log("no changes");
                return new Pair<>(NO_CHANGES, null);
            } else if (lscm != rscm) {
                Timber.i("Sync: full sync necessary - returning");
                mCol.log("schema diff");
                return new Pair<>(FULL_SYNC, null);
            }
            mLNewer = lMod > rMod;
            // step 1.5: check collection is valid
            if (!mCol.basicCheck()) {
                mCol.log("basic check");
                return new Pair<>(BASIC_CHECK_FAILED, null);
            }
            throwExceptionIfCancelled(con);
            // step 2: deletions
            publishProgress(con, R.string.sync_deletions_message);
            Timber.i("Sync: collection removed data");
            JSONObject lrem = removed();
            JSONObject o = new JSONObject();
            o.put("minUsn", mMinUsn);
            o.put("lnewer", mLNewer);
            o.put("graves", lrem);
            Timber.i("Sync: sending and receiving removed data");
            JSONObject rrem = mRemoteServer.start(o);
            Timber.i("Sync: applying removed data");
            throwExceptionIfCancelled(con);
            remove(rrem);
            // ... and small objects
            publishProgress(con, R.string.sync_small_objects_message);
            Timber.i("Sync: collection small changes");
            JSONObject lchg = changes();
            JSONObject sch = new JSONObject();
            sch.put("changes", lchg);
            Timber.i("Sync: sending and receiving small changes");
            JSONObject rchg = mRemoteServer.applyChanges(sch);
            throwExceptionIfCancelled(con);
            Timber.i("Sync: merging small changes");
            try {
                mergeChanges(lchg, rchg);
            } catch (UnexpectedSchemaChange e) {
                Timber.w(e);
                mRemoteServer.abort();
                _forceFullSync();
            }
            // step 3: stream large tables from server
            publishProgress(con, R.string.sync_download_chunk);
            while (true) {
                throwExceptionIfCancelled(con);
                Timber.i("Sync: downloading chunked data");
                JSONObject chunk = mRemoteServer.chunk();
                mCol.log("server chunk", chunk);
                Timber.i("Sync: applying chunked data");
                applyChunk(chunk);
                if (chunk.getBoolean("done")) {
                    break;
                }
            }
            // step 4: stream to server
            publishProgress(con, R.string.sync_upload_chunk);
            while (true) {
                throwExceptionIfCancelled(con);
                Timber.i("Sync: collecting chunked data");
                JSONObject chunk = chunk();
                mCol.log("client chunk", chunk);
                JSONObject sech = new JSONObject();
                sech.put("chunk", chunk);
                Timber.i("Sync: sending chunked data");
                mRemoteServer.applyChunk(sech);
                if (chunk.getBoolean("done")) {
                    break;
                }
            }
            // step 5: sanity check
            JSONObject c = sanityCheck();
            JSONObject sanity = mRemoteServer.sanityCheck2(c);
            if (sanity == null || !"ok".equals(sanity.optString("status", "bad"))) {
                return sanityCheckError(c, sanity);
            }
            // finalize
            publishProgress(con, R.string.sync_finish_message);
            Timber.i("Sync: sending finish command");
            long mod = mRemoteServer.finish();
            if (mod == 0) {
                return new Pair<>(FINISH_ERROR, null);
            }
            Timber.i("Sync: finishing");
            finish(mod);
            publishProgress(con, R.string.sync_writing_db);
            mCol.getDb().getDatabase().setTransactionSuccessful();
        } finally {
            DB.safeEndInTransaction(mCol.getDb());
        }
    } catch (IllegalStateException e) {
        throw new RuntimeException(e);
    } catch (OutOfMemoryError e) {
        AnkiDroidApp.sendExceptionReport(e, "Syncer-sync");
        Timber.w(e);
        return new Pair<>(OUT_OF_MEMORY_ERROR, null);
    } catch (IOException e) {
        AnkiDroidApp.sendExceptionReport(e, "Syncer-sync");
        Timber.w(e);
        return new Pair<>(IO_EXCEPTION, null);
    }
    return new Pair<>(SUCCESS, null);
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException) Pair(android.util.Pair)

Example 3 with ConnectionResultType

use of com.ichi2.libanki.sync.Syncer.ConnectionResultType in project Anki-Android by ankidroid.

the class Connection method doInBackgroundSync.

/**
 * In the payload, success means that the sync did occur correctly and that a change did occur.
 * So success can be false without error, if no change occurred at all.
 */
private Payload doInBackgroundSync(Payload data) {
    sIsCancellable = true;
    Timber.d("doInBackgroundSync()");
    // Block execution until any previous background task finishes, or timeout after 5s
    boolean ok = TaskManager.waitToFinish(5);
    // Unique key allowing to identify the user to AnkiWeb without password
    String hkey = (String) data.data[0];
    // Whether media should be synced too
    boolean media = (Boolean) data.data[1];
    // If normal sync can't occur, what to do
    ConflictResolution conflictResolution = (ConflictResolution) data.data[2];
    // A number AnkiWeb told us to send back. Probably to choose the best server for the user
    HostNum hostNum = (HostNum) data.data[3];
    // Use safe version that catches exceptions so that full sync is still possible
    Collection col = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
    boolean colCorruptFullSync = false;
    if (!CollectionHelper.getInstance().colIsOpen() || !ok) {
        if (FULL_DOWNLOAD == conflictResolution) {
            colCorruptFullSync = true;
        } else {
            return returnGenericError(data);
        }
    }
    try {
        CollectionHelper.getInstance().lockCollection();
        RemoteServer remoteServer = new RemoteServer(this, hkey, hostNum);
        Syncer client = new Syncer(col, remoteServer, hostNum);
        // run sync and check state
        boolean noChanges = false;
        if (conflictResolution == null) {
            Timber.i("Sync - starting sync");
            publishProgress(R.string.sync_prepare_syncing);
            Pair<ConnectionResultType, Object> ret = client.sync(this);
            data.message = client.getSyncMsg();
            if (ret == null) {
                return returnGenericError(data);
            }
            if (NO_CHANGES != ret.first && SUCCESS != ret.first) {
                data.success = false;
                data.resultType = ret.first;
                data.result = new Object[] { ret.second };
                // Check if there was a sanity check error
                if (SANITY_CHECK_ERROR == ret.first) {
                    // Force full sync next time
                    col.modSchemaNoCheck();
                    col.save();
                }
                return data;
            }
            // save and note success state
            if (NO_CHANGES == ret.first) {
                // publishProgress(R.string.sync_no_changes_message);
                noChanges = true;
            }
        } else {
            try {
                // Disable sync cancellation for full-sync
                sIsCancellable = false;
                FullSyncer fullSyncServer = new FullSyncer(col, hkey, this, hostNum);
                switch(conflictResolution) {
                    case FULL_UPLOAD:
                        {
                            Timber.i("Sync - fullsync - upload collection");
                            publishProgress(R.string.sync_preparing_full_sync_message);
                            Pair<ConnectionResultType, Object[]> ret = fullSyncServer.upload();
                            col.reopen();
                            if (ret == null) {
                                return returnGenericError(data);
                            }
                            if (ret.first == ARBITRARY_STRING && !ret.second[0].equals(HttpSyncer.ANKIWEB_STATUS_OK)) {
                                data.success = false;
                                data.resultType = ret.first;
                                data.result = ret.second;
                                return data;
                            }
                            break;
                        }
                    case FULL_DOWNLOAD:
                        {
                            Timber.i("Sync - fullsync - download collection");
                            publishProgress(R.string.sync_downloading_message);
                            ConnectionResultType ret = fullSyncServer.download();
                            if (ret == null) {
                                Timber.w("Sync - fullsync - unknown error");
                                return returnGenericError(data);
                            }
                            if (SUCCESS == ret) {
                                data.success = true;
                                col.reopen();
                            }
                            if (SUCCESS != ret) {
                                Timber.w("Sync - fullsync - download failed");
                                data.success = false;
                                data.resultType = ret;
                                if (!colCorruptFullSync) {
                                    col.reopen();
                                }
                                return data;
                            }
                            break;
                        }
                    default:
                }
            } catch (OutOfMemoryError e) {
                Timber.w(e);
                AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync-fullSync");
                data.success = false;
                data.resultType = OUT_OF_MEMORY_ERROR;
                data.result = new Object[0];
                return data;
            } catch (RuntimeException e) {
                Timber.w(e);
                if (timeoutOccurred(e)) {
                    data.resultType = CONNECTION_ERROR;
                } else if (USER_ABORTED_SYNC.toString().equals(e.getMessage())) {
                    data.resultType = USER_ABORTED_SYNC;
                } else {
                    AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync-fullSync");
                    data.resultType = IO_EXCEPTION;
                }
                data.result = new Object[] { e };
                data.success = false;
                return data;
            }
        }
        // clear undo to avoid non syncing orphans (because undo resets usn too
        if (!noChanges) {
            col.clearUndo();
        }
        // then move on to media sync
        sIsCancellable = true;
        boolean noMediaChanges = false;
        String mediaError = null;
        if (media) {
            RemoteMediaServer mediaServer = new RemoteMediaServer(col, hkey, this, hostNum);
            MediaSyncer mediaClient = new MediaSyncer(col, mediaServer, this);
            Pair<ConnectionResultType, String> ret;
            try {
                Timber.i("Sync - Performing media sync");
                ret = mediaClient.sync();
                if (ret == null || ret.first == null) {
                    mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error);
                } else {
                    if (CORRUPT == ret.first) {
                        mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_db_error);
                        noMediaChanges = true;
                    }
                    if (NO_CHANGES == ret.first) {
                        publishProgress(R.string.sync_media_no_changes);
                        noMediaChanges = true;
                    }
                    if (MEDIA_SANITY_FAILED == ret.first) {
                        mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_sanity_failed);
                    } else {
                        publishProgress(R.string.sync_media_success);
                    }
                }
            } catch (RuntimeException e) {
                Timber.w(e);
                if (timeoutOccurred(e)) {
                    data.resultType = CONNECTION_ERROR;
                    data.result = new Object[] { e };
                } else if (USER_ABORTED_SYNC.toString().equals(e.getMessage())) {
                    data.resultType = USER_ABORTED_SYNC;
                    data.result = new Object[] { e };
                }
                int downloadedCount = mediaClient.getDownloadCount();
                int uploadedCount = mediaClient.getUploadCount();
                if (downloadedCount == 0 && uploadedCount == 0) {
                    mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error) + "\n\n" + e.getLocalizedMessage();
                } else {
                    mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_partial_updated, downloadedCount, uploadedCount) + "\n\n" + e.getLocalizedMessage();
                }
            }
        }
        if (noChanges && (!media || noMediaChanges)) {
            // This means that there is no change at all, neither media nor collection. Not that there was an error.
            data.success = false;
            data.resultType = NO_CHANGES;
            data.result = new Object[0];
        } else {
            data.success = true;
            data.data = new Object[] { conflictResolution, col, mediaError };
        }
        return data;
    } catch (MediaSyncException e) {
        Timber.e("Media sync rejected by server");
        data.success = false;
        data.resultType = MEDIA_SYNC_SERVER_ERROR;
        data.result = new Object[] { e };
        AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync");
        return data;
    } catch (UnknownHttpResponseException e) {
        Timber.e(e, "doInBackgroundSync -- unknown response code error");
        data.success = false;
        int code = e.getResponseCode();
        String msg = e.getLocalizedMessage();
        data.resultType = ERROR;
        data.result = new Object[] { code, msg };
        return data;
    } catch (Exception e) {
        // Global error catcher.
        // Try to give a human readable error, otherwise print the raw error message
        Timber.e(e, "doInBackgroundSync error");
        data.success = false;
        if (timeoutOccurred(e)) {
            data.resultType = CONNECTION_ERROR;
            data.result = new Object[] { e };
        } else if (USER_ABORTED_SYNC.toString().equals(e.getMessage())) {
            data.resultType = USER_ABORTED_SYNC;
            data.result = new Object[] { e };
        } else {
            AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync");
            data.resultType = ARBITRARY_STRING;
            data.result = new Object[] { e.getLocalizedMessage(), e };
        }
        return data;
    } finally {
        Timber.i("Sync Finished - Closing Collection");
        // don't bump mod time unless we explicitly save
        if (col != null) {
            col.close(false);
        }
        CollectionHelper.getInstance().unlockCollection();
    }
}
Also used : ConnectionResultType(com.ichi2.libanki.sync.Syncer.ConnectionResultType) HostNum(com.ichi2.libanki.sync.HostNum) FullSyncer(com.ichi2.libanki.sync.FullSyncer) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) CustomSyncServerUrlException(com.ichi2.libanki.sync.CustomSyncServerUrlException) JSONException(com.ichi2.utils.JSONException) MediaSyncException(com.ichi2.anki.exception.MediaSyncException) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) HttpSyncer(com.ichi2.libanki.sync.HttpSyncer) FullSyncer(com.ichi2.libanki.sync.FullSyncer) Syncer(com.ichi2.libanki.sync.Syncer) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) MediaSyncException(com.ichi2.anki.exception.MediaSyncException) Collection(com.ichi2.libanki.Collection) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) JSONObject(com.ichi2.utils.JSONObject) ConflictResolution(com.ichi2.async.Connection.ConflictResolution) RemoteServer(com.ichi2.libanki.sync.RemoteServer) RemoteMediaServer(com.ichi2.libanki.sync.RemoteMediaServer) Pair(android.util.Pair)

Aggregations

IOException (java.io.IOException)3 Pair (android.util.Pair)2 JSONObject (com.ichi2.utils.JSONObject)2 Response (okhttp3.Response)2 SQLiteDatabaseCorruptException (android.database.sqlite.SQLiteDatabaseCorruptException)1 NonNull (androidx.annotation.NonNull)1 MediaSyncException (com.ichi2.anki.exception.MediaSyncException)1 UnknownHttpResponseException (com.ichi2.anki.exception.UnknownHttpResponseException)1 ConflictResolution (com.ichi2.async.Connection.ConflictResolution)1 Collection (com.ichi2.libanki.Collection)1 DB (com.ichi2.libanki.DB)1 CustomSyncServerUrlException (com.ichi2.libanki.sync.CustomSyncServerUrlException)1 FullSyncer (com.ichi2.libanki.sync.FullSyncer)1 HostNum (com.ichi2.libanki.sync.HostNum)1 HttpSyncer (com.ichi2.libanki.sync.HttpSyncer)1 MediaSyncer (com.ichi2.libanki.sync.MediaSyncer)1 RemoteMediaServer (com.ichi2.libanki.sync.RemoteMediaServer)1 RemoteServer (com.ichi2.libanki.sync.RemoteServer)1 Syncer (com.ichi2.libanki.sync.Syncer)1 ConnectionResultType (com.ichi2.libanki.sync.Syncer.ConnectionResultType)1