schedule regular check to indicate proxy status in sparrow terminal

This commit is contained in:
Craig Raw 2023-11-08 17:08:44 +02:00
parent 36c2181a7f
commit d0f21eafd1
3 changed files with 93 additions and 3 deletions

View file

@ -0,0 +1,65 @@
package com.sparrowwallet.sparrow.terminal;
import com.google.common.net.HostAndPort;
import com.googlecode.lanterna.gui2.Label;
import com.sparrowwallet.sparrow.AppServices;
import com.sparrowwallet.sparrow.io.Config;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.util.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.Socket;
public class ProxyStatusLabel extends Label {
private static final Logger log = LoggerFactory.getLogger(ProxyStatusLabel.class);
private final ProxyConnectionTest proxyConnectionTest = new ProxyConnectionTest();
public ProxyStatusLabel() {
super("");
}
public void update() {
if(!AppServices.isUsingProxy()) {
getTextGUI().getGUIThread().invokeLater(() -> setText(""));
} else if(!Config.get().isUseProxy()) {
proxyConnectionTest.cancel();
if(AppServices.isTorRunning()) {
getTextGUI().getGUIThread().invokeLater(() -> setText("Proxy enabled"));
}
} else if(!proxyConnectionTest.isRunning()) {
if(proxyConnectionTest.getState() == Worker.State.CANCELLED || proxyConnectionTest.getState() == Worker.State.FAILED) {
proxyConnectionTest.reset();
}
proxyConnectionTest.setPeriod(Duration.seconds(20.0));
proxyConnectionTest.setBackoffStrategy(null);
proxyConnectionTest.setOnSucceeded(workerStateEvent -> {
getTextGUI().getGUIThread().invokeLater(() -> setText("Proxy enabled"));
});
proxyConnectionTest.setOnFailed(workerStateEvent -> {
getTextGUI().getGUIThread().invokeLater(() -> setText("Proxy error!"));
log.warn("Failed to connect to external Tor proxy: " + workerStateEvent.getSource().getException().getMessage());
});
proxyConnectionTest.start();
}
}
private static class ProxyConnectionTest extends ScheduledService<Void> {
@Override
protected Task<Void> createTask() {
return new Task<>() {
protected Void call() throws IOException {
HostAndPort proxyHostAndPort = HostAndPort.fromString(Config.get().getProxyServer());
Socket socket = new Socket(proxyHostAndPort.getHost(), proxyHostAndPort.getPort());
socket.close();
return null;
}
};
}
}
}

View file

@ -10,6 +10,7 @@ import com.sparrowwallet.sparrow.event.*;
import com.sparrowwallet.sparrow.io.Config;
import com.sparrowwallet.sparrow.net.ServerType;
import javafx.animation.*;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.util.Duration;
@ -20,6 +21,7 @@ public class SparrowTextGui extends MultiWindowTextGUI {
private final Panel titleBar;
private final Panel statusBar;
private final ProxyStatusLabel proxyStatusLabel;
private final Label connectedLabel;
private final Label statusLabel;
private final ProgressBar statusProgress;
@ -36,8 +38,10 @@ public class SparrowTextGui extends MultiWindowTextGUI {
Panel panel = new Panel(new BorderLayout());
titleBar = new Panel(new GridLayout(2));
titleBar = new Panel(new GridLayout(3));
new Label("Sparrow Terminal").addTo(titleBar);
this.proxyStatusLabel = new ProxyStatusLabel();
titleBar.addComponent(proxyStatusLabel, GridLayout.createLayoutData(GridLayout.Alignment.END, GridLayout.Alignment.CENTER, true, false));
this.connectedLabel = new Label("Disconnected");
titleBar.addComponent(connectedLabel, GridLayout.createLayoutData(GridLayout.Alignment.END, GridLayout.Alignment.CENTER, true, false));
panel.addComponent(titleBar, BorderLayout.Location.TOP);
@ -73,10 +77,14 @@ public class SparrowTextGui extends MultiWindowTextGUI {
private void setConnectedLabel(Integer height) {
getGUIThread().invokeLater(() -> {
connectedLabel.setText(height == null ? "Disconnected" : "Connected " + (AppServices.isUsingProxy() ? "with proxy " : "") + "at " + height);
connectedLabel.setText(height == null ? "Disconnected" : "Connected at " + height);
});
}
private void updateProxyStatusLabel() {
Platform.runLater(proxyStatusLabel::update);
}
@Subscribe
public void connectionStart(ConnectionStartEvent event) {
statusUpdated(new StatusEvent(event.getStatus(), 120));
@ -85,12 +93,14 @@ public class SparrowTextGui extends MultiWindowTextGUI {
@Subscribe
public void connectionFailed(ConnectionFailedEvent event) {
setDisconnectedLabel();
updateProxyStatusLabel();
statusUpdated(new StatusEvent("Connection failed: " + event.getMessage()));
}
@Subscribe
public void connection(ConnectionEvent event) {
setConnectedLabel(event.getBlockHeight());
updateProxyStatusLabel();
statusUpdated(new StatusEvent("Connected to " + Config.get().getServerDisplayName() + " at height " + event.getBlockHeight()));
}
@ -107,6 +117,21 @@ public class SparrowTextGui extends MultiWindowTextGUI {
setConnectedLabel(event.getHeight());
}
@Subscribe
public void torBootStatus(TorBootStatusEvent event) {
updateProxyStatusLabel();
}
@Subscribe
public void torFailedStatus(TorFailedStatusEvent event) {
updateProxyStatusLabel();
}
@Subscribe
public void torReadyStatus(TorReadyStatusEvent event) {
updateProxyStatusLabel();
}
@Subscribe
public void statusUpdated(StatusEvent event) {
getGUIThread().invokeLater(() -> statusLabel.setText(event.getStatus()));

View file

@ -191,7 +191,7 @@
</DynamicForm>
<Form fx:id="signingWalletForm" GridPane.columnIndex="0" GridPane.rowIndex="6">
<Fieldset text="Signatures" inputGrow="SOMETIMES">
<Fieldset text="Signatures" inputGrow="SOMETIMES" styleClass="relaxedLabelFieldSet">
<Field text="Signing Wallet:">
<ComboBox fx:id="signingWallet" />
<Label fx:id="noWalletsWarning" graphicTextGap="5" text="No open wallets can sign. ">