Active relay mode (working!)

This commit is contained in:
Manfred Karrer 2015-02-15 00:05:49 +01:00
parent 9c75b9951b
commit 309cda7143

View file

@ -46,6 +46,7 @@ import net.tomp2p.futures.BaseFutureListener;
import net.tomp2p.futures.FutureBootstrap; import net.tomp2p.futures.FutureBootstrap;
import net.tomp2p.futures.FutureDiscover; import net.tomp2p.futures.FutureDiscover;
import net.tomp2p.nat.FutureNAT; import net.tomp2p.nat.FutureNAT;
import net.tomp2p.nat.FutureRelayNAT;
import net.tomp2p.nat.PeerBuilderNAT; import net.tomp2p.nat.PeerBuilderNAT;
import net.tomp2p.nat.PeerNAT; import net.tomp2p.nat.PeerNAT;
import net.tomp2p.p2p.Peer; import net.tomp2p.p2p.Peer;
@ -54,6 +55,7 @@ import net.tomp2p.peers.Number160;
import net.tomp2p.peers.PeerAddress; import net.tomp2p.peers.PeerAddress;
import net.tomp2p.peers.PeerMapChangeListener; import net.tomp2p.peers.PeerMapChangeListener;
import net.tomp2p.peers.PeerStatistic; import net.tomp2p.peers.PeerStatistic;
import net.tomp2p.relay.RelayConfig;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -194,17 +196,18 @@ class BootstrappedPeerBuilder {
// If that is successfully setup we need to try again a discover so we find out our external address and have // If that is successfully setup we need to try again a discover so we find out our external address and have
// tested successfully our reachability (the additional discover is done internally from startSetupPortforwarding) // tested successfully our reachability (the additional discover is done internally from startSetupPortforwarding)
// 4. If the port forwarding failed we can try as last resort to open a permanent TCP connection to the // 4. If the port forwarding failed we can try as last resort to open a permanent TCP connection to the
// bootstrap node and use that peer as relay (currently not supported as its too unstable) // bootstrap node and use that peer as relay
private void discoverExternalAddress() { private void discoverExternalAddress() {
FutureDiscover futureDiscover = peer.discover().peerAddress(getBootstrapAddress()).start(); FutureDiscover futureDiscover = peer.discover().peerAddress(getBootstrapAddress()).start();
setState(BootstrapState.DISCOVERY_STARTED, "Starting discovery..."); setState(BootstrapState.DISCOVERY_STARTED, "Starting discovery...");
PeerNAT peerNAT = new PeerBuilderNAT(peer).start(); PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover); FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover);
futureNAT.addListener(new BaseFutureListener<BaseFuture>() { FutureRelayNAT futureRelayNAT = peerNAT.startRelay(RelayConfig.OpenTCP(), futureDiscover, futureNAT);
futureRelayNAT.addListener(new BaseFutureListener<BaseFuture>() {
@Override @Override
public void operationComplete(BaseFuture future) throws Exception { public void operationComplete(BaseFuture future) throws Exception {
// If futureDiscover was successful we are directly connected (or manual port forwarding is set)
if (futureDiscover.isSuccess()) { if (futureDiscover.isSuccess()) {
if (useManualPortForwarding) { if (useManualPortForwarding) {
setState(BootstrapState.DISCOVERY_MANUAL_PORT_FORWARDING_SUCCEEDED, setState(BootstrapState.DISCOVERY_MANUAL_PORT_FORWARDING_SUCCEEDED,
@ -225,57 +228,27 @@ class BootstrappedPeerBuilder {
bootstrap(); bootstrap();
} }
else { else {
handleError(BootstrapState.DISCOVERY_AUTO_PORT_FORWARDING_FAILED, if (future.isSuccess()) {
"Automatic port forwarding failed.\n\n" + // relay mode succeeded
"Check whether UPnP (Universal Plug and Play) is enabled on your router.\n\n" + setState(BootstrapState.RELAY_SUCCEEDED, "Bootstrap using relay was successful.");
"If UPnP is enabled and you still cannot connect, you will need to set up " + bootstrap();
"manual port forwarding.\n\n" + }
"See https://github.com/bitsquare/bitsquare/wiki for instructions."); else {
// All attempts failed. Give up...
// For the moment we don't support relay mode as it has too much problems handleError(BootstrapState.RELAY_FAILED, "Bootstrap using relay has failed " +
/* setState(BootstrapState.AUTO_PORT_FORWARDING_NOT_SUCCEEDED, "Port forwarding has failed. " + futureRelayNAT.failedReason());
"We try to use a relay as next step."); }
bootstrapWithRelay();*/
} }
} }
} }
@Override @Override
public void exceptionCaught(Throwable t) throws Exception { public void exceptionCaught(Throwable t) throws Exception {
handleError(BootstrapState.DISCOVERY_FAILED, "Exception at discover visibility: " + t handleError(BootstrapState.RELAY_FAILED, "Exception at bootstrap: " + t.getMessage());
.getMessage());
} }
}); });
} }
// For the moment we don't support relay mode as it has too much problems
// 3. Attempt: We try to use another peer as relay
/* private void bootstrapWithRelay() {
setState(BootstrapState.RELAY_INIT, "We try to use another peer as relay.");
FutureDiscover futureDiscover = peer.discover().peerAddress(getBootstrapAddress()).start();
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover);
FutureRelayNAT futureRelayNAT = peerNAT.startRelay(RelayConfig.OpenTCP(), futureDiscover, futureNAT);
futureRelayNAT.addListener(new BaseFutureListener<BaseFuture>() {
@Override
public void operationComplete(BaseFuture future) throws Exception {
if (future.isSuccess()) {
setState(BootstrapState.RELAY_SUCCESS, "Bootstrap using relay was successful.");
bootstrap(BootstrapState.RELAY_SUCCESS);
}
else {
handleError(BootstrapState.RELAY_FAILED, "Bootstrap using relay has failed " +
futureRelayNAT.failedReason());
}
}
@Override
public void exceptionCaught(Throwable t) throws Exception {
handleError(BootstrapState.RELAY_FAILED, "Exception at bootstrapWithRelay: " + t.getMessage());
}
});
}*/
private void bootstrap() { private void bootstrap() {
FutureBootstrap futureBootstrap = peer.bootstrap().peerAddress(getBootstrapAddress()).start(); FutureBootstrap futureBootstrap = peer.bootstrap().peerAddress(getBootstrapAddress()).start();
futureBootstrap.addListener(new BaseFutureListener<BaseFuture>() { futureBootstrap.addListener(new BaseFutureListener<BaseFuture>() {