diff --git a/src/main/java/io/bitsquare/SeedNode.java b/src/main/java/io/bitsquare/SeedNode.java index eb035c5001..8ebd285ff3 100644 --- a/src/main/java/io/bitsquare/SeedNode.java +++ b/src/main/java/io/bitsquare/SeedNode.java @@ -1,6 +1,7 @@ package io.bitsquare; import io.bitsquare.msg.SeedNodeAddress; +import java.io.IOException; import java.util.List; import net.tomp2p.dht.PeerBuilderDHT; import net.tomp2p.futures.BaseFuture; @@ -25,20 +26,17 @@ import org.slf4j.LoggerFactory; *

* TODO: Alternative bootstrap methods will follow later (save locally list of known nodes reported form other peers,...) */ -public class SeedNode +public class SeedNode extends Thread { private static final Logger log = LoggerFactory.getLogger(SeedNode.class); - private static final List staticSedNodeAddresses = SeedNodeAddress.StaticSeedNodeAddresses.getAllSeedNodeAddresses(); /** * @param args If no args passed we use localhost, otherwise the param is used as index for selecting an address from seedNodeAddresses - * @throws Exception */ public static void main(String[] args) { int index = 0; - SeedNode seedNode = new SeedNode(); if (args.length > 0) { // use host index passes as param @@ -46,24 +44,32 @@ public class SeedNode if (param < staticSedNodeAddresses.size()) index = param; } + + SeedNode seedNode = new SeedNode(new SeedNodeAddress(staticSedNodeAddresses.get(index))); + seedNode.setDaemon(true); + seedNode.start(); + try { - seedNode.startupUsingAddress(new SeedNodeAddress(staticSedNodeAddresses.get(index))); - } catch (Exception e) + // keep main thread up + Thread.sleep(Long.MAX_VALUE); + } catch (InterruptedException e) { - e.printStackTrace(); log.error(e.toString()); } } + private final SeedNodeAddress seedNodeAddress; + + /////////////////////////////////////////////////////////////////////////////////////////// // Constructor /////////////////////////////////////////////////////////////////////////////////////////// - - public SeedNode() + public SeedNode(SeedNodeAddress seedNodeAddress) { + this.seedNodeAddress = seedNodeAddress; } @@ -71,11 +77,30 @@ public class SeedNode // Public Methods /////////////////////////////////////////////////////////////////////////////////////////// - public void startupUsingAddress(SeedNodeAddress seedNodeAddress) + public void run() { + Peer peer = startupPeer(); + + for (; ; ) + { + try + { + // ping(peer); + + Thread.sleep(300); + } catch (InterruptedException e) + { + log.error(e.toString()); + } + } + } + + public Peer startupPeer() + { + Peer peer = null; try { - Peer peer = new PeerBuilder(Number160.createHash(seedNodeAddress.getId())).ports(seedNodeAddress.getPort()).start(); + peer = new PeerBuilder(Number160.createHash(seedNodeAddress.getId())).ports(seedNodeAddress.getPort()).start(); // Need to add all features the clients will use (otherwise msg type is UNKNOWN_ID) new PeerBuilderDHT(peer).start(); @@ -106,64 +131,69 @@ public class SeedNode log.debug("Peer updated: peerAddress=" + peerAddress + ", peerStatistics=" + peerStatistics); } }); + } catch (IOException e) + { + e.printStackTrace(); + } + return peer; + } - // We keep server in endless loop - for (; ; ) + private void ping(Peer peer) + { + if (peer != null) + return; + + try + { + // Optional pinging + for (PeerAddress peerAddress : peer.peerBean().peerMap().all()) { - // Optional pinging - boolean pingPeers = false; - if (pingPeers) + BaseFuture future = peer.ping().peerAddress(peerAddress).tcpPing().start(); + future.addListener(new BaseFutureListener() { - for (PeerAddress peerAddress : peer.peerBean().peerMap().all()) + @Override + public void operationComplete(BaseFuture future) throws Exception { - BaseFuture future = peer.ping().peerAddress(peerAddress).tcpPing().start(); - future.addListener(new BaseFutureListener() + if (future.isSuccess()) { - @Override - public void operationComplete(BaseFuture future) throws Exception - { - if (future.isSuccess()) - { - log.debug("peer online (TCP):" + peerAddress); - } - else - { - log.debug("offline " + peerAddress); - } - } - - @Override - public void exceptionCaught(Throwable t) throws Exception - { - log.error("exceptionCaught " + t); - } - }); - - future = peer.ping().peerAddress(peerAddress).start(); - future.addListener(new BaseFutureListener() + log.debug("peer online (TCP):" + peerAddress); + } + else { - @Override - public void operationComplete(BaseFuture future) throws Exception - { - if (future.isSuccess()) - { - log.debug("peer online (UDP):" + peerAddress); - } - else - { - log.debug("offline " + peerAddress); - } - } - - @Override - public void exceptionCaught(Throwable t) throws Exception - { - log.error("exceptionCaught " + t); - } - }); + log.debug("offline " + peerAddress); + } } - Thread.sleep(1500); - } + + @Override + public void exceptionCaught(Throwable t) throws Exception + { + log.error("exceptionCaught " + t); + } + }); + + future = peer.ping().peerAddress(peerAddress).start(); + future.addListener(new BaseFutureListener() + { + @Override + public void operationComplete(BaseFuture future) throws Exception + { + if (future.isSuccess()) + { + log.debug("peer online (UDP):" + peerAddress); + } + else + { + log.debug("offline " + peerAddress); + } + } + + @Override + public void exceptionCaught(Throwable t) throws Exception + { + log.error("exceptionCaught " + t); + } + }); + Thread.sleep(1500); } } catch (Exception e) { diff --git a/src/main/java/io/bitsquare/di/GuiceControllerFactory.java b/src/main/java/io/bitsquare/di/GuiceControllerFactory.java deleted file mode 100644 index dda883f700..0000000000 --- a/src/main/java/io/bitsquare/di/GuiceControllerFactory.java +++ /dev/null @@ -1,29 +0,0 @@ -package io.bitsquare.di; - -import com.google.inject.Injector; -import javafx.util.Callback; - -/** - * A JavaFX controller factory for constructing controllers via Guice DI. To - * install this in the {@link javafx.fxml.FXMLLoader}, pass it as a parameter to - * {@link javafx.fxml.FXMLLoader#setControllerFactory(Callback)}. - *

- * Once set, make sure you do not use the static methods on - * {@link javafx.fxml.FXMLLoader} when creating your JavaFX node. - */ -class GuiceControllerFactory implements Callback, Object> -{ - - private final Injector injector; - - public GuiceControllerFactory(Injector injector) - { - this.injector = injector; - } - - @Override - public Object call(Class aClass) - { - return injector.getInstance(aClass); - } -} diff --git a/src/main/java/io/bitsquare/di/GuiceFXMLLoader.java b/src/main/java/io/bitsquare/di/GuiceFXMLLoader.java index 3fb216331c..7bb7fb9786 100644 --- a/src/main/java/io/bitsquare/di/GuiceFXMLLoader.java +++ b/src/main/java/io/bitsquare/di/GuiceFXMLLoader.java @@ -4,6 +4,7 @@ import com.google.inject.Injector; import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXMLLoader; +import javafx.util.Callback; /** * Guice support for fxml controllers @@ -16,10 +17,12 @@ public class GuiceFXMLLoader extends FXMLLoader { GuiceFXMLLoader.injector = injector; } - + // private static ClassLoader cachingClassLoader = new CachingClassLoader(FXMLLoader.getDefaultClassLoader()); public GuiceFXMLLoader(URL url, ResourceBundle resourceBundle) { super(url, resourceBundle); + // might be helpful for performance, but need further profiling. has memory drawbacks + //setClassLoader(cachingClassLoader); setupControllerFactory(); } @@ -30,5 +33,30 @@ public class GuiceFXMLLoader extends FXMLLoader setControllerFactory(new GuiceControllerFactory(GuiceFXMLLoader.injector)); } } - } + +/** + * A JavaFX controller factory for constructing controllers via Guice DI. To + * install this in the {@link javafx.fxml.FXMLLoader}, pass it as a parameter to + * {@link javafx.fxml.FXMLLoader#setControllerFactory(javafx.util.Callback)}. + *

+ * Once set, make sure you do not use the static methods on + * {@link javafx.fxml.FXMLLoader} when creating your JavaFX node. + */ +class GuiceControllerFactory implements Callback, Object> +{ + + private final Injector injector; + + public GuiceControllerFactory(Injector injector) + { + this.injector = injector; + } + + @Override + public Object call(Class aClass) + { + return injector.getInstance(aClass); + } +} + diff --git a/src/main/java/io/bitsquare/gui/components/LazyLoadingTabPane.java b/src/main/java/io/bitsquare/gui/components/LazyLoadingTabPane.java index 12f9377262..273faad488 100644 --- a/src/main/java/io/bitsquare/gui/components/LazyLoadingTabPane.java +++ b/src/main/java/io/bitsquare/gui/components/LazyLoadingTabPane.java @@ -45,7 +45,6 @@ public class LazyLoadingTabPane extends TabPane throw new IllegalArgumentException("No tabContentFXMLUrls defined"); } - this.tabContentFXMLUrls = tabContentFXMLUrls; this.navigationController = navigationController; this.tabContentFXMLUrls = tabContentFXMLUrls; this.persistence = persistence; @@ -117,6 +116,7 @@ public class LazyLoadingTabPane extends TabPane try { view = loader.load(); + log.debug("######## " + view.toString()); views.put(index, view); childController = loader.getController(); diff --git a/src/main/java/io/bitsquare/gui/market/createOffer/CreateOfferController.java b/src/main/java/io/bitsquare/gui/market/createOffer/CreateOfferController.java index 7348f6d11b..618629a750 100644 --- a/src/main/java/io/bitsquare/gui/market/createOffer/CreateOfferController.java +++ b/src/main/java/io/bitsquare/gui/market/createOffer/CreateOfferController.java @@ -57,14 +57,12 @@ class ViewModel final StringProperty directionLabel = new SimpleStringProperty(); final StringProperty collateralLabel = new SimpleStringProperty(); final StringProperty feeLabel = new SimpleStringProperty(); - final StringProperty bankAccountType = new SimpleStringProperty(); final StringProperty bankAccountCurrency = new SimpleStringProperty(); final StringProperty bankAccountCounty = new SimpleStringProperty(); final StringProperty acceptedCountries = new SimpleStringProperty(); final StringProperty acceptedLanguages = new SimpleStringProperty(); final StringProperty transactionId = new SimpleStringProperty(); - final BooleanProperty isOfferPlacedScreen = new SimpleBooleanProperty(); final BooleanProperty isPlaceOfferButtonDisabled = new SimpleBooleanProperty(); } @@ -149,7 +147,6 @@ public class CreateOfferController implements Initializable, ChildController, Hi // Interface implementation: Initializable /////////////////////////////////////////////////////////////////////////////////////////// - @Override public void initialize(URL url, ResourceBundle rb) { @@ -232,6 +229,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi /////////////////////////////////////////////////////////////////////////////////////////// // UI Handlers /////////////////////////////////////////////////////////////////////////////////////////// + @FXML public void onPlaceOffer() { diff --git a/src/main/java/io/bitsquare/util/CachingClassLoader.java b/src/main/java/io/bitsquare/util/CachingClassLoader.java new file mode 100644 index 0000000000..ac6e3bdc24 --- /dev/null +++ b/src/main/java/io/bitsquare/util/CachingClassLoader.java @@ -0,0 +1,98 @@ +package io.bitsquare.util; + +import java.io.IOException; +import java.net.URL; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +public class CachingClassLoader extends ClassLoader +{ + private final Map classes = new HashMap(); + private final ClassLoader parent; + + public CachingClassLoader(ClassLoader parent) + { + this.parent = parent; + } + + @Override + public Class loadClass(String name) throws ClassNotFoundException + { + Class c = findClass(name); + if (c == null) + { + throw new ClassNotFoundException(name); + } + return c; + } + + @Override + protected Class findClass(String className) throws ClassNotFoundException + { + if (classes.containsKey(className)) + { + // System.out.print("############## cached " + className); + Class result = classes.get(className); + return result; + } + else + { + try + { + Class result = parent.loadClass(className); + System.out.print("############## not cached " + className); + classes.put(className, result); + return result; + } catch (ClassNotFoundException ignore) + { + // System.out.println(); + classes.put(className, null); + return null; + } + } + } + + // ========= delegating methods ============= + @Override + public URL getResource(String name) + { + return parent.getResource(name); + } + + @Override + public Enumeration getResources(String name) throws IOException + { + return parent.getResources(name); + } + + @Override + public String toString() + { + return parent.toString(); + } + + @Override + public void setDefaultAssertionStatus(boolean enabled) + { + parent.setDefaultAssertionStatus(enabled); + } + + @Override + public void setPackageAssertionStatus(String packageName, boolean enabled) + { + parent.setPackageAssertionStatus(packageName, enabled); + } + + @Override + public void setClassAssertionStatus(String className, boolean enabled) + { + parent.setClassAssertionStatus(className, enabled); + } + + @Override + public void clearAssertionStatus() + { + parent.clearAssertionStatus(); + } +} \ No newline at end of file