mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-24 20:56:45 +00:00
add ping service
This commit is contained in:
parent
1e250193fd
commit
f5a857317d
5 changed files with 97 additions and 16 deletions
|
@ -5,7 +5,8 @@ import com.google.common.eventbus.Subscribe;
|
||||||
import com.google.common.io.ByteSource;
|
import com.google.common.io.ByteSource;
|
||||||
import com.sparrowwallet.drongo.SecureString;
|
import com.sparrowwallet.drongo.SecureString;
|
||||||
import com.sparrowwallet.drongo.Utils;
|
import com.sparrowwallet.drongo.Utils;
|
||||||
import com.sparrowwallet.drongo.crypto.*;
|
import com.sparrowwallet.drongo.crypto.InvalidPasswordException;
|
||||||
|
import com.sparrowwallet.drongo.crypto.Key;
|
||||||
import com.sparrowwallet.drongo.policy.PolicyType;
|
import com.sparrowwallet.drongo.policy.PolicyType;
|
||||||
import com.sparrowwallet.drongo.protocol.ScriptType;
|
import com.sparrowwallet.drongo.protocol.ScriptType;
|
||||||
import com.sparrowwallet.drongo.protocol.Transaction;
|
import com.sparrowwallet.drongo.protocol.Transaction;
|
||||||
|
@ -25,6 +26,7 @@ import javafx.animation.Animation;
|
||||||
import javafx.animation.KeyFrame;
|
import javafx.animation.KeyFrame;
|
||||||
import javafx.animation.KeyValue;
|
import javafx.animation.KeyValue;
|
||||||
import javafx.animation.Timeline;
|
import javafx.animation.Timeline;
|
||||||
|
import javafx.concurrent.Worker;
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
|
@ -45,6 +47,9 @@ import java.text.ParseException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class AppController implements Initializable {
|
public class AppController implements Initializable {
|
||||||
|
private static final int SERVER_PING_PERIOD = 10 * 1000;
|
||||||
|
private static final int ENUMERATE_HW_PERIOD = 30 * 1000;
|
||||||
|
|
||||||
private static final String TRANSACTION_TAB_TYPE = "transaction";
|
private static final String TRANSACTION_TAB_TYPE = "transaction";
|
||||||
public static final String DRAG_OVER_CLASS = "drag-over";
|
public static final String DRAG_OVER_CLASS = "drag-over";
|
||||||
|
|
||||||
|
@ -63,16 +68,18 @@ public class AppController implements Initializable {
|
||||||
@FXML
|
@FXML
|
||||||
private StatusBar statusBar;
|
private StatusBar statusBar;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private UnlabeledToggleSwitch serverToggle;
|
||||||
|
|
||||||
private Timeline statusTimeline;
|
private Timeline statusTimeline;
|
||||||
|
|
||||||
|
private ElectrumServer.PingService pingService;
|
||||||
|
|
||||||
public static boolean showTxHexProperty;
|
public static boolean showTxHexProperty;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(URL location, ResourceBundle resources) {
|
public void initialize(URL location, ResourceBundle resources) {
|
||||||
EventManager.get().register(this);
|
EventManager.get().register(this);
|
||||||
|
|
||||||
ElectrumServer.PingService pingService = new ElectrumServer.PingService();
|
|
||||||
//pingService.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void initializeView() {
|
void initializeView() {
|
||||||
|
@ -124,7 +131,45 @@ public class AppController implements Initializable {
|
||||||
showTxHexProperty = true;
|
showTxHexProperty = true;
|
||||||
exportWallet.setDisable(true);
|
exportWallet.setDisable(true);
|
||||||
|
|
||||||
//addWalletTab("newWallet", new Wallet(PolicyType.SINGLE, ScriptType.P2WPKH));
|
serverToggle.selectedProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
|
Config.get().setMode(newValue ? Mode.ONLINE : Mode.OFFLINE);
|
||||||
|
if(newValue) {
|
||||||
|
if(pingService.getState() == Worker.State.CANCELLED) {
|
||||||
|
pingService.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!pingService.isRunning()) {
|
||||||
|
pingService.start();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
pingService.cancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
pingService = createPingService();
|
||||||
|
Config config = Config.get();
|
||||||
|
if(config.getMode() == Mode.ONLINE && config.getElectrumServer() != null && !config.getElectrumServer().isEmpty()) {
|
||||||
|
pingService.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ElectrumServer.PingService createPingService() {
|
||||||
|
ElectrumServer.PingService pingService = new ElectrumServer.PingService();
|
||||||
|
pingService.setPeriod(new Duration(SERVER_PING_PERIOD));
|
||||||
|
pingService.setOnSucceeded(successEvent -> {
|
||||||
|
serverToggle.setSelected(true);
|
||||||
|
if(pingService.getValue() != null) {
|
||||||
|
statusBar.setText("Connected: " + pingService.getValue().split(System.lineSeparator(), 2)[0]);
|
||||||
|
} else {
|
||||||
|
statusBar.setText("");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
pingService.setOnFailed(failEvent -> {
|
||||||
|
serverToggle.setSelected(false);
|
||||||
|
statusBar.setText(failEvent.getSource().getException().getMessage());
|
||||||
|
});
|
||||||
|
|
||||||
|
return pingService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void openFromFile(ActionEvent event) {
|
public void openFromFile(ActionEvent event) {
|
||||||
|
@ -387,7 +432,7 @@ public class AppController implements Initializable {
|
||||||
|
|
||||||
if(!storage.getWalletFile().exists() || wallet.containsSource(KeystoreSource.HW_USB)) {
|
if(!storage.getWalletFile().exists() || wallet.containsSource(KeystoreSource.HW_USB)) {
|
||||||
Hwi.ScheduledEnumerateService enumerateService = new Hwi.ScheduledEnumerateService(null);
|
Hwi.ScheduledEnumerateService enumerateService = new Hwi.ScheduledEnumerateService(null);
|
||||||
enumerateService.setPeriod(new Duration(30 * 1000));
|
enumerateService.setPeriod(new Duration(ENUMERATE_HW_PERIOD));
|
||||||
enumerateService.setOnSucceeded(workerStateEvent -> {
|
enumerateService.setOnSucceeded(workerStateEvent -> {
|
||||||
List<Device> devices = enumerateService.getValue();
|
List<Device> devices = enumerateService.getValue();
|
||||||
EventManager.get().post(new UsbDeviceEvent(devices));
|
EventManager.get().post(new UsbDeviceEvent(devices));
|
||||||
|
|
|
@ -7,7 +7,6 @@ import javafx.geometry.Insets;
|
||||||
import javafx.scene.control.*;
|
import javafx.scene.control.*;
|
||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
import javafx.scene.image.ImageView;
|
import javafx.scene.image.ImageView;
|
||||||
import javafx.scene.layout.Region;
|
|
||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
import org.controlsfx.control.HyperlinkLabel;
|
import org.controlsfx.control.HyperlinkLabel;
|
||||||
import org.controlsfx.control.StatusBar;
|
import org.controlsfx.control.StatusBar;
|
||||||
|
@ -42,8 +41,8 @@ public class WelcomeDialog extends Dialog<Mode> {
|
||||||
dialogPane.setGraphic(imageView);
|
dialogPane.setGraphic(imageView);
|
||||||
}
|
}
|
||||||
|
|
||||||
final ButtonType onlineButtonType = new javafx.scene.control.ButtonType("Configure Now", ButtonBar.ButtonData.OK_DONE);
|
final ButtonType onlineButtonType = new javafx.scene.control.ButtonType("Configure Server", ButtonBar.ButtonData.OK_DONE);
|
||||||
final ButtonType offlineButtonType = new javafx.scene.control.ButtonType("Configure Later or Use Offline", ButtonBar.ButtonData.CANCEL_CLOSE);
|
final ButtonType offlineButtonType = new javafx.scene.control.ButtonType("Later or Offline Mode", ButtonBar.ButtonData.CANCEL_CLOSE);
|
||||||
dialogPane.getButtonTypes().addAll(onlineButtonType, offlineButtonType);
|
dialogPane.getButtonTypes().addAll(onlineButtonType, offlineButtonType);
|
||||||
|
|
||||||
final VBox content = new VBox(20);
|
final VBox content = new VBox(20);
|
||||||
|
|
|
@ -7,6 +7,7 @@ import com.sparrowwallet.drongo.KeyPurpose;
|
||||||
import com.sparrowwallet.drongo.Utils;
|
import com.sparrowwallet.drongo.Utils;
|
||||||
import com.sparrowwallet.drongo.protocol.*;
|
import com.sparrowwallet.drongo.protocol.*;
|
||||||
import com.sparrowwallet.drongo.wallet.*;
|
import com.sparrowwallet.drongo.wallet.*;
|
||||||
|
import javafx.concurrent.ScheduledService;
|
||||||
import javafx.concurrent.Service;
|
import javafx.concurrent.Service;
|
||||||
import javafx.concurrent.Task;
|
import javafx.concurrent.Task;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
@ -76,7 +77,7 @@ public class ElectrumServer {
|
||||||
|
|
||||||
public void ping() throws ServerException {
|
public void ping() throws ServerException {
|
||||||
JsonRpcClient client = new JsonRpcClient(getTransport());
|
JsonRpcClient client = new JsonRpcClient(getTransport());
|
||||||
client.createRequest().returnAs(Void.class).method("server.ping").id(1).execute();
|
client.createRequest().method("server.ping").id(1).executeNullable();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getServerVersion() throws ServerException {
|
public List<String> getServerVersion() throws ServerException {
|
||||||
|
@ -526,17 +527,43 @@ public class ElectrumServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class PingService extends Service<Boolean> {
|
public static class PingService extends ScheduledService<String> {
|
||||||
|
private boolean firstCall = true;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Task<Boolean> createTask() {
|
protected Task<String> createTask() {
|
||||||
return new Task<>() {
|
return new Task<>() {
|
||||||
protected Boolean call() throws ServerException {
|
protected String call() throws ServerException {
|
||||||
ElectrumServer electrumServer = new ElectrumServer();
|
ElectrumServer electrumServer = new ElectrumServer();
|
||||||
|
if(firstCall) {
|
||||||
|
electrumServer.getServerVersion();
|
||||||
|
firstCall = false;
|
||||||
|
return electrumServer.getServerBanner();
|
||||||
|
} else {
|
||||||
electrumServer.ping();
|
electrumServer.ping();
|
||||||
return true;
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean cancel() {
|
||||||
|
try {
|
||||||
|
closeActiveConnection();
|
||||||
|
} catch (ServerException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
super.reset();
|
||||||
|
firstCall = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class TransactionHistoryService extends Service<Boolean> {
|
public static class TransactionHistoryService extends Service<Boolean> {
|
||||||
|
|
|
@ -20,6 +20,11 @@
|
||||||
-fx-fill: #383a42;
|
-fx-fill: #383a42;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.status-bar .status-label {
|
||||||
|
-fx-alignment: center-left;
|
||||||
|
}
|
||||||
|
|
||||||
.status-bar .right-items {
|
.status-bar .right-items {
|
||||||
|
-fx-alignment: center-right;
|
||||||
-fx-padding: 0 0 0 8;
|
-fx-padding: 0 0 0 8;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
<?import org.controlsfx.control.StatusBar?>
|
<?import org.controlsfx.control.StatusBar?>
|
||||||
<?import javafx.scene.text.Text?>
|
<?import javafx.scene.text.Text?>
|
||||||
<?import javafx.scene.shape.Rectangle?>
|
<?import javafx.scene.shape.Rectangle?>
|
||||||
|
<?import com.sparrowwallet.sparrow.control.UnlabeledToggleSwitch?>
|
||||||
|
|
||||||
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="200" minWidth="350" prefHeight="750.0" prefWidth="1000.0" fx:controller="com.sparrowwallet.sparrow.AppController" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
|
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="200" minWidth="350" prefHeight="750.0" prefWidth="1000.0" fx:controller="com.sparrowwallet.sparrow.AppController" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
|
||||||
<children>
|
<children>
|
||||||
|
@ -43,6 +44,10 @@
|
||||||
<TabPane fx:id="tabs" />
|
<TabPane fx:id="tabs" />
|
||||||
</StackPane>
|
</StackPane>
|
||||||
|
|
||||||
<StatusBar fx:id="statusBar" text="" minHeight="36"/>
|
<StatusBar fx:id="statusBar" text="" minHeight="36">
|
||||||
|
<rightItems>
|
||||||
|
<UnlabeledToggleSwitch fx:id="serverToggle" />
|
||||||
|
</rightItems>
|
||||||
|
</StatusBar>
|
||||||
</children>
|
</children>
|
||||||
</VBox>
|
</VBox>
|
||||||
|
|
Loading…
Reference in a new issue