Introduce io.bitsquare.msg.tomp2p package

This commit is contained in:
Chris Beams 2014-11-06 16:03:42 +01:00
parent 467f76fd76
commit 96fa93f608
No known key found for this signature in database
GPG key ID: 3D214F8F5BC5ED73
10 changed files with 117 additions and 96 deletions

View file

@ -21,8 +21,8 @@ import io.bitsquare.AbstractBitsquareModule;
import io.bitsquare.btc.BitcoinModule; import io.bitsquare.btc.BitcoinModule;
import io.bitsquare.crypto.CryptoModule; import io.bitsquare.crypto.CryptoModule;
import io.bitsquare.gui.GuiModule; import io.bitsquare.gui.GuiModule;
import io.bitsquare.msg.DefaultMessageModule;
import io.bitsquare.msg.MessageModule; import io.bitsquare.msg.MessageModule;
import io.bitsquare.msg.tomp2p.TomP2PMessageModule;
import io.bitsquare.offer.OfferModule; import io.bitsquare.offer.OfferModule;
import io.bitsquare.offer.tomp2p.TomP2POfferModule; import io.bitsquare.offer.tomp2p.TomP2POfferModule;
import io.bitsquare.persistence.Persistence; import io.bitsquare.persistence.Persistence;
@ -83,7 +83,7 @@ public class BitsquareModule extends AbstractBitsquareModule {
} }
protected MessageModule messageModule() { protected MessageModule messageModule() {
return new DefaultMessageModule(properties); return new TomP2PMessageModule(properties);
} }
protected BitcoinModule bitcoinModule() { protected BitcoinModule bitcoinModule() {

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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();
}
}

View file

@ -17,7 +17,43 @@
package io.bitsquare.msg; 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<? extends MessageFacade> messageFacade();
@Override
protected void doClose(Injector injector) {
injector.getInstance(MessageFacade.class).shutDown();
}
} }

View file

@ -15,7 +15,7 @@
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>. * along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/ */
package io.bitsquare.msg; package io.bitsquare.msg.tomp2p;
import io.bitsquare.network.BootstrapState; import io.bitsquare.network.BootstrapState;
import io.bitsquare.network.Node; 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 * 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 static final Logger log = LoggerFactory.getLogger(BootstrappedPeerFactory.class);
private KeyPair keyPair; private KeyPair keyPair;

View file

@ -15,9 +15,11 @@
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>. * along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/ */
package io.bitsquare.msg; package io.bitsquare.msg.tomp2p;
import io.bitsquare.arbitrator.Arbitrator; 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.ArbitratorListener;
import io.bitsquare.msg.listeners.BootstrapListener; import io.bitsquare.msg.listeners.BootstrapListener;
import io.bitsquare.msg.listeners.GetPeerAddressListener; 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 Logger log = LoggerFactory.getLogger(TomP2PMessageFacade.class);
private static final String ARBITRATORS_ROOT = "ArbitratorsRoot"; private static final String ARBITRATORS_ROOT = "ArbitratorsRoot";
private final P2PNode p2pNode; private final TomP2PNode p2pNode;
private final User user; private final User user;
private final List<ArbitratorListener> arbitratorListeners = new ArrayList<>(); private final List<ArbitratorListener> arbitratorListeners = new ArrayList<>();
@ -80,7 +82,7 @@ class TomP2PMessageFacade implements MessageFacade {
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@Inject @Inject
public TomP2PMessageFacade(User user, P2PNode p2pNode) { public TomP2PMessageFacade(User user, TomP2PNode p2pNode) {
this.user = user; this.user = user;
this.p2pNode = p2pNode; this.p2pNode = p2pNode;
} }

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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<? extends MessageFacade> messageFacade() {
return TomP2PMessageFacade.class;
}
}

View file

