diff --git a/src/main/java/io/bitsquare/app/BitsquareModule.java b/src/main/java/io/bitsquare/app/BitsquareModule.java index 32312a5913..afb3b548ab 100644 --- a/src/main/java/io/bitsquare/app/BitsquareModule.java +++ b/src/main/java/io/bitsquare/app/BitsquareModule.java @@ -21,8 +21,8 @@ import io.bitsquare.AbstractBitsquareModule; 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.msg.tomp2p.TomP2PMessageModule; import io.bitsquare.offer.OfferModule; import io.bitsquare.offer.tomp2p.TomP2POfferModule; import io.bitsquare.persistence.Persistence; @@ -83,7 +83,7 @@ public class BitsquareModule extends AbstractBitsquareModule { } protected MessageModule messageModule() { - return new DefaultMessageModule(properties); + return new TomP2PMessageModule(properties); } protected BitcoinModule bitcoinModule() { diff --git a/src/main/java/io/bitsquare/msg/DefaultMessageModule.java b/src/main/java/io/bitsquare/msg/DefaultMessageModule.java deleted file mode 100644 index 4e1e75c2fe..0000000000 --- a/src/main/java/io/bitsquare/msg/DefaultMessageModule.java +++ /dev/null @@ -1,54 +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.msg; - -import io.bitsquare.AbstractBitsquareModule; -import io.bitsquare.network.BootstrapNodes; -import io.bitsquare.network.Node; - -import com.google.inject.Injector; -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(Node.class) - .annotatedWith(Names.named("bootstrapNode")) - .toInstance(BootstrapNodes.LOCALHOST); - } - - @Override - protected void doClose(Injector injector) { - injector.getInstance(MessageFacade.class).shutDown(); - } -} diff --git a/src/main/java/io/bitsquare/msg/MessageModule.java b/src/main/java/io/bitsquare/msg/MessageModule.java index c87f29439d..31de0addba 100644 --- a/src/main/java/io/bitsquare/msg/MessageModule.java +++ b/src/main/java/io/bitsquare/msg/MessageModule.java @@ -17,7 +17,43 @@ package io.bitsquare.msg; -import com.google.inject.Module; +import io.bitsquare.AbstractBitsquareModule; +import io.bitsquare.network.BootstrapNodes; +import io.bitsquare.network.Node; -public interface MessageModule extends Module { +import com.google.inject.Injector; +import com.google.inject.name.Names; + +import java.util.Properties; + +public abstract class MessageModule extends AbstractBitsquareModule { + + protected MessageModule(Properties properties) { + super(properties); + } + + @Override + protected final void configure() { + bind(MessageFacade.class).to(messageFacade()).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(Node.class) + .annotatedWith(Names.named("bootstrapNode")) + .toInstance(BootstrapNodes.DIGITAL_OCEAN_1); + + doConfigure(); + } + + protected void doConfigure() { + } + + protected abstract Class messageFacade(); + + @Override + protected void doClose(Injector injector) { + injector.getInstance(MessageFacade.class).shutDown(); + } } diff --git a/src/main/java/io/bitsquare/msg/BootstrappedPeerFactory.java b/src/main/java/io/bitsquare/msg/tomp2p/BootstrappedPeerFactory.java similarity index 99% rename from src/main/java/io/bitsquare/msg/BootstrappedPeerFactory.java rename to src/main/java/io/bitsquare/msg/tomp2p/BootstrappedPeerFactory.java index df591d90b7..ba92febd6a 100644 --- a/src/main/java/io/bitsquare/msg/BootstrappedPeerFactory.java +++ b/src/main/java/io/bitsquare/msg/tomp2p/BootstrappedPeerFactory.java @@ -15,7 +15,7 @@ * along with Bitsquare. If not, see . */ -package io.bitsquare.msg; +package io.bitsquare.msg.tomp2p; import io.bitsquare.network.BootstrapState; import io.bitsquare.network.Node; @@ -65,7 +65,7 @@ import org.slf4j.LoggerFactory; /** * Creates a DHT peer and bootstrap to the network via a seed node */ -public class BootstrappedPeerFactory { +class BootstrappedPeerFactory { private static final Logger log = LoggerFactory.getLogger(BootstrappedPeerFactory.class); private KeyPair keyPair; diff --git a/src/main/java/io/bitsquare/msg/TomP2PMessageFacade.java b/src/main/java/io/bitsquare/msg/tomp2p/TomP2PMessageFacade.java similarity index 98% rename from src/main/java/io/bitsquare/msg/TomP2PMessageFacade.java rename to src/main/java/io/bitsquare/msg/tomp2p/TomP2PMessageFacade.java index 614586e053..0acba66858 100644 --- a/src/main/java/io/bitsquare/msg/TomP2PMessageFacade.java +++ b/src/main/java/io/bitsquare/msg/tomp2p/TomP2PMessageFacade.java @@ -15,9 +15,11 @@ * along with Bitsquare. If not, see . */ -package io.bitsquare.msg; +package io.bitsquare.msg.tomp2p; import io.bitsquare.arbitrator.Arbitrator; +import io.bitsquare.msg.Message; +import io.bitsquare.msg.MessageFacade; import io.bitsquare.msg.listeners.ArbitratorListener; import io.bitsquare.msg.listeners.BootstrapListener; import io.bitsquare.msg.listeners.GetPeerAddressListener; @@ -68,7 +70,7 @@ class TomP2PMessageFacade implements MessageFacade { private static final Logger log = LoggerFactory.getLogger(TomP2PMessageFacade.class); private static final String ARBITRATORS_ROOT = "ArbitratorsRoot"; - private final P2PNode p2pNode; + private final TomP2PNode p2pNode; private final User user; private final List arbitratorListeners = new ArrayList<>(); @@ -80,7 +82,7 @@ class TomP2PMessageFacade implements MessageFacade { /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public TomP2PMessageFacade(User user, P2PNode p2pNode) { + public TomP2PMessageFacade(User user, TomP2PNode p2pNode) { this.user = user; this.p2pNode = p2pNode; } diff --git a/src/main/java/io/bitsquare/msg/tomp2p/TomP2PMessageModule.java b/src/main/java/io/bitsquare/msg/tomp2p/TomP2PMessageModule.java new file mode 100644 index 0000000000..3e31154074 --- /dev/null +++ b/src/main/java/io/bitsquare/msg/tomp2p/TomP2PMessageModule.java @@ -0,0 +1,41 @@ +/* + * 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.tomp2p; + +import io.bitsquare.msg.MessageFacade; +import io.bitsquare.msg.MessageModule; + +import java.util.Properties; + +public class TomP2PMessageModule extends MessageModule { + + public TomP2PMessageModule(Properties properties) { + super(properties); + } + + @Override + protected void doConfigure() { + bind(TomP2PNode.class).asEagerSingleton(); + bind(BootstrappedPeerFactory.class).asEagerSingleton(); + } + + @Override + protected Class messageFacade() { + return TomP2PMessageFacade.class; + } +} diff --git a/src/main/java/io/bitsquare/msg/P2PNode.java b/src/main/java/io/bitsquare/msg/tomp2p/TomP2PNode.java similarity index 96% rename from src/main/java/io/bitsquare/msg/P2PNode.java rename to src/main/java/io/bitsquare/msg/tomp2p/TomP2PNode.java index 58ae953cec..aed00768d6 100644 --- a/src/main/java/io/bitsquare/msg/P2PNode.java +++ b/src/main/java/io/bitsquare/msg/tomp2p/TomP2PNode.java @@ -15,8 +15,9 @@ * along with Bitsquare. If not, see . */ -package io.bitsquare.msg; +package io.bitsquare.msg.tomp2p; +import io.bitsquare.msg.MessageBroker; import io.bitsquare.msg.listeners.BootstrapListener; import io.bitsquare.network.tomp2p.TomP2PPeer; @@ -66,14 +67,16 @@ import org.slf4j.LoggerFactory; import lighthouse.files.AppDirectory; +import static io.bitsquare.network.tomp2p.BaseFutureUtil.isSuccess; + /** * The fully bootstrapped P2PNode which is responsible himself for his availability in the messaging system. It saves * for instance the IP address periodically. * This class is offering generic functionality of TomP2P needed for Bitsquare, like data and domain protection. * It does not handle any domain aspects of Bitsquare. */ -public class P2PNode { - private static final Logger log = LoggerFactory.getLogger(P2PNode.class); +public class TomP2PNode { + private static final Logger log = LoggerFactory.getLogger(TomP2PNode.class); private KeyPair keyPair; private final Boolean useDiskStorage; @@ -90,14 +93,14 @@ public class P2PNode { /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public P2PNode(BootstrappedPeerFactory bootstrappedPeerFactory, - @Named("useDiskStorage") Boolean useDiskStorage) { + public TomP2PNode(BootstrappedPeerFactory bootstrappedPeerFactory, + @Named("useDiskStorage") Boolean useDiskStorage) { this.bootstrappedPeerFactory = bootstrappedPeerFactory; this.useDiskStorage = useDiskStorage; } // for unit testing - P2PNode(KeyPair keyPair, PeerDHT peerDHT) { + TomP2PNode(KeyPair keyPair, PeerDHT peerDHT) { this.keyPair = keyPair; this.peerDHT = peerDHT; peerDHT.peerBean().keyPair(keyPair); @@ -309,7 +312,7 @@ public class P2PNode { public void onSuccess(@Nullable PeerDHT peerDHT) { try { if (peerDHT != null) { - P2PNode.this.peerDHT = peerDHT; + TomP2PNode.this.peerDHT = peerDHT; setupReplyHandler(); FuturePut futurePut = storePeerAddress(); futurePut.addListener(new BaseFutureListener() { @@ -400,10 +403,4 @@ public class P2PNode { storage = new StorageMemory(); } } - - // Isolate the success handling as there is bug in port forwarding mode - private boolean isSuccess(BaseFuture baseFuture) { - // return baseFuture.isSuccess(); - return true; - } } diff --git a/src/main/java/io/bitsquare/offer/tomp2p/TomP2POfferModule.java b/src/main/java/io/bitsquare/offer/tomp2p/TomP2POfferModule.java index e60ea70bfb..fb7e3729ad 100644 --- a/src/main/java/io/bitsquare/offer/tomp2p/TomP2POfferModule.java +++ b/src/main/java/io/bitsquare/offer/tomp2p/TomP2POfferModule.java @@ -17,7 +17,6 @@ package io.bitsquare.offer.tomp2p; -import io.bitsquare.AbstractBitsquareModule; import io.bitsquare.offer.OfferModule; import io.bitsquare.offer.OfferRepository; diff --git a/src/main/java/io/bitsquare/offer/tomp2p/TomP2POfferRepository.java b/src/main/java/io/bitsquare/offer/tomp2p/TomP2POfferRepository.java index 2f18103e56..e7bc05df90 100644 --- a/src/main/java/io/bitsquare/offer/tomp2p/TomP2POfferRepository.java +++ b/src/main/java/io/bitsquare/offer/tomp2p/TomP2POfferRepository.java @@ -17,7 +17,7 @@ package io.bitsquare.offer.tomp2p; -import io.bitsquare.msg.P2PNode; +import io.bitsquare.msg.tomp2p.TomP2PNode; import io.bitsquare.offer.Offer; import io.bitsquare.offer.OfferRepository; import io.bitsquare.util.task.FaultHandler; @@ -57,10 +57,10 @@ class TomP2POfferRepository implements OfferRepository { private final List offerRepositoryListeners = new ArrayList<>(); private final LongProperty invalidationTimestamp = new SimpleLongProperty(0); - private final P2PNode p2pNode; + private final TomP2PNode p2pNode; @Inject - public TomP2POfferRepository(P2PNode p2pNode) { + public TomP2POfferRepository(TomP2PNode p2pNode) { this.p2pNode = p2pNode; } diff --git a/src/test/java/io/bitsquare/msg/P2PNodeTest.java b/src/test/java/io/bitsquare/msg/tomp2p/TomP2PNodeTest.java similarity index 94% rename from src/test/java/io/bitsquare/msg/P2PNodeTest.java rename to src/test/java/io/bitsquare/msg/tomp2p/TomP2PNodeTest.java index 05d79f07d7..1d1742d5ba 100644 --- a/src/test/java/io/bitsquare/msg/P2PNodeTest.java +++ b/src/test/java/io/bitsquare/msg/tomp2p/TomP2PNodeTest.java @@ -15,7 +15,7 @@ * along with Bitsquare. If not, see . */ -package io.bitsquare.msg; +package io.bitsquare.msg.tomp2p; import java.io.IOException; @@ -48,12 +48,12 @@ import org.slf4j.LoggerFactory; import static org.junit.Assert.*; -// TODO Reactivate tests when P2PNode is using original code again. we deactivated the security features atm. +// TODO Reactivate tests when TomP2PNode is using original code again. we deactivated the security features atm. // cause IOException: Not listening to anything. Maybe your binding information is wrong. // investigate what has broken it, probably from update to latest head @Ignore -public class P2PNodeTest { - private static final Logger log = LoggerFactory.getLogger(P2PNodeTest.class); +public class TomP2PNodeTest { + private static final Logger log = LoggerFactory.getLogger(TomP2PNodeTest.class); final private static Random rnd = new Random(42L); @@ -80,12 +80,12 @@ public class P2PNodeTest { KeyPair keyPairClient = keyGen.genKeyPair(); KeyPair keyPairOtherPeer = keyGen.genKeyPair(); - P2PNode node; + TomP2PNode node; Number160 locationKey; Object object; FutureDirect futureDirect; - node = new P2PNode(keyPairClient, client); + node = new TomP2PNode(keyPairClient, client); object = "clients data"; futureDirect = node.sendData(otherPeer.peerAddress(), object); futureDirect.awaitUninterruptibly(); @@ -110,7 +110,7 @@ public class P2PNodeTest { KeyPair keyPairClient = keyGen.genKeyPair(); KeyPair keyPairOtherPeer = keyGen.genKeyPair(); - P2PNode node; + TomP2PNode node; Number160 locationKey; Data data; FuturePut futurePut; @@ -119,7 +119,7 @@ public class P2PNodeTest { // otherPeer tries to squat clients location store // he can do it but as he has not the domain key of the client he cannot do any harm // he only can store und that path: locationKey.otherPeerDomainKey.data - node = new P2PNode(keyPairOtherPeer, otherPeer); + node = new TomP2PNode(keyPairOtherPeer, otherPeer); locationKey = Number160.createHash("clients location"); data = new Data("otherPeer data"); futurePut = node.putDomainProtectedData(locationKey, data); @@ -133,7 +133,7 @@ public class P2PNodeTest { // client store his data und his domainkey, no problem with previous occupied // he only can store und that path: locationKey.clientDomainKey.data - node = new P2PNode(keyPairClient, client); + node = new TomP2PNode(keyPairClient, client); locationKey = Number160.createHash("clients location"); data = new Data("client data"); futurePut = node.putDomainProtectedData(locationKey, data); @@ -146,7 +146,7 @@ public class P2PNodeTest { assertEquals("client data", futureGet.data().object()); // also other peers can read that data if they know the public key of the client - node = new P2PNode(keyPairOtherPeer, otherPeer); + node = new TomP2PNode(keyPairOtherPeer, otherPeer); futureGet = node.getDomainProtectedData(locationKey, keyPairClient.getPublic()); futureGet.awaitUninterruptibly(); assertTrue(futureGet.isSuccess()); @@ -168,7 +168,7 @@ public class P2PNodeTest { assertFalse(futurePut.isSuccess()); // he can read his prev. stored data - node = new P2PNode(keyPairOtherPeer, otherPeer); + node = new TomP2PNode(keyPairOtherPeer, otherPeer); futureGet = node.getDomainProtectedData(locationKey, keyPairOtherPeer.getPublic()); futureGet.awaitUninterruptibly(); assertTrue(futureGet.isSuccess()); @@ -226,7 +226,7 @@ public class P2PNodeTest { PeerDHT otherPeer = peers[2]; UtilsDHT2.perfectRouting(peers); - P2PNode node; + TomP2PNode node; final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024); KeyPair keyPairClient = keyGen.genKeyPair(); @@ -243,7 +243,7 @@ public class P2PNodeTest { KeyPair keyPair1 = gen.generateKeyPair(); keyPairClient = keyPair1; - node = new P2PNode(keyPairClient, client); + node = new TomP2PNode(keyPairClient, client); locationKey = Number160.createHash("add to list clients location"); data = new Data("add to list client data1"); Data data_1 = data; @@ -298,7 +298,7 @@ public class P2PNodeTest { futurePut.awaitUninterruptibly(); assertTrue(futurePut.isSuccess()); - node = new P2PNode(keyPairOtherPeer, otherPeer); + node = new TomP2PNode(keyPairOtherPeer, otherPeer); futureGet = node.getDataMap(locationKey); futureGet.awaitUninterruptibly(); assertTrue(futureGet.isSuccess()); @@ -334,7 +334,7 @@ public class P2PNodeTest { // client removes his entry -> OK - node = new P2PNode(keyPairClient, client); + node = new TomP2PNode(keyPairClient, client); FutureRemove futureRemove = node.removeFromDataMap(locationKey, data_1); futureRemove.awaitUninterruptibly(); assertTrue(futureRemove.isSuccess()); @@ -375,7 +375,7 @@ public class P2PNodeTest { // otherPeer tries to removes client entry -> FAIL - node = new P2PNode(keyPairOtherPeer, otherPeer); + node = new TomP2PNode(keyPairOtherPeer, otherPeer); futureRemove = node.removeFromDataMap(locationKey, data_2); futureRemove.awaitUninterruptibly(); assertFalse(futureRemove.isSuccess());