3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-14 19:25:51 +00:00

WalletAppKit: Finish the non-blocking startup code, oops.

This commit is contained in:
Mike Hearn 2013-09-16 11:37:05 +02:00
parent 6a84f55727
commit 01e7d63948

View File

@ -22,6 +22,8 @@ import com.google.bitcoin.store.BlockStoreException;
import com.google.bitcoin.store.SPVBlockStore; import com.google.bitcoin.store.SPVBlockStore;
import com.google.bitcoin.store.WalletProtobufSerializer; import com.google.bitcoin.store.WalletProtobufSerializer;
import com.google.common.util.concurrent.AbstractIdleService; import com.google.common.util.concurrent.AbstractIdleService;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -198,8 +200,37 @@ public class WalletAppKit extends AbstractIdleService {
vChain.addWallet(vWallet); vChain.addWallet(vWallet);
vPeerGroup.addWallet(vWallet); vPeerGroup.addWallet(vWallet);
onSetupCompleted(); onSetupCompleted();
if (blockingStartup) {
vPeerGroup.startAndWait(); vPeerGroup.startAndWait();
// Make sure we shut down cleanly. // Make sure we shut down cleanly.
installShutdownHook();
// TODO: Be able to use the provided download listener when doing a blocking startup.
final DownloadListener listener = new DownloadListener();
vPeerGroup.startBlockChainDownload(listener);
listener.await();
} else {
Futures.addCallback(vPeerGroup.start(), new FutureCallback<State>() {
@Override
public void onSuccess(State result) {
final PeerEventListener l = downloadListener == null ? new DownloadListener() : downloadListener;
vPeerGroup.startBlockChainDownload(l);
}
@Override
public void onFailure(Throwable t) {
throw new RuntimeException(t);
}
});
}
} catch (BlockStoreException e) {
throw new IOException(e);
} finally {
if (walletStream != null) walletStream.close();
}
}
private void installShutdownHook() {
if (autoStop) Runtime.getRuntime().addShutdownHook(new Thread() { if (autoStop) Runtime.getRuntime().addShutdownHook(new Thread() {
@Override public void run() { @Override public void run() {
try { try {
@ -209,12 +240,6 @@ public class WalletAppKit extends AbstractIdleService {
} }
} }
}); });
vPeerGroup.startBlockChainDownload(downloadListener == null ? new DownloadListener() : downloadListener);
} catch (BlockStoreException e) {
throw new IOException(e);
} finally {
if (walletStream != null) walletStream.close();
}
} }
@Override @Override