Refactor bootstrap node definitions

This commit is contained in:
Manfred Karrer 2015-05-17 14:56:05 +02:00
parent 68a4178bf3
commit 19bb552224
14 changed files with 214 additions and 138 deletions

View file

@ -44,9 +44,6 @@ public class BootstrapNode {
private static final String VERSION = "0.1.3";
public static final String P2P_ID = "node.p2pId";
public static int DEFAULT_P2P_ID = 2; // 0 | 1 | 2 for mainnet/testnet/regtest
private static Peer peer = null;
private final Environment env;
@ -58,9 +55,9 @@ public class BootstrapNode {
public void start() {
int p2pId = env.getProperty(P2P_ID, Integer.class, DEFAULT_P2P_ID);
int port = env.getProperty(Node.PORT_KEY, Integer.class, BootstrapNodes.BASE_PORT + p2pId);
String name = env.getProperty(Node.NAME_KEY, BootstrapNodes.LOCALHOST.getName());
String name = env.getProperty(Node.NAME_KEY, BootstrapNodes.getLocalhostNode().getName());
int p2pId = env.getProperty(Node.P2P_ID_KEY, Integer.class, BootstrapNodes.getLocalhostNode().getP2pId());
int port = env.getProperty(Node.PORT_KEY, Integer.class, BootstrapNodes.getLocalhostNode().getPort());
Logging.setup(name + "_" + port);
try {
@ -89,16 +86,17 @@ public class BootstrapNode {
PeerDHT peerDHT = new PeerBuilderDHT(peer).start();
new PeerBuilderNAT(peer).start();
if (!name.equals(BootstrapNodes.LOCALHOST.getName())) {
Collection<PeerAddress> bootstrapNodes = BootstrapNodes.getAllBootstrapNodes().stream().filter(e -> !e.getName().equals(name))
.map(e -> e.toPeerAddressWithPort(port)).collect(Collectors.toList());
final int _port = port;
if (!name.equals(BootstrapNodes.getLocalhostNode().getName())) {
Collection<PeerAddress> bootstrapNodes = BootstrapNodes.getAllBootstrapNodes(p2pId).stream().filter(e -> !e.getName().equals(name))
.map(e -> e.toPeerAddressWithPort(_port)).collect(Collectors.toList());
log.info("Bootstrapping to " + bootstrapNodes.size() + " bootstrapNode(s)");
log.info("Bootstrapping bootstrapNodes " + bootstrapNodes);
peer.bootstrap().bootstrapTo(bootstrapNodes).start().awaitUninterruptibly();
}
else {
log.info("Localhost, no bootstrap");
log.info("We are localhost, we do not bootstrap to other nodes");
}
peer.peerBean().peerMap().addPeerMapChangeListener(new PeerMapChangeListener() {
@Override
@ -129,7 +127,7 @@ public class BootstrapNode {
}
});
log.info("Bootstrap node started with name " + name + " ,port " + port + " and version " + VERSION);
log.info("Bootstrap node started with name=" + name + " ,p2pId=" + p2pId + " ,port=" + port + " and version=" + VERSION);
new Thread(() -> {
while (true) {
if (peer.peerBean().peerMap().all().size() > 0) {

View file

@ -32,13 +32,14 @@ public class BootstrapNodeMain extends BitsquareExecutable {
}
protected void customizeOptionParsing(OptionParser parser) {
parser.accepts(Node.NAME_KEY, description("Name of this node", BootstrapNodes.LOCALHOST.getName()))
parser.accepts(Node.NAME_KEY, description("Name of this node", BootstrapNodes.getLocalhostNode().getName()))
.withRequiredArg()
.ofType(String.class);
parser.accepts(Node.PORT_KEY, description("Port to listen on", BootstrapNodes.BASE_PORT))
parser.accepts(Node.P2P_ID_KEY, description("P2P network ID",
BootstrapNodes.getLocalhostNode().getP2pId()))
.withRequiredArg()
.ofType(int.class);
parser.accepts(BootstrapNode.P2P_ID, description("P2P Network ID [0 | 1 | 2 for mainnet/testnet/regtest]", BootstrapNode.DEFAULT_P2P_ID))
parser.accepts(Node.PORT_KEY, description("Port to listen on", BootstrapNodes.getLocalhostNode().getPort()))
.withRequiredArg()
.ofType(int.class);
}