From c7f7b3757240b1017227c01856da1dfebf2c42dc Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Wed, 12 Nov 2014 18:39:55 +0100 Subject: [PATCH 1/5] Expose network information to GUI cleanly This commit introduces io.bitsquare.network.ClientNode--an interface whose name and structure will surely change--as a simplistic abstraction over TomP2PNode that allows for exposing information to the "Network" tab of the Preferences section of the GUI without actually requiring the injection of TomP2PNode and other tomp2p internals into the GUI layer. Changes to 'network' and 'msg' packages: ---------------------------------------- - Move ConnectionType enum from test into main tree, and expose ClientNode#getConnectionType. - Both ClientNode and TomP2P are now available for injection. Both types are bound to the same TomP2P singleton instance. Note especially how NetworkPreferencesViewCB now receives a ClientNode instead of a TomP2PNode. - Restore package-private visibility to BootstrappedPeerFactory - Remove no longer necessary TomP2PNode#getPeerDHT - Expose getter for BootstrappedPeerFactory#bootstrapState Changes to 'gui' package: ------------------------- - NetworkPreferencesViewCB has been simplified. All no-op methods have been removed, and the class now simply implements JavaFX's Initializable interface as opposed to Bitsquare's own ViewCB hierarchy, because the latter is not actually necessary (no caching is required for the data handled by this controller, etc. - In order to make the above possible, PreferencesViewCB now tolerates adding non-ViewCB child controllers. - NetworkPreferencesPM has been removed (perhaps temporarily), in an experiment to see "just how simple" CB controller classes can be. - Text fields in NetworkPreferencesView have been renamed. Notes: ------ The data that now shows up in the "Network" tab is no longer formatted as it once was; values are essentially nothing more than their #toString representations. Again, this can be tweaked further, but leaving things in this raw state provides an opportunity to discuss the current presentation model approach, ViewCB hierarchy, etc. --- .../main/preferences/PreferencesViewCB.java | 3 +- .../network/NetworkPreferencesPM.java | 136 ------------------ .../network/NetworkPreferencesView.fxml | 8 +- .../network/NetworkPreferencesViewCB.java | 62 ++------ .../msg/tomp2p/BootstrappedPeerFactory.java | 15 +- .../msg/tomp2p/TomP2PMessageModule.java | 6 +- .../io/bitsquare/msg/tomp2p/TomP2PNode.java | 42 +++++- .../java/io/bitsquare/network/ClientNode.java | 26 ++++ .../io/bitsquare/network/ConnectionType.java | 22 +++ .../java/io/bitsquare/msg/TomP2PTests.java | 5 +- 10 files changed, 123 insertions(+), 202 deletions(-) delete mode 100644 src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesPM.java create mode 100644 src/main/java/io/bitsquare/network/ClientNode.java create mode 100644 src/main/java/io/bitsquare/network/ConnectionType.java diff --git a/src/main/java/io/bitsquare/gui/main/preferences/PreferencesViewCB.java b/src/main/java/io/bitsquare/gui/main/preferences/PreferencesViewCB.java index c16d48e61b..86316b84b7 100644 --- a/src/main/java/io/bitsquare/gui/main/preferences/PreferencesViewCB.java +++ b/src/main/java/io/bitsquare/gui/main/preferences/PreferencesViewCB.java @@ -141,7 +141,8 @@ public class PreferencesViewCB extends CachedViewCB { tab.setContent(view); ((TabPane) root).getSelectionModel().select(tab); Initializable childController = loader.getController(); - ((ViewCB) childController).setParent(this); + if (childController instanceof ViewCB) + ((ViewCB) childController).setParent(this); return childController; } diff --git a/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesPM.java b/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesPM.java deleted file mode 100644 index e0625e61ef..0000000000 --- a/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesPM.java +++ /dev/null @@ -1,136 +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 . - */ - -package io.bitsquare.gui.main.preferences.network; - -import io.bitsquare.BitsquareException; -import io.bitsquare.gui.PresentationModel; -import io.bitsquare.msg.tomp2p.BootstrappedPeerFactory; -import io.bitsquare.msg.tomp2p.TomP2PNode; -import io.bitsquare.network.BootstrapState; -import io.bitsquare.network.Node; - -import org.bitcoinj.core.NetworkParameters; - -import com.google.inject.Inject; -import com.google.inject.name.Named; - -import net.tomp2p.peers.PeerSocketAddress; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class NetworkPreferencesPM extends PresentationModel { - private static final Logger log = LoggerFactory.getLogger(NetworkPreferencesPM.class); - - final String bitcoinNetworkType; - final String p2pNetworkConnection; - final String p2pNetworkAddress; - final String bootstrapAddress; - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Constructor - /////////////////////////////////////////////////////////////////////////////////////////// - - @Inject - NetworkPreferencesPM(NetworkParameters networkParameters, - BootstrappedPeerFactory bootstrappedPeerFactory, - TomP2PNode tomP2PNode, - @Named(BootstrappedPeerFactory.BOOTSTRAP_NODE_KEY) Node bootstrapNode) { - - switch (networkParameters.getId()) { - case NetworkParameters.ID_REGTEST: - bitcoinNetworkType = "Regtest"; - break; - case NetworkParameters.ID_TESTNET: - bitcoinNetworkType = "Testnet"; - break; - case NetworkParameters.ID_MAINNET: - bitcoinNetworkType = "Mainnet"; - break; - default: - bitcoinNetworkType = "Undefined"; - throw new BitsquareException("Invalid networkParameters " + networkParameters.getId()); - } - - PeerSocketAddress socketAddress = tomP2PNode.getPeerDHT().peerAddress().peerSocketAddress(); - p2pNetworkAddress = "IP: " + socketAddress.inetAddress().getHostAddress() - + ", TCP port: " + socketAddress.tcpPort() - + ", UDP port: " + socketAddress.udpPort(); - - bootstrapAddress = "ID: " + bootstrapNode.getName() - + ", IP: " + bootstrapNode.getIp() - + ", Port: " + bootstrapNode.getPortAsString(); - - BootstrapState state = bootstrappedPeerFactory.bootstrapState.get(); - if (state == BootstrapState.DIRECT_SUCCESS) - p2pNetworkConnection = "Direct connection"; - else if (state == BootstrapState.NAT_SUCCESS) - p2pNetworkConnection = "Connected with automatic port forwarding"; - else if (state == BootstrapState.RELAY_SUCCESS) - p2pNetworkConnection = "Relayed by other peers"; - else - throw new BitsquareException("Invalid BootstrapState " + state); - } - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Lifecycle - /////////////////////////////////////////////////////////////////////////////////////////// - - @SuppressWarnings("EmptyMethod") - @Override - public void initialize() { - super.initialize(); - } - - @SuppressWarnings("EmptyMethod") - @Override - public void activate() { - super.activate(); - } - - @SuppressWarnings("EmptyMethod") - @Override - public void deactivate() { - super.deactivate(); - } - - @SuppressWarnings("EmptyMethod") - @Override - public void terminate() { - super.terminate(); - } - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Methods - /////////////////////////////////////////////////////////////////////////////////////////// - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Getters - /////////////////////////////////////////////////////////////////////////////////////////// - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Private - /////////////////////////////////////////////////////////////////////////////////////////// - - -} diff --git a/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesView.fxml b/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesView.fxml index 2f8dd60c8b..e9a326e0dd 100644 --- a/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesView.fxml +++ b/src/main/java/io/bitsquare/gui/main/preferences/network/NetworkPreferencesView.fxml @@ -38,7 +38,7 @@ - @@ -46,11 +46,11 @@