@ -15,8 +15,9 @@
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>. * along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/ */
package io.bitsquare.msg; package io.bitsquare.msg.tomp2p;
import io.bitsquare.msg.MessageBroker;
import io.bitsquare.msg.listeners.BootstrapListener; import io.bitsquare.msg.listeners.BootstrapListener;
import io.bitsquare.network.tomp2p.TomP2PPeer; import io.bitsquare.network.tomp2p.TomP2PPeer;
@ -66,14 +67,16 @@ import org.slf4j.LoggerFactory;
import lighthouse.files.AppDirectory; 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 * The fully bootstrapped P2PNode which is responsible himself for his availability in the messaging system. It saves
* for instance the IP address periodically. * for instance the IP address periodically.
* This class is offering generic functionality of TomP2P needed for Bitsquare, like data and domain protection. * 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. * It does not handle any domain aspects of Bitsquare.
*/ */
public class P2PNode { public class TomP2PNode {
private static final Logger log = LoggerFactory.getLogger(P2PNode.class); private static final Logger log = LoggerFactory.getLogger(TomP2PNode.class);
private KeyPair keyPair; private KeyPair keyPair;
private final Boolean useDiskStorage; private final Boolean useDiskStorage;
@ -90,14 +93,14 @@ public class P2PNode {
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@Inject @Inject
public P2PNode(BootstrappedPeerFactory bootstrappedPeerFactory, public TomP2PNode(BootstrappedPeerFactory bootstrappedPeerFactory,
@Named("useDiskStorage") Boolean useDiskStorage) { @Named("useDiskStorage") Boolean useDiskStorage) {
this.bootstrappedPeerFactory = bootstrappedPeerFactory; this.bootstrappedPeerFactory = bootstrappedPeerFactory;
this.useDiskStorage = useDiskStorage; this.useDiskStorage = useDiskStorage;
} }
// for unit testing // for unit testing
P2PNode(KeyPair keyPair, PeerDHT peerDHT) { TomP2PNode(KeyPair keyPair, PeerDHT peerDHT) {
this.keyPair = keyPair; this.keyPair = keyPair;
this.peerDHT = peerDHT; this.peerDHT = peerDHT;
peerDHT.peerBean().keyPair(keyPair); peerDHT.peerBean().keyPair(keyPair);
@ -309,7 +312,7 @@ public class P2PNode {
public void onSuccess(@Nullable PeerDHT peerDHT) { public void onSuccess(@Nullable PeerDHT peerDHT) {
try { try {
if (peerDHT != null) { if (peerDHT != null) {
P2PNode.this.peerDHT = peerDHT; TomP2PNode.this.peerDHT = peerDHT;
setupReplyHandler(); setupReplyHandler();
FuturePut futurePut = storePeerAddress(); FuturePut futurePut = storePeerAddress();
futurePut.addListener(new BaseFutureListener<BaseFuture>() { futurePut.addListener(new BaseFutureListener<BaseFuture>() {
@ -400,10 +403,4 @@ public class P2PNode {
storage = new StorageMemory(); 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;
}
} }

View file

@ -17,7 +17,6 @@
package io.bitsquare.offer.tomp2p; package io.bitsquare.offer.tomp2p;
import io.bitsquare.AbstractBitsquareModule;
import io.bitsquare.offer.OfferModule; import io.bitsquare.offer.OfferModule;
import io.bitsquare.offer.OfferRepository; import io.bitsquare.offer.OfferRepository;

View file

@ -17,7 +17,7 @@
package io.bitsquare.offer.tomp2p; 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.Offer;
import io.bitsquare.offer.OfferRepository; import io.bitsquare.offer.OfferRepository;
import io.bitsquare.util.task.FaultHandler; import io.bitsquare.util.task.FaultHandler;
@ -57,10 +57,10 @@ class TomP2POfferRepository implements OfferRepository {
private final List<Listener> offerRepositoryListeners = new ArrayList<>(); private final List<Listener> offerRepositoryListeners = new ArrayList<>();
private final LongProperty invalidationTimestamp = new SimpleLongProperty(0); private final LongProperty invalidationTimestamp = new SimpleLongProperty(0);
private final P2PNode p2pNode; private final TomP2PNode p2pNode;
@Inject @Inject
public TomP2POfferRepository(P2PNode p2pNode) { public TomP2POfferRepository(TomP2PNode p2pNode) {
this.p2pNode = p2pNode; this.p2pNode = p2pNode;
} }

View file

@ -15,7 +15,7 @@
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>. * along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/ */
package io.bitsquare.msg; package io.bitsquare.msg.tomp2p;
import java.io.IOException; import java.io.IOException;
@ -48,12 +48,12 @@ import org.slf4j.LoggerFactory;
import static org.junit.Assert.*; 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. // cause IOException: Not listening to anything. Maybe your binding information is wrong.
// investigate what has broken it, probably from update to latest head // investigate what has broken it, probably from update to latest head
@Ignore @Ignore
public class P2PNodeTest { public class TomP2PNodeTest {
private static final Logger log = LoggerFactory.getLogger(P2PNodeTest.class); private static final Logger log = LoggerFactory.getLogger(TomP2PNodeTest.class);
final private static Random rnd = new Random(42L); final private static Random rnd = new Random(42L);
@ -80,12 +80,12 @@ public class P2PNodeTest {
KeyPair keyPairClient = keyGen.genKeyPair(); KeyPair keyPairClient = keyGen.genKeyPair();
KeyPair keyPairOtherPeer = keyGen.genKeyPair(); KeyPair keyPairOtherPeer = keyGen.genKeyPair();
P2PNode node; TomP2PNode node;
Number160 locationKey; Number160 locationKey;
Object object; Object object;
FutureDirect futureDirect; FutureDirect futureDirect;
node = new P2PNode(keyPairClient, client); node = new TomP2PNode(keyPairClient, client);
object = "clients data"; object = "clients data";
futureDirect = node.sendData(otherPeer.peerAddress(), object); futureDirect = node.sendData(otherPeer.peerAddress(), object);
futureDirect.awaitUninterruptibly(); futureDirect.awaitUninterruptibly();
@ -110,7 +110,7 @@ public class P2PNodeTest {
KeyPair keyPairClient = keyGen.genKeyPair(); KeyPair keyPairClient = keyGen.genKeyPair();
KeyPair keyPairOtherPeer = keyGen.genKeyPair(); KeyPair keyPairOtherPeer = keyGen.genKeyPair();
P2PNode node; TomP2PNode node;
Number160 locationKey; Number160 locationKey;
Data data; Data data;
FuturePut futurePut; FuturePut futurePut;
@ -119,7 +119,7 @@ public class P2PNodeTest {
// otherPeer tries to squat clients location store // 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 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 // 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"); locationKey = Number160.createHash("clients location");
data = new Data("otherPeer data"); data = new Data("otherPeer data");
futurePut = node.putDomainProtectedData(locationKey, 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 // client store his data und his domainkey, no problem with previous occupied
// he only can store und that path: locationKey.clientDomainKey.data // 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"); locationKey = Number160.createHash("clients location");
data = new Data("client data"); data = new Data("client data");
futurePut = node.putDomainProtectedData(locationKey, data); futurePut = node.putDomainProtectedData(locationKey, data);
@ -146,7 +146,7 @@ public class P2PNodeTest {
assertEquals("client data", futureGet.data().object()); assertEquals("client data", futureGet.data().object());
// also other peers can read that data if they know the public key of the client // 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 = node.getDomainProtectedData(locationKey, keyPairClient.getPublic());
futureGet.awaitUninterruptibly(); futureGet.awaitUninterruptibly();
assertTrue(futureGet.isSuccess()); assertTrue(futureGet.isSuccess());
@ -168,7 +168,7 @@ public class P2PNodeTest {
assertFalse(futurePut.isSuccess()); assertFalse(futurePut.isSuccess());
// he can read his prev. stored data // he can read his prev. stored data
node = new P2PNode(keyPairOtherPeer, otherPeer); node = new TomP2PNode(keyPairOtherPeer, otherPeer);
futureGet = node.getDomainProtectedData(locationKey, keyPairOtherPeer.getPublic()); futureGet = node.getDomainProtectedData(locationKey, keyPairOtherPeer.getPublic());
futureGet.awaitUninterruptibly(); futureGet.awaitUninterruptibly();
assertTrue(futureGet.isSuccess()); assertTrue(futureGet.isSuccess());
@ -226,7 +226,7 @@ public class P2PNodeTest {
PeerDHT otherPeer = peers[2]; PeerDHT otherPeer = peers[2];
UtilsDHT2.perfectRouting(peers); UtilsDHT2.perfectRouting(peers);
P2PNode node; TomP2PNode node;
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
keyGen.initialize(1024); keyGen.initialize(1024);
KeyPair keyPairClient = keyGen.genKeyPair(); KeyPair keyPairClient = keyGen.genKeyPair();
@ -243,7 +243,7 @@ public class P2PNodeTest {
KeyPair keyPair1 = gen.generateKeyPair(); KeyPair keyPair1 = gen.generateKeyPair();
keyPairClient = keyPair1; keyPairClient = keyPair1;
node = new P2PNode(keyPairClient, client); node = new TomP2PNode(keyPairClient, client);
locationKey = Number160.createHash("add to list clients location"); locationKey = Number160.createHash("add to list clients location");
data = new Data("add to list client data1"); data = new Data("add to list client data1");
Data data_1 = data; Data data_1 = data;
@ -298,7 +298,7 @@ public class P2PNodeTest {
futurePut.awaitUninterruptibly(); futurePut.awaitUninterruptibly();
assertTrue(futurePut.isSuccess()); assertTrue(futurePut.isSuccess());
node = new P2PNode(keyPairOtherPeer, otherPeer); node = new TomP2PNode(keyPairOtherPeer, otherPeer);
futureGet = node.getDataMap(locationKey); futureGet = node.getDataMap(locationKey);
futureGet.awaitUninterruptibly(); futureGet.awaitUninterruptibly();
assertTrue(futureGet.isSuccess()); assertTrue(futureGet.isSuccess());
@ -334,7 +334,7 @@ public class P2PNodeTest {
// client removes his entry -> OK // client removes his entry -> OK
node = new P2PNode(keyPairClient, client); node = new TomP2PNode(keyPairClient, client);
FutureRemove futureRemove = node.removeFromDataMap(locationKey, data_1); FutureRemove futureRemove = node.removeFromDataMap(locationKey, data_1);
futureRemove.awaitUninterruptibly(); futureRemove.awaitUninterruptibly();
assertTrue(futureRemove.isSuccess()); assertTrue(futureRemove.isSuccess());
@ -375,7 +375,7 @@ public class P2PNodeTest {
// otherPeer tries to removes client entry -> FAIL // otherPeer tries to removes client entry -> FAIL
node = new P2PNode(keyPairOtherPeer, otherPeer); node = new TomP2PNode(keyPairOtherPeer, otherPeer);
futureRemove = node.removeFromDataMap(locationKey, data_2); futureRemove = node.removeFromDataMap(locationKey, data_2);
futureRemove.awaitUninterruptibly(); futureRemove.awaitUninterruptibly();
assertFalse(futureRemove.isSuccess()); assertFalse(futureRemove.isSuccess());