diff --git a/src/main/java/io/bitsquare/btc/BitcoinModule.java b/src/main/java/io/bitsquare/btc/BitcoinModule.java new file mode 100644 index 0000000000..9997669378 --- /dev/null +++ b/src/main/java/io/bitsquare/btc/BitcoinModule.java @@ -0,0 +1,67 @@ +/* + * 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.btc; + +import org.bitcoinj.core.NetworkParameters; +import org.bitcoinj.params.MainNetParams; +import org.bitcoinj.params.RegTestParams; +import org.bitcoinj.params.TestNet3Params; + +import com.google.inject.AbstractModule; + +import java.util.Properties; + +public class BitcoinModule extends AbstractModule { + + private final Properties properties; + private final BitcoinNetwork defaultNetwork; + + public BitcoinModule(Properties properties) { + this(properties, BitcoinNetwork.TESTNET); + } + + public BitcoinModule(Properties properties, BitcoinNetwork defaultNetwork) { + this.properties = properties; + this.defaultNetwork = defaultNetwork; + } + + @Override + protected void configure() { + bind(WalletFacade.class).asEagerSingleton(); + bind(FeePolicy.class).asEagerSingleton(); + bind(BlockChainFacade.class).asEagerSingleton(); + bind(NetworkParameters.class).toInstance(network()); + } + + private NetworkParameters network() { + String networkName = properties.getProperty("networkType", defaultNetwork.name()); + + switch (BitcoinNetwork.valueOf(networkName.toUpperCase())) { + case MAINNET: + return MainNetParams.get(); + case TESTNET: + return TestNet3Params.get(); + case REGTEST: + return RegTestParams.get(); + default: + throw new IllegalArgumentException("Unknown bitcoin network name: " + networkName); + } + } + +} + diff --git a/src/main/java/io/bitsquare/btc/BitcoinNetwork.java b/src/main/java/io/bitsquare/btc/BitcoinNetwork.java new file mode 100644 index 0000000000..d0a1fa2007 --- /dev/null +++ b/src/main/java/io/bitsquare/btc/BitcoinNetwork.java @@ -0,0 +1,22 @@ +/* + * 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.btc; + +public enum BitcoinNetwork { + MAINNET, TESTNET, REGTEST; +} diff --git a/src/main/java/io/bitsquare/btc/WalletFacade.java b/src/main/java/io/bitsquare/btc/WalletFacade.java index 95cda59203..b7af16e3c1 100644 --- a/src/main/java/io/bitsquare/btc/WalletFacade.java +++ b/src/main/java/io/bitsquare/btc/WalletFacade.java @@ -92,9 +92,6 @@ import static org.bitcoinj.script.ScriptOpCodes.OP_RETURN; public class WalletFacade { private static final Logger log = LoggerFactory.getLogger(WalletFacade.class); - public static final String MAIN_NET = "mainnet"; - public static final String TEST_NET = "testnet"; - public static final String REG_TEST_NET = "regtest"; public static final String WALLET_PREFIX = Bitsquare.getAppName(); private final ReentrantLock lock = Threading.lock("lock"); diff --git a/src/main/java/io/bitsquare/crypto/CryptoModule.java b/src/main/java/io/bitsquare/crypto/CryptoModule.java new file mode 100644 index 0000000000..84452ba2a4 --- /dev/null +++ b/src/main/java/io/bitsquare/crypto/CryptoModule.java @@ -0,0 +1,34 @@ +/* + * 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.crypto; + +import io.bitsquare.di.AbstractBitsquareModule; + +import java.util.Properties; + +public class CryptoModule extends AbstractBitsquareModule { + + public CryptoModule(Properties properties) { + super(properties); + } + + @Override + protected void configure() { + bind(CryptoFacade.class).asEagerSingleton(); + } +} diff --git a/src/main/java/io/bitsquare/di/AbstractBitsquareModule.java b/src/main/java/io/bitsquare/di/AbstractBitsquareModule.java new file mode 100644 index 0000000000..74d8f34219 --- /dev/null +++ b/src/main/java/io/bitsquare/di/AbstractBitsquareModule.java @@ -0,0 +1,31 @@ +/* + * 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.di; + +import com.google.inject.AbstractModule; + +import java.util.Properties; + +public abstract class AbstractBitsquareModule extends AbstractModule { + + protected final Properties properties; + + public AbstractBitsquareModule(Properties properties) { + this.properties = properties; + } +} diff --git a/src/main/java/io/bitsquare/di/BitsquareModule.java b/src/main/java/io/bitsquare/di/BitsquareModule.java index dfb675e4a5..7a0a926eef 100644 --- a/src/main/java/io/bitsquare/di/BitsquareModule.java +++ b/src/main/java/io/bitsquare/di/BitsquareModule.java @@ -17,53 +17,31 @@ package io.bitsquare.di; - import io.bitsquare.Bitsquare; -import io.bitsquare.btc.BlockChainFacade; -import io.bitsquare.btc.FeePolicy; -import io.bitsquare.btc.WalletFacade; -import io.bitsquare.crypto.CryptoFacade; -import io.bitsquare.gui.Navigation; -import io.bitsquare.gui.OverlayManager; -import io.bitsquare.gui.main.trade.offerbook.OfferBook; -import io.bitsquare.gui.util.BSFormatter; -import io.bitsquare.gui.util.validation.BankAccountNumberValidator; -import io.bitsquare.gui.util.validation.BtcValidator; -import io.bitsquare.gui.util.validation.FiatValidator; -import io.bitsquare.gui.util.validation.InputValidator; -import io.bitsquare.gui.util.validation.PasswordValidator; -import io.bitsquare.msg.BootstrappedPeerFactory; -import io.bitsquare.msg.DHTSeedService; -import io.bitsquare.msg.MessageFacade; -import io.bitsquare.msg.P2PNode; -import io.bitsquare.msg.SeedNodeAddress; -import io.bitsquare.msg.TomP2PMessageFacade; +import io.bitsquare.btc.BitcoinModule; +import io.bitsquare.crypto.CryptoModule; +import io.bitsquare.gui.GuiModule; +import io.bitsquare.msg.DefaultMessageModule; +import io.bitsquare.msg.MessageModule; import io.bitsquare.persistence.Persistence; import io.bitsquare.settings.Settings; -import io.bitsquare.trade.TradeManager; +import io.bitsquare.trade.TradeModule; import io.bitsquare.user.User; import io.bitsquare.util.ConfigLoader; -import org.bitcoinj.core.NetworkParameters; -import org.bitcoinj.params.MainNetParams; -import org.bitcoinj.params.RegTestParams; -import org.bitcoinj.params.TestNet3Params; - -import com.google.inject.AbstractModule; -import com.google.inject.Provider; -import com.google.inject.name.Names; - import java.util.Properties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import akka.actor.ActorSystem; -public class BitsquareModule extends AbstractModule { - private static final Logger log = LoggerFactory.getLogger(BitsquareModule.class); +public class BitsquareModule extends AbstractBitsquareModule { - static Properties properties; + public BitsquareModule() { + this(ConfigLoader.loadConfig()); + } + + public BitsquareModule(Properties properties) { + super(properties); + } @Override protected void configure() { @@ -71,112 +49,33 @@ public class BitsquareModule extends AbstractModule { bind(Persistence.class).asEagerSingleton(); bind(Settings.class).asEagerSingleton(); - bind(CryptoFacade.class).asEagerSingleton(); - bind(WalletFacade.class).asEagerSingleton(); - bind(FeePolicy.class).asEagerSingleton(); + install(messageModule()); + install(bitcoinModule()); + install(cryptoModule()); + install(tradeModule()); + install(guiModule()); - bind(BlockChainFacade.class).asEagerSingleton(); - bind(MessageFacade.class).to(TomP2PMessageFacade.class).asEagerSingleton(); - bind(P2PNode.class).asEagerSingleton(); - bind(BootstrappedPeerFactory.class).asEagerSingleton(); + bind(ActorSystem.class).toInstance(ActorSystem.create(Bitsquare.getAppName())); + } - bind(TradeManager.class).asEagerSingleton(); - bind(OfferBook.class).asEagerSingleton(); - bind(Navigation.class).asEagerSingleton(); - bind(OverlayManager.class).asEagerSingleton(); - bind(BSFormatter.class).asEagerSingleton(); + protected MessageModule messageModule() { + return new DefaultMessageModule(properties); + } - bind(BankAccountNumberValidator.class).asEagerSingleton(); - bind(BtcValidator.class).asEagerSingleton(); - bind(FiatValidator.class).asEagerSingleton(); - bind(InputValidator.class).asEagerSingleton(); - bind(PasswordValidator.class).asEagerSingleton(); + protected BitcoinModule bitcoinModule() { + return new BitcoinModule(properties); + } - bind(NetworkParameters.class).toProvider(NetworkParametersProvider.class).asEagerSingleton(); + protected CryptoModule cryptoModule() { + return new CryptoModule(properties); + } - // we will probably later disc storage instead of memory storage for TomP2P - // bind(Boolean.class).annotatedWith(Names.named("useDiskStorage")).toInstance(true); - bind(Boolean.class).annotatedWith(Names.named("useDiskStorage")).toInstance(false); + protected TradeModule tradeModule() { + return new TradeModule(properties); + } - bind(SeedNodeAddress.StaticSeedNodeAddresses.class).annotatedWith(Names.named("defaultSeedNode")) - .toProvider(StaticSeedNodeAddressesProvider.class).asEagerSingleton(); - - // Actor Related Classes to Inject - bind(ActorSystem.class).toProvider(ActorSystemProvider.class).asEagerSingleton(); - - bind(DHTSeedService.class); + protected GuiModule guiModule() { + return new GuiModule(properties); } } -class StaticSeedNodeAddressesProvider implements Provider { - private static final Logger log = LoggerFactory.getLogger(StaticSeedNodeAddressesProvider.class); - - public SeedNodeAddress.StaticSeedNodeAddresses get() { - if (BitsquareModule.properties == null) - BitsquareModule.properties = ConfigLoader.loadConfig(); - - log.info("seedNode from config file: " + BitsquareModule.properties.getProperty("defaultSeedNode")); - String seedNodeFromConfig = BitsquareModule.properties.getProperty("defaultSeedNode"); - - // Set default - //SeedNodeAddress.StaticSeedNodeAddresses seedNode = SeedNodeAddress.StaticSeedNodeAddresses.LOCALHOST; - SeedNodeAddress.StaticSeedNodeAddresses seedNode = SeedNodeAddress.StaticSeedNodeAddresses.DIGITAL_OCEAN1; - - // if defined in config we override the above -// if (seedNodeFromConfig != null) -// seedNode = seedNodeFromConfig.equals("localhost") ? -// SeedNodeAddress.StaticSeedNodeAddresses.LOCALHOST : -// SeedNodeAddress.StaticSeedNodeAddresses.DIGITAL_OCEAN; - - return seedNode; - } -} - -class NetworkParametersProvider implements Provider { - private static final Logger log = LoggerFactory.getLogger(NetworkParametersProvider.class); - - public NetworkParameters get() { - NetworkParameters result = null; - - //If config is available we override the networkType defined in Guice with the one from the config file - if (BitsquareModule.properties == null) - BitsquareModule.properties = ConfigLoader.loadConfig(); - - log.info("networkType from config file: " + BitsquareModule.properties.getProperty("networkType")); - String networkTypeFromConfig = BitsquareModule.properties.getProperty("networkType"); - - // Set default - // String networkType= WalletFacade.MAIN_NET; - String networkType = WalletFacade.TEST_NET; - //String networkType = WalletFacade.REG_TEST_NET; - - if (networkTypeFromConfig != null) - networkType = networkTypeFromConfig; - - switch (networkType) { - case WalletFacade.MAIN_NET: - result = MainNetParams.get(); - break; - case WalletFacade.TEST_NET: - result = TestNet3Params.get(); - break; - case WalletFacade.REG_TEST_NET: - result = RegTestParams.get(); - break; - } - return result; - } -} - -class ActorSystemProvider implements Provider { - - @Override - public ActorSystem get() { - ActorSystem system = ActorSystem.create(Bitsquare.getAppName()); - - // create top level actors - //system.actorOf(DHTManager.getProps(), DHTManager.SEED_NAME); - - return system; - } -} \ No newline at end of file diff --git a/src/main/java/io/bitsquare/gui/GuiModule.java b/src/main/java/io/bitsquare/gui/GuiModule.java new file mode 100644 index 0000000000..f71b626a84 --- /dev/null +++ b/src/main/java/io/bitsquare/gui/GuiModule.java @@ -0,0 +1,50 @@ +/* + * 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; + +import io.bitsquare.di.AbstractBitsquareModule; +import io.bitsquare.gui.main.trade.offerbook.OfferBook; +import io.bitsquare.gui.util.BSFormatter; +import io.bitsquare.gui.util.validation.BankAccountNumberValidator; +import io.bitsquare.gui.util.validation.BtcValidator; +import io.bitsquare.gui.util.validation.FiatValidator; +import io.bitsquare.gui.util.validation.InputValidator; +import io.bitsquare.gui.util.validation.PasswordValidator; + +import java.util.Properties; + +public class GuiModule extends AbstractBitsquareModule { + + public GuiModule(Properties properties) { + super(properties); + } + + @Override + protected void configure() { + bind(OfferBook.class).asEagerSingleton(); + bind(Navigation.class).asEagerSingleton(); + bind(OverlayManager.class).asEagerSingleton(); + bind(BSFormatter.class).asEagerSingleton(); + + bind(BankAccountNumberValidator.class).asEagerSingleton(); + bind(BtcValidator.class).asEagerSingleton(); + bind(FiatValidator.class).asEagerSingleton(); + bind(InputValidator.class).asEagerSingleton(); + bind(PasswordValidator.class).asEagerSingleton(); + } +} diff --git a/src/main/java/io/bitsquare/util/ActorService.java b/src/main/java/io/bitsquare/msg/ActorService.java similarity index 97% rename from src/main/java/io/bitsquare/util/ActorService.java rename to src/main/java/io/bitsquare/msg/ActorService.java index 12e9a2bc86..46cdb686ca 100644 --- a/src/main/java/io/bitsquare/util/ActorService.java +++ b/src/main/java/io/bitsquare/msg/ActorService.java @@ -15,7 +15,9 @@ * along with Bitsquare. If not, see . */ -package io.bitsquare.util; +package io.bitsquare.msg; + +import io.bitsquare.util.MessageHandler; import javafx.concurrent.Service; import javafx.concurrent.Task; diff --git a/src/main/java/io/bitsquare/msg/DHTSeedService.java b/src/main/java/io/bitsquare/msg/DHTSeedService.java index 829815c83b..ab4912cf36 100644 --- a/src/main/java/io/bitsquare/msg/DHTSeedService.java +++ b/src/main/java/io/bitsquare/msg/DHTSeedService.java @@ -19,7 +19,6 @@ package io.bitsquare.msg; import io.bitsquare.msg.actor.DHTManager; import io.bitsquare.msg.actor.command.InitializePeer; -import io.bitsquare.util.ActorService; import com.google.inject.Inject; diff --git a/src/main/java/io/bitsquare/msg/DefaultMessageModule.java b/src/main/java/io/bitsquare/msg/DefaultMessageModule.java new file mode 100644 index 0000000000..758e6a4677 --- /dev/null +++ b/src/main/java/io/bitsquare/msg/DefaultMessageModule.java @@ -0,0 +1,46 @@ +/* + * 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.msg; + +import io.bitsquare.di.AbstractBitsquareModule; + +import com.google.inject.name.Names; + +import java.util.Properties; + +public class DefaultMessageModule extends AbstractBitsquareModule implements MessageModule { + + public DefaultMessageModule(Properties properties) { + super(properties); + } + + @Override + protected void configure() { + bind(MessageFacade.class).to(TomP2PMessageFacade.class).asEagerSingleton(); + bind(P2PNode.class).asEagerSingleton(); + bind(BootstrappedPeerFactory.class).asEagerSingleton(); + bind(DHTSeedService.class); + + // we will probably later use disk storage instead of memory storage for TomP2P + bind(Boolean.class).annotatedWith(Names.named("useDiskStorage")).toInstance(false); + + bind(SeedNodeAddress.StaticSeedNodeAddresses.class) + .annotatedWith(Names.named("defaultSeedNode")) + .toInstance(SeedNodeAddress.StaticSeedNodeAddresses.DIGITAL_OCEAN1); + } +} diff --git a/src/main/java/io/bitsquare/msg/MessageModule.java b/src/main/java/io/bitsquare/msg/MessageModule.java new file mode 100644 index 0000000000..c87f29439d --- /dev/null +++ b/src/main/java/io/bitsquare/msg/MessageModule.java @@ -0,0 +1,23 @@ +/* + * 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.msg; + +import com.google.inject.Module; + +public interface MessageModule extends Module { +} diff --git a/src/main/java/io/bitsquare/msg/TomP2PMessageFacade.java b/src/main/java/io/bitsquare/msg/TomP2PMessageFacade.java index ba478154a4..7ef5af737b 100644 --- a/src/main/java/io/bitsquare/msg/TomP2PMessageFacade.java +++ b/src/main/java/io/bitsquare/msg/TomP2PMessageFacade.java @@ -31,7 +31,6 @@ import io.bitsquare.trade.Offer; import io.bitsquare.trade.protocol.trade.TradeMessage; import io.bitsquare.user.User; -import com.google.common.base.Preconditions; import com.google.common.util.concurrent.FutureCallback; import java.io.IOException; @@ -77,8 +76,8 @@ import org.slf4j.LoggerFactory; *

* TODO: improve callbacks that Platform.runLater is not necessary. We call usually that methods form teh UI thread. */ -public class TomP2PMessageFacade implements MessageFacade { - private static final Logger log = LoggerFactory.getLogger(MessageFacade.class); +class TomP2PMessageFacade implements MessageFacade { + private static final Logger log = LoggerFactory.getLogger(TomP2PMessageFacade.class); private static final String ARBITRATORS_ROOT = "ArbitratorsRoot"; private final P2PNode p2pNode; diff --git a/src/main/java/io/bitsquare/network/Peer.java b/src/main/java/io/bitsquare/network/Peer.java index a7428b0ea1..5012ce0d3e 100644 --- a/src/main/java/io/bitsquare/network/Peer.java +++ b/src/main/java/io/bitsquare/network/Peer.java @@ -17,5 +17,10 @@ package io.bitsquare.network; +/** + * A peer on the Bitsquare network. + * + * @author Chris Beams + */ public interface Peer { } diff --git a/src/main/java/io/bitsquare/network/tomp2p/TomP2PPeer.java b/src/main/java/io/bitsquare/network/tomp2p/TomP2PPeer.java index 1b78db1ac1..daaecd5ba0 100644 --- a/src/main/java/io/bitsquare/network/tomp2p/TomP2PPeer.java +++ b/src/main/java/io/bitsquare/network/tomp2p/TomP2PPeer.java @@ -23,6 +23,11 @@ import com.google.common.base.Objects; import net.tomp2p.peers.PeerAddress; +/** + * A {@link Peer} implementation that encapsulates a TomP2P {@link PeerAddress}. + * + * @author Chris Beams + */ public class TomP2PPeer implements Peer { private final PeerAddress peerAddress; diff --git a/src/main/java/io/bitsquare/trade/TradeModule.java b/src/main/java/io/bitsquare/trade/TradeModule.java new file mode 100644 index 0000000000..6290a39fba --- /dev/null +++ b/src/main/java/io/bitsquare/trade/TradeModule.java @@ -0,0 +1,34 @@ +/* + * 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.trade; + +import io.bitsquare.di.AbstractBitsquareModule; + +import java.util.Properties; + +public class TradeModule extends AbstractBitsquareModule { + + public TradeModule(Properties properties) { + super(properties); + } + + @Override + protected void configure() { + bind(TradeManager.class).asEagerSingleton(); + } +}