2012-03-13 02:17:56 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-24 20:17:46 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-21 16:44:08 +00:00
|
|
|
|
|
|
|
package org.mozilla.gecko.sync.synchronizer;
|
|
|
|
|
|
|
|
|
2012-01-14 17:20:31 +00:00
|
|
|
import java.util.concurrent.ExecutorService;
|
2012-07-11 21:34:22 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2012-01-14 17:20:31 +00:00
|
|
|
|
2013-02-27 23:44:21 +00:00
|
|
|
import org.mozilla.gecko.background.common.log.Logger;
|
2012-03-06 04:53:14 +00:00
|
|
|
import org.mozilla.gecko.sync.repositories.InactiveSessionException;
|
|
|
|
import org.mozilla.gecko.sync.repositories.InvalidSessionTransitionException;
|
2011-12-21 16:44:08 +00:00
|
|
|
import org.mozilla.gecko.sync.repositories.RepositorySession;
|
|
|
|
import org.mozilla.gecko.sync.repositories.RepositorySessionBundle;
|
|
|
|
import org.mozilla.gecko.sync.repositories.delegates.DeferrableRepositorySessionCreationDelegate;
|
2012-01-14 17:20:31 +00:00
|
|
|
import org.mozilla.gecko.sync.repositories.delegates.DeferredRepositorySessionFinishDelegate;
|
2011-12-21 16:44:08 +00:00
|
|
|
import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFinishDelegate;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
2012-03-13 02:17:56 +00:00
|
|
|
/**
|
|
|
|
* I coordinate the moving parts of a sync started by
|
|
|
|
* {@link Synchronizer#synchronize}.
|
|
|
|
*
|
|
|
|
* I flow records twice: first from A to B, and then from B to A. I provide
|
|
|
|
* fine-grained feedback by calling my delegate's callback methods.
|
|
|
|
*
|
|
|
|
* Initialize me by creating me with a Synchronizer and a
|
|
|
|
* SynchronizerSessionDelegate. Kick things off by calling `init` with two
|
|
|
|
* RepositorySessionBundles, and then call `synchronize` in your `onInitialized`
|
|
|
|
* callback.
|
|
|
|
*
|
|
|
|
* I always call exactly one of my delegate's `onInitialized` or
|
|
|
|
* `onSessionError` callback methods from `init`.
|
|
|
|
*
|
|
|
|
* I call my delegate's `onSynchronizeSkipped` callback method if there is no
|
|
|
|
* data to be synchronized in `synchronize`.
|
|
|
|
*
|
|
|
|
* In addition, I call `onFetchError`, `onStoreError`, and `onSessionError` when
|
|
|
|
* I encounter a fetch, store, or session error while synchronizing.
|
|
|
|
*
|
|
|
|
* Typically my delegate will call `abort` in its error callbacks, which will
|
|
|
|
* call my delegate's `onSynchronizeAborted` method and halt the sync.
|
|
|
|
*
|
|
|
|
* I always call exactly one of my delegate's `onSynchronized` or
|
|
|
|
* `onSynchronizeFailed` callback methods if I have not seen an error.
|
|
|
|
*/
|
2011-12-21 16:44:08 +00:00
|
|
|
public class SynchronizerSession
|
|
|
|
extends DeferrableRepositorySessionCreationDelegate
|
|
|
|
implements RecordsChannelDelegate,
|
|
|
|
RepositorySessionFinishDelegate {
|
|
|
|
|
|
|
|
protected static final String LOG_TAG = "SynchronizerSession";
|
2012-05-31 19:21:08 +00:00
|
|
|
protected Synchronizer synchronizer;
|
|
|
|
protected SynchronizerSessionDelegate delegate;
|
|
|
|
protected Context context;
|
2011-12-21 16:44:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Computed during init.
|
|
|
|
*/
|
|
|
|
private RepositorySession sessionA;
|
|
|
|
private RepositorySession sessionB;
|
|
|
|
private RepositorySessionBundle bundleA;
|
|
|
|
private RepositorySessionBundle bundleB;
|
2012-02-16 06:05:53 +00:00
|
|
|
|
|
|
|
// Bug 726054: just like desktop, we track our last interaction with the server,
|
|
|
|
// not the last record timestamp that we fetched. This ensures that we don't re-
|
|
|
|
// download the records we just uploaded, at the cost of skipping any records
|
|
|
|
// that a concurrently syncing client has uploaded.
|
2011-12-21 16:44:08 +00:00
|
|
|
private long pendingATimestamp = -1;
|
|
|
|
private long pendingBTimestamp = -1;
|
2012-02-16 06:05:53 +00:00
|
|
|
private long storeEndATimestamp = -1;
|
|
|
|
private long storeEndBTimestamp = -1;
|
2011-12-21 16:44:08 +00:00
|
|
|
private boolean flowAToBCompleted = false;
|
|
|
|
private boolean flowBToACompleted = false;
|
|
|
|
|
2012-07-11 21:34:22 +00:00
|
|
|
protected final AtomicInteger numInboundRecords = new AtomicInteger(-1);
|
|
|
|
protected final AtomicInteger numOutboundRecords = new AtomicInteger(-1);
|
|
|
|
|
2011-12-21 16:44:08 +00:00
|
|
|
/*
|
|
|
|
* Public API: constructor, init, synchronize.
|
|
|
|
*/
|
|
|
|
public SynchronizerSession(Synchronizer synchronizer, SynchronizerSessionDelegate delegate) {
|
|
|
|
this.setSynchronizer(synchronizer);
|
|
|
|
this.delegate = delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Synchronizer getSynchronizer() {
|
|
|
|
return synchronizer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSynchronizer(Synchronizer synchronizer) {
|
|
|
|
this.synchronizer = synchronizer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void init(Context context, RepositorySessionBundle bundleA, RepositorySessionBundle bundleB) {
|
|
|
|
this.context = context;
|
|
|
|
this.bundleA = bundleA;
|
|
|
|
this.bundleB = bundleB;
|
|
|
|
// Begin sessionA and sessionB, call onInitialized in callbacks.
|
|
|
|
this.getSynchronizer().repositoryA.createSession(this, context);
|
|
|
|
}
|
|
|
|
|
2012-07-11 21:34:22 +00:00
|
|
|
/**
|
|
|
|
* Get the number of records fetched from the first repository (usually the
|
|
|
|
* server, hence inbound).
|
|
|
|
* <p>
|
|
|
|
* Valid only after first flow has completed.
|
|
|
|
*
|
|
|
|
* @return number of records, or -1 if not valid.
|
|
|
|
*/
|
|
|
|
public int getInboundCount() {
|
|
|
|
return numInboundRecords.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of records fetched from the second repository (usually the
|
|
|
|
* local store, hence outbound).
|
|
|
|
* <p>
|
|
|
|
* Valid only after second flow has completed.
|
|
|
|
*
|
|
|
|
* @return number of records, or -1 if not valid.
|
|
|
|
*/
|
|
|
|
public int getOutboundCount() {
|
|
|
|
return numOutboundRecords.get();
|
|
|
|
}
|
|
|
|
|
2012-03-20 20:35:09 +00:00
|
|
|
// These are accessed by `abort` and `synchronize`, both of which are synchronized.
|
|
|
|
// Guarded by `this`.
|
|
|
|
protected RecordsChannel channelAToB;
|
|
|
|
protected RecordsChannel channelBToA;
|
|
|
|
|
2011-12-21 16:44:08 +00:00
|
|
|
/**
|
|
|
|
* Please don't call this until you've been notified with onInitialized.
|
|
|
|
*/
|
2012-03-20 20:35:09 +00:00
|
|
|
public synchronized void synchronize() {
|
2012-07-11 21:34:22 +00:00
|
|
|
numInboundRecords.set(-1);
|
|
|
|
numOutboundRecords.set(-1);
|
|
|
|
|
2012-01-14 17:20:31 +00:00
|
|
|
// First thing: decide whether we should.
|
2012-12-11 07:03:14 +00:00
|
|
|
if (sessionA.shouldSkip() ||
|
|
|
|
sessionB.shouldSkip()) {
|
|
|
|
Logger.info(LOG_TAG, "Session requested skip. Short-circuiting sync.");
|
|
|
|
sessionA.abort();
|
|
|
|
sessionB.abort();
|
|
|
|
this.delegate.onSynchronizeSkipped(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-14 17:20:31 +00:00
|
|
|
if (!sessionA.dataAvailable() &&
|
|
|
|
!sessionB.dataAvailable()) {
|
2012-03-13 02:17:56 +00:00
|
|
|
Logger.info(LOG_TAG, "Neither session reports data available. Short-circuiting sync.");
|
2012-01-14 17:20:31 +00:00
|
|
|
sessionA.abort();
|
|
|
|
sessionB.abort();
|
|
|
|
this.delegate.onSynchronizeSkipped(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-21 16:44:08 +00:00
|
|
|
final SynchronizerSession session = this;
|
|
|
|
|
|
|
|
// TODO: failed record handling.
|
2012-03-13 02:17:56 +00:00
|
|
|
|
|
|
|
// This is the *second* record channel to flow.
|
|
|
|
// I, SynchronizerSession, am the delegate for the *second* flow.
|
2012-03-20 20:35:09 +00:00
|
|
|
channelBToA = new RecordsChannel(this.sessionB, this.sessionA, this);
|
2012-03-13 02:17:56 +00:00
|
|
|
|
|
|
|
// This is the delegate for the *first* flow.
|
|
|
|
RecordsChannelDelegate channelAToBDelegate = new RecordsChannelDelegate() {
|
2012-02-16 06:05:53 +00:00
|
|
|
public void onFlowCompleted(RecordsChannel recordsChannel, long fetchEnd, long storeEnd) {
|
2012-05-31 19:21:08 +00:00
|
|
|
session.onFirstFlowCompleted(recordsChannel, fetchEnd, storeEnd);
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFlowBeginFailed(RecordsChannel recordsChannel, Exception ex) {
|
2012-05-31 19:21:08 +00:00
|
|
|
Logger.warn(LOG_TAG, "First RecordsChannel onFlowBeginFailed. Logging session error.", ex);
|
|
|
|
session.delegate.onSynchronizeFailed(session, ex, "Failed to begin first flow.");
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 02:17:56 +00:00
|
|
|
@Override
|
|
|
|
public void onFlowFetchFailed(RecordsChannel recordsChannel, Exception ex) {
|
2012-05-31 19:21:08 +00:00
|
|
|
Logger.warn(LOG_TAG, "First RecordsChannel onFlowFetchFailed. Logging remote fetch error.", ex);
|
2012-03-13 02:17:56 +00:00
|
|
|
}
|
|
|
|
|
2011-12-21 16:44:08 +00:00
|
|
|
@Override
|
2012-05-31 19:21:08 +00:00
|
|
|
public void onFlowStoreFailed(RecordsChannel recordsChannel, Exception ex, String recordGuid) {
|
|
|
|
Logger.warn(LOG_TAG, "First RecordsChannel onFlowStoreFailed. Logging local store error.", ex);
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFlowFinishFailed(RecordsChannel recordsChannel, Exception ex) {
|
2012-05-31 19:21:08 +00:00
|
|
|
Logger.warn(LOG_TAG, "First RecordsChannel onFlowFinishedFailed. Logging session error.", ex);
|
|
|
|
session.delegate.onSynchronizeFailed(session, ex, "Failed to finish first flow.");
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
};
|
2012-03-13 02:17:56 +00:00
|
|
|
|
|
|
|
// This is the *first* channel to flow.
|
2012-03-20 20:35:09 +00:00
|
|
|
channelAToB = new RecordsChannel(this.sessionA, this.sessionB, channelAToBDelegate);
|
2012-03-13 02:17:56 +00:00
|
|
|
|
2012-07-06 20:01:10 +00:00
|
|
|
Logger.trace(LOG_TAG, "Starting A to B flow. Channel is " + channelAToB);
|
2012-03-06 04:53:14 +00:00
|
|
|
try {
|
|
|
|
channelAToB.beginAndFlow();
|
|
|
|
} catch (InvalidSessionTransitionException e) {
|
|
|
|
onFlowBeginFailed(channelAToB, e);
|
|
|
|
}
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
2012-05-31 19:21:08 +00:00
|
|
|
/**
|
|
|
|
* Called after the first flow completes.
|
|
|
|
* <p>
|
|
|
|
* By default, any fetch and store failures are ignored.
|
|
|
|
* @param recordsChannel the <code>RecordsChannel</code> (for error testing).
|
|
|
|
* @param fetchEnd timestamp when fetches completed.
|
|
|
|
* @param storeEnd timestamp when stores completed.
|
|
|
|
*/
|
|
|
|
public void onFirstFlowCompleted(RecordsChannel recordsChannel, long fetchEnd, long storeEnd) {
|
2012-07-06 20:01:10 +00:00
|
|
|
Logger.trace(LOG_TAG, "First RecordsChannel onFlowCompleted.");
|
|
|
|
Logger.debug(LOG_TAG, "Fetch end is " + fetchEnd + ". Store end is " + storeEnd + ". Starting next.");
|
2012-05-31 19:21:08 +00:00
|
|
|
pendingATimestamp = fetchEnd;
|
|
|
|
storeEndBTimestamp = storeEnd;
|
2012-07-11 21:34:22 +00:00
|
|
|
numInboundRecords.set(recordsChannel.getFetchCount());
|
2012-05-31 19:21:08 +00:00
|
|
|
flowAToBCompleted = true;
|
|
|
|
channelBToA.flow();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called after the second flow completes.
|
|
|
|
* <p>
|
|
|
|
* By default, any fetch and store failures are ignored.
|
|
|
|
* @param recordsChannel the <code>RecordsChannel</code> (for error testing).
|
|
|
|
* @param fetchEnd timestamp when fetches completed.
|
|
|
|
* @param storeEnd timestamp when stores completed.
|
|
|
|
*/
|
|
|
|
public void onSecondFlowCompleted(RecordsChannel recordsChannel, long fetchEnd, long storeEnd) {
|
2012-07-06 20:01:10 +00:00
|
|
|
Logger.trace(LOG_TAG, "Second RecordsChannel onFlowCompleted.");
|
|
|
|
Logger.debug(LOG_TAG, "Fetch end is " + fetchEnd + ". Store end is " + storeEnd + ". Finishing.");
|
2012-02-16 06:05:53 +00:00
|
|
|
|
|
|
|
pendingBTimestamp = fetchEnd;
|
|
|
|
storeEndATimestamp = storeEnd;
|
2012-07-11 21:34:22 +00:00
|
|
|
numOutboundRecords.set(recordsChannel.getFetchCount());
|
2011-12-21 16:44:08 +00:00
|
|
|
flowBToACompleted = true;
|
|
|
|
|
|
|
|
// Finish the two sessions.
|
2012-03-06 04:53:14 +00:00
|
|
|
try {
|
|
|
|
this.sessionA.finish(this);
|
|
|
|
} catch (InactiveSessionException e) {
|
|
|
|
this.onFinishFailed(e);
|
2012-05-31 19:21:08 +00:00
|
|
|
return;
|
2012-03-06 04:53:14 +00:00
|
|
|
}
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
2012-05-31 19:21:08 +00:00
|
|
|
@Override
|
|
|
|
public void onFlowCompleted(RecordsChannel recordsChannel, long fetchEnd, long storeEnd) {
|
|
|
|
onSecondFlowCompleted(recordsChannel, fetchEnd, storeEnd);
|
|
|
|
}
|
|
|
|
|
2011-12-21 16:44:08 +00:00
|
|
|
@Override
|
|
|
|
public void onFlowBeginFailed(RecordsChannel recordsChannel, Exception ex) {
|
2012-05-31 19:21:08 +00:00
|
|
|
Logger.warn(LOG_TAG, "Second RecordsChannel onFlowBeginFailed. Logging session error.", ex);
|
|
|
|
this.delegate.onSynchronizeFailed(this, ex, "Failed to begin second flow.");
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 02:17:56 +00:00
|
|
|
@Override
|
|
|
|
public void onFlowFetchFailed(RecordsChannel recordsChannel, Exception ex) {
|
2012-05-31 19:21:08 +00:00
|
|
|
Logger.warn(LOG_TAG, "Second RecordsChannel onFlowFetchFailed. Logging local fetch error.", ex);
|
2012-03-13 02:17:56 +00:00
|
|
|
}
|
|
|
|
|
2011-12-21 16:44:08 +00:00
|
|
|
@Override
|
2012-05-31 19:21:08 +00:00
|
|
|
public void onFlowStoreFailed(RecordsChannel recordsChannel, Exception ex, String recordGuid) {
|
|
|
|
Logger.warn(LOG_TAG, "Second RecordsChannel onFlowStoreFailed. Logging remote store error.", ex);
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFlowFinishFailed(RecordsChannel recordsChannel, Exception ex) {
|
2012-05-31 19:21:08 +00:00
|
|
|
Logger.warn(LOG_TAG, "Second RecordsChannel onFlowFinishedFailed. Logging session error.", ex);
|
|
|
|
this.delegate.onSynchronizeFailed(this, ex, "Failed to finish second flow.");
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RepositorySessionCreationDelegate methods.
|
|
|
|
*/
|
2012-03-13 02:17:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* I could be called twice: once for sessionA and once for sessionB.
|
|
|
|
*
|
|
|
|
* I try to clean up sessionA if it is not null, since the creation of
|
|
|
|
* sessionB must have failed.
|
|
|
|
*/
|
2011-12-21 16:44:08 +00:00
|
|
|
@Override
|
|
|
|
public void onSessionCreateFailed(Exception ex) {
|
|
|
|
// Attempt to finish the first session, if the second is the one that failed.
|
|
|
|
if (this.sessionA != null) {
|
|
|
|
try {
|
|
|
|
// We no longer need a reference to our context.
|
|
|
|
this.context = null;
|
|
|
|
this.sessionA.finish(this);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Never mind; best-effort finish.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We no longer need a reference to our context.
|
|
|
|
this.context = null;
|
2012-05-31 19:21:08 +00:00
|
|
|
this.delegate.onSynchronizeFailed(this, ex, "Failed to create session");
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 02:17:56 +00:00
|
|
|
/**
|
|
|
|
* I should be called twice: first for sessionA and second for sessionB.
|
|
|
|
*
|
|
|
|
* If I am called for sessionB, I call my delegate's `onInitialized` callback
|
|
|
|
* method because my repository sessions are correctly initialized.
|
|
|
|
*/
|
2011-12-21 16:44:08 +00:00
|
|
|
// TODO: some of this "finish and clean up" code can be refactored out.
|
|
|
|
@Override
|
|
|
|
public void onSessionCreated(RepositorySession session) {
|
|
|
|
if (session == null ||
|
|
|
|
this.sessionA == session) {
|
|
|
|
// TODO: clean up sessionA.
|
2012-05-31 19:21:08 +00:00
|
|
|
this.delegate.onSynchronizeFailed(this, new UnexpectedSessionException(session), "Failed to create session.");
|
2011-12-21 16:44:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.sessionA == null) {
|
|
|
|
this.sessionA = session;
|
|
|
|
|
|
|
|
// Unbundle.
|
|
|
|
try {
|
|
|
|
this.sessionA.unbundle(this.bundleA);
|
|
|
|
} catch (Exception e) {
|
2012-05-31 19:21:08 +00:00
|
|
|
this.delegate.onSynchronizeFailed(this, new UnbundleError(e, sessionA), "Failed to unbundle first session.");
|
2011-12-21 16:44:08 +00:00
|
|
|
// TODO: abort
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.getSynchronizer().repositoryB.createSession(this, this.context);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.sessionB == null) {
|
|
|
|
this.sessionB = session;
|
|
|
|
// We no longer need a reference to our context.
|
|
|
|
this.context = null;
|
|
|
|
|
|
|
|
// Unbundle. We unbundled sessionA when that session was created.
|
|
|
|
try {
|
|
|
|
this.sessionB.unbundle(this.bundleB);
|
|
|
|
} catch (Exception e) {
|
2012-05-31 19:21:08 +00:00
|
|
|
this.delegate.onSynchronizeFailed(this, new UnbundleError(e, sessionA), "Failed to unbundle second session.");
|
2011-12-21 16:44:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.delegate.onInitialized(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO: need a way to make sure we don't call any more delegate methods.
|
2012-05-31 19:21:08 +00:00
|
|
|
this.delegate.onSynchronizeFailed(this, new UnexpectedSessionException(session), "Failed to create session.");
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RepositorySessionFinishDelegate methods.
|
|
|
|
*/
|
2012-03-13 02:17:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* I could be called twice: once for sessionA and once for sessionB.
|
|
|
|
*
|
|
|
|
* If sessionB couldn't be created, I don't fail again.
|
|
|
|
*/
|
2011-12-21 16:44:08 +00:00
|
|
|
@Override
|
|
|
|
public void onFinishFailed(Exception ex) {
|
|
|
|
if (this.sessionB == null) {
|
|
|
|
// Ah, it was a problem cleaning up. Never mind.
|
2012-03-13 02:17:56 +00:00
|
|
|
Logger.warn(LOG_TAG, "Got exception cleaning up first after second session creation failed.", ex);
|
2011-12-21 16:44:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-01-14 17:20:31 +00:00
|
|
|
String session = (this.sessionA == null) ? "B" : "A";
|
|
|
|
this.delegate.onSynchronizeFailed(this, ex, "Finish of session " + session + " failed.");
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 02:17:56 +00:00
|
|
|
/**
|
|
|
|
* I should be called twice: first for sessionA and second for sessionB.
|
|
|
|
*
|
|
|
|
* If I am called for sessionA, I try to finish sessionB.
|
|
|
|
*
|
|
|
|
* If I am called for sessionB, I call my delegate's `onSynchronized` callback
|
|
|
|
* method because my flows should have completed.
|
|
|
|
*/
|
2011-12-21 16:44:08 +00:00
|
|
|
@Override
|
|
|
|
public void onFinishSucceeded(RepositorySession session,
|
|
|
|
RepositorySessionBundle bundle) {
|
2012-07-06 20:01:10 +00:00
|
|
|
Logger.debug(LOG_TAG, "onFinishSucceeded. Flows? " + flowAToBCompleted + ", " + flowBToACompleted);
|
2011-12-21 16:44:08 +00:00
|
|
|
|
|
|
|
if (session == sessionA) {
|
|
|
|
if (flowAToBCompleted) {
|
2012-07-06 20:01:10 +00:00
|
|
|
Logger.debug(LOG_TAG, "onFinishSucceeded: bumping session A's timestamp to " + pendingATimestamp + " or " + storeEndATimestamp);
|
2012-02-16 06:05:53 +00:00
|
|
|
bundle.bumpTimestamp(Math.max(pendingATimestamp, storeEndATimestamp));
|
2011-12-21 16:44:08 +00:00
|
|
|
this.synchronizer.bundleA = bundle;
|
2012-03-13 02:17:56 +00:00
|
|
|
} else {
|
|
|
|
// Should not happen!
|
2012-05-31 19:21:08 +00:00
|
|
|
this.delegate.onSynchronizeFailed(this, new UnexpectedSessionException(sessionA), "Failed to finish first session.");
|
|
|
|
return;
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
if (this.sessionB != null) {
|
2012-07-06 20:01:10 +00:00
|
|
|
Logger.trace(LOG_TAG, "Finishing session B.");
|
2011-12-21 16:44:08 +00:00
|
|
|
// On to the next.
|
2012-03-06 04:53:14 +00:00
|
|
|
try {
|
|
|
|
this.sessionB.finish(this);
|
|
|
|
} catch (InactiveSessionException e) {
|
|
|
|
this.onFinishFailed(e);
|
2012-05-31 19:21:08 +00:00
|
|
|
return;
|
2012-03-06 04:53:14 +00:00
|
|
|
}
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
} else if (session == sessionB) {
|
|
|
|
if (flowBToACompleted) {
|
2012-07-06 20:01:10 +00:00
|
|
|
Logger.debug(LOG_TAG, "onFinishSucceeded: bumping session B's timestamp to " + pendingBTimestamp + " or " + storeEndBTimestamp);
|
2012-02-16 06:05:53 +00:00
|
|
|
bundle.bumpTimestamp(Math.max(pendingBTimestamp, storeEndBTimestamp));
|
2011-12-21 16:44:08 +00:00
|
|
|
this.synchronizer.bundleB = bundle;
|
2012-07-06 20:01:10 +00:00
|
|
|
Logger.trace(LOG_TAG, "Notifying delegate.onSynchronized.");
|
2011-12-21 16:44:08 +00:00
|
|
|
this.delegate.onSynchronized(this);
|
2012-03-13 02:17:56 +00:00
|
|
|
} else {
|
|
|
|
// Should not happen!
|
2012-05-31 19:21:08 +00:00
|
|
|
this.delegate.onSynchronizeFailed(this, new UnexpectedSessionException(sessionB), "Failed to finish second session.");
|
|
|
|
return;
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: hurrrrrr...
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.sessionB == null) {
|
|
|
|
this.sessionA = null; // We're done.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-01-14 17:20:31 +00:00
|
|
|
public RepositorySessionFinishDelegate deferredFinishDelegate(final ExecutorService executor) {
|
|
|
|
return new DeferredRepositorySessionFinishDelegate(this, executor);
|
2011-12-21 16:44:08 +00:00
|
|
|
}
|
|
|
|
}
|