mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-23 20:36:44 +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.sparrowwallet.drongo.SecureString;
|
||||
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.protocol.ScriptType;
|
||||
import com.sparrowwallet.drongo.protocol.Transaction;
|
||||
|
@ -25,6 +26,7 @@ import javafx.animation.Animation;
|
|||
import javafx.animation.KeyFrame;
|
||||
import javafx.animation.KeyValue;
|
||||
import javafx.animation.Timeline;
|
||||
import javafx.concurrent.Worker;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
|
@ -45,6 +47,9 @@ import java.text.ParseException;
|
|||
import java.util.*;
|
||||
|
||||
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";
|
||||
public static final String DRAG_OVER_CLASS = "drag-over";
|
||||
|
||||
|
@ -63,16 +68,18 @@ public class AppController implements Initializable {
|
|||
@FXML
|
||||
private StatusBar statusBar;
|
||||
|
||||
@FXML
|
||||
private UnlabeledToggleSwitch serverToggle;
|
||||
|
||||
private Timeline statusTimeline;
|
||||
|
||||
private ElectrumServer.PingService pingService;
|
||||
|
||||
public static boolean showTxHexProperty;
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
EventManager.get().register(this);
|
||||
|
||||
ElectrumServer.PingService pingService = new ElectrumServer.PingService();
|
||||
//pingService.
|
||||
}
|
||||
|
||||
void initializeView() {
|
||||
|
@ -124,7 +131,45 @@ public class AppController implements Initializable {
|
|||
showTxHexProperty = 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) {
|
||||
|
@ -387,7 +432,7 @@ public class AppController implements Initializable {
|
|||
|
||||
if(!storage.getWalletFile().exists() || wallet.containsSource(KeystoreSource.HW_USB)) {
|
||||
Hwi.ScheduledEnumerateService enumerateService = new Hwi.ScheduledEnumerateService(null);
|
||||
enumerateService.setPeriod(new Duration(30 * 1000));
|
||||
enumerateService.setPeriod(new Duration(ENUMERATE_HW_PERIOD));
|
||||
enumerateService.setOnSucceeded(workerStateEvent -> {
|
||||
List<Device> devices = enumerateService.getValue();
|
||||
EventManager.get().post(new UsbDeviceEvent(devices));
|
||||
|
|
|
@ -7,7 +7,6 @@ import javafx.geometry.Insets;
|
|||
import javafx.scene.control.*;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.layout.Region;
|
||||
import javafx.scene.layout.VBox;
|
||||
import org.controlsfx.control.HyperlinkLabel;
|
||||
import org.controlsfx.control.StatusBar;
|
||||
|
@ -42,8 +41,8 @@ public class WelcomeDialog extends Dialog<Mode> {
|
|||
dialogPane.setGraphic(imageView);
|
||||
}
|
||||
|
||||
final ButtonType onlineButtonType = new javafx.scene.control.ButtonType("Configure Now", ButtonBar.ButtonData.OK_DONE);
|
||||
final ButtonType offlineButtonType = new javafx.scene.control.ButtonType("Configure Later or Use Offline", ButtonBar.ButtonData.CANCEL_CLOSE);
|
||||
final ButtonType onlineButtonType = new javafx.scene.control.ButtonType("Configure Server", ButtonBar.ButtonData.OK_DONE);
|
||||
final ButtonType offlineButtonType = new javafx.scene.control.ButtonType("Later or Offline Mode", ButtonBar.ButtonData.CANCEL_CLOSE);
|
||||
dialogPane.getButtonTypes().addAll(onlineButtonType, offlineButtonType);
|
||||
|
||||
final VBox content = new VBox(20);
|
||||
|
|
|
@ -7,6 +7,7 @@ import com.sparrowwallet.drongo.KeyPurpose;
|
|||
import com.sparrowwallet.drongo.Utils;
|
||||
import com.sparrowwallet.drongo.protocol.*;
|
||||
import com.sparrowwallet.drongo.wallet.*;
|
||||
import javafx.concurrent.ScheduledService;
|
||||
import javafx.concurrent.Service;
|
||||
import javafx.concurrent.Task;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
@ -76,7 +77,7 @@ public class ElectrumServer {
|
|||
|
||||
public void ping() throws ServerException {
|
||||
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 {
|
||||
|
@ -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
|
||||
protected Task<Boolean> createTask() {
|
||||
protected Task<String> createTask() {
|
||||
return new Task<>() {
|
||||
protected Boolean call() throws ServerException {
|
||||
protected String call() throws ServerException {
|
||||
ElectrumServer electrumServer = new ElectrumServer();
|
||||
electrumServer.ping();
|
||||
return true;
|
||||
if(firstCall) {
|
||||
electrumServer.getServerVersion();
|
||||
firstCall = false;
|
||||
return electrumServer.getServerBanner();
|
||||
} else {
|
||||
electrumServer.ping();
|
||||
}
|
||||
|
||||
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> {
|
||||
|
|
|
@ -20,6 +20,11 @@
|
|||
-fx-fill: #383a42;
|
||||
}
|
||||
|
||||
.status-bar .status-label {
|
||||
-fx-alignment: center-left;
|
||||
}
|
||||
|
||||
.status-bar .right-items {
|
||||
-fx-alignment: center-right;
|
||||
-fx-padding: 0 0 0 8;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<?import org.controlsfx.control.StatusBar?>
|
||||
<?import javafx.scene.text.Text?>
|
||||
<?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">
|
||||
<children>
|
||||
|
@ -43,6 +44,10 @@
|
|||
<TabPane fx:id="tabs" />
|
||||
</StackPane>
|
||||
|
||||
<StatusBar fx:id="statusBar" text="" minHeight="36"/>
|
||||
<StatusBar fx:id="statusBar" text="" minHeight="36">
|
||||
<rightItems>
|
||||
<UnlabeledToggleSwitch fx:id="serverToggle" />
|
||||
</rightItems>
|
||||
</StatusBar>
|
||||
</children>
|
||||
</VBox>
|
||||
|
|
Loading…
Reference in a new issue