Allow command-line configuration of local node id and port

This change does away with the notion of "clientPort" and replaces it,
simply, with "port". There are only two ports we care about in
Bitsquare:

 1. The port that the local node (i.e. a Bitsquare UI running on
    your laptop) listens on. This value is now specified with `--port`

 2. The port of the bootstrap node that the local node will connect to on
    its first run. This value is specified with `--bootstrap.node.port`

So, for example, the following is a valid commandline invocation:

    java -jar bitsquare.jar --port 1234 --bootstrap.node.port=9876

Both of these values default to Node.DEFAULT_PORT (currently 7366)

This commit also introduces the --id flag for configuring the ID of the
local node.
This commit is contained in:
Chris Beams 2014-11-10 12:21:43 +01:00
parent cb0e214a28
commit 2ae5949448
No known key found for this signature in database
GPG key ID: 3D214F8F5BC5ED73
6 changed files with 38 additions and 43 deletions

View file

@ -17,6 +17,8 @@
package io.bitsquare.app;
import io.bitsquare.network.Node;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
@ -33,6 +35,12 @@ public class ArgumentParser {
.defaultHelp(true)
.description("Bitsquare - The decentralized bitcoin exchange");
// Args for local node config
parser.addArgument("--" + Node.ID_KEY)
.help("Local node ID");
parser.addArgument("--" + Node.PORT_KEY)
.help("Local port to listen on");
// Args for seed node config
parser.addArgument("--" + BOOTSTRAP_NODE_ID_KEY)
.help("Seed node ID");

View file

@ -18,7 +18,6 @@
package io.bitsquare.app.cli;
import io.bitsquare.app.ArgumentParser;
import io.bitsquare.network.BootstrapNodes;
import io.bitsquare.network.Node;
import net.tomp2p.dht.PeerBuilderDHT;
@ -36,8 +35,6 @@ import org.slf4j.LoggerFactory;
import net.sourceforge.argparse4j.inf.Namespace;
import static io.bitsquare.msg.tomp2p.TomP2PMessageModule.*;
public class SeedNode {
private static final Logger log = LoggerFactory.getLogger(SeedNode.class);
@ -48,18 +45,12 @@ public class SeedNode {
ArgumentParser parser = new ArgumentParser();
Namespace namespace = parser.parseArgs(args);
Node defaultNode = BootstrapNodes.DIGITAL_OCEAN_1;
String id = defaultNode.getId();
int port = defaultNode.getPort();
String id = namespace.getString(Node.ID_KEY);
if (id == null)
throw new IllegalArgumentException(String.format("--%s option is required", Node.ID_KEY));
// Passed program args will override the properties of the default bootstrapNode
// So you can use the same id but different ports (e.g. running several nodes on one server with
// different ports)
if (namespace.getString(BOOTSTRAP_NODE_ID_KEY) != null)
id = namespace.getString(BOOTSTRAP_NODE_ID_KEY);
if (namespace.getString(BOOTSTRAP_NODE_PORT_KEY) != null)
port = Integer.valueOf(namespace.getString(BOOTSTRAP_NODE_PORT_KEY));
String portValue = namespace.getString(Node.PORT_KEY);
int port = portValue != null ? Integer.valueOf(portValue) : Node.DEFAULT_PORT;
try {
Number160 peerId = Number160.createHash(id);

View file

@ -25,8 +25,6 @@ import com.google.inject.name.Names;
import java.util.Properties;
import net.tomp2p.connection.Ports;
import static io.bitsquare.msg.tomp2p.BootstrappedPeerFactory.*;
import static io.bitsquare.network.BootstrapNodes.DEFAULT_BOOTSTRAP_NODE;
@ -43,8 +41,8 @@ public class TomP2PMessageModule extends MessageModule {
@Override
protected void doConfigure() {
Integer randomPort = new Ports().tcpPort();
bindConstant().annotatedWith(Names.named(TomP2PNode.CLIENT_PORT_KEY)).to(randomPort);
bind(int.class).annotatedWith(Names.named(Node.PORT_KEY)).toInstance(
Integer.valueOf(properties.getProperty(Node.PORT_KEY, String.valueOf(Node.DEFAULT_PORT))));
bind(boolean.class).annotatedWith(Names.named(TomP2PNode.USE_DISK_STORAGE_KEY)).toInstance(false);
bind(TomP2PNode.class).asEagerSingleton();

View file

@ -19,6 +19,7 @@ package io.bitsquare.msg.tomp2p;
import io.bitsquare.msg.MessageBroker;
import io.bitsquare.msg.listeners.BootstrapListener;
import io.bitsquare.network.Node;
import io.bitsquare.network.tomp2p.TomP2PPeer;
import com.google.common.util.concurrent.FutureCallback;
@ -79,11 +80,10 @@ public class TomP2PNode {
private static final Logger log = LoggerFactory.getLogger(TomP2PNode.class);
static final String USE_DISK_STORAGE_KEY = "useDiskStorage";
static final String CLIENT_PORT_KEY = "clientPort";
private KeyPair keyPair;
private String appName;
private final int clientPort;
private final int port;
private final Boolean useDiskStorage;
private MessageBroker messageBroker;
@ -100,11 +100,11 @@ public class TomP2PNode {
@Inject
public TomP2PNode(BootstrappedPeerFactory bootstrappedPeerFactory,
@Named("appName") String appName,
@Named(CLIENT_PORT_KEY) int clientPort,
@Named(Node.PORT_KEY) int port,
@Named(USE_DISK_STORAGE_KEY) Boolean useDiskStorage) {
this.bootstrappedPeerFactory = bootstrappedPeerFactory;
this.appName = appName;
this.clientPort = clientPort;
this.port = port;
this.useDiskStorage = useDiskStorage;
}
@ -115,7 +115,7 @@ public class TomP2PNode {
peerDHT.peerBean().keyPair(keyPair);
messageBroker = (message, peerAddress) -> {
};
clientPort = -1;
port = Node.DEFAULT_PORT;
useDiskStorage = false;
}
@ -139,7 +139,7 @@ public class TomP2PNode {
bootstrappedPeerFactory.setStorage(storage);
setupTimerForIPCheck();
ListenableFuture<PeerDHT> bootstrapComplete = bootstrap(clientPort);
ListenableFuture<PeerDHT> bootstrapComplete = bootstrap(port);
Futures.addCallback(bootstrapComplete, new FutureCallback<PeerDHT>() {
@Override
public void onSuccess(@Nullable PeerDHT result) {

View file

@ -20,6 +20,8 @@ package io.bitsquare.network;
import com.google.common.base.Objects;
public final class Node {
public static final String ID_KEY = "id";
public static final String PORT_KEY = "port";
public static final int DEFAULT_PORT = 7366;
private final String id;

View file

@ -304,8 +304,7 @@ public class TomP2PTests {
assertEquals("pong", futureDirect.object());
}
private Peer bootstrapDirectConnection(String clientId, int clientPort) {
final String id = clientId + clientPort;
private Peer bootstrapDirectConnection(String clientId, int port) {
Peer peer = null;
try {
Number160 peerId = Number160.createHash(clientId);
@ -315,7 +314,7 @@ public class TomP2PTests {
cc.maxPermitsTCP(100);
cc.maxPermitsUDP(100);
peer = new PeerBuilder(peerId).bindings(getBindings()).channelClientConfiguration(cc).peerMap(pm)
.ports(clientPort).start();
.ports(port).start();
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
futureDiscover.awaitUninterruptibly();
if (futureDiscover.isSuccess()) {
@ -347,12 +346,11 @@ public class TomP2PTests {
}
}
private Peer bootstrapWithPortForwarding(String clientId, int clientPort) {
final String id = clientId + clientPort;
private Peer bootstrapWithPortForwarding(String clientId, int port) {
Peer peer = null;
try {
peer = new PeerBuilder(Number160.createHash(clientId)).bindings(getBindings()).behindFirewall()
.ports(clientPort).start();
.ports(port).start();
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
@ -401,13 +399,11 @@ public class TomP2PTests {
}
}
private Peer bootstrapInRelayMode(String clientId, int clientPort) {
final String id = clientId + clientPort;
private Peer bootstrapInRelayMode(String clientId, int port) {
Peer peer = null;
try {
peer = new PeerBuilder(Number160.createHash(clientId)).bindings(getBindings()).behindFirewall()
.ports(clientPort).start();
.ports(port).start();
PeerNAT peerNAT = new PeerBuilderNAT(peer).start();
FutureDiscover futureDiscover = peer.discover().peerAddress(BOOTSTRAP_NODE_ADDRESS).start();
@ -444,19 +440,19 @@ public class TomP2PTests {
}
}
private Peer bootstrapInUnknownMode(String clientId, int clientPort) {
private Peer bootstrapInUnknownMode(String clientId, int port) {
resolvedConnectionType = ConnectionType.DIRECT;
Peer peer = bootstrapDirectConnection(clientId, clientPort);
Peer peer = bootstrapDirectConnection(clientId, port);
if (peer != null)
return peer;
resolvedConnectionType = ConnectionType.NAT;
peer = bootstrapWithPortForwarding(clientId, clientPort);
peer = bootstrapWithPortForwarding(clientId, port);
if (peer != null)
return peer;
resolvedConnectionType = ConnectionType.RELAY;
peer = bootstrapInRelayMode(clientId, clientPort);
peer = bootstrapInRelayMode(clientId, port);
if (peer != null)
return peer;
else
@ -466,19 +462,19 @@ public class TomP2PTests {
return peer;
}
private PeerDHT getDHTPeer(String clientId, int clientPort) {
private PeerDHT getDHTPeer(String clientId, int port) {
Peer peer;
if (FORCED_CONNECTION_TYPE == ConnectionType.DIRECT) {
peer = bootstrapDirectConnection(clientId, clientPort);
peer = bootstrapDirectConnection(clientId, port);
}
else if (FORCED_CONNECTION_TYPE == ConnectionType.NAT) {
peer = bootstrapWithPortForwarding(clientId, clientPort);
peer = bootstrapWithPortForwarding(clientId, port);
}
else if (FORCED_CONNECTION_TYPE == ConnectionType.RELAY) {
peer = bootstrapInRelayMode(clientId, clientPort);
peer = bootstrapInRelayMode(clientId, port);
}
else {
peer = bootstrapInUnknownMode(clientId, clientPort);
peer = bootstrapInUnknownMode(clientId, port);
}
if (peer == null)