3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 10:15:52 +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,18 +200,29 @@ public class WalletAppKit extends AbstractIdleService {
vChain.addWallet(vWallet); vChain.addWallet(vWallet);
vPeerGroup.addWallet(vWallet); vPeerGroup.addWallet(vWallet);
onSetupCompleted(); onSetupCompleted();
vPeerGroup.startAndWait();
// Make sure we shut down cleanly. if (blockingStartup) {
if (autoStop) Runtime.getRuntime().addShutdownHook(new Thread() { vPeerGroup.startAndWait();
@Override public void run() { // Make sure we shut down cleanly.
try { installShutdownHook();
WalletAppKit.this.stopAndWait(); // TODO: Be able to use the provided download listener when doing a blocking startup.
} catch (Exception e) { final DownloadListener listener = new DownloadListener();
throw new RuntimeException(e); vPeerGroup.startBlockChainDownload(listener);
} listener.await();
} } else {
}); Futures.addCallback(vPeerGroup.start(), new FutureCallback<State>() {
vPeerGroup.startBlockChainDownload(downloadListener == null ? new DownloadListener() : downloadListener); @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) { } catch (BlockStoreException e) {
throw new IOException(e); throw new IOException(e);
} finally { } finally {
@ -217,7 +230,19 @@ public class WalletAppKit extends AbstractIdleService {
} }
} }
@Override private void installShutdownHook() {
if (autoStop) Runtime.getRuntime().addShutdownHook(new Thread() {
@Override public void run() {
try {
WalletAppKit.this.stopAndWait();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
@Override
protected void shutDown() throws Exception { protected void shutDown() throws Exception {
// Runs in a separate thread. // Runs in a separate thread.
try { try {