Add footer with status info

This commit is contained in:
Manfred Karrer 2015-04-16 17:53:03 +02:00
parent 33a539ca52
commit 24d29eaa83
15 changed files with 380 additions and 207 deletions

View file

@ -66,6 +66,10 @@ import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Named;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
@ -87,8 +91,7 @@ public class WalletService {
private final List<TxConfidenceListener> txConfidenceListeners = new CopyOnWriteArrayList<>();
private final List<BalanceListener> balanceListeners = new CopyOnWriteArrayList<>();
private final ObservableDownloadListener downloadListener = new ObservableDownloadListener();
private final Observable<Double> downloadProgress = downloadListener.getObservable();
private final DownloadListener downloadListener = new DownloadListener();
private final WalletEventListener walletEventListener = new BitsquareWalletEventListener();
private RegTestHost regTestHost;
@ -209,7 +212,6 @@ public class WalletService {
walletAppKit.startAsync();
return status.timeout(30, TimeUnit.SECONDS);
//return status.mergeWith(downloadProgress).timeout(30, TimeUnit.SECONDS);
}
private void initWallet() {
@ -227,8 +229,8 @@ public class WalletService {
walletAppKit.stopAsync();
}
public Observable<Double> getDownloadProgress() {
return downloadProgress;
public ReadOnlyDoubleProperty downloadPercentageProperty() {
return downloadListener.percentageProperty();
}
public Wallet getWallet() {
@ -536,24 +538,23 @@ public class WalletService {
// Inner classes
///////////////////////////////////////////////////////////////////////////////////////////
private static class ObservableDownloadListener extends DownloadProgressTracker {
private final Subject<Double, Double> subject = BehaviorSubject.create(0d);
private static class DownloadListener extends DownloadProgressTracker {
private final DoubleProperty percentage = new SimpleDoubleProperty(-1);
@Override
protected void progress(double percentage, int blocksLeft, Date date) {
super.progress(percentage, blocksLeft, date);
subject.onNext(percentage);
Threading.USER_THREAD.execute(() -> this.percentage.set(percentage / 100d));
}
@Override
protected void doneDownload() {
super.doneDownload();
subject.onCompleted();
Threading.USER_THREAD.execute(() -> this.percentage.set(1));
}
public Observable<Double> getObservable() {
return subject.asObservable();
public ReadOnlyDoubleProperty percentageProperty() {
return percentage;
}
}

View file

@ -1,47 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.p2p;
public enum BootstrapState {
PEER_CREATION_FAILED,
DISCOVERY_STARTED,
DISCOVERY_DIRECT_SUCCEEDED,
DISCOVERY_MANUAL_PORT_FORWARDING_SUCCEEDED,
DISCOVERY_FAILED,
DISCOVERY_AUTO_PORT_FORWARDING_STARTED,
DISCOVERY_AUTO_PORT_FORWARDING_SUCCEEDED,
DISCOVERY_AUTO_PORT_FORWARDING_FAILED,
RELAY_STARTED,
RELAY_SUCCEEDED,
RELAY_FAILED,
BOOT_STRAP_SUCCEEDED,
BOOT_STRAP_FAILED;
private String message;
BootstrapState() {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View file

@ -17,16 +17,18 @@
package io.bitsquare.p2p;
import io.bitsquare.p2p.tomp2p.BootstrappedPeerBuilder;
import java.security.KeyPair;
import rx.Observable;
public interface ClientNode {
ConnectionType getConnectionType();
BootstrappedPeerBuilder.ConnectionType getConnectionType();
Node getAddress();
Node getBootstrapNodeAddress();
Observable<BootstrapState> bootstrap(KeyPair keyPair);
Observable<BootstrappedPeerBuilder.State> bootstrap(KeyPair keyPair);
}

View file

@ -1,22 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.p2p;
public enum ConnectionType {
UNKNOWN, DIRECT, MANUAL_PORT_FORWARDING, AUTO_PORT_FORWARDING, RELAY
}

View file

@ -17,7 +17,6 @@
package io.bitsquare.p2p.tomp2p;
import io.bitsquare.p2p.BootstrapState;
import io.bitsquare.p2p.Node;
import com.google.common.util.concurrent.SettableFuture;
@ -34,6 +33,7 @@ import java.security.KeyPair;
import javax.inject.Inject;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import net.tomp2p.connection.Bindings;
@ -76,6 +76,40 @@ public class BootstrappedPeerBuilder {
static final String NETWORK_INTERFACE_UNSPECIFIED = "<unspecified>";
static final String USE_MANUAL_PORT_FORWARDING_KEY = "node.useManualPortForwarding";
public enum ConnectionType {
UNDEFINED, DIRECT, MANUAL_PORT_FORWARDING, AUTO_PORT_FORWARDING, RELAY
}
public enum State {
UNDEFINED,
PEER_CREATION_FAILED,
DISCOVERY_STARTED,
DISCOVERY_DIRECT_SUCCEEDED,
DISCOVERY_MANUAL_PORT_FORWARDING_SUCCEEDED,
DISCOVERY_FAILED,
DISCOVERY_AUTO_PORT_FORWARDING_STARTED,
DISCOVERY_AUTO_PORT_FORWARDING_SUCCEEDED,
DISCOVERY_AUTO_PORT_FORWARDING_FAILED,
RELAY_STARTED,
RELAY_SUCCEEDED,
RELAY_FAILED,
BOOT_STRAP_SUCCEEDED,
BOOT_STRAP_FAILED;
private String message;
State() {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
private KeyPair keyPair;
private final int port;
private final boolean useManualPortForwarding;
@ -84,7 +118,8 @@ public class BootstrappedPeerBuilder {
private final SettableFuture<PeerDHT> settableFuture = SettableFuture.create();
private final ObjectProperty<BootstrapState> bootstrapState = new SimpleObjectProperty<>();
private final ObjectProperty<State> state = new SimpleObjectProperty<>(State.UNDEFINED);
private final ObjectProperty<ConnectionType> connectionType = new SimpleObjectProperty<>(ConnectionType.UNDEFINED);
private Peer peer;
private PeerDHT peerDHT;
@ -175,7 +210,7 @@ public class BootstrappedPeerBuilder {
discoverExternalAddress();
} catch (IOException e) {
handleError(BootstrapState.PEER_CREATION_FAILED, "Cannot create a peer with port: " +
handleError(State.PEER_CREATION_FAILED, "Cannot create a peer with port: " +
port + ". Exception: " + e);
}
@ -202,7 +237,7 @@ public class BootstrappedPeerBuilder {
private void discoverExternalAddress() {
FutureDiscover futureDiscover = peer.discover().peerAddress(getBootstrapAddress()).start();
setState(BootstrapState.DISCOVERY_STARTED, "Starting discovery...");
setState(State.DISCOVERY_STARTED);
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover);
FutureRelayNAT futureRelayNAT = peerNAT.startRelay(new TCPRelayClientConfig(), futureDiscover, futureNAT);
@ -212,32 +247,35 @@ public class BootstrappedPeerBuilder {
public void operationComplete(BaseFuture futureRelayNAT) throws Exception {
if (futureDiscover.isSuccess()) {
if (useManualPortForwarding) {
setState(BootstrapState.DISCOVERY_MANUAL_PORT_FORWARDING_SUCCEEDED,
"Now visible to the Bitsquare network (with manual port forwarding).");
setState(State.DISCOVERY_MANUAL_PORT_FORWARDING_SUCCEEDED,
"NAT traversal successful with manual port forwarding.");
setConnectionType(ConnectionType.MANUAL_PORT_FORWARDING);
bootstrap();
}
else {
setState(BootstrapState.DISCOVERY_DIRECT_SUCCEEDED, "Now visible to the Bitsquare network.");
setState(State.DISCOVERY_DIRECT_SUCCEEDED, "Visible to the network. No NAT traversal needed.");
setConnectionType(ConnectionType.DIRECT);
bootstrap();
}
}
else {
setState(BootstrapState.DISCOVERY_AUTO_PORT_FORWARDING_STARTED,
"Configuring automatic port forwarding");
setState(State.DISCOVERY_AUTO_PORT_FORWARDING_STARTED);
if (futureNAT.isSuccess()) {
setState(BootstrapState.DISCOVERY_AUTO_PORT_FORWARDING_SUCCEEDED,
"Now visible to the Bitsquare network (with automatic port forwarding).");
setState(State.DISCOVERY_AUTO_PORT_FORWARDING_SUCCEEDED,
"NAT traversal successful with automatic port forwarding.");
setConnectionType(ConnectionType.AUTO_PORT_FORWARDING);
bootstrap();
}
else {
if (futureRelayNAT.isSuccess()) {
// relay mode succeeded
setState(BootstrapState.RELAY_SUCCEEDED, "Bootstrap using relay was successful.");
setState(State.RELAY_SUCCEEDED, "NAT traversal not successful. Using relay mode.");
setConnectionType(ConnectionType.RELAY);
bootstrap();
}
else {
// All attempts failed. Give up...
handleError(BootstrapState.RELAY_FAILED, "Bootstrap using relay has failed " +
handleError(State.RELAY_FAILED, "NAT traversal using relay mode failed " +
futureRelayNAT.failedReason());
}
}
@ -246,7 +284,7 @@ public class BootstrappedPeerBuilder {
@Override
public void exceptionCaught(Throwable t) throws Exception {
handleError(BootstrapState.RELAY_FAILED, "Exception at bootstrap: " + t.getMessage());
handleError(State.RELAY_FAILED, "Exception at bootstrap: " + t.getMessage());
}
});
}
@ -263,18 +301,18 @@ public class BootstrappedPeerBuilder {
public void operationComplete(BaseFuture future) throws Exception {
if (futureBootstrap.isSuccess()) {
log.trace("bootstrap complete");
setState(BootstrapState.BOOT_STRAP_SUCCEEDED, "Bootstrap using relay was successful.");
setState(State.BOOT_STRAP_SUCCEEDED, "Bootstrap was successful.");
settableFuture.set(peerDHT);
}
else {
handleError(BootstrapState.BOOT_STRAP_FAILED, "Bootstrapping failed. " +
handleError(State.BOOT_STRAP_FAILED, "Bootstrap failed. " +
futureBootstrap.failedReason());
}
}
@Override
public void exceptionCaught(Throwable t) throws Exception {
handleError(BootstrapState.BOOT_STRAP_FAILED, "Exception at bootstrap: " + t.getMessage());
handleError(State.BOOT_STRAP_FAILED, "Exception at bootstrap: " + t.getMessage());
}
});
}
@ -295,25 +333,41 @@ public class BootstrappedPeerBuilder {
return bootstrapNode;
}
public ObjectProperty<BootstrapState> getBootstrapState() {
return bootstrapState;
public ConnectionType getConnectionType() {
return connectionType.get();
}
private void setState(BootstrapState bootstrapState, String message) {
setState(bootstrapState, message, true);
public ReadOnlyObjectProperty<ConnectionType> connectionTypeProperty() {
return connectionType;
}
private void setState(BootstrapState bootstrapState, String message, boolean isSuccess) {
private void setConnectionType(ConnectionType discoveryState) {
this.connectionType.set(discoveryState);
}
public ObjectProperty<State> getState() {
return state;
}
private void setState(State state) {
setState(state, "", true);
}
private void setState(State state, String message) {
setState(state, message, true);
}
private void setState(State state, String message, boolean isSuccess) {
if (isSuccess)
log.info(message);
else
log.error(message);
bootstrapState.setMessage(message);
this.bootstrapState.set(bootstrapState);
state.setMessage(message);
this.state.set(state);
}
private void handleError(BootstrapState state, String errorMessage) {
private void handleError(State state, String errorMessage) {
setState(state, errorMessage, false);
peerDHT.shutdown();
settableFuture.setException(new Exception(errorMessage));

View file

@ -19,9 +19,7 @@ package io.bitsquare.p2p.tomp2p;
import io.bitsquare.BitsquareException;
import io.bitsquare.common.handlers.ResultHandler;
import io.bitsquare.p2p.BootstrapState;
import io.bitsquare.p2p.ClientNode;
import io.bitsquare.p2p.ConnectionType;
import io.bitsquare.p2p.Node;
import com.google.common.util.concurrent.FutureCallback;
@ -54,7 +52,7 @@ public class TomP2PNode implements ClientNode {
private PeerDHT peerDHT;
private BootstrappedPeerBuilder bootstrappedPeerBuilder;
private final Subject<BootstrapState, BootstrapState> bootstrapStateSubject;
private final Subject<BootstrappedPeerBuilder.State, BootstrappedPeerBuilder.State> bootstrapStateSubject;
private final List<ResultHandler> resultHandlers = new CopyOnWriteArrayList<>();
@ -80,10 +78,10 @@ public class TomP2PNode implements ClientNode {
// Public methods
///////////////////////////////////////////////////////////////////////////////////////////
public Observable<BootstrapState> bootstrap(KeyPair keyPair) {
public Observable<BootstrappedPeerBuilder.State> bootstrap(KeyPair keyPair) {
bootstrappedPeerBuilder.setKeyPair(keyPair);
bootstrappedPeerBuilder.getBootstrapState().addListener((ov, oldValue, newValue) -> {
bootstrappedPeerBuilder.getState().addListener((ov, oldValue, newValue) -> {
log.debug("BootstrapState changed " + newValue);
bootstrapStateSubject.onNext(newValue);
});
@ -118,20 +116,8 @@ public class TomP2PNode implements ClientNode {
}
@Override
public ConnectionType getConnectionType() {
BootstrapState bootstrapState = bootstrappedPeerBuilder.getBootstrapState().get();
switch (bootstrapState) {
case DISCOVERY_DIRECT_SUCCEEDED:
return ConnectionType.DIRECT;
case DISCOVERY_MANUAL_PORT_FORWARDING_SUCCEEDED:
return ConnectionType.MANUAL_PORT_FORWARDING;
case DISCOVERY_AUTO_PORT_FORWARDING_SUCCEEDED:
return ConnectionType.AUTO_PORT_FORWARDING;
case RELAY_SUCCEEDED:
return ConnectionType.RELAY;
default:
throw new BitsquareException("Invalid bootstrap state: %s", bootstrapState);
}
public BootstrappedPeerBuilder.ConnectionType getConnectionType() {
return bootstrappedPeerBuilder.getConnectionType();
}
@Override

View file

@ -18,8 +18,8 @@
package io.bitsquare.msg;
import io.bitsquare.p2p.BootstrapNodes;
import io.bitsquare.p2p.ConnectionType;
import io.bitsquare.p2p.Node;
import io.bitsquare.p2p.tomp2p.BootstrappedPeerBuilder;
import io.bitsquare.util.Repeat;
import io.bitsquare.util.RepeatRule;
@ -84,11 +84,11 @@ public class TomP2PTests {
private static final Logger log = LoggerFactory.getLogger(TomP2PTests.class);
// If you want to test in one specific connection mode define it directly, otherwise use UNKNOWN
private static final ConnectionType FORCED_CONNECTION_TYPE = ConnectionType.RELAY;
private static final BootstrappedPeerBuilder.ConnectionType FORCED_CONNECTION_TYPE = BootstrappedPeerBuilder.ConnectionType.RELAY;
// Typically you run the bootstrap node in localhost to test direct connection.
// If you have a setup where you are not behind a router you can also use a WAN bootstrap node.
private static final Node BOOTSTRAP_NODE = (FORCED_CONNECTION_TYPE == ConnectionType.DIRECT) ?
private static final Node BOOTSTRAP_NODE = (FORCED_CONNECTION_TYPE == BootstrappedPeerBuilder.ConnectionType.DIRECT) ?
BootstrapNodes.LOCALHOST : Node.at("digitalocean1.dev.bitsquare.io", "188.226.179.109", 7367);
private static final PeerAddress BOOTSTRAP_NODE_ADDRESS;
@ -111,7 +111,7 @@ public class TomP2PTests {
private PeerDHT peer2DHT;
private int client1Port;
private int client2Port;
private ConnectionType resolvedConnectionType;
private BootstrappedPeerBuilder.ConnectionType resolvedConnectionType;
public @Rule RepeatRule repeatRule = new RepeatRule();
@Before
@ -135,7 +135,7 @@ public class TomP2PTests {
@Test
@Repeat(STRESS_TEST_COUNT)
public void bootstrapInUnknownMode() throws Exception {
if (FORCED_CONNECTION_TYPE == ConnectionType.UNKNOWN) {
if (FORCED_CONNECTION_TYPE == BootstrappedPeerBuilder.ConnectionType.UNDEFINED) {
peer = bootstrapInUnknownMode(client1Port);
assertNotNull(peer);
}
@ -144,7 +144,7 @@ public class TomP2PTests {
@Test
@Repeat(STRESS_TEST_COUNT)
public void testBootstrapDirectConnection() throws Exception {
if (FORCED_CONNECTION_TYPE == ConnectionType.DIRECT) {
if (FORCED_CONNECTION_TYPE == BootstrappedPeerBuilder.ConnectionType.DIRECT) {
peer = bootstrapDirectConnection(client1Port);
assertNotNull(peer);
}
@ -153,8 +153,8 @@ public class TomP2PTests {
@Test
@Repeat(STRESS_TEST_COUNT)
public void testBootstrapWithPortForwarding() throws Exception {
if (FORCED_CONNECTION_TYPE == ConnectionType.AUTO_PORT_FORWARDING ||
FORCED_CONNECTION_TYPE == ConnectionType.MANUAL_PORT_FORWARDING) {
if (FORCED_CONNECTION_TYPE == BootstrappedPeerBuilder.ConnectionType.AUTO_PORT_FORWARDING ||
FORCED_CONNECTION_TYPE == BootstrappedPeerBuilder.ConnectionType.MANUAL_PORT_FORWARDING) {
peer = bootstrapWithPortForwarding(client2Port);
assertNotNull(peer);
}
@ -163,7 +163,7 @@ public class TomP2PTests {
@Test
@Repeat(STRESS_TEST_COUNT)
public void testBootstrapInRelayMode() throws Exception {
if (FORCED_CONNECTION_TYPE == ConnectionType.RELAY) {
if (FORCED_CONNECTION_TYPE == BootstrappedPeerBuilder.ConnectionType.RELAY) {
peer = bootstrapInRelayMode(client1Port);
assertNotNull(peer);
}
@ -498,7 +498,8 @@ public class TomP2PTests {
@Test
@Repeat(STRESS_TEST_COUNT)
public void testSendDirectBetweenLocalPeers() throws Exception {
if (FORCED_CONNECTION_TYPE == ConnectionType.DIRECT || resolvedConnectionType == ConnectionType.DIRECT) {
if (FORCED_CONNECTION_TYPE == BootstrappedPeerBuilder.ConnectionType.DIRECT || resolvedConnectionType == BootstrappedPeerBuilder.ConnectionType
.DIRECT) {
peer1DHT = getDHTPeer(client1Port);
peer2DHT = getDHTPeer(client2Port);
@ -594,8 +595,8 @@ public class TomP2PTests {
Number160 peerId = Number160.createHash(UUID.randomUUID().toString());
Peer peer = null;
try {
if (FORCED_CONNECTION_TYPE == ConnectionType.MANUAL_PORT_FORWARDING ||
resolvedConnectionType == ConnectionType.MANUAL_PORT_FORWARDING) {
if (FORCED_CONNECTION_TYPE == BootstrappedPeerBuilder.ConnectionType.MANUAL_PORT_FORWARDING ||
resolvedConnectionType == BootstrappedPeerBuilder.ConnectionType.MANUAL_PORT_FORWARDING) {
peer = new PeerBuilder(peerId).bindings(getBindings())
.behindFirewall()
.tcpPortForwarding(clientPort)
@ -700,22 +701,22 @@ public class TomP2PTests {
}
private Peer bootstrapInUnknownMode(int clientPort) {
resolvedConnectionType = ConnectionType.DIRECT;
resolvedConnectionType = BootstrappedPeerBuilder.ConnectionType.DIRECT;
Peer peer = bootstrapDirectConnection(clientPort);
if (peer != null)
return peer;
resolvedConnectionType = ConnectionType.MANUAL_PORT_FORWARDING;
resolvedConnectionType = BootstrappedPeerBuilder.ConnectionType.MANUAL_PORT_FORWARDING;
peer = bootstrapWithPortForwarding(clientPort);
if (peer != null)
return peer;
resolvedConnectionType = ConnectionType.AUTO_PORT_FORWARDING;
resolvedConnectionType = BootstrappedPeerBuilder.ConnectionType.AUTO_PORT_FORWARDING;
peer = bootstrapWithPortForwarding(clientPort);
if (peer != null)
return peer;
resolvedConnectionType = ConnectionType.RELAY;
resolvedConnectionType = BootstrappedPeerBuilder.ConnectionType.RELAY;
peer = bootstrapInRelayMode(clientPort);
if (peer != null)
return peer;
@ -728,13 +729,13 @@ public class TomP2PTests {
private PeerDHT getDHTPeer(int clientPort) {
Peer peer;
if (FORCED_CONNECTION_TYPE == ConnectionType.DIRECT) {
if (FORCED_CONNECTION_TYPE == BootstrappedPeerBuilder.ConnectionType.DIRECT) {
peer = bootstrapDirectConnection(clientPort);
}
else if (FORCED_CONNECTION_TYPE == ConnectionType.AUTO_PORT_FORWARDING) {
else if (FORCED_CONNECTION_TYPE == BootstrappedPeerBuilder.ConnectionType.AUTO_PORT_FORWARDING) {
peer = bootstrapWithPortForwarding(clientPort);
}
else if (FORCED_CONNECTION_TYPE == ConnectionType.RELAY) {
else if (FORCED_CONNECTION_TYPE == BootstrappedPeerBuilder.ConnectionType.RELAY) {
peer = bootstrapInRelayMode(clientPort);
}
else {