Adopt tests for manual port forwarding, rename NAT to AUTO_PORT_FORWARDING

This commit is contained in:
Manfred Karrer 2014-11-15 18:33:11 +01:00
parent dc464b36e4
commit 103542dd87
6 changed files with 51 additions and 30 deletions

View file

@ -88,21 +88,21 @@ class MainPM extends PresentationModel<MainModel> {
model.bootstrapState.addListener((ov, oldValue, newValue) -> { model.bootstrapState.addListener((ov, oldValue, newValue) -> {
if (newValue == BootstrapState.DIRECT_SUCCESS || if (newValue == BootstrapState.DIRECT_SUCCESS ||
newValue == BootstrapState.NAT_SUCCESS || newValue == BootstrapState.AUTO_PORT_FORWARDING_SUCCESS ||
newValue == BootstrapState.RELAY_SUCCESS) { newValue == BootstrapState.RELAY_SUCCESS) {
bootstrapState.set("Successfully connected to P2P network: " + newValue.getMessage()); bootstrapState.set("Successfully connected to P2P network: " + newValue.getMessage());
bootstrapProgress.set(1); bootstrapProgress.set(1);
if (newValue == BootstrapState.DIRECT_SUCCESS) if (newValue == BootstrapState.DIRECT_SUCCESS)
bootstrapIconId.set("image-connection-direct"); bootstrapIconId.set("image-connection-direct");
else if (newValue == BootstrapState.NAT_SUCCESS) else if (newValue == BootstrapState.AUTO_PORT_FORWARDING_SUCCESS)
bootstrapIconId.set("image-connection-nat"); bootstrapIconId.set("image-connection-nat");
else if (newValue == BootstrapState.RELAY_SUCCESS) else if (newValue == BootstrapState.RELAY_SUCCESS)
bootstrapIconId.set("image-connection-relay"); bootstrapIconId.set("image-connection-relay");
} }
else if (newValue == BootstrapState.PEER_CREATION_FAILED || else if (newValue == BootstrapState.PEER_CREATION_FAILED ||
newValue == BootstrapState.DIRECT_FAILED || newValue == BootstrapState.DIRECT_FAILED ||
newValue == BootstrapState.NAT_FAILED || newValue == BootstrapState.AUTO_PORT_FORWARDING_FAILED ||
newValue == BootstrapState.RELAY_FAILED) { newValue == BootstrapState.RELAY_FAILED) {
bootstrapErrorMsg.set(newValue.getMessage()); bootstrapErrorMsg.set(newValue.getMessage());

View file

@ -191,7 +191,7 @@ class BootstrappedPeerFactory {
case RELAY_SUCCESS: case RELAY_SUCCESS:
bootstrapWithRelay(); bootstrapWithRelay();
break; break;
case NAT_SUCCESS: case AUTO_PORT_FORWARDING_SUCCESS:
tryPortForwarding(); tryPortForwarding();
break; break;
case DIRECT_SUCCESS: case DIRECT_SUCCESS:
@ -246,7 +246,7 @@ class BootstrappedPeerFactory {
// 2. Attempt: Try to set up port forwarding with UPNP and NAT-PMP // 2. Attempt: Try to set up port forwarding with UPNP and NAT-PMP
private void tryPortForwarding() { private void tryPortForwarding() {
setState(BootstrapState.NAT_INIT, "We are trying with automatic port forwarding."); setState(BootstrapState.AUTO_PORT_FORWARDING_INIT, "We are trying with automatic port forwarding.");
FutureDiscover futureDiscover = peer.discover().peerAddress(getBootstrapAddress()).start(); FutureDiscover futureDiscover = peer.discover().peerAddress(getBootstrapAddress()).start();
PeerNAT peerNAT = new PeerBuilderNAT(peer).start(); PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover); FutureNAT futureNAT = peerNAT.startSetupPortforwarding(futureDiscover);
@ -254,13 +254,13 @@ class BootstrappedPeerFactory {
@Override @Override
public void operationComplete(BaseFuture future) throws Exception { public void operationComplete(BaseFuture future) throws Exception {
if (future.isSuccess()) { if (future.isSuccess()) {
setState(BootstrapState.NAT_SETUP_DONE, "Automatic port forwarding is setup. " + setState(BootstrapState.AUTO_PORT_FORWARDING_SETUP_DONE, "Automatic port forwarding is setup. " +
"We need to do a discover process again."); "We need to do a discover process again.");
// we need a second discover process // we need a second discover process
discoverAfterPortForwarding(); discoverAfterPortForwarding();
} }
else { else {
setState(BootstrapState.NAT_NOT_SUCCEEDED, "Port forwarding has failed. " + setState(BootstrapState.AUTO_PORT_FORWARDING_NOT_SUCCEEDED, "Port forwarding has failed. " +
"We try to use a relay as next step."); "We try to use a relay as next step.");
bootstrapWithRelay(); bootstrapWithRelay();
} }
@ -268,7 +268,8 @@ class BootstrappedPeerFactory {
@Override @Override
public void exceptionCaught(Throwable t) throws Exception { public void exceptionCaught(Throwable t) throws Exception {
handleError(BootstrapState.NAT_FAILED, "Exception at port forwarding: " + t.getMessage()); handleError(BootstrapState.AUTO_PORT_FORWARDING_FAILED, "Exception at port forwarding: " + t
.getMessage());
} }
}); });
} }
@ -280,18 +281,19 @@ class BootstrappedPeerFactory {
@Override @Override
public void operationComplete(BaseFuture future) throws Exception { public void operationComplete(BaseFuture future) throws Exception {
if (future.isSuccess()) { if (future.isSuccess()) {
setState(BootstrapState.NAT_SUCCESS, "Discover with automatic port forwarding was successful."); setState(BootstrapState.AUTO_PORT_FORWARDING_SUCCESS, "Discover with automatic port forwarding " +
bootstrap(BootstrapState.NAT_SUCCESS); "was successful.");
bootstrap(BootstrapState.AUTO_PORT_FORWARDING_SUCCESS);
} }
else { else {
handleError(BootstrapState.NAT_FAILED, "Discover with automatic port forwarding has failed " + handleError(BootstrapState.AUTO_PORT_FORWARDING_FAILED, "Discover with automatic port forwarding has failed " +
futureDiscover.failedReason()); futureDiscover.failedReason());
} }
} }
@Override @Override
public void exceptionCaught(Throwable t) throws Exception { public void exceptionCaught(Throwable t) throws Exception {
handleError(BootstrapState.NAT_FAILED, "Exception at discover: " + t.getMessage()); handleError(BootstrapState.AUTO_PORT_FORWARDING_FAILED, "Exception at discover: " + t.getMessage());
} }
}); });
} }

View file

@ -382,8 +382,8 @@ public class TomP2PNode implements ClientNode {
return ConnectionType.DIRECT; return ConnectionType.DIRECT;
case MANUAL_PORT_FORWARDING_SUCCESS: case MANUAL_PORT_FORWARDING_SUCCESS:
return ConnectionType.MANUAL_PORT_FORWARDING; return ConnectionType.MANUAL_PORT_FORWARDING;
case NAT_SUCCESS: case AUTO_PORT_FORWARDING_SUCCESS:
return ConnectionType.NAT; return ConnectionType.AUTO_PORT_FORWARDING;
case RELAY_SUCCESS: case RELAY_SUCCESS:
return ConnectionType.RELAY; return ConnectionType.RELAY;
default: default:

View file

@ -28,11 +28,11 @@ public enum BootstrapState {
DIRECT_NOT_SUCCEEDED, DIRECT_NOT_SUCCEEDED,
DIRECT_FAILED, DIRECT_FAILED,
MANUAL_PORT_FORWARDING_SUCCESS, MANUAL_PORT_FORWARDING_SUCCESS,
NAT_INIT, AUTO_PORT_FORWARDING_INIT,
NAT_SETUP_DONE, AUTO_PORT_FORWARDING_SETUP_DONE,
NAT_SUCCESS, AUTO_PORT_FORWARDING_SUCCESS,
NAT_NOT_SUCCEEDED, AUTO_PORT_FORWARDING_NOT_SUCCEEDED,
NAT_FAILED, AUTO_PORT_FORWARDING_FAILED,
RELAY_INIT, RELAY_INIT,
RELAY_SUCCESS, RELAY_SUCCESS,
RELAY_FAILED; RELAY_FAILED;

View file

@ -18,5 +18,5 @@
package io.bitsquare.network; package io.bitsquare.network;
public enum ConnectionType { public enum ConnectionType {
UNKNOWN, DIRECT, MANUAL_PORT_FORWARDING, NAT, RELAY UNKNOWN, DIRECT, MANUAL_PORT_FORWARDING, AUTO_PORT_FORWARDING, RELAY
} }

View file

@ -84,7 +84,7 @@ public class TomP2PTests {
private static final Logger log = LoggerFactory.getLogger(TomP2PTests.class); private static final Logger log = LoggerFactory.getLogger(TomP2PTests.class);
// If you want to test in one specific connection mode define it directly, otherwise use UNKNOWN // If you want to test in one specific connection mode define it directly, otherwise use UNKNOWN
private static final ConnectionType FORCED_CONNECTION_TYPE = ConnectionType.DIRECT; private static final ConnectionType FORCED_CONNECTION_TYPE = ConnectionType.MANUAL_PORT_FORWARDING;
// Typically you run the bootstrap node in localhost to test direct connection. // Typically you run the bootstrap node in localhost to test direct connection.
// If you have a setup where you are not behind a router you can also use a WAN bootstrap node. // If you have a setup where you are not behind a router you can also use a WAN bootstrap node.
@ -116,8 +116,8 @@ public class TomP2PTests {
@Before @Before
public void setUp() { public void setUp() {
client1Port = 7777; client1Port = 7367;
client2Port = 7778; client2Port = 7368;
} }
@After @After
@ -153,8 +153,9 @@ public class TomP2PTests {
@Test @Test
@Repeat(STRESS_TEST_COUNT) @Repeat(STRESS_TEST_COUNT)
public void testBootstrapWithPortForwarding() throws Exception { public void testBootstrapWithPortForwarding() throws Exception {
if (FORCED_CONNECTION_TYPE == ConnectionType.NAT) { if (FORCED_CONNECTION_TYPE == ConnectionType.AUTO_PORT_FORWARDING ||
peer = bootstrapWithPortForwarding(client1Port); FORCED_CONNECTION_TYPE == ConnectionType.MANUAL_PORT_FORWARDING) {
peer = bootstrapWithPortForwarding(client2Port);
assertNotNull(peer); assertNotNull(peer);
} }
} }
@ -570,8 +571,21 @@ public class TomP2PTests {
Number160 peerId = Number160.createHash(UUID.randomUUID().toString()); Number160 peerId = Number160.createHash(UUID.randomUUID().toString());
Peer peer = null; Peer peer = null;
try { try {
peer = new PeerBuilder(peerId).bindings(getBindings()).behindFirewall() if (FORCED_CONNECTION_TYPE == ConnectionType.MANUAL_PORT_FORWARDING ||
.ports(clientPort).start(); resolvedConnectionType == ConnectionType.MANUAL_PORT_FORWARDING) {
peer = new PeerBuilder(peerId).bindings(getBindings())
.behindFirewall()
.tcpPortForwarding(clientPort)
.udpPortForwarding(clientPort)
.ports(clientPort)
.start();
}
else {
peer = new PeerBuilder(peerId).bindings(getBindings())
.behindFirewall()
.ports(clientPort)
.start();
}
PeerNAT peerNAT = new PeerBuilderNAT(peer).start(); PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start(); FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
@ -668,7 +682,12 @@ public class TomP2PTests {
if (peer != null) if (peer != null)
return peer; return peer;
resolvedConnectionType = ConnectionType.NAT; resolvedConnectionType = ConnectionType.MANUAL_PORT_FORWARDING;
peer = bootstrapWithPortForwarding(clientPort);
if (peer != null)
return peer;
resolvedConnectionType = ConnectionType.AUTO_PORT_FORWARDING;
peer = bootstrapWithPortForwarding(clientPort); peer = bootstrapWithPortForwarding(clientPort);
if (peer != null) if (peer != null)
return peer; return peer;
@ -689,7 +708,7 @@ public class TomP2PTests {
if (FORCED_CONNECTION_TYPE == ConnectionType.DIRECT) { if (FORCED_CONNECTION_TYPE == ConnectionType.DIRECT) {
peer = bootstrapDirectConnection(clientPort); peer = bootstrapDirectConnection(clientPort);
} }
else if (FORCED_CONNECTION_TYPE == ConnectionType.NAT) { else if (FORCED_CONNECTION_TYPE == ConnectionType.AUTO_PORT_FORWARDING) {
peer = bootstrapWithPortForwarding(clientPort); peer = bootstrapWithPortForwarding(clientPort);
} }
else if (FORCED_CONNECTION_TYPE == ConnectionType.RELAY) { else if (FORCED_CONNECTION_TYPE == ConnectionType.RELAY) {