mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-12 02:05:53 +00:00
WalletAppKit/Template: Cleaner way to check if the app is already running. Backport from Lighthouse.
This commit is contained in:
parent
13b2f2104c
commit
8e6e2256bc
@ -30,12 +30,10 @@ import com.google.common.util.concurrent.Service;
|
|||||||
import com.subgraph.orchid.TorClient;
|
import com.subgraph.orchid.TorClient;
|
||||||
import org.bitcoinj.wallet.Protos;
|
import org.bitcoinj.wallet.Protos;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.nio.channels.FileLock;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
@ -188,6 +186,29 @@ public class WalletAppKit extends AbstractIdleService {
|
|||||||
*/
|
*/
|
||||||
protected void onSetupCompleted() { }
|
protected void onSetupCompleted() { }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests to see if the spvchain file has an operating system file lock on it. Useful for checking if your app
|
||||||
|
* is already running. If another copy of your app is running and you start the appkit anyway, an exception will
|
||||||
|
* be thrown during the startup process. Returns false if the chain file does not exist.
|
||||||
|
*/
|
||||||
|
public boolean isChainFileLocked() throws IOException {
|
||||||
|
RandomAccessFile file2 = null;
|
||||||
|
try {
|
||||||
|
File file = new File(directory, filePrefix + ".spvchain");
|
||||||
|
if (!file.exists())
|
||||||
|
return false;
|
||||||
|
file2 = new RandomAccessFile(file, "rw");
|
||||||
|
FileLock lock = file2.getChannel().tryLock();
|
||||||
|
if (lock == null)
|
||||||
|
return true;
|
||||||
|
lock.close();
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
if (file2 != null)
|
||||||
|
file2.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void startUp() throws Exception {
|
protected void startUp() throws Exception {
|
||||||
// Runs in a separate thread.
|
// Runs in a separate thread.
|
||||||
|
@ -5,10 +5,8 @@ import com.google.bitcoin.core.NetworkParameters;
|
|||||||
import com.google.bitcoin.kits.WalletAppKit;
|
import com.google.bitcoin.kits.WalletAppKit;
|
||||||
import com.google.bitcoin.params.MainNetParams;
|
import com.google.bitcoin.params.MainNetParams;
|
||||||
import com.google.bitcoin.params.RegTestParams;
|
import com.google.bitcoin.params.RegTestParams;
|
||||||
import com.google.bitcoin.store.BlockStoreException;
|
|
||||||
import com.google.bitcoin.utils.BriefLogFormatter;
|
import com.google.bitcoin.utils.BriefLogFormatter;
|
||||||
import com.google.bitcoin.utils.Threading;
|
import com.google.bitcoin.utils.Threading;
|
||||||
import com.google.common.base.Throwables;
|
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
@ -41,22 +39,12 @@ public class Main extends Application {
|
|||||||
instance = this;
|
instance = this;
|
||||||
// Show the crash dialog for any exceptions that we don't handle and that hit the main loop.
|
// Show the crash dialog for any exceptions that we don't handle and that hit the main loop.
|
||||||
GuiUtils.handleCrashesOnThisThread();
|
GuiUtils.handleCrashesOnThisThread();
|
||||||
try {
|
|
||||||
init(mainWindow);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
// Nicer message for the case where the block store file is locked.
|
|
||||||
if (Throwables.getRootCause(t) instanceof BlockStoreException) {
|
|
||||||
GuiUtils.informationalAlert("Already running", "This application is already running and cannot be started twice.");
|
|
||||||
} else {
|
|
||||||
throw t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void init(Stage mainWindow) throws IOException {
|
// Match Aqua UI style.
|
||||||
if (System.getProperty("os.name").toLowerCase().contains("mac")) {
|
if (System.getProperty("os.name").toLowerCase().contains("mac")) {
|
||||||
AquaFx.style();
|
AquaFx.style();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the GUI. The Controller class will be automagically created and wired up.
|
// Load the GUI. The Controller class will be automagically created and wired up.
|
||||||
URL location = getClass().getResource("main.fxml");
|
URL location = getClass().getResource("main.fxml");
|
||||||
FXMLLoader loader = new FXMLLoader(location);
|
FXMLLoader loader = new FXMLLoader(location);
|
||||||
@ -78,6 +66,16 @@ public class Main extends Application {
|
|||||||
Threading.USER_THREAD = Platform::runLater;
|
Threading.USER_THREAD = Platform::runLater;
|
||||||
// Create the app kit. It won't do any heavyweight initialization until after we start it.
|
// Create the app kit. It won't do any heavyweight initialization until after we start it.
|
||||||
bitcoin = new WalletAppKit(params, new File("."), APP_NAME);
|
bitcoin = new WalletAppKit(params, new File("."), APP_NAME);
|
||||||
|
if (bitcoin.isChainFileLocked()) {
|
||||||
|
informationalAlert("Already running", "This application is already running and cannot be started twice.");
|
||||||
|
Platform.exit();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mainWindow.show();
|
||||||
|
|
||||||
|
// Now configure and start the appkit. This will take a second or two - we could show a temporary splash screen
|
||||||
|
// or progress widget to keep the user engaged whilst we initialise, but we don't.
|
||||||
if (params == RegTestParams.get()) {
|
if (params == RegTestParams.get()) {
|
||||||
bitcoin.connectToLocalHost(); // You should run a regtest mode bitcoind locally.
|
bitcoin.connectToLocalHost(); // You should run a regtest mode bitcoind locally.
|
||||||
} else if (params == MainNetParams.get()) {
|
} else if (params == MainNetParams.get()) {
|
||||||
@ -89,9 +87,6 @@ public class Main extends Application {
|
|||||||
// As an example!
|
// As an example!
|
||||||
// bitcoin.useTor();
|
// bitcoin.useTor();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now configure and start the appkit. This will take a second or two - we could show a temporary splash screen
|
|
||||||
// or progress widget to keep the user engaged whilst we initialise, but we don't.
|
|
||||||
bitcoin.setDownloadListener(controller.progressBarUpdater())
|
bitcoin.setDownloadListener(controller.progressBarUpdater())
|
||||||
.setBlockingStartup(false)
|
.setBlockingStartup(false)
|
||||||
.setUserAgent(APP_NAME, "1.0");
|
.setUserAgent(APP_NAME, "1.0");
|
||||||
@ -102,7 +97,6 @@ public class Main extends Application {
|
|||||||
bitcoin.peerGroup().setMaxConnections(11);
|
bitcoin.peerGroup().setMaxConnections(11);
|
||||||
System.out.println(bitcoin.wallet());
|
System.out.println(bitcoin.wallet());
|
||||||
controller.onBitcoinSetup();
|
controller.onBitcoinSetup();
|
||||||
mainWindow.show();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class OverlayUI<T> {
|
public class OverlayUI<T> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